From 620a5bd8c64073d15ac350917428848a89ff90d3 Mon Sep 17 00:00:00 2001 From: Aleksandr Maus Date: Thu, 2 Jun 2022 12:09:56 -0400 Subject: [PATCH] Fix Agent failing to run in standalone mode --- internal/pkg/agent/cmd/run.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/pkg/agent/cmd/run.go b/internal/pkg/agent/cmd/run.go index 7f3f3684d4e..b584baf2f09 100644 --- a/internal/pkg/agent/cmd/run.go +++ b/internal/pkg/agent/cmd/run.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/elastic-agent/internal/pkg/agent/application/info" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" "github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec" + "github.com/elastic/elastic-agent/internal/pkg/agent/application/secret" "github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade" "github.com/elastic/elastic-agent/internal/pkg/agent/configuration" "github.com/elastic/elastic-agent/internal/pkg/agent/control/server" @@ -115,6 +116,16 @@ func run(override cfgOverrider) error { if cfg.Fleet != nil && cfg.Fleet.Server != nil && cfg.Fleet.Server.Bootstrap { createAgentID = false } + + // Ensure we have the agent secret created. + // The secret is not created here if it exists already from the previous enrollment. + // This is needed for compatibility with agent running in standalone mode, + // that writes the agentID into fleet.enc (encrypted fleet.yml) before even loading the configuration. + err = secret.CreateAgentSecret() + if err != nil { + return err + } + agentInfo, err := info.NewAgentInfoWithLog(defaultLogLevel(cfg), createAgentID) if err != nil { return errors.New(err,