Skip to content

Commit

Permalink
policy to config changed in kibana
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Mar 3, 2020
1 parent 7689ca7 commit 1d35566
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 75 deletions.
4 changes: 2 additions & 2 deletions x-pack/agent/dev-tools/cmd/fakewebapi/action_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"success": true,
"actions": [
{
"type": "POLICY_CHANGE",
"type": "CONFIG_CHANGE",
"data": {
"policy": {
"config": {
"id": "default",
"outputs": {
"default": {
Expand Down
18 changes: 9 additions & 9 deletions x-pack/agent/pkg/agent/application/action_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) {
return &actionStore{log: log, store: store}, nil
}

var action actionPolicyChangeSerializer
var action actionConfigChangeSerializer

dec := yaml.NewDecoder(reader)
if err := dec.Decode(&action); err != nil {
return nil, err
}

apc := fleetapi.ActionPolicyChange(action)
apc := fleetapi.ActionConfigChange(action)

return &actionStore{
log: log,
Expand All @@ -53,7 +53,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.ActionPolicyChange:
case *fleetapi.ActionConfigChange:
// Only persist the action if the action is different.
if s.action != nil && s.action.ID() == v.ID() {
return
Expand All @@ -69,12 +69,12 @@ func (s *actionStore) Save() error {
return nil
}

apc, ok := s.action.(*fleetapi.ActionPolicyChange)
apc, ok := s.action.(*fleetapi.ActionConfigChange)
if !ok {
return fmt.Errorf("incompatible type, expected ActionPolicyChange and received %T", s.action)
}

serialize := actionPolicyChangeSerializer(*apc)
serialize := actionConfigChangeSerializer(*apc)

reader, err := yamlToReader(&serialize)
if err != nil {
Expand All @@ -98,7 +98,7 @@ func (s *actionStore) Actions() []action {
return []action{s.action}
}

// actionPolicyChangeSerializer is a struct that add YAML serialization, I don't think serialization
// actionConfigChangeSerializer is a struct that add 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 @@ -108,14 +108,14 @@ func (s *actionStore) Actions() []action {
// 4. We have two sets of type.
//
// This could be done in a refactoring.
type actionPolicyChangeSerializer struct {
type actionConfigChangeSerializer struct {
ActionID string `yaml:"action_id"`
ActionType string `yaml:"action_type"`
Policy map[string]interface{} `yaml:"policy"`
Config map[string]interface{} `yaml:"config"`
}

// Add a guards between the serializer structs and the original struct.
var _ actionPolicyChangeSerializer = actionPolicyChangeSerializer(fleetapi.ActionPolicyChange{})
var _ actionConfigChangeSerializer = actionConfigChangeSerializer(fleetapi.ActionConfigChange{})

// actionStoreAcker wraps an existing acker and will send any acked event to the action store,
// its up to the action store to decide if we need to persist the event for future replay or just
Expand Down
14 changes: 7 additions & 7 deletions x-pack/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) {
actionPolicyChange := &fleetapi.ActionPolicyChange{
actionConfigChange := &fleetapi.ActionConfigChange{
ActionID: "abc123",
ActionType: "POLICY_CHANGE",
Policy: map[string]interface{}{
ActionType: "CONFIG_CHANGE",
Config: 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(actionPolicyChange)
store.Add(actionConfigChange)
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, actionPolicyChange, actions[0])
require.Equal(t, actionConfigChange, actions[0])
}))

t.Run("when we ACK we save to disk",
withFile(func(t *testing.T, file string) {
actionPolicyChange := &fleetapi.ActionPolicyChange{
actionConfigChange := &fleetapi.ActionConfigChange{
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(), actionPolicyChange))
require.NoError(t, acker.Ack(context.Background(), actionConfigChange))
require.Equal(t, 1, len(store.Actions()))
}))
}
4 changes: 2 additions & 2 deletions x-pack/agent/pkg/agent/application/fleet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ func TestFleetGateway(t *testing.T) {
{
"actions": [
{
"type": "POLICY_CHANGE",
"type": "CONFIG_CHANGE",
"id": "id1",
"data": {
"policy": {
"config": {
"id": "policy-id"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (
"github.com/elastic/beats/x-pack/agent/pkg/fleetapi"
)

type handlerPolicyChange struct {
type handlerConfigChange struct {
log *logger.Logger
emitter emitterFunc
}

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)
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)
if !ok {
return fmt.Errorf("invalid type, expected ActionPolicyChange and received %T", a)
return fmt.Errorf("invalid type, expected ActionConfigChange and received %T", a)
}

c, err := config.NewConfigFrom(action.Policy)
c, err := config.NewConfigFrom(action.Config)
if err != nil {
return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig)
}

h.log.Debugf("HandlerPolicyChange: emit configuration for action %+v", a)
h.log.Debugf("handlerConfigChange: 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 @@ -32,35 +32,35 @@ func TestPolicyChange(t *testing.T) {
log, _ := logger.New()
ack := newNoopAcker()

t.Run("Receive a policy change and successfully emits a raw configuration", func(t *testing.T) {
t.Run("Receive a config change and successfully emits a raw configuration", func(t *testing.T) {
emitter := &mockEmitter{}

policy := map[string]interface{}{"hello": "world"}
action := &fleetapi.ActionPolicyChange{
conf := map[string]interface{}{"hello": "world"}
action := &fleetapi.ActionConfigChange{
ActionID: "abc123",
ActionType: "POLICY_CHANGE",
Policy: policy,
ActionType: "CONFIG_CHANGE",
Config: conf,
}

handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}

err := handler.Handle(context.Background(), action, ack)
require.NoError(t, err)
require.Equal(t, config.MustNewConfigFrom(policy), emitter.policy)
require.Equal(t, config.MustNewConfigFrom(conf), emitter.policy)
})

t.Run("Receive a policy and fail to emits a raw configuration", func(t *testing.T) {
t.Run("Receive a config and fail to emits a raw configuration", func(t *testing.T) {
mockErr := errors.New("error returned")
emitter := &mockEmitter{err: mockErr}

policy := map[string]interface{}{"hello": "world"}
action := &fleetapi.ActionPolicyChange{
conf := map[string]interface{}{"hello": "world"}
action := &fleetapi.ActionConfigChange{
ActionID: "abc123",
ActionType: "POLICY_CHANGE",
Policy: policy,
ActionType: "CONFIG_CHANGE",
Config: conf,
}

handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}

err := handler.Handle(context.Background(), action, ack)
require.Error(t, err)
Expand All @@ -69,21 +69,21 @@ func TestPolicyChange(t *testing.T) {

func TestPolicyAcked(t *testing.T) {
log, _ := logger.New()
t.Run("Policy change should not ACK on error", func(t *testing.T) {
t.Run("Config change should not ACK on error", func(t *testing.T) {
tacker := &testAcker{}

mockErr := errors.New("error returned")
emitter := &mockEmitter{err: mockErr}

policy := map[string]interface{}{"hello": "world"}
config := map[string]interface{}{"hello": "world"}
actionID := "abc123"
action := &fleetapi.ActionPolicyChange{
action := &fleetapi.ActionConfigChange{
ActionID: actionID,
ActionType: "POLICY_CHANGE",
Policy: policy,
ActionType: "CONFIG_CHANGE",
Config: config,
}

handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}

err := handler.Handle(context.Background(), action, tacker)
require.Error(t, err)
Expand All @@ -92,20 +92,20 @@ func TestPolicyAcked(t *testing.T) {
assert.EqualValues(t, 0, len(actions))
})

t.Run("Policy change should ACK", func(t *testing.T) {
t.Run("Config change should ACK", func(t *testing.T) {
tacker := &testAcker{}

emitter := &mockEmitter{}

policy := map[string]interface{}{"hello": "world"}
config := map[string]interface{}{"hello": "world"}
actionID := "abc123"
action := &fleetapi.ActionPolicyChange{
action := &fleetapi.ActionConfigChange{
ActionID: actionID,
ActionType: "POLICY_CHANGE",
Policy: policy,
ActionType: "CONFIG_CHANGE",
Config: config,
}

handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}

err := handler.Handle(context.Background(), action, tacker)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions x-pack/agent/pkg/agent/application/managed_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func newManaged(
}

actionDispatcher.MustRegister(
&fleetapi.ActionPolicyChange{},
&handlerPolicyChange{
&fleetapi.ActionConfigChange{},
&handlerConfigChange{
log: log,
emitter: emit,
},
Expand Down
8 changes: 4 additions & 4 deletions x-pack/agent/pkg/fleetapi/ack_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func TestAck(t *testing.T) {
return mux
}, withAPIKey,
func(t *testing.T, client clienter) {
action := &ActionPolicyChange{
action := &ActionConfigChange{
ActionID: "my-id",
ActionType: "POLICY_CHANGE",
Policy: map[string]interface{}{
"id": "policy_id",
ActionType: "CONFIG_CHANGE",
Config: map[string]interface{}{
"id": "config_id",
},
}

Expand Down
18 changes: 9 additions & 9 deletions x-pack/agent/pkg/fleetapi/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func (a *ActionUnknown) OriginalType() string {
return a.originalType
}

// ActionPolicyChange is a request to apply a new
type ActionPolicyChange struct {
// ActionConfigChange is a request to apply a new
type ActionConfigChange struct {
ActionID string
ActionType string
Policy map[string]interface{} `json:"policy"`
Config map[string]interface{} `json:"config"`
}

func (a *ActionPolicyChange) String() string {
func (a *ActionConfigChange) String() string {
var s strings.Builder
s.WriteString("action_id: ")
s.WriteString(a.ActionID)
Expand All @@ -74,12 +74,12 @@ func (a *ActionPolicyChange) String() string {
}

// Type returns the type of the Action.
func (a *ActionPolicyChange) Type() string {
func (a *ActionConfigChange) Type() string {
return a.ActionType
}

// ID returns the ID of the Action.
func (a *ActionPolicyChange) ID() string {
func (a *ActionConfigChange) ID() string {
return a.ActionID
}

Expand Down Expand Up @@ -107,14 +107,14 @@ func (a *Actions) UnmarshalJSON(data []byte) error {

for _, response := range responses {
switch response.ActionType {
case "POLICY_CHANGE":
action = &ActionPolicyChange{
case "CONFIG_CHANGE":
action = &ActionConfigChange{
ActionID: response.ActionID,
ActionType: response.ActionType,
}
if err := json.Unmarshal(response.Data, action); err != nil {
return errors.New(err,
"fail to decode POLICY_CHANGE action",
"fail to decode CONFIG_CHANGE action",
errors.TypeConfig)
}
default:
Expand Down
7 changes: 5 additions & 2 deletions x-pack/agent/pkg/fleetapi/checkin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -100,15 +101,17 @@ func (e *CheckinCmd) Execute(ctx context.Context, r *CheckinRequest) (*CheckinRe
return nil, extract(resp.Body)
}

rs, _ := ioutil.ReadAll(resp.Body)

checkinResponse := &CheckinResponse{}
decoder := json.NewDecoder(resp.Body)
decoder := json.NewDecoder(bytes.NewReader(rs))
if err := decoder.Decode(checkinResponse); err != nil {
return nil, errors.New(err,
"fail to decode checkin response",
errors.TypeNetwork,
errors.M(errors.MetaKeyURI, cp))
}

fmt.Println(string(rs))
if err := checkinResponse.Validate(); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 1d35566

Please sign in to comment.