diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 3963dda9a12..cac7a0ae5ca 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -10,6 +10,7 @@ - Docker container is not run as root by default. {pull}21213[21213] ==== Bugfixes +- Fix rename *ConfigChange to *PolicyChange to align on changes in the UI. {pull}20779[20779] - Thread safe sorted set {pull}21290[21290] - Copy Action store on upgrade {pull}21298[21298] - Include inputs in action store actions {pull}21298[21298] diff --git a/x-pack/elastic-agent/dev-tools/cmd/fakewebapi/action_example.json b/x-pack/elastic-agent/dev-tools/cmd/fakewebapi/action_example.json index 327b79ed347..f08e98942ea 100644 --- a/x-pack/elastic-agent/dev-tools/cmd/fakewebapi/action_example.json +++ b/x-pack/elastic-agent/dev-tools/cmd/fakewebapi/action_example.json @@ -2,9 +2,9 @@ "action": "checkin", "actions": [ { - "type": "CONFIG_CHANGE", + "type": "POLICY_CHANGE", "data": { - "config": { + "policy": { "id": "default", "outputs": { "default": { diff --git a/x-pack/elastic-agent/pkg/agent/application/action_store.go b/x-pack/elastic-agent/pkg/agent/application/action_store.go index 25dbf7a5b82..ce4ea785cf7 100644 --- a/x-pack/elastic-agent/pkg/agent/application/action_store.go +++ b/x-pack/elastic-agent/pkg/agent/application/action_store.go @@ -35,7 +35,7 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) { } defer reader.Close() - var action actionConfigChangeSerializer + var action ActionPolicyChangeSerializer dec := yaml.NewDecoder(reader) err = dec.Decode(&action) @@ -49,7 +49,7 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) { return nil, err } - apc := fleetapi.ActionConfigChange(action) + apc := fleetapi.ActionPolicyChange(action) return &actionStore{ log: log, @@ -62,7 +62,7 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) { // any other type of action will be silently ignored. func (s *actionStore) Add(a action) { switch v := a.(type) { - case *fleetapi.ActionConfigChange, *fleetapi.ActionUnenroll: + case *fleetapi.ActionPolicyChange, *fleetapi.ActionUnenroll: // Only persist the action if the action is different. if s.action != nil && s.action.ID() == v.ID() { return @@ -79,8 +79,8 @@ func (s *actionStore) Save() error { } var reader io.Reader - if apc, ok := s.action.(*fleetapi.ActionConfigChange); ok { - serialize := actionConfigChangeSerializer(*apc) + if apc, ok := s.action.(*fleetapi.ActionPolicyChange); ok { + serialize := ActionPolicyChangeSerializer(*apc) r, err := yamlToReader(&serialize) if err != nil { @@ -120,7 +120,7 @@ func (s *actionStore) Actions() []action { return []action{s.action} } -// actionConfigChangeSerializer is a struct that adds a YAML serialization, I don't think serialization +// ActionPolicyChangeSerializer is a struct that adds a YAML serialization, I don't think serialization // is a concern of the fleetapi package. I went this route so I don't have to do much refactoring. // // There are four ways to achieve the same results: @@ -130,14 +130,14 @@ func (s *actionStore) Actions() []action { // 4. We have two sets of type. // // This could be done in a refactoring. -type actionConfigChangeSerializer struct { +type ActionPolicyChangeSerializer struct { ActionID string `yaml:"action_id"` ActionType string `yaml:"action_type"` - Config map[string]interface{} `yaml:"config"` + Policy map[string]interface{} `yaml:"policy"` } // Add a guards between the serializer structs and the original struct. -var _ actionConfigChangeSerializer = actionConfigChangeSerializer(fleetapi.ActionConfigChange{}) +var _ ActionPolicyChangeSerializer = ActionPolicyChangeSerializer(fleetapi.ActionPolicyChange{}) // actionUnenrollSerializer is a struct that adds a YAML serialization, type actionUnenrollSerializer struct { diff --git a/x-pack/elastic-agent/pkg/agent/application/action_store_test.go b/x-pack/elastic-agent/pkg/agent/application/action_store_test.go index 4205deda8b6..f2691d66db6 100644 --- a/x-pack/elastic-agent/pkg/agent/application/action_store_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/action_store_test.go @@ -57,10 +57,10 @@ func TestActionStore(t *testing.T) { t.Run("can save to disk known action type", withFile(func(t *testing.T, file string) { - actionConfigChange := &fleetapi.ActionConfigChange{ + ActionPolicyChange := &fleetapi.ActionPolicyChange{ ActionID: "abc123", - ActionType: "CONFIG_CHANGE", - Config: map[string]interface{}{ + ActionType: "POLICY_CHANGE", + Policy: map[string]interface{}{ "hello": "world", }, } @@ -70,7 +70,7 @@ func TestActionStore(t *testing.T) { require.NoError(t, err) require.Equal(t, 0, len(store.Actions())) - store.Add(actionConfigChange) + store.Add(ActionPolicyChange) err = store.Save() require.NoError(t, err) require.Equal(t, 1, len(store.Actions())) @@ -82,12 +82,12 @@ func TestActionStore(t *testing.T) { actions := store1.Actions() require.Equal(t, 1, len(actions)) - require.Equal(t, actionConfigChange, actions[0]) + require.Equal(t, ActionPolicyChange, actions[0]) })) t.Run("when we ACK we save to disk", withFile(func(t *testing.T, file string) { - actionConfigChange := &fleetapi.ActionConfigChange{ + ActionPolicyChange := &fleetapi.ActionPolicyChange{ ActionID: "abc123", } @@ -98,7 +98,7 @@ func TestActionStore(t *testing.T) { acker := newActionStoreAcker(&testAcker{}, store) require.Equal(t, 0, len(store.Actions())) - require.NoError(t, acker.Ack(context.Background(), actionConfigChange)) + require.NoError(t, acker.Ack(context.Background(), ActionPolicyChange)) require.Equal(t, 1, len(store.Actions())) })) } diff --git a/x-pack/elastic-agent/pkg/agent/application/fleet_gateway_test.go b/x-pack/elastic-agent/pkg/agent/application/fleet_gateway_test.go index bd9037416dc..cfcd1f46994 100644 --- a/x-pack/elastic-agent/pkg/agent/application/fleet_gateway_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/fleet_gateway_test.go @@ -208,10 +208,10 @@ func TestFleetGateway(t *testing.T) { { "actions": [ { - "type": "CONFIG_CHANGE", + "type": "POLICY_CHANGE", "id": "id1", "data": { - "config": { + "policy": { "id": "policy-id" } } diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go index 34fd5716980..81dc1444816 100644 --- a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go @@ -13,24 +13,24 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi" ) -type handlerConfigChange struct { +type handlerPolicyChange struct { log *logger.Logger emitter emitterFunc } -func (h *handlerConfigChange) Handle(ctx context.Context, a action, acker fleetAcker) error { - h.log.Debugf("handlerConfigChange: action '%+v' received", a) - action, ok := a.(*fleetapi.ActionConfigChange) +func (h *handlerPolicyChange) Handle(ctx context.Context, a action, acker fleetAcker) error { + h.log.Debugf("handlerPolicyChange: action '%+v' received", a) + action, ok := a.(*fleetapi.ActionPolicyChange) if !ok { - return fmt.Errorf("invalid type, expected ActionConfigChange and received %T", a) + return fmt.Errorf("invalid type, expected ActionPolicyChange and received %T", a) } - c, err := LoadConfig(action.Config) + c, err := LoadConfig(action.Policy) if err != nil { return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig) } - h.log.Debugf("handlerConfigChange: emit configuration for action %+v", a) + h.log.Debugf("handlerPolicyChange: emit configuration for action %+v", a) if err := h.emitter(c); err != nil { return err } diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change_test.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change_test.go index b95c259e7c7..ce4802b68e6 100644 --- a/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change_test.go @@ -36,13 +36,13 @@ func TestPolicyChange(t *testing.T) { emitter := &mockEmitter{} conf := map[string]interface{}{"hello": "world"} - action := &fleetapi.ActionConfigChange{ + action := &fleetapi.ActionPolicyChange{ ActionID: "abc123", - ActionType: "CONFIG_CHANGE", - Config: conf, + ActionType: "POLICY_CHANGE", + Policy: conf, } - handler := &handlerConfigChange{log: log, emitter: emitter.Emitter} + handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter} err := handler.Handle(context.Background(), action, ack) require.NoError(t, err) @@ -54,13 +54,13 @@ func TestPolicyChange(t *testing.T) { emitter := &mockEmitter{err: mockErr} conf := map[string]interface{}{"hello": "world"} - action := &fleetapi.ActionConfigChange{ + action := &fleetapi.ActionPolicyChange{ ActionID: "abc123", - ActionType: "CONFIG_CHANGE", - Config: conf, + ActionType: "POLICY_CHANGE", + Policy: conf, } - handler := &handlerConfigChange{log: log, emitter: emitter.Emitter} + handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter} err := handler.Handle(context.Background(), action, ack) require.Error(t, err) @@ -77,13 +77,13 @@ func TestPolicyAcked(t *testing.T) { config := map[string]interface{}{"hello": "world"} actionID := "abc123" - action := &fleetapi.ActionConfigChange{ + action := &fleetapi.ActionPolicyChange{ ActionID: actionID, - ActionType: "CONFIG_CHANGE", - Config: config, + ActionType: "POLICY_CHANGE", + Policy: config, } - handler := &handlerConfigChange{log: log, emitter: emitter.Emitter} + handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter} err := handler.Handle(context.Background(), action, tacker) require.Error(t, err) @@ -99,13 +99,13 @@ func TestPolicyAcked(t *testing.T) { config := map[string]interface{}{"hello": "world"} actionID := "abc123" - action := &fleetapi.ActionConfigChange{ + action := &fleetapi.ActionPolicyChange{ ActionID: actionID, - ActionType: "CONFIG_CHANGE", - Config: config, + ActionType: "POLICY_CHANGE", + Policy: config, } - handler := &handlerConfigChange{log: log, emitter: emitter.Emitter} + handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter} err := handler.Handle(context.Background(), action, tacker) require.NoError(t, err) diff --git a/x-pack/elastic-agent/pkg/agent/application/inspect_config_cmd.go b/x-pack/elastic-agent/pkg/agent/application/inspect_config_cmd.go index 2c53fc62bf2..edf1ad8cdf2 100644 --- a/x-pack/elastic-agent/pkg/agent/application/inspect_config_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/inspect_config_cmd.go @@ -106,13 +106,13 @@ func loadFleetConfig(cfg *config.Config) (map[string]interface{}, error) { } for _, c := range as.Actions() { - cfgChange, ok := c.(*fleetapi.ActionConfigChange) + cfgChange, ok := c.(*fleetapi.ActionPolicyChange) if !ok { continue } fmt.Println("Action ID:", cfgChange.ID()) - return cfgChange.Config, nil + return cfgChange.Policy, nil } return nil, 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 a4e4bf92379..12a9c242780 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -207,8 +207,8 @@ func newManaged( acker) actionDispatcher.MustRegister( - &fleetapi.ActionConfigChange{}, - &handlerConfigChange{ + &fleetapi.ActionPolicyChange{}, + &handlerPolicyChange{ log: log, emitter: emit, }, diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go index 9b51016a126..81f2419f936 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go @@ -40,8 +40,8 @@ func TestManagedModeRouting(t *testing.T) { require.NoError(t, err) actionDispatcher.MustRegister( - &fleetapi.ActionConfigChange{}, - &handlerConfigChange{ + &fleetapi.ActionPolicyChange{}, + &handlerPolicyChange{ log: log, emitter: emit, }, @@ -100,9 +100,9 @@ const fleetResponse = ` "action": "checkin", "actions": [{ "agent_id": "17e93530-7f42-11ea-9330-71e968b29fa4", - "type": "CONFIG_CHANGE", + "type": "POLICY_CHANGE", "data": { - "config": { + "policy": { "id": "86561d50-7f3b-11ea-9fab-3db3bdb4efa4", "outputs": { "default": { diff --git a/x-pack/elastic-agent/pkg/fleetapi/ack_cmd_test.go b/x-pack/elastic-agent/pkg/fleetapi/ack_cmd_test.go index a9e3aebc25b..75940e928b7 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/ack_cmd_test.go +++ b/x-pack/elastic-agent/pkg/fleetapi/ack_cmd_test.go @@ -46,10 +46,10 @@ func TestAck(t *testing.T) { return mux }, withAPIKey, func(t *testing.T, client clienter) { - action := &ActionConfigChange{ + action := &ActionPolicyChange{ ActionID: "my-id", - ActionType: "CONFIG_CHANGE", - Config: map[string]interface{}{ + ActionType: "POLICY_CHANGE", + Policy: map[string]interface{}{ "id": "config_id", }, } diff --git a/x-pack/elastic-agent/pkg/fleetapi/action.go b/x-pack/elastic-agent/pkg/fleetapi/action.go index efb4e1672aa..d53b7fdfcfb 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/action.go +++ b/x-pack/elastic-agent/pkg/fleetapi/action.go @@ -17,8 +17,8 @@ const ( ActionTypeUpgrade = "UPGRADE" // ActionTypeUnenroll specifies unenroll action. ActionTypeUnenroll = "UNENROLL" - // ActionTypeConfigChange specifies config change action. - ActionTypeConfigChange = "CONFIG_CHANGE" + // ActionTypePolicyChange specifies policy change action. + ActionTypePolicyChange = "POLICY_CHANGE" ) // Action base interface for all the implemented action from the fleet API. @@ -66,14 +66,14 @@ func (a *ActionUnknown) OriginalType() string { return a.originalType } -// ActionConfigChange is a request to apply a new -type ActionConfigChange struct { +// ActionPolicyChange is a request to apply a new +type ActionPolicyChange struct { ActionID string ActionType string - Config map[string]interface{} `json:"config"` + Policy map[string]interface{} `json:"policy"` } -func (a *ActionConfigChange) String() string { +func (a *ActionPolicyChange) String() string { var s strings.Builder s.WriteString("action_id: ") s.WriteString(a.ActionID) @@ -83,12 +83,12 @@ func (a *ActionConfigChange) String() string { } // Type returns the type of the Action. -func (a *ActionConfigChange) Type() string { +func (a *ActionPolicyChange) Type() string { return a.ActionType } // ID returns the ID of the Action. -func (a *ActionConfigChange) ID() string { +func (a *ActionPolicyChange) ID() string { return a.ActionID } @@ -169,14 +169,14 @@ func (a *Actions) UnmarshalJSON(data []byte) error { for _, response := range responses { switch response.ActionType { - case ActionTypeConfigChange: - action = &ActionConfigChange{ + case ActionTypePolicyChange: + action = &ActionPolicyChange{ ActionID: response.ActionID, ActionType: response.ActionType, } if err := json.Unmarshal(response.Data, action); err != nil { return errors.New(err, - "fail to decode CONFIG_CHANGE action", + "fail to decode POLICY_CHANGE action", errors.TypeConfig) } case ActionTypeUnenroll: diff --git a/x-pack/elastic-agent/pkg/fleetapi/checkin_cmd_test.go b/x-pack/elastic-agent/pkg/fleetapi/checkin_cmd_test.go index 953b86a260e..af8b4da81ea 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/checkin_cmd_test.go +++ b/x-pack/elastic-agent/pkg/fleetapi/checkin_cmd_test.go @@ -56,10 +56,10 @@ func TestCheckin(t *testing.T) { raw := ` { "actions": [{ - "type": "CONFIG_CHANGE", + "type": "POLICY_CHANGE", "id": "id1", "data": { - "config": { + "policy": { "id": "policy-id", "outputs": { "default": { @@ -102,7 +102,7 @@ func TestCheckin(t *testing.T) { // ActionPolicyChange require.Equal(t, "id1", r.Actions[0].ID()) - require.Equal(t, "CONFIG_CHANGE", r.Actions[0].Type()) + require.Equal(t, "POLICY_CHANGE", r.Actions[0].Type()) }, )) @@ -112,10 +112,10 @@ func TestCheckin(t *testing.T) { { "actions": [ { - "type": "CONFIG_CHANGE", + "type": "POLICY_CHANGE", "id": "id1", "data": { - "config": { + "policy": { "id": "policy-id", "outputs": { "default": { @@ -163,7 +163,7 @@ func TestCheckin(t *testing.T) { // ActionPolicyChange require.Equal(t, "id1", r.Actions[0].ID()) - require.Equal(t, "CONFIG_CHANGE", r.Actions[0].Type()) + require.Equal(t, "POLICY_CHANGE", r.Actions[0].Type()) // UnknownAction require.Equal(t, "id2", r.Actions[1].ID())