Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Allow multiple ES outputs as long as they are the same ES" #1878

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions internal/pkg/dl/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ ctx._source['` + fieldOutputs + `']['default'].to_retire_api_key_ids=new ArrayLi

// copy 'default_api_key_history' to new 'outputs' field
ctx._source['` + fieldOutputs + `']['default'].type="elasticsearch";
if (ctx._source.default_api_key_history != null && ctx._source.default_api_key_history.length > 0) {
ctx._source['` + fieldOutputs + `']['default'].to_retire_api_key_ids=ctx._source.default_api_key_history;
}
ctx._source['` + fieldOutputs + `']['default'].to_retire_api_key_ids=ctx._source.default_api_key_history;

Map map = new HashMap();
map.put("retired_at", params.` + fieldRetiredAt + `);
Expand Down
95 changes: 2 additions & 93 deletions internal/pkg/dl/migration_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestPolicyCoordinatorIdx(t *testing.T) {
}
}

func TestMigrateOutputs_withDefaultAPIKeyHistory(t *testing.T) {
func TestMigrateOutputs(t *testing.T) {
now, err := time.Parse(time.RFC3339, nowStr)
require.NoError(t, err, "could not parse time "+nowStr)
timeNow = func() time.Time {
Expand All @@ -153,7 +153,7 @@ func TestMigrateOutputs_withDefaultAPIKeyHistory(t *testing.T) {
assert.Equal(t, len(agentIDs), migratedAgents)

for i, id := range agentIDs {
wantOutputType := "elasticsearch" //nolint:goconst // test cases have some duplication
wantOutputType := "elasticsearch"

got, err := FindAgent(
context.Background(), bulker, QueryAgentByID, FieldID, id, WithIndexName(index))
Expand Down Expand Up @@ -202,94 +202,3 @@ func TestMigrateOutputs_withDefaultAPIKeyHistory(t *testing.T) {
assert.Nil(t, got.DefaultAPIKeyHistory)
}
}

func TestMigrateOutputs_nil_DefaultAPIKeyHistory(t *testing.T) {
wantOutputType := "elasticsearch" //nolint:goconst // test cases have some duplication

now, err := time.Parse(time.RFC3339, nowStr)
require.NoError(t, err, "could not parse time "+nowStr)
timeNow = func() time.Time {
return now
}

index, bulker := ftesting.SetupCleanIndex(context.Background(), t, FleetAgents)
apiKey := bulk.APIKey{
ID: "testAgent_",
Key: "testAgent_key_",
}

i := 0
outputAPIKey := bulk.APIKey{
ID: fmt.Sprint(apiKey.ID, i),
Key: fmt.Sprint(apiKey.Key, i),
}

agentID := uuid.Must(uuid.NewV4()).String()
policyID := uuid.Must(uuid.NewV4()).String()

agentModel := model.Agent{
PolicyID: policyID,
Active: true,
LastCheckin: nowStr,
LastCheckinStatus: "",
UpdatedAt: nowStr,
EnrolledAt: nowStr,
DefaultAPIKeyID: outputAPIKey.ID,
DefaultAPIKey: outputAPIKey.Agent(),
PolicyOutputPermissionsHash: fmt.Sprint("a_output_permission_SHA_", i),
}

body, err := json.Marshal(agentModel)
require.NoError(t, err)

_, err = bulker.Create(
context.Background(), index, agentID, body, bulk.WithRefresh())
require.NoError(t, err)

migratedAgents, err := migrate(context.Background(), bulker, migrateAgentOutputs)
require.NoError(t, err)

got, err := FindAgent(
context.Background(), bulker, QueryAgentByID, FieldID, agentID, WithIndexName(index))
require.NoError(t, err, "failed to find agent ID %q", agentID) // we want to continue even if a single agent fails

assert.Equal(t, 1, migratedAgents)

// Assert new fields
require.Len(t, got.Outputs, 1)
// Default API key is empty to force fleet-server to regenerate them.
assert.Empty(t, got.Outputs["default"].APIKey)
assert.Empty(t, got.Outputs["default"].APIKeyID)
assert.Equal(t, wantOutputType, got.Outputs["default"].Type)
assert.Equal(t,
fmt.Sprint("a_output_permission_SHA_", i),
got.Outputs["default"].PermissionsHash)

// Assert ToRetireAPIKeyIds contains the expected values, regardless of the order.
if assert.Len(t, got.Outputs["default"].ToRetireAPIKeyIds, 1) {
assert.Equal(t,
model.ToRetireAPIKeyIdsItems{ID: outputAPIKey.ID, RetiredAt: nowStr},
got.Outputs["default"].ToRetireAPIKeyIds[0])
}

// Assert deprecated fields
assert.Empty(t, got.DefaultAPIKey)
assert.Empty(t, got.DefaultAPIKey)
assert.Empty(t, got.PolicyOutputPermissionsHash)
assert.Nil(t, got.DefaultAPIKeyHistory)
}

func TestMigrateOutputs_no_agent_document(t *testing.T) {
now, err := time.Parse(time.RFC3339, nowStr)
require.NoError(t, err, "could not parse time "+nowStr)
timeNow = func() time.Time {
return now
}

_, bulker := ftesting.SetupCleanIndex(context.Background(), t, FleetAgents)

migratedAgents, err := migrate(context.Background(), bulker, migrateAgentOutputs)
require.NoError(t, err)

assert.Equal(t, 0, migratedAgents)
}