From dc53f879cec4711ac3c07eec2e29119a47d32e1b Mon Sep 17 00:00:00 2001 From: Panos Koutsovasilis Date: Thu, 13 Jun 2024 20:11:03 +0300 Subject: [PATCH] fix: refactor skip-file-capabilities flag --- internal/pkg/agent/cmd/container.go | 15 +++++++++------ internal/pkg/agent/cmd/container_init_linux.go | 2 +- internal/pkg/agent/cmd/container_init_other.go | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/pkg/agent/cmd/container.go b/internal/pkg/agent/cmd/container.go index 5068fbd22e3..973530a1775 100644 --- a/internal/pkg/agent/cmd/container.go +++ b/internal/pkg/agent/cmd/container.go @@ -54,8 +54,6 @@ var ( // Used to strip the appended ({uuid}) from the name of an enrollment token. This makes much easier for // a container to reference a token by name, without having to know what the generated UUID is for that name. tokenNameStrip = regexp.MustCompile(`\s\([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)$`) - - skipFileCapabilities bool ) func newContainerCommand(_ []string, streams *cli.IOStreams) *cobra.Command { @@ -144,14 +142,14 @@ all the above actions will be skipped, because the Elastic Agent has already bee occurs on every start of the container set FLEET_FORCE to 1. `, Run: func(c *cobra.Command, args []string) { - if err := logContainerCmd(streams); err != nil { + if err := logContainerCmd(c, streams); err != nil { logError(streams, err) os.Exit(1) } }, } - cmd.Flags().BoolVar(&skipFileCapabilities, skipFileCapabilitiesFlag, false, "") + cmd.Flags().Bool(skipFileCapabilitiesFlag, false, "skip setting file capabilities") return &cmd } @@ -164,8 +162,13 @@ func logInfo(streams *cli.IOStreams, a ...interface{}) { fmt.Fprintln(streams.Out, a...) } -func logContainerCmd(streams *cli.IOStreams) error { - shouldExit, err := initContainer(streams) +func logContainerCmd(cmd *cobra.Command, streams *cli.IOStreams) error { + skipFileCapabilities, err := cmd.Flags().GetBool(skipFileCapabilitiesFlag) + if err != nil { + return err + } + + shouldExit, err := initContainer(streams, skipFileCapabilities) if err != nil { return err } diff --git a/internal/pkg/agent/cmd/container_init_linux.go b/internal/pkg/agent/cmd/container_init_linux.go index f82988b9a0b..df0173e40f4 100644 --- a/internal/pkg/agent/cmd/container_init_linux.go +++ b/internal/pkg/agent/cmd/container_init_linux.go @@ -33,7 +33,7 @@ var ( // - chown all agent-related paths if DAC_OVERRIDE capability is not in the Effective set // If new binary capabilities are set then the returned cmd will be not nil. Note that it is up to caller to invoke // the returned cmd and spawn an agent instance with all the capabilities. -func initContainer(streams *cli.IOStreams) (shouldExit bool, err error) { +func initContainer(streams *cli.IOStreams, skipFileCapabilities bool) (shouldExit bool, err error) { isRoot, err := utils.HasRoot() if err != nil { return true, err diff --git a/internal/pkg/agent/cmd/container_init_other.go b/internal/pkg/agent/cmd/container_init_other.go index ba17210fc3a..e883c1c4ca4 100644 --- a/internal/pkg/agent/cmd/container_init_other.go +++ b/internal/pkg/agent/cmd/container_init_other.go @@ -10,6 +10,6 @@ import ( "github.com/elastic/elastic-agent/internal/pkg/cli" ) -func initContainer(streams *cli.IOStreams) (shouldExit bool, err error) { +func initContainer(streams *cli.IOStreams, skipFileCapabilities bool) (shouldExit bool, err error) { return false, nil }