Skip to content

Commit

Permalink
[Elastic Agent] Rename *ConfigChange to PolicyChange (elastic#20779)
Browse files Browse the repository at this point in the history
[Elastic Agent] Rename *ConfigChange to PolicyChange (elastic#20779)

Co-authored-by: Michal Pristas <michal.pristas@gmail.com>
  • Loading branch information
ph and michalpristas committed Sep 29, 2020
1 parent 6cfe43e commit 5377374
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 71 deletions.
2 changes: 2 additions & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- 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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"action": "checkin",
"actions": [
{
"type": "CONFIG_CHANGE",
"type": "POLICY_CHANGE",
"data": {
"config": {
"policy": {
"id": "default",
"outputs": {
"default": {
Expand Down
18 changes: 9 additions & 9 deletions x-pack/elastic-agent/pkg/agent/application/action_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions x-pack/elastic-agent/pkg/agent/application/action_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand All @@ -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()))
Expand All @@ -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",
}

Expand All @@ -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()))
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ func TestFleetGateway(t *testing.T) {
{
"actions": [
{
"type": "CONFIG_CHANGE",
"type": "POLICY_CHANGE",
"id": "id1",
"data": {
"config": {
"policy": {
"id": "policy-id"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ func newManaged(
acker)

actionDispatcher.MustRegister(
&fleetapi.ActionConfigChange{},
&handlerConfigChange{
&fleetapi.ActionPolicyChange{},
&handlerPolicyChange{
log: log,
emitter: emit,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions x-pack/elastic-agent/pkg/fleetapi/ack_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand Down
22 changes: 11 additions & 11 deletions x-pack/elastic-agent/pkg/fleetapi/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 5377374

Please sign in to comment.