Skip to content

Commit ae7ce8a

Browse files
Original ID is used when replace token provided (#10169)
1 parent 08dde42 commit ae7ce8a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

internal/pkg/agent/application/enroll/options.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,17 @@ func MergeOptionsWithMigrateAction(action *fleetapi.ActionMigrate, options Enrol
139139
return EnrollOptions{}, fmt.Errorf("failed to decode enroll options: %w", err)
140140
}
141141

142-
if len(options.ReplaceToken) == 0 {
143-
// do not preserve ID by default
144-
delete(configMap, "id")
145-
options.ID = ""
146-
}
147-
148142
// overwriting what's needed
149143
if len(action.Data.Settings) > 0 {
150144
if err := json.Unmarshal(action.Data.Settings, &configMap); err != nil {
151145
return EnrollOptions{}, fmt.Errorf("failed to decode migrate setting: %w", err)
152146
}
147+
}
153148

149+
if _, ok := configMap["replace_token"]; !ok {
150+
// do not preserve ID by default
151+
delete(configMap, "id")
152+
options.ID = ""
154153
}
155154

156155
cmBytes, err := json.Marshal(configMap)

internal/pkg/agent/application/enroll/options_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,28 @@ func TestMergeOptionsWithMigrateAction(t *testing.T) {
347347
},
348348
false,
349349
},
350+
{
351+
"original id is used when replace token provided",
352+
&fleetapi.ActionMigrate{
353+
Data: fleetapi.ActionMigrateData{
354+
EnrollmentToken: "token",
355+
TargetURI: "uri",
356+
Settings: json.RawMessage(`{"insecure": true, "replace_token": "replace-token", "tags":["a","b"]}`),
357+
},
358+
},
359+
EnrollOptions{
360+
ID: "test-id",
361+
},
362+
EnrollOptions{
363+
EnrollAPIKey: "token",
364+
ID: "test-id",
365+
Insecure: true,
366+
ReplaceToken: "replace-token",
367+
Tags: []string{"a", "b"},
368+
URL: "uri",
369+
},
370+
false,
371+
},
350372
}
351373

352374
for _, tc := range cases {

0 commit comments

Comments
 (0)