@@ -15,8 +15,6 @@ import (
1515 "strings"
1616 "time"
1717
18- "github.com/pkg/errors"
19-
2018 "github.com/elastic/fleet-server/v7/internal/pkg/bulk"
2119 "github.com/elastic/fleet-server/v7/internal/pkg/cache"
2220 "github.com/elastic/fleet-server/v7/internal/pkg/config"
@@ -26,6 +24,7 @@ import (
2624 "github.com/elastic/fleet-server/v7/internal/pkg/logger"
2725 "github.com/elastic/fleet-server/v7/internal/pkg/model"
2826 "github.com/elastic/fleet-server/v7/internal/pkg/policy"
27+ "github.com/pkg/errors"
2928
3029 "github.com/julienschmidt/httprouter"
3130 "github.com/rs/zerolog"
@@ -338,9 +337,8 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag
338337 Int64 ("rev.coordinatorIdx" , rev .CoordinatorIdx ).
339338 Msg ("ack policy revision" )
340339
341- if ok && rev .PolicyID == agent .PolicyID &&
342- (rev .RevisionIdx > currRev ||
343- (rev .RevisionIdx == currRev && rev .CoordinatorIdx > currCoord )) {
340+ if ok && rev .PolicyID == agent .PolicyID && (rev .RevisionIdx > currRev ||
341+ (rev .RevisionIdx == currRev && rev .CoordinatorIdx > currCoord )) {
344342 found = true
345343 currRev = rev .RevisionIdx
346344 currCoord = rev .CoordinatorIdx
@@ -351,7 +349,17 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag
351349 return nil
352350 }
353351
354- ack .invalidateAPIKeys (ctx , agent )
352+ sz := len (agent .DefaultAPIKeyHistory )
353+ if sz > 0 {
354+ ids := make ([]string , sz )
355+ for i := 0 ; i < sz ; i ++ {
356+ ids [i ] = agent .DefaultAPIKeyHistory [i ].ID
357+ }
358+ log .Info ().Strs ("ids" , ids ).Msg ("Invalidate old API keys" )
359+ if err := ack .bulk .APIKeyInvalidate (ctx , ids ... ); err != nil {
360+ log .Info ().Err (err ).Strs ("ids" , ids ).Msg ("Failed to invalidate API keys" )
361+ }
362+ }
355363
356364 body := makeUpdatePolicyBody (
357365 agent .PolicyID ,
@@ -377,24 +385,8 @@ func (ack *AckT) handlePolicyChange(ctx context.Context, zlog zerolog.Logger, ag
377385 return errors .Wrap (err , "handlePolicyChange update" )
378386}
379387
380- func (ack * AckT ) invalidateAPIKeys (ctx context.Context , agent * model.Agent ) {
381- var ids []string
382- for _ , out := range agent .Outputs {
383- for _ , k := range out .ToRetireAPIKeyIds {
384- ids = append (ids , k .ID )
385- }
386- }
387-
388- if len (ids ) > 0 {
389- log .Info ().Strs ("fleet.policy.apiKeyIDsToRetire" , ids ).Msg ("Invalidate old API keys" )
390- if err := ack .bulk .APIKeyInvalidate (ctx , ids ... ); err != nil {
391- log .Info ().Err (err ).Strs ("ids" , ids ).Msg ("Failed to invalidate API keys" )
392- }
393- }
394- }
395-
396388func (ack * AckT ) handleUnenroll (ctx context.Context , zlog zerolog.Logger , agent * model.Agent ) error {
397- apiKeys := agent . APIKeyIDs ( )
389+ apiKeys := _getAPIKeyIDs ( agent )
398390 if len (apiKeys ) > 0 {
399391 zlog = zlog .With ().Strs (LogAPIKeyID , apiKeys ).Logger ()
400392
@@ -449,6 +441,17 @@ func (ack *AckT) handleUpgrade(ctx context.Context, zlog zerolog.Logger, agent *
449441 return nil
450442}
451443
444+ func _getAPIKeyIDs (agent * model.Agent ) []string {
445+ keys := make ([]string , 0 , 1 )
446+ if agent .AccessAPIKeyID != "" {
447+ keys = append (keys , agent .AccessAPIKeyID )
448+ }
449+ if agent .DefaultAPIKeyID != "" {
450+ keys = append (keys , agent .DefaultAPIKeyID )
451+ }
452+ return keys
453+ }
454+
452455// Generate an update script that validates that the policy_id
453456// has not changed underneath us by an upstream process (Kibana or otherwise).
454457// We have a race condition where a user could have assigned a new policy to
0 commit comments