Skip to content

Commit

Permalink
[Fleet] Handle POLICY_REASSIGN action (#24616)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Mar 18, 2021
1 parent 5c6f1b6 commit f8ca092
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions x-pack/elastic-agent/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ func newManaged(
policyChanger,
)

actionDispatcher.MustRegister(
&fleetapi.ActionPolicyReassign{},
&handlerPolicyReassign{log: log},
)

actionDispatcher.MustRegister(
&fleetapi.ActionUnenroll{},
&handlerUnenroll{
Expand Down
32 changes: 32 additions & 0 deletions x-pack/elastic-agent/pkg/fleetapi/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f8ca092

Please sign in to comment.