-
Notifications
You must be signed in to change notification settings - Fork 20
Add support of non-secret configs #1619
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
Open
nullfunc
wants to merge
1
commit into
main
Choose a base branch
from
revert-1618-revert-1616-non-secret-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -227,11 +227,15 @@ func SetupCommands(ctx context.Context, version string) { | |||
| configSetCmd.Flags().BoolP("name", "n", false, "name of the config (backwards compat)") | ||||
| configSetCmd.Flags().BoolP("env", "e", false, "set the config from an environment variable") | ||||
| configSetCmd.Flags().Bool("random", false, "set a secure randomly generated value for config") | ||||
| configSetCmd.Flags().Bool("secret", true, "set a secret config") | ||||
| configSetCmd.Flags().String("env-file", "", "load config values from an .env file") | ||||
| _ = configSetCmd.Flags().MarkHidden("name") | ||||
|
|
||||
| configCmd.AddCommand(configSetCmd) | ||||
|
|
||||
| configGetCmd.Flags().BoolP("name", "n", false, "name of the config") | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd do this using positional arg(s). For a while we only had
Suggested change
|
||||
| configCmd.AddCommand(configGetCmd) | ||||
|
|
||||
| configDeleteCmd.Flags().BoolP("name", "n", false, "name of the config(s) (backwards compat)") | ||||
| _ = configDeleteCmd.Flags().MarkHidden("name") | ||||
| configCmd.AddCommand(configDeleteCmd) | ||||
|
|
@@ -639,15 +643,69 @@ var configCmd = &cobra.Command{ | |||
| Short: "Add, update, or delete service config", | ||||
| } | ||||
|
|
||||
| var configGetCmd = &cobra.Command{ | ||||
| Use: "get CONFIG", // like Docker | ||||
| Annotations: authNeededAnnotation, | ||||
| Args: cobra.ArbitraryArgs, | ||||
| Aliases: []string{"show"}, | ||||
| Short: "Show config value", | ||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||
|
|
||||
| // Make sure we have a project to set config for before asking for a value | ||||
| loader := configureLoader(cmd) | ||||
| provider, err := newProviderChecked(cmd.Context(), loader) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
|
|
||||
| _, isPlayground := provider.(*cliClient.PlaygroundProvider) | ||||
| if isPlayground { | ||||
| return errors.New("non-secret configs are not supported in playground") | ||||
| } | ||||
|
|
||||
| projectName, err := cliClient.LoadProjectNameWithFallback(cmd.Context(), loader, provider) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
|
|
||||
| for _, name := range args { | ||||
| if !pkg.IsValidSecretName(name) { | ||||
| return fmt.Errorf("invalid config name: %q", name) | ||||
| } | ||||
| } | ||||
|
|
||||
| resp, err := cli.ConfigGet(cmd.Context(), projectName, args, provider) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
|
|
||||
| for _, config := range resp.Configs { | ||||
| if config.Type != defangv1.ConfigType_CONFIGTYPE_INSENSITIVE { | ||||
| config.Value = "<value hidden>" | ||||
| } | ||||
|
|
||||
| config.Name = config.Name[strings.LastIndex(config.Name, "/")+1:] | ||||
| } | ||||
|
|
||||
| if len(resp.Configs) == 0 { | ||||
| term.Info("No configs found") | ||||
| } else { | ||||
| term.Table(resp.Configs, "Name", "Value") | ||||
| } | ||||
| return nil | ||||
| }, | ||||
| } | ||||
|
|
||||
| var configSetCmd = &cobra.Command{ | ||||
| Use: "create CONFIG [file|-]", // like Docker | ||||
| Annotations: authNeededAnnotation, | ||||
| Args: cobra.RangeArgs(0, 2), // Allow 0 args when using --env-file | ||||
| Aliases: []string{"set", "add", "put"}, | ||||
| Short: "Adds or updates a sensitive config value", | ||||
| Short: "Adds or updates a config value", | ||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||
| fromEnv, _ := cmd.Flags().GetBool("env") | ||||
| random, _ := cmd.Flags().GetBool("random") | ||||
| isSecret, _ := cmd.Flags().GetBool("secret") // intentional typo for backwards compat | ||||
| envFile, _ := cmd.Flags().GetString("env-file") | ||||
|
|
||||
| // Make sure we have a project to set config for before asking for a value | ||||
|
|
@@ -657,6 +715,11 @@ var configSetCmd = &cobra.Command{ | |||
| return err | ||||
| } | ||||
|
|
||||
| _, isPlayground := provider.(*cliClient.PlaygroundProvider) | ||||
| if !isSecret && isPlayground { | ||||
| return errors.New("non-secret configs are not supported in playground") | ||||
| } | ||||
|
|
||||
| projectName, err := cliClient.LoadProjectNameWithFallback(cmd.Context(), loader, provider) | ||||
| if err != nil { | ||||
| return err | ||||
|
|
@@ -688,7 +751,7 @@ var configSetCmd = &cobra.Command{ | |||
| continue | ||||
| } | ||||
|
|
||||
| if err := cli.ConfigSet(cmd.Context(), projectName, provider, name, value); err != nil { | ||||
| if err := cli.ConfigSet(cmd.Context(), isSecret, projectName, provider, name, value); err != nil { | ||||
| term.Warnf("Failed to set %q: %v", name, err) | ||||
| } else { | ||||
| term.Info("Updated value for", name) | ||||
|
|
@@ -753,19 +816,28 @@ var configSetCmd = &cobra.Command{ | |||
| value = CreateRandomConfigValue() | ||||
| term.Info("Generated random value: " + value) | ||||
| } else { | ||||
| // Prompt for sensitive value | ||||
| var sensitivePrompt = &survey.Password{ | ||||
| Message: fmt.Sprintf("Enter value for %q:", name), | ||||
| Help: "The value will be stored securely and cannot be retrieved later.", | ||||
| } | ||||
|
|
||||
| err := survey.AskOne(sensitivePrompt, &value, survey.WithStdio(term.DefaultTerm.Stdio())) | ||||
| if err != nil { | ||||
| return err | ||||
| if isSecret { | ||||
| secretPrompt := &survey.Password{ | ||||
| Message: fmt.Sprintf("Enter value for %q:", name), | ||||
| Help: "The value will be stored securely and cannot be retrieved later.", | ||||
| } | ||||
| err := survey.AskOne(secretPrompt, &value, survey.WithStdio(term.DefaultTerm.Stdio())) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
| } else { | ||||
| var nonSecretPrompt = &survey.Input{ | ||||
| Message: fmt.Sprintf("Enter value for %q:", name), | ||||
| Help: "The value will be stored securely.", | ||||
| } | ||||
| err := survey.AskOne(nonSecretPrompt, &value, survey.WithStdio(term.DefaultTerm.Stdio())) | ||||
| if err != nil { | ||||
| return err | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| if err := cli.ConfigSet(cmd.Context(), projectName, provider, name, value); err != nil { | ||||
| if err := cli.ConfigSet(cmd.Context(), isSecret, projectName, provider, name, value); err != nil { | ||||
| return err | ||||
| } | ||||
| term.Info("Updated value for", name) | ||||
|
|
||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean flags with
trueas default are a little awkward since the only way to really use it is to say--secret=false, which can perhaps be considered a feature since you make it explicit that a config can be read?