From f8ca092166b2588134ea11ce76d72b4bf9ae47e3 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Thu, 18 Mar 2021 09:33:15 -0400 Subject: [PATCH] [Fleet] Handle POLICY_REASSIGN action (#24616) --- .../handler_action_policy_reassign.go | 29 +++++++++++++++++ .../pkg/agent/application/managed_mode.go | 5 +++ x-pack/elastic-agent/pkg/fleetapi/action.go | 32 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 x-pack/elastic-agent/pkg/agent/application/handler_action_policy_reassign.go diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_reassign.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_reassign.go new file mode 100644 index 000000000000..41cc5d788efb --- /dev/null +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_reassign.go @@ -0,0 +1,29 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package application + +import ( + "context" + + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" +) + +// handlerPolicyReassign handles policy reassign change coming from fleet. +type handlerPolicyReassign struct { + log *logger.Logger +} + +// Handle handles POLICY_REASSIGN action. +func (h *handlerPolicyReassign) Handle(ctx context.Context, a action, acker fleetAcker) error { + h.log.Debugf("handlerPolicyReassign: action '%+v' received", a) + + if err := acker.Ack(ctx, a); err != nil { + h.log.Errorf("failed to acknowledge POLICY_REASSIGN action with id '%s'", a.ID) + } else if err := acker.Commit(ctx); err != nil { + h.log.Errorf("failed to commit acker after acknowledging action with id '%s'", a.ID) + } + + return nil +} diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go index 7eb95709a7e8..af80927c3e5d 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -193,6 +193,11 @@ func newManaged( policyChanger, ) + actionDispatcher.MustRegister( + &fleetapi.ActionPolicyReassign{}, + &handlerPolicyReassign{log: log}, + ) + actionDispatcher.MustRegister( &fleetapi.ActionUnenroll{}, &handlerUnenroll{ diff --git a/x-pack/elastic-agent/pkg/fleetapi/action.go b/x-pack/elastic-agent/pkg/fleetapi/action.go index d836aa801c23..55bf9d1ce269 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/action.go +++ b/x-pack/elastic-agent/pkg/fleetapi/action.go @@ -19,6 +19,8 @@ const ( ActionTypeUnenroll = "UNENROLL" // ActionTypePolicyChange specifies policy change action. ActionTypePolicyChange = "POLICY_CHANGE" + // ActionTypePolicyReassign specifies policy reassign action. + ActionTypePolicyReassign = "POLICY_REASSIGN" // ActionTypeSettings specifies change of agent settings. ActionTypeSettings = "SETTINGS" // ActionTypeApplication specifies agent action. @@ -70,6 +72,31 @@ func (a *ActionUnknown) OriginalType() string { return a.originalType } +// ActionPolicyReassign is a request to apply a new +type ActionPolicyReassign struct { + ActionID string + ActionType string +} + +func (a *ActionPolicyReassign) String() string { + var s strings.Builder + s.WriteString("action_id: ") + s.WriteString(a.ActionID) + s.WriteString(", type: ") + s.WriteString(a.ActionType) + return s.String() +} + +// Type returns the type of the Action. +func (a *ActionPolicyReassign) Type() string { + return a.ActionType +} + +// ID returns the ID of the Action. +func (a *ActionPolicyReassign) ID() string { + return a.ActionID +} + // ActionPolicyChange is a request to apply a new type ActionPolicyChange struct { ActionID string @@ -241,6 +268,11 @@ func (a *Actions) UnmarshalJSON(data []byte) error { "fail to decode POLICY_CHANGE action", errors.TypeConfig) } + case ActionTypePolicyReassign: + action = &ActionPolicyReassign{ + ActionID: response.ActionID, + ActionType: response.ActionType, + } case ActionTypeApplication: action = &ActionApp{ ActionID: response.ActionID,