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

[Fleet] Handle POLICY_REASSIGN action #24616

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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