Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CWS] Convert SecAgent config subcommand to fx #15178

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 108 additions & 72 deletions cmd/security-agent/subcommands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,128 @@ package config
import (
"fmt"
"github.com/spf13/cobra"
"go.uber.org/fx"

"github.com/DataDog/datadog-agent/cmd/security-agent/command"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/config"
pkgconfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/config/settings"
settingshttp "github.com/DataDog/datadog-agent/pkg/config/settings/http"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)

func Commands(globalParams *command.GlobalParams) []*cobra.Command {
cmd := Config(getSettingsClient)
return []*cobra.Command{cmd}
type cliParams struct {
*command.GlobalParams

command *cobra.Command
args []string
getClient settings.ClientBuilder
}

// Config returns the main cobra config command.
func Config(getClient settings.ClientBuilder) *cobra.Command {
// TODO: Convert to fx
func Commands(globalParams *command.GlobalParams) []*cobra.Command {
cliParams := &cliParams{
GlobalParams: globalParams,
}

cmd := &cobra.Command{
Use: "config",
Short: "Print the runtime configuration of a running agent",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { return showRuntimeConfiguration(getClient, cmd, args) },
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cliParams.command = cmd
cliParams.args = args
cliParams.getClient = func(cmd *cobra.Command, args []string) (settings.Client, error) {
return getSettingsClient(cmd, args)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
return fxutil.OneShot(
showRuntimeConfiguration,
fx.Supply(cliParams),
fx.Supply(core.BundleParams{
ConfigParams: config.NewSecurityAgentParams(globalParams.ConfigFilePaths),
LogParams: log.LogForOneShot(command.LoggerName, "off", true)}),
core.Bundle,
)
},
}

cmd.AddCommand(listRuntime(getClient))
cmd.AddCommand(set(getClient))
cmd.AddCommand(get(getClient))
// listRuntime returns a cobra command to list the settings that can be changed at runtime.
cmd.AddCommand(
&cobra.Command{
Use: "list-runtime",
Short: "List settings that can be changed at runtime",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return fxutil.OneShot(
listRuntimeConfigurableValue,
fx.Supply(cliParams),
fx.Supply(core.BundleParams{
ConfigParams: config.NewSecurityAgentParams(globalParams.ConfigFilePaths),
LogParams: log.LogForOneShot(command.LoggerName, "off", true)}),
core.Bundle,
)
},
},
)

// set returns a cobra command to set a config value at runtime.
cmd.AddCommand(
&cobra.Command{
Use: "set [setting] [value]",
Short: "Set, for the current runtime, the value of a given configuration setting",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return fxutil.OneShot(
setConfigValue,
fx.Supply(cliParams),
fx.Supply(core.BundleParams{
ConfigParams: config.NewSecurityAgentParams(globalParams.ConfigFilePaths),
LogParams: log.LogForOneShot(command.LoggerName, "off", true)}),
core.Bundle,
)
},
},
)

// get returns a cobra command to get a runtime config value.
cmd.AddCommand(
&cobra.Command{
Use: "get [setting]",
Short: "Get, for the current runtime, the value of a given configuration setting",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return fxutil.OneShot(
getConfigValue,
fx.Supply(cliParams),
fx.Supply(core.BundleParams{
ConfigParams: config.NewSecurityAgentParams(globalParams.ConfigFilePaths),
LogParams: log.LogForOneShot(command.LoggerName, "off", true)}),
core.Bundle,
)
},
},
)

return cmd
return []*cobra.Command{cmd}
}

