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

FSOC-37 Added argument support to fsoc config set #132

Merged
merged 2 commits into from
Jul 9, 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
56 changes: 45 additions & 11 deletions cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ if on context name is specified, the current context is created/updated.`

setContextExample = `
# Set oauth credentials (recommended for interactive use)
fsoc config set --auth=oauth --url=https://mytenant.observe.appdynamics.com
fsoc config set auth=oauth url=https://mytenant.observe.appdynamics.com

# Set service or agent principal credentials (secret file must remain accessible)
fsoc config set --auth=service-principal --secret-file=my-service-principal.json
fsoc config set --auth=agent-principal --secret-file=agent-helm-values.yaml
fsoc config set --auth=agent-principal --secret-file=client-values.json --tenant=123456 --url=https://mytenant.observe.appdynamics.com
fsoc config set auth=service-principal secret-file=my-service-principal.json
fsoc config set auth=agent-principal secret-file=agent-helm-values.yaml
fsoc config set auth=agent-principal secret-file=client-values.json tenant=123456 url=https://mytenant.observe.appdynamics.com

# Set local access
fsoc config set --auth=local url=http://localhost --appd-pid=PID --appd-tid=TID --appd-pty=PTY
fsoc config set auth=local url=http://localhost appd-pid=PID appd-tid=TID appd-pty=PTY

# Set the token field on the "prod" context entry without touching other values
fsoc config set --profile prod --token=top-secret`
fsoc config set profile prod token=top-secret`
)

func newCmdConfigSet() *cobra.Command {
Expand All @@ -57,21 +57,29 @@ func newCmdConfigSet() *cobra.Command {
Use: "set [--profile CONTEXT] --auth=AUTH [flags]",
Short: "Create or modify a context entry in an fsoc config file",
Long: setContextLong,
Args: cobra.MaximumNArgs(1),
Args: cobra.MaximumNArgs(9),
Example: setContextExample,
Annotations: map[string]string{AnnotationForConfigBypass: ""},
Run: configSetContext,
}
cmd.Flags().String(AppdPid, "", "pid to use (local auth type only, provide raw value to be encoded)")
_ = cmd.Flags().MarkDeprecated(AppdPid, "the --"+AppdPid+" flag is deprecated, please use arguments supplied as "+AppdPid+"="+strings.ToUpper(AppdPid))
cmd.Flags().String(AppdTid, "", "tid to use (local auth type only, provide raw value to be encoded)")
_ = cmd.Flags().MarkDeprecated(AppdTid, "the --"+AppdTid+" flag is deprecated, please use arguments supplied as "+AppdTid+"="+strings.ToUpper(AppdTid))
cmd.Flags().String(AppdPty, "", "pty to use (local auth type only, provide raw value to be encoded)")
_ = cmd.Flags().MarkDeprecated(AppdPty, "the --"+AppdPty+" flag is deprecated, please use arguments supplied as "+AppdPty+"="+strings.ToUpper(AppdPty))
cmd.Flags().String("auth", "", fmt.Sprintf(`Select authentication method, one of {"%v"}`, strings.Join(GetAuthMethodsStringList(), `", "`)))
_ = cmd.Flags().MarkDeprecated("auth", "the --auth flag is deprecated, please use arguments supplied as auth=AUTH")
cmd.Flags().String("server", "", "Set server host name")
_ = cmd.Flags().MarkDeprecated("server", "The --server flag is deprecated, please use --url instead.")
_ = cmd.Flags().MarkDeprecated("server", "the --server flag is deprecated, please use arguments supplied as url=URL")
cmd.Flags().String("url", "", "Set server URL (with http or https schema)")
_ = cmd.Flags().MarkDeprecated("url", "the --url flag is deprecated, please use arguments supplied as url=URL")
cmd.Flags().String("tenant", "", "Set tenant ID")
_ = cmd.Flags().MarkDeprecated("tenant", "the --tenant flag is deprecated, please use arguments supplied as tenant=TENANT")
cmd.Flags().String("token", "", "Set token value (use --token=- to get from stdin)")
_ = cmd.Flags().MarkDeprecated("token", "the --token flag is deprecated, please use arguments supplied as token=TOKEN")
cmd.Flags().String("secret-file", "", "Set a credentials file to use for service principal (.json or .csv) or agent principal (.yaml)")
_ = cmd.Flags().MarkDeprecated("secret-file", "the --secret-file flag is deprecated, please use arguments supplied as secret-file=SECRET-TOKEN")
return cmd
}

Expand All @@ -95,13 +103,39 @@ func validateUrl(providedUrl string) (string, error) {
return parsedUrl.String(), nil
}

func validateArgs(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
allowedArgs := []string{AppdPid, AppdTid, AppdPty, "auth", "server", "url", "tenant", "token", "secret-file"}
for i := 0; i < len(args); i++ {
// check arg format ∑+=∑+
stringSegments := strings.Split(args[i], "=")
name, value := stringSegments[0], stringSegments[1]
if len(stringSegments) != 2 {
return fmt.Errorf("parameter name and value cannot contain \"=\"")
}
// check arg name is valid (i.e. no disallowed flags)
if !slices.Contains(allowedArgs, name) {
return fmt.Errorf("argument name %s must be one of the following values %s", name, strings.Join(allowedArgs, ", "))
}
// make sure flag isn't already set
if flags.Changed(name) {
return fmt.Errorf("cannot have both flag and argument with same name")
}
// Set flag manually
err := flags.Set(name, value)
if err != nil {
return err
}
}
return nil
}

func configSetContext(cmd *cobra.Command, args []string) {
var contextName string

// Check that either context name or current context is specified
if len(args) > 0 {
_ = cmd.Help()
log.Fatalf("Unexpected args: %v", args)
if err := validateArgs(cmd, args); err != nil {
log.Fatalf("%v", err)
}

// Check that at least one value is specified (including empty)
Expand Down