diff --git a/internal/pkg/agent/application/managed_mode.go b/internal/pkg/agent/application/managed_mode.go index d334ae0198c..3f98e78fd62 100644 --- a/internal/pkg/agent/application/managed_mode.go +++ b/internal/pkg/agent/application/managed_mode.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/elastic-agent/internal/pkg/agent/operation" "github.com/elastic/elastic-agent/internal/pkg/agent/storage" "github.com/elastic/elastic-agent/internal/pkg/agent/storage/store" + "github.com/elastic/elastic-agent/internal/pkg/artifact" "github.com/elastic/elastic-agent/internal/pkg/capabilities" "github.com/elastic/elastic-agent/internal/pkg/composable" "github.com/elastic/elastic-agent/internal/pkg/config" @@ -157,6 +158,7 @@ func newManaged( }, caps, monitor, + artifact.NewReloader(cfg.Settings.DownloadConfig, log), ) if err != nil { return nil, err diff --git a/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change.go b/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change.go index 3775d12b352..ad75299e420 100644 --- a/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change.go +++ b/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change.go @@ -151,7 +151,7 @@ func (h *PolicyChange) handleFleetServerHosts(ctx context.Context, c *config.Con errors.TypeNetwork, errors.M("hosts", h.config.Fleet.Client.Hosts)) } // discard body for proper cancellation and connection reuse - io.Copy(ioutil.Discard, resp.Body) + _, _ = io.Copy(ioutil.Discard, resp.Body) resp.Body.Close() reader, err := fleetToReader(h.agentInfo, h.config) diff --git a/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change_test.go b/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change_test.go index d887e755154..e2d480ee6fe 100644 --- a/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change_test.go +++ b/internal/pkg/agent/application/pipeline/actions/handlers/handler_action_policy_change_test.go @@ -44,7 +44,7 @@ func TestPolicyChange(t *testing.T) { conf := map[string]interface{}{"hello": "world"} action := &fleetapi.ActionPolicyChange{ - ActionID: "abc123", + ActionID: "TestPolicyChange-abc1", ActionType: "POLICY_CHANGE", Policy: conf, } @@ -69,7 +69,7 @@ func TestPolicyChange(t *testing.T) { conf := map[string]interface{}{"hello": "world"} action := &fleetapi.ActionPolicyChange{ - ActionID: "abc123", + ActionID: "TestPolicyChange-abc2", ActionType: "POLICY_CHANGE", Policy: conf, } @@ -100,7 +100,7 @@ func TestPolicyAcked(t *testing.T) { emitter := &mockEmitter{err: mockErr} config := map[string]interface{}{"hello": "world"} - actionID := "abc123" + actionID := "TestPolicyAcked-abc1" action := &fleetapi.ActionPolicyChange{ ActionID: actionID, ActionType: "POLICY_CHANGE", @@ -129,7 +129,7 @@ func TestPolicyAcked(t *testing.T) { emitter := &mockEmitter{} config := map[string]interface{}{"hello": "world"} - actionID := "abc123" + actionID := "TestPolicyAcked-abc2" action := &fleetapi.ActionPolicyChange{ ActionID: actionID, ActionType: "POLICY_CHANGE", diff --git a/internal/pkg/artifact/config.go b/internal/pkg/artifact/config.go index c190c02d239..077b4fe4410 100644 --- a/internal/pkg/artifact/config.go +++ b/internal/pkg/artifact/config.go @@ -11,6 +11,8 @@ import ( "github.com/elastic/elastic-agent-libs/transport/httpcommon" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" + "github.com/elastic/elastic-agent/internal/pkg/config" + "github.com/elastic/elastic-agent/pkg/core/logger" ) const ( @@ -46,6 +48,38 @@ type Config struct { httpcommon.HTTPTransportSettings `config:",inline" yaml:",inline"` // Note: use anonymous struct for json inline } +type Reloader struct { + log *logger.Logger + cfg *Config +} + +func NewReloader(cfg *Config, log *logger.Logger) *Reloader { + return &Reloader{ + log: log, + cfg: cfg, + } +} + +func (r *Reloader) Reload(rawConfig *config.Config) error { + type c struct { + Config *Config `config:"agent.download" yaml:"agent.download" json:"agent.download"` + } + + cfg := &c{ + Config: DefaultConfig(), + } + if err := rawConfig.Unpack(&cfg); err != nil { + return err + } + + r.log.Debugf("Source URI changed from %q to %q", r.cfg.SourceURI, cfg.Config.SourceURI) + if cfg.Config.SourceURI != "" { + r.cfg.SourceURI = cfg.Config.SourceURI + } + + return nil +} + // DefaultConfig creates a config with pre-set default values. func DefaultConfig() *Config { transport := httpcommon.DefaultHTTPTransportSettings()