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

[Elastic Agent] Rename *ConfigChange to PolicyChange #20779

Merged
merged 9 commits into from
Sep 29, 2020
Merged
Changes from 6 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
12 changes: 12 additions & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
@@ -84,6 +84,18 @@
- Correctly report platform and family. {issue}18665[18665]
- Guard against empty stream.datasource and namespace {pull}18769[18769]
- Fix install service script for windows {pull}18814[18814]
- Properly stops subprocess on shutdown {pull}19567[19567]
- Forward revision number of the configuration to the endpoint. {pull}19759[19759]
- Remove support for logs type and use logfile {pull}19761[19761]
- Avoid comparing uncomparable types on enroll {issue}19976[19976]
- Fix issues with merging of elastic-agent.yml and fleet.yml {pull}20026[20026]
- Unzip failures on Windows 8/Windows server 2012 {pull}20088[20088]
- Fix failing unit tests on windows {pull}20127[20127]
- Improve GRPC stop to be more relaxed {pull}20118[20118]
- Prevent closing closed reader {pull}20214[20214]
- Fix Windows service installation script {pull}20203[20203]
- Fix timeout issue stopping service applications {pull}20256[20256]
- Fix rename *ConfigChange to *PolicyChange to align on changes in the UI. {pull}20779[20779]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops changelog issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wou

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changelog vs changelog.next


==== New features

Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@
"action": "checkin",
"actions": [
{
"type": "CONFIG_CHANGE",
"type": "POLICY_CHANGE",
"data": {
"config": {
"policy": {
"id": "default",
"outputs": {
"default": {
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
@@ -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 {
Original file line number Diff line number Diff line change
@@ -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()))
}))
}
Original file line number Diff line number Diff line change
@@ -208,10 +208,10 @@ func TestFleetGateway(t *testing.T) {
{
"actions": [
{
"type": "CONFIG_CHANGE",
"type": "POLICY_CHANGE",
"id": "id1",
"data": {
"config": {
"policy": {
"id": "policy-id"
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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
}
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
@@ -207,8 +207,8 @@ func newManaged(
acker)

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