-
Notifications
You must be signed in to change notification settings - Fork 37
feat(secret flag) add reusable secret flag implementation, update guide #1366
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4bf611a
refac(io) set IO once in main to allow overriding with in memory io i…
cgoetz-inovex b1d9bfe
fix(fmt) run formatter
cgoetz-inovex 72658b0
fix(test) adapt cli args after changing arg slicing
cgoetz-inovex 0958388
feat(secret flag) add reusable secret flag implementation, update guide
cgoetz-inovex e738d95
Merge branch 'main' into feat/STACKITCLI-318_secret-flags-2
cgoetz-inovex d36a7e2
fix(docs) generate docs
cgoetz-inovex 3e41c7c
fix(lint) fix linting errors
cgoetz-inovex 70b40bb
Merge branch 'main' into feat/STACKITCLI-318_secret-flags-2
cgoetz-inovex 67a95cd
fix(secretflag) use title case when printing flag usage
cgoetz-inovex acdfe1b
fix(fmt) run formatter
cgoetz-inovex 8a0af9e
Merge branch 'main' into feat/STACKITCLI-318_secret-flags-2
cgoetz-inovex 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package flags | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| "golang.org/x/text/cases" | ||
| "golang.org/x/text/language" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/print" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/types" | ||
| ) | ||
|
|
||
| type secretFlag struct { | ||
| printer *print.Printer | ||
| fs fs.FS | ||
| value string | ||
| name string | ||
| } | ||
|
|
||
| func SecretFlag(name string, params *types.CmdParams) *secretFlag { | ||
| f := &secretFlag{ | ||
| printer: params.Printer, | ||
| fs: params.Fs, | ||
| name: name, | ||
| } | ||
| return f | ||
| } | ||
|
|
||
| var _ pflag.Value = &secretFlag{} | ||
|
|
||
| func (f *secretFlag) String() string { | ||
| return f.value | ||
| } | ||
|
|
||
| func (f *secretFlag) Set(value string) error { | ||
| if strings.HasPrefix(value, "@") { | ||
| path := strings.Trim(value[1:], `"'`) | ||
| bytes, err := fs.ReadFile(f.fs, path) | ||
| if err != nil { | ||
| return fmt.Errorf("reading secret %s: %w", f.name, err) | ||
| } | ||
| f.value = string(bytes) | ||
| return nil | ||
| } | ||
| f.printer.Warn("Passing a secret value on the command line is insecure and deprecated. This usage will stop working October 2026.\n") | ||
| f.value = value | ||
| return nil | ||
| } | ||
|
|
||
| func (f *secretFlag) Type() string { | ||
| return "string" | ||
| } | ||
|
|
||
| func (f *secretFlag) Usage() string { | ||
| name := cases.Title(language.AmericanEnglish).String(f.name) | ||
| return fmt.Sprintf("%s. Can be a string (deprecated) or a file path, if prefixed with '@' (example: @./secret.txt). Will be read from stdin when empty.", name) | ||
| } | ||
|
|
||
| func SecretFlagToStringPointer(p *print.Printer, cmd *cobra.Command, flag string) *string { | ||
| value, err := cmd.Flags().GetString(flag) | ||
| if err != nil { | ||
| p.Debug(print.ErrorLevel, "convert secret flag to string pointer: %v", err) | ||
| return nil | ||
| } | ||
| if value == "" { | ||
| input, err := p.PromptForPassword(fmt.Sprintf("enter %s: ", flag)) | ||
| if err != nil { | ||
| p.Debug(print.ErrorLevel, "convert secret flag %q to string pointer: %v", flag, err) | ||
| return nil | ||
| } | ||
| return &input | ||
| } | ||
| if cmd.Flag(flag).Changed { | ||
| return &value | ||
| } | ||
| return nil | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package flags | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| "testing" | ||
| "testing/fstest" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/testparams" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
| ) | ||
|
|
||
| type testFile struct { | ||
| path, content string | ||
| } | ||
|
|
||
| func TestSecretFlag(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| value string | ||
| want *string | ||
| file *testFile | ||
| stdin string | ||
| wantErr bool | ||
| wantStdErr string | ||
| }{ | ||
| { | ||
| name: "no value: prompts", | ||
| value: "", | ||
| want: utils.Ptr("from stdin"), | ||
| stdin: "from stdin", | ||
| }, | ||
| { | ||
| name: "a value: prints deprecation", | ||
| value: "a value", | ||
| want: utils.Ptr("a value"), | ||
| wantStdErr: "Warning: Passing a secret value on the command line is insecure and deprecated. This usage will stop working October 2026.\n", | ||
| }, | ||
| { | ||
| name: "from an existing file", | ||
| value: "@some-file.txt", | ||
| want: utils.Ptr("from file"), | ||
| file: &testFile{ | ||
| path: "some-file.txt", | ||
| content: "from file", | ||
| }, | ||
| }, | ||
| { | ||
| name: "from a non-existing file", | ||
| value: "@some-file-with-typo.txt", | ||
| wantErr: true, | ||
| file: &testFile{ | ||
| path: "some-file.txt", | ||
| content: "from file", | ||
| }, | ||
| }, | ||
| { | ||
| name: "from an existing double-quoted file", | ||
| value: `@"some-file.txt"`, | ||
| want: utils.Ptr("from file"), | ||
| file: &testFile{ | ||
| path: "some-file.txt", | ||
| content: "from file", | ||
| }, | ||
| }, | ||
| { | ||
| name: "from an existing single-quoted file", | ||
| value: "@'some-file.txt'", | ||
| want: utils.Ptr("from file"), | ||
| file: &testFile{ | ||
| path: "some-file.txt", | ||
| content: "from file", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| params := testparams.NewTestParams() | ||
| if tt.file != nil { | ||
| params.Fs = fstest.MapFS{ | ||
| tt.file.path: &fstest.MapFile{ | ||
| Data: []byte(tt.file.content), | ||
| }, | ||
| } | ||
| } | ||
| flag := SecretFlag("test", params.CmdParams) | ||
| cmd := cobra.Command{} | ||
| cmd.Flags().Var(flag, "test", flag.Usage()) | ||
| if tt.stdin != "" { | ||
| params.In.WriteString(tt.stdin) | ||
| params.In.WriteString("\n") | ||
| } | ||
|
|
||
| if tt.value != "" { // emulate pflag only calling set when flag is specified on the command line | ||
| err := cmd.Flags().Set("test", tt.value) | ||
| if err != nil && !tt.wantErr { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if err == nil && tt.wantErr { | ||
| t.Fatalf("expected error, got none") | ||
| } | ||
| } | ||
|
|
||
| got := SecretFlagToStringPointer(params.Printer, &cmd, "test") | ||
|
|
||
| if got != tt.want && *got != *tt.want { | ||
| t.Fatalf("unexpected value: got %q, want %q", *got, *tt.want) | ||
| } | ||
| if tt.wantStdErr != "" { | ||
| message, err := params.Err.ReadString('\n') | ||
| if err != nil && err != io.EOF { | ||
| t.Fatalf("reading stderr: %v", err) | ||
| } | ||
| if message != tt.wantStdErr { | ||
| t.Fatalf("unexpected stderr: got %q, want %q", message, tt.wantStdErr) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSecretFlag_Usage(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| in string | ||
| want string | ||
| }{ | ||
| { | ||
| in: "password", | ||
| want: "Password", | ||
| }, | ||
| { | ||
| in: "Password", | ||
| want: "Password", | ||
| }, | ||
| { | ||
| in: "", | ||
| want: "", | ||
| }, | ||
| { | ||
| in: "secret-key", | ||
| want: "Secret-Key", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(fmt.Sprintf("%q -> %q", tt.in, tt.want), func(t *testing.T) { | ||
| t.Parallel() | ||
| params := testparams.NewTestParams() | ||
| flag := SecretFlag(tt.in, params.CmdParams) | ||
| got := flag.Usage() | ||
| if !strings.HasPrefix(got, tt.want) { | ||
| t.Fatalf("unexpected usage: got %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.