From 8a8f503cf9dc26ea1c81640e0158e5efbdd6add5 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 29 Jun 2020 13:02:16 +0200 Subject: [PATCH 1/4] phase 1 unenroll --- .../pkg/agent/application/managed_mode.go | 8 +++++ x-pack/elastic-agent/pkg/fleetapi/action.go | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+) 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 6ecd6321cb2..7b1e32873a8 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -177,6 +177,14 @@ func newManaged( }, ) + actionDispatcher.MustRegister( + &fleetapi.ActionUnenroll{}, + &handlerUnenroll{ + log: log, + emitter: emit, + }, + ) + actionDispatcher.MustRegister( &fleetapi.ActionUnknown{}, &handlerUnknown{log: log}, diff --git a/x-pack/elastic-agent/pkg/fleetapi/action.go b/x-pack/elastic-agent/pkg/fleetapi/action.go index bf59bc22e1a..3894fdfbf09 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/action.go +++ b/x-pack/elastic-agent/pkg/fleetapi/action.go @@ -83,6 +83,31 @@ func (a *ActionConfigChange) ID() string { return a.ActionID } +// ActionUnenroll is a request for agent to unhook from fleet. +type ActionUnenroll struct { + ActionID string + ActionType string +} + +func (a *ActionUnenroll) 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 *ActionUnenroll) Type() string { + return a.ActionType +} + +// ID returns the ID of the Action. +func (a *ActionUnenroll) ID() string { + return a.ActionID +} + // Actions is a list of Actions to executes and allow to unmarshal heterogenous action type. type Actions []Action @@ -117,6 +142,16 @@ func (a *Actions) UnmarshalJSON(data []byte) error { "fail to decode CONFIG_CHANGE action", errors.TypeConfig) } + case "UNENROLL": + action = &ActionUnenroll{ + ActionID: response.ActionID, + ActionType: response.ActionType, + } + if err := json.Unmarshal(response.Data, action); err != nil { + return errors.New(err, + "fail to decode UNENROLL action", + errors.TypeConfig) + } default: action = &ActionUnknown{ ActionID: response.ActionID, From 5dbb99f2160d9555b3104133706f6387724920b4 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 29 Jun 2020 13:02:43 +0200 Subject: [PATCH 2/4] missing file --- .../application/handler_action_unenroll.go | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go new file mode 100644 index 00000000000..b5be5d6e5bc --- /dev/null +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go @@ -0,0 +1,42 @@ +// 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" + "fmt" + + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi" +) + +type handlerUnenroll struct { + log *logger.Logger + emitter emitterFunc +} + +func (h *handlerUnenroll) Handle(ctx context.Context, a action, acker fleetAcker) error { + h.log.Debugf("handlerUnenroll: action '%+v' received", a) + action, ok := a.(*fleetapi.ActionUnenroll) + if !ok { + return fmt.Errorf("invalid type, expected ActionUnenroll and received %T", a) + } + + // executing empty config stops all the running processes + emptyConfig := make(map[string]interface{}) + c, err := config.NewConfigFrom(emptyConfig) + if err != nil { + return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig) + } + + h.log.Debugf("handlerUnenroll: emit configuration for action %+v", a) + if err := h.emitter(c); err != nil { + return err + } + + return acker.Ack(ctx, action) +} From d152222d523d85d99b691cc1f550488bf44554de Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 30 Jun 2020 11:00:54 +0200 Subject: [PATCH 3/4] unenroll working --- .../application/handler_action_unenroll.go | 22 +++++++------------ .../pkg/agent/application/local_mode.go | 1 + .../pkg/agent/application/managed_mode.go | 5 +++-- .../agent/application/managed_mode_test.go | 7 +++--- .../pkg/agent/application/router.go | 9 ++++---- .../pkg/agent/application/router_test.go | 3 ++- .../pkg/agent/application/stream.go | 5 +++-- .../config_request.go | 11 +++++++++- .../config_request_test.go | 2 +- .../pkg/agent/configrequest/request.go | 2 ++ .../pkg/agent/operation/monitoring.go | 2 +- .../pkg/agent/operation/monitoring_test.go | 3 +++ .../pkg/agent/operation/operator.go | 7 ++++++ .../pkg/agent/stateresolver/resolve_test.go | 12 ++++++++++ .../pkg/artifact/install/tar/tar_installer.go | 2 +- .../core/monitoring/beats/beats_monitor.go | 7 ++++++ .../pkg/core/monitoring/monitor.go | 1 + .../pkg/core/monitoring/noop/noop_monitor.go | 3 +++ x-pack/elastic-agent/pkg/fleetapi/action.go | 5 ----- 19 files changed, 73 insertions(+), 36 deletions(-) rename x-pack/elastic-agent/pkg/agent/{application => configrequest}/config_request.go (83%) rename x-pack/elastic-agent/pkg/agent/{application => configrequest}/config_request_test.go (97%) diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go index b5be5d6e5bc..a1f12179b42 100644 --- a/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go @@ -8,15 +8,15 @@ import ( "context" "fmt" - "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" - "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi" ) type handlerUnenroll struct { - log *logger.Logger - emitter emitterFunc + log *logger.Logger + emitter emitterFunc + dispatcher programsDispatcher } func (h *handlerUnenroll) Handle(ctx context.Context, a action, acker fleetAcker) error { @@ -26,17 +26,11 @@ func (h *handlerUnenroll) Handle(ctx context.Context, a action, acker fleetAcker return fmt.Errorf("invalid type, expected ActionUnenroll and received %T", a) } - // executing empty config stops all the running processes - emptyConfig := make(map[string]interface{}) - c, err := config.NewConfigFrom(emptyConfig) - if err != nil { - return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig) - } + // Providing empty map will close all pipelines + noPrograms := make(map[routingKey][]program.Program) + h.dispatcher.Dispatch(a.ID(), noPrograms) - h.log.Debugf("handlerUnenroll: emit configuration for action %+v", a) - if err := h.emitter(c); err != nil { - return err - } + // TODO: clean action store return acker.Ack(ctx, action) } diff --git a/x-pack/elastic-agent/pkg/agent/application/local_mode.go b/x-pack/elastic-agent/pkg/agent/application/local_mode.go index dc7a81e198a..c6dc88739a0 100644 --- a/x-pack/elastic-agent/pkg/agent/application/local_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/local_mode.go @@ -26,6 +26,7 @@ type emitterFunc func(*config.Config) error // ConfigHandler is capable of handling config and perform actions at it. type ConfigHandler interface { HandleConfig(configrequest.Request) error + Close() error } type discoverFunc func() ([]string, error) 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 7b1e32873a8..c9fc13e9b9d 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -180,8 +180,9 @@ func newManaged( actionDispatcher.MustRegister( &fleetapi.ActionUnenroll{}, &handlerUnenroll{ - log: log, - emitter: emit, + log: log, + emitter: emit, + dispatcher: router, }, ) 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 b5303d0dc60..9229294e007 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 @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/filters" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi" ) @@ -68,16 +69,16 @@ func testActions() ([]action, error) { } type mockStreamStore struct { - store []*configRequest + store []configrequest.Request } func newMockStreamStore() *mockStreamStore { return &mockStreamStore{ - store: make([]*configRequest, 0), + store: make([]configrequest.Request, 0), } } -func (m *mockStreamStore) Execute(cr *configRequest) error { +func (m *mockStreamStore) Execute(cr configrequest.Request) error { m.store = append(m.store, cr) return nil } diff --git a/x-pack/elastic-agent/pkg/agent/application/router.go b/x-pack/elastic-agent/pkg/agent/application/router.go index b48156d7d48..2329ff91b23 100644 --- a/x-pack/elastic-agent/pkg/agent/application/router.go +++ b/x-pack/elastic-agent/pkg/agent/application/router.go @@ -7,7 +7,9 @@ package application import ( "fmt" "strings" + "time" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/sorted" @@ -19,7 +21,7 @@ var defautlRK = "DEFAULT" type routingKey = string type stream interface { - Execute(*configRequest) error + Execute(configrequest.Request) error Close() error } @@ -72,10 +74,7 @@ func (r *router) Dispatch(id string, grpProg map[routingKey][]program.Program) e return fmt.Errorf("could not find programs for routing key %s", rk) } - req := &configRequest{ - id: id, - programs: programs.([]program.Program), - } + req := configrequest.New(id, time.Now(), programs.([]program.Program)) r.log.Debugf( "Streams %s need to run config with ID %s and programs: %s", diff --git a/x-pack/elastic-agent/pkg/agent/application/router_test.go b/x-pack/elastic-agent/pkg/agent/application/router_test.go index 6edcd145eea..acb106eb1c5 100644 --- a/x-pack/elastic-agent/pkg/agent/application/router_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/router_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) @@ -199,7 +200,7 @@ func newMockStream(rk routingKey, notify notifyFunc) *mockStream { } } -func (m *mockStream) Execute(req *configRequest) error { +func (m *mockStream) Execute(req configrequest.Request) error { m.event(executeOp, req) return nil } diff --git a/x-pack/elastic-agent/pkg/agent/application/stream.go b/x-pack/elastic-agent/pkg/agent/application/stream.go index 88262300d9a..fbd8321e249 100644 --- a/x-pack/elastic-agent/pkg/agent/application/stream.go +++ b/x-pack/elastic-agent/pkg/agent/application/stream.go @@ -7,6 +7,7 @@ package application import ( "context" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/operation" operatorCfg "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/operation/config" @@ -28,10 +29,10 @@ type operatorStream struct { } func (b *operatorStream) Close() error { - return b.configHandler.HandleConfig(&configRequest{}) + return b.configHandler.Close() } -func (b *operatorStream) Execute(cfg *configRequest) error { +func (b *operatorStream) Execute(cfg configrequest.Request) error { return b.configHandler.HandleConfig(cfg) } diff --git a/x-pack/elastic-agent/pkg/agent/application/config_request.go b/x-pack/elastic-agent/pkg/agent/configrequest/config_request.go similarity index 83% rename from x-pack/elastic-agent/pkg/agent/application/config_request.go rename to x-pack/elastic-agent/pkg/agent/configrequest/config_request.go index ae3c067429f..1f08d95de2f 100644 --- a/x-pack/elastic-agent/pkg/agent/application/config_request.go +++ b/x-pack/elastic-agent/pkg/agent/configrequest/config_request.go @@ -2,7 +2,7 @@ // 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 +package configrequest import ( "strings" @@ -19,6 +19,15 @@ type configRequest struct { programs []program.Program } +// New created a new Request. +func New(id string, createdAt time.Time, programs []program.Program) Request { + return &configRequest{ + id: id, + createdAt: createdAt, + programs: programs, + } +} + func (c *configRequest) String() string { names := c.ProgramNames() return "[" + c.ShortID() + "] Config: " + strings.Join(names, ", ") diff --git a/x-pack/elastic-agent/pkg/agent/application/config_request_test.go b/x-pack/elastic-agent/pkg/agent/configrequest/config_request_test.go similarity index 97% rename from x-pack/elastic-agent/pkg/agent/application/config_request_test.go rename to x-pack/elastic-agent/pkg/agent/configrequest/config_request_test.go index f0140aa8ffd..eb294d73650 100644 --- a/x-pack/elastic-agent/pkg/agent/application/config_request_test.go +++ b/x-pack/elastic-agent/pkg/agent/configrequest/config_request_test.go @@ -2,7 +2,7 @@ // 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 +package configrequest import ( "testing" diff --git a/x-pack/elastic-agent/pkg/agent/configrequest/request.go b/x-pack/elastic-agent/pkg/agent/configrequest/request.go index b3cb3063ecc..d5ac8759677 100644 --- a/x-pack/elastic-agent/pkg/agent/configrequest/request.go +++ b/x-pack/elastic-agent/pkg/agent/configrequest/request.go @@ -13,6 +13,8 @@ import ( // Request is the minimal interface a config request must have. type Request interface { ID() string + ShortID() string CreatedAt() time.Time Programs() []program.Program + ProgramNames() []string } diff --git a/x-pack/elastic-agent/pkg/agent/operation/monitoring.go b/x-pack/elastic-agent/pkg/agent/operation/monitoring.go index 6aad94a2e62..81fa1bc8c7a 100644 --- a/x-pack/elastic-agent/pkg/agent/operation/monitoring.go +++ b/x-pack/elastic-agent/pkg/agent/operation/monitoring.go @@ -62,7 +62,7 @@ func (o *Operator) handleStartSidecar(s configrequest.Step) (result error) { } func (o *Operator) handleStopSidecar(s configrequest.Step) (result error) { - for _, step := range o.getMonitoringSteps(s) { + for _, step := range o.generateMonitoringSteps(s.Version, nil) { p, _, err := getProgramFromStepWithTags(step, o.config.DownloadConfig, monitoringTags()) if err != nil { return errors.New(err, diff --git a/x-pack/elastic-agent/pkg/agent/operation/monitoring_test.go b/x-pack/elastic-agent/pkg/agent/operation/monitoring_test.go index e53e16b08e5..e77e27b0ad2 100644 --- a/x-pack/elastic-agent/pkg/agent/operation/monitoring_test.go +++ b/x-pack/elastic-agent/pkg/agent/operation/monitoring_test.go @@ -174,6 +174,9 @@ func (b *testMonitor) EnrichArgs(_ string, _ string, args []string, _ bool) []st // Cleanup cleans up all drops. func (b *testMonitor) Cleanup(string, string) error { return nil } +// Close closes the monitor. +func (b *testMonitor) Close() {} + // Prepare executes steps in order for monitoring to work correctly func (b *testMonitor) Prepare(string, string, int, int) error { return nil } diff --git a/x-pack/elastic-agent/pkg/agent/operation/operator.go b/x-pack/elastic-agent/pkg/agent/operation/operator.go index ed0b2d0ba43..21a5adf653b 100644 --- a/x-pack/elastic-agent/pkg/agent/operation/operator.go +++ b/x-pack/elastic-agent/pkg/agent/operation/operator.go @@ -10,6 +10,7 @@ import ( "os" "strings" "sync" + "time" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configrequest" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" @@ -126,6 +127,12 @@ func (o *Operator) State() map[string]state.State { return result } +// Close stops all programs handled by operator +func (o *Operator) Close() error { + o.monitor.Close() + return o.HandleConfig(configrequest.New("", time.Now(), nil)) +} + // HandleConfig handles configuration for a pipeline and performs actions to achieve this configuration. func (o *Operator) HandleConfig(cfg configrequest.Request) error { _, steps, ack, err := o.stateResolver.Resolve(cfg) diff --git a/x-pack/elastic-agent/pkg/agent/stateresolver/resolve_test.go b/x-pack/elastic-agent/pkg/agent/stateresolver/resolve_test.go index e6bf53a0ad4..b284ee51b33 100644 --- a/x-pack/elastic-agent/pkg/agent/stateresolver/resolve_test.go +++ b/x-pack/elastic-agent/pkg/agent/stateresolver/resolve_test.go @@ -346,6 +346,10 @@ func (c *cfg) ID() string { return c.id } +func (c *cfg) ShortID() string { + return c.id +} + func (c *cfg) Programs() []program.Program { return c.programs } @@ -354,6 +358,14 @@ func (c *cfg) CreatedAt() time.Time { return c.createdAt } +func (c *cfg) ProgramNames() []string { + names := make([]string, 0, len(c.programs)) + for _, name := range c.programs { + names = append(names, name.Spec.Name) + } + return names +} + func p(identifier, checksum string) program.Program { s, ok := program.FindSpecByName(identifier) if !ok { diff --git a/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go b/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go index f73ace5765a..c048c129445 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go @@ -50,7 +50,7 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir os.RemoveAll(installDir) } - return unpack(f, installDir) + return unpack(f, i.config.InstallPath) } diff --git a/x-pack/elastic-agent/pkg/core/monitoring/beats/beats_monitor.go b/x-pack/elastic-agent/pkg/core/monitoring/beats/beats_monitor.go index 93a5b8ba549..ac7bf0e7aca 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/beats/beats_monitor.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/beats/beats_monitor.go @@ -66,6 +66,13 @@ func (b *Monitor) Reload(rawConfig *config.Config) error { return nil } +// Close disables monitoring +func (b *Monitor) Close() { + b.config.Enabled = false + b.config.MonitorMetrics = false + b.config.MonitorLogs = false +} + // IsMonitoringEnabled returns true if monitoring is enabled. func (b *Monitor) IsMonitoringEnabled() bool { return b.config.Enabled } diff --git a/x-pack/elastic-agent/pkg/core/monitoring/monitor.go b/x-pack/elastic-agent/pkg/core/monitoring/monitor.go index 0767c28055a..409938a4d4f 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/monitor.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/monitor.go @@ -25,6 +25,7 @@ type Monitor interface { IsMonitoringEnabled() bool WatchLogs() bool WatchMetrics() bool + Close() } type wrappedConfig struct { diff --git a/x-pack/elastic-agent/pkg/core/monitoring/noop/noop_monitor.go b/x-pack/elastic-agent/pkg/core/monitoring/noop/noop_monitor.go index 97582de7242..ae573d591b9 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/noop/noop_monitor.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/noop/noop_monitor.go @@ -27,6 +27,9 @@ func (b *Monitor) Cleanup(string, string) error { return nil } +// Close closes the monitor. +func (b *Monitor) Close() {} + // Prepare executes steps in order for monitoring to work correctly func (b *Monitor) Prepare(string, string, int, int) error { return nil diff --git a/x-pack/elastic-agent/pkg/fleetapi/action.go b/x-pack/elastic-agent/pkg/fleetapi/action.go index 3894fdfbf09..83d53eb9d06 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/action.go +++ b/x-pack/elastic-agent/pkg/fleetapi/action.go @@ -147,11 +147,6 @@ func (a *Actions) UnmarshalJSON(data []byte) error { ActionID: response.ActionID, ActionType: response.ActionType, } - if err := json.Unmarshal(response.Data, action); err != nil { - return errors.New(err, - "fail to decode UNENROLL action", - errors.TypeConfig) - } default: action = &ActionUnknown{ ActionID: response.ActionID, From 5d7c8fafaf573ae7fdd9684ebb2311eb547f05c4 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 7 Jul 2020 10:46:16 +0200 Subject: [PATCH 4/4] stopping fleet gateway on unenroll --- .../application/handler_action_unenroll.go | 27 +++++++++++++++++-- .../pkg/agent/application/managed_mode.go | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go b/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go index a1f12179b42..a0c038352b2 100644 --- a/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go +++ b/x-pack/elastic-agent/pkg/agent/application/handler_action_unenroll.go @@ -7,16 +7,22 @@ package application import ( "context" "fmt" + "os" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi" ) +// After running Unenroll agent is in idle state, non managed non standalone. +// For it to be operational again it needs to be either enrolled or reconfigured. type handlerUnenroll struct { log *logger.Logger emitter emitterFunc dispatcher programsDispatcher + closers []context.CancelFunc } func (h *handlerUnenroll) Handle(ctx context.Context, a action, acker fleetAcker) error { @@ -30,7 +36,24 @@ func (h *handlerUnenroll) Handle(ctx context.Context, a action, acker fleetAcker noPrograms := make(map[routingKey][]program.Program) h.dispatcher.Dispatch(a.ID(), noPrograms) - // TODO: clean action store + if err := acker.Ack(ctx, action); err != nil { + return err + } + + // commit all acks before quitting. + if err := acker.Commit(ctx); err != nil { + return err + } + + // close fleet gateway loop + for _, c := range h.closers { + c() + } + + // clean action store + if err := os.Remove(info.AgentActionStoreFile()); err != nil && !os.IsNotExist(err) { + return errors.New(err, "failed to clear action store") + } - return acker.Ack(ctx, action) + return 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 c9fc13e9b9d..8fe5e3d9807 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -183,6 +183,7 @@ func newManaged( log: log, emitter: emit, dispatcher: router, + closers: []context.CancelFunc{managedApplication.cancelCtxFn}, }, )