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

Fix v8.5.0 migration painless script #1839

Merged
merged 7 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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: 3 additions & 1 deletion internal/pkg/dl/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ 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";
ctx._source['` + fieldOutputs + `']['default'].to_retire_api_key_ids=ctx._source.default_api_key_history;
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;
}

Map map = new HashMap();
map.put("retired_at", params.` + fieldRetiredAt + `);
Expand Down
78 changes: 77 additions & 1 deletion 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(t *testing.T) {
func TestMigrateOutputs_withDefaultAPIKeyHistory(t *testing.T) {
now, err := time.Parse(time.RFC3339, nowStr)
require.NoError(t, err, "could not parse time "+nowStr)
timeNow = func() time.Time {
Expand Down Expand Up @@ -202,3 +202,79 @@ func TestMigrateOutputs(t *testing.T) {
assert.Nil(t, got.DefaultAPIKeyHistory)
}
}

func TestMigrateOutputs_nil_DefaultAPIKeyHistory(t *testing.T) {
wantOutputType := "elasticsearch"

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)
}