Skip to content

Commit 7badd90

Browse files
author
Devansh Thakur
committed
updated to internal secret pkg
1 parent 3eae549 commit 7badd90

2 files changed

Lines changed: 6 additions & 112 deletions

File tree

internal/cmd/beta/intake/user/create/create.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package create
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
"github.com/spf13/cobra"
98
intake "github.com/stackitcloud/stackit-sdk-go/services/intake/v1betaapi"
@@ -128,16 +127,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
128127
return nil, &cliErr.ProjectIdError{}
129128
}
130129

131-
password, err := parsePassword(p, cmd)
132-
if err != nil {
133-
return nil, err
134-
}
135-
136130
model := inputModel{
137131
GlobalFlagModel: globalFlags,
138132
DisplayName: flags.FlagToStringPointer(p, cmd, displayNameFlag),
139133
IntakeId: flags.FlagToStringPointer(p, cmd, intakeIdFlag),
140-
Password: password,
134+
Password: flags.SecretFlagToStringPointer(p, cmd, passwordFlag),
141135
UserType: flags.FlagToStringPointer(p, cmd, userTypeFlag),
142136
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
143137
Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag),
@@ -147,31 +141,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
147141
return &model, nil
148142
}
149143

150-
func parsePassword(p *print.Printer, cmd *cobra.Command) (*string, error) {
151-
if cmd.Flag(passwordFlag).Changed {
152-
val, err := cmd.Flags().GetString(passwordFlag)
153-
if err != nil {
154-
return nil, fmt.Errorf("reading password: %w", err)
155-
}
156-
val = strings.TrimRight(val, "\r\n")
157-
if val == "" {
158-
return nil, fmt.Errorf("the provided password (or secret file) is empty")
159-
}
160-
return &val, nil
161-
}
162-
163-
password := flags.SecretFlagToStringPointer(p, cmd, passwordFlag)
164-
if password != nil {
165-
trimmed := strings.TrimRight(*password, "\r\n")
166-
if trimmed == "" {
167-
return nil, fmt.Errorf("password cannot be empty")
168-
}
169-
return &trimmed, nil
170-
}
171-
172-
return nil, nil
173-
}
174-
175144
func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIClient) intake.ApiCreateIntakeUserRequest {
176145
req := apiClient.DefaultAPI.CreateIntakeUser(ctx, model.ProjectId, model.Region, *model.IntakeId)
177146

internal/cmd/beta/intake/user/update/update.go

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package update
33
import (
44
"context"
55
"fmt"
6-
"io/fs"
7-
"strings"
86

97
"github.com/spf13/cobra"
108
intake "github.com/stackitcloud/stackit-sdk-go/services/intake/v1betaapi"
@@ -31,48 +29,8 @@ const (
3129
passwordFlag = "password"
3230
userTypeFlag = "type"
3331
labelsFlag = "labels"
34-
35-
interactivePasswordPlaceholder = "__INTERACTIVE__"
3632
)
3733

38-
type secretUpdateFlag struct {
39-
printer *print.Printer
40-
fs fs.FS
41-
value string
42-
isPrompt bool
43-
}
44-
45-
func (f *secretUpdateFlag) String() string {
46-
return f.value
47-
}
48-
49-
func (f *secretUpdateFlag) Set(value string) error {
50-
if value == interactivePasswordPlaceholder {
51-
f.isPrompt = true
52-
return nil
53-
}
54-
if strings.HasPrefix(value, "@") {
55-
path := strings.Trim(value[1:], `"'`)
56-
bytes, err := fs.ReadFile(f.fs, path)
57-
if err != nil {
58-
return fmt.Errorf("reading secret %s: %w", passwordFlag, err)
59-
}
60-
val := strings.TrimRight(string(bytes), "\r\n")
61-
if val == "" {
62-
return fmt.Errorf("the provided secret file %q is empty", path)
63-
}
64-
f.value = val
65-
return nil
66-
}
67-
f.printer.Warn("Passing a secret value on the command line is insecure and deprecated. This usage will stop working October 2026.\n")
68-
f.value = value
69-
return nil
70-
}
71-
72-
func (f *secretUpdateFlag) Type() string {
73-
return "string"
74-
}
75-
7634
type inputModel struct {
7735
*globalflags.GlobalFlagModel
7836
IntakeId string
@@ -85,11 +43,6 @@ type inputModel struct {
8543
}
8644

8745
func NewCmd(p *types.CmdParams) *cobra.Command {
88-
password := &secretUpdateFlag{
89-
printer: p.Printer,
90-
fs: p.Fs,
91-
}
92-
9346
cmd := &cobra.Command{
9447
Use: fmt.Sprintf("update %s", userIdArg),
9548
Short: "Updates an Intake User",
@@ -140,16 +93,16 @@ func NewCmd(p *types.CmdParams) *cobra.Command {
14093
return outputResult(p.Printer, model, resp)
14194
},
14295
}
143-
configureFlags(cmd, password)
96+
configureFlags(cmd, p)
14497
return cmd
14598
}
14699

147-
func configureFlags(cmd *cobra.Command, password *secretUpdateFlag) {
100+
func configureFlags(cmd *cobra.Command, params *types.CmdParams) {
148101
cmd.Flags().Var(flags.UUIDFlag(), intakeIdFlag, "Intake ID")
149102
cmd.Flags().String(displayNameFlag, "", "Display name")
150103
cmd.Flags().String(descriptionFlag, "", "Description")
151-
cmd.Flags().Var(password, passwordFlag, "Password. Can be a string (deprecated) or a file path, if prefixed with '@' (example: @./secret.txt). If provided without a value, you will be prompted interactively. Must contain lower, upper, digits, and special characters (min 12 chars).")
152-
cmd.Flags().Lookup(passwordFlag).NoOptDefVal = interactivePasswordPlaceholder
104+
password := flags.SecretFlag(passwordFlag, params)
105+
cmd.Flags().Var(password, passwordFlag, password.Usage())
153106
cmd.Flags().String(userTypeFlag, "", "Type of user. One of 'intake' or 'dead-letter'")
154107
cmd.Flags().StringToString(labelsFlag, nil, `Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2".`)
155108

@@ -165,18 +118,13 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
165118
return nil, &cliErr.ProjectIdError{}
166119
}
167120

168-
password, err := parsePassword(p, cmd)
169-
if err != nil {
170-
return nil, err
171-
}
172-
173121
model := &inputModel{
174122
GlobalFlagModel: globalFlags,
175123
IntakeId: flags.FlagToStringValue(p, cmd, intakeIdFlag),
176124
UserId: userId,
177125
DisplayName: flags.FlagToStringPointer(p, cmd, displayNameFlag),
178126
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
179-
Password: password,
127+
Password: flags.SecretFlagToStringPointer(p, cmd, passwordFlag),
180128
UserType: flags.FlagToStringPointer(p, cmd, userTypeFlag),
181129
Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag),
182130
}
@@ -189,29 +137,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
189137
return model, nil
190138
}
191139

192-
func parsePassword(p *print.Printer, cmd *cobra.Command) (*string, error) {
193-
flag := cmd.Flag(passwordFlag)
194-
if flag == nil || !flag.Changed {
195-
return nil, nil
196-
}
197-
if secretFlag, ok := flag.Value.(*secretUpdateFlag); ok && secretFlag.isPrompt {
198-
input, err := p.PromptForPassword("enter new password: ")
199-
if err != nil {
200-
return nil, fmt.Errorf("prompt for password: %w", err)
201-
}
202-
input = strings.TrimRight(input, "\r\n")
203-
if input == "" {
204-
return nil, fmt.Errorf("password cannot be empty")
205-
}
206-
return &input, nil
207-
}
208-
val := strings.TrimRight(flag.Value.String(), "\r\n")
209-
if val == "" {
210-
return nil, fmt.Errorf("the provided password (or secret file) is empty")
211-
}
212-
return &val, nil
213-
}
214-
215140
func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIClient) intake.ApiUpdateIntakeUserRequest {
216141
req := apiClient.DefaultAPI.UpdateIntakeUser(ctx, model.ProjectId, model.Region, model.IntakeId, model.UserId)
217142

0 commit comments

Comments
 (0)