func getSettingsClient(cmd *cobra.Command, _ []string) (settings.Client, error) {
err := setupConfig(cmd)
func getSettingsClient(_ *cobra.Command, _ []string) (settings.Client, error) {
err := util.SetAuthToken()
if err != nil {
return nil, err
}

c := util.GetClient(false)
apiConfigURL := fmt.Sprintf("https://localhost:%v/agent/config", config.Datadog.GetInt("security_agent.cmd_port"))
apiConfigURL := fmt.Sprintf("https://localhost:%v/agent/config", pkgconfig.Datadog.GetInt("security_agent.cmd_port"))

return settingshttp.NewClient(c, apiConfigURL, "security-agent"), nil
}

func setupConfig(cmd *cobra.Command) error {
err := config.SetupLogger(command.LoggerName, config.GetEnvDefault("DD_LOG_LEVEL", "off"), "", "", false, true, false)
if err != nil {
fmt.Printf("Cannot setup logger, exiting: %v\n", err)
return err
}

return util.SetAuthToken()
}

func showRuntimeConfiguration(getClient settings.ClientBuilder, cmd *cobra.Command, args []string) error {
c, err := getClient(cmd, args)
func showRuntimeConfiguration(log log.Component, config config.Component, params *cliParams) error {
c, err := params.getClient(params.command, params.args)
if err != nil {
return err
}
Expand All @@ -79,49 +147,17 @@ func showRuntimeConfiguration(getClient settings.ClientBuilder, cmd *cobra.Comma
return nil
}

// listRuntime returns a cobra command to list the settings that can be changed at runtime.
func listRuntime(getClient settings.ClientBuilder) *cobra.Command {
return &cobra.Command{
Use: "list-runtime",
Short: "List settings that can be changed at runtime",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return listRuntimeConfigurableValue(getClient, cmd, args)
},
}
}

// set returns a cobra command to set a config value at runtime.
func set(getClient settings.ClientBuilder) *cobra.Command {
return &cobra.Command{
Use: "set [setting] [value]",
Short: "Set, for the current runtime, the value of a given configuration setting",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { return setConfigValue(getClient, cmd, args) },
}
}

// get returns a cobra command to get a runtime config value.
func get(getClient settings.ClientBuilder) *cobra.Command {
return &cobra.Command{
Use: "get [setting]",
Short: "Get, for the current runtime, the value of a given configuration setting",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error { return getConfigValue(getClient, cmd, args) },
}
}

func setConfigValue(getClient settings.ClientBuilder, cmd *cobra.Command, args []string) error {
if len(args) != 2 {
func setConfigValue(log log.Component, config config.Component, params *cliParams) error {
if len(params.args) != 2 {
return fmt.Errorf("exactly two parameters are required: the setting name and its value")
}

c, err := getClient(cmd, args)
c, err := params.getClient(params.command, params.args)
if err != nil {
return err
}

hidden, err := c.Set(args[0], args[1])
hidden, err := c.Set(params.args[0], params.args[1])
if err != nil {
return err
}
Expand All @@ -130,33 +166,33 @@ func setConfigValue(getClient settings.ClientBuilder, cmd *cobra.Command, args [
fmt.Printf("IMPORTANT: you have modified a hidden option, this may incur billing charges or have other unexpected side-effects.\n")
}

fmt.Printf("Configuration setting %s is now set to: %s\n", args[0], args[1])
fmt.Printf("Configuration setting %s is now set to: %s\n", params.args[0], params.args[1])

return nil
}

func getConfigValue(getClient settings.ClientBuilder, cmd *cobra.Command, args []string) error {
if len(args) != 1 {
func getConfigValue(log log.Component, config config.Component, params *cliParams) error {
if len(params.args) != 1 {
return fmt.Errorf("a single setting name must be specified")
}

c, err := getClient(cmd, args)
c, err := params.getClient(params.command, params.args)
if err != nil {
return err
}

value, err := c.Get(args[0])
value, err := c.Get(params.args[0])
if err != nil {
return err
}

fmt.Printf("%s is set to: %v\n", args[0], value)
fmt.Printf("%s is set to: %v\n", params.args[0], value)

return nil
}

func listRuntimeConfigurableValue(getClient settings.ClientBuilder, cmd *cobra.Command, args []string) error {
c, err := getClient(cmd, args)
func listRuntimeConfigurableValue(log log.Component, config config.Component, params *cliParams) error {
c, err := params.getClient(params.command, params.args)
if err != nil {
return err
}
Expand Down