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

cmd/setec: add a flag to allow "put" of an empty secret value #87

Merged
merged 2 commits into from
Oct 15, 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
10 changes: 7 additions & 3 deletions cmd/setec/setec.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ func runGet(env *command.Env, name string) error {
}

var putArgs struct {
File string `flag:"from-file,Read secret value from this file instead of stdin"`
File string `flag:"from-file,Read secret value from this file instead of stdin"`
EmptyOK bool `flag:"empty-ok,Allow an empty secret value"`
}

func runPut(env *command.Env, name string) error {
Expand All @@ -385,6 +386,9 @@ func runPut(env *command.Env, name string) error {
if utf8.Valid(value) {
value = bytes.TrimSpace(value)
}
if len(value) == 0 && !putArgs.EmptyOK {
return errors.New("empty secret value")
}
} else if term.IsTerminal(int(os.Stdin.Fd())) {
// Standard input is connected to a terminal; prompt the human to type or
// paste the value and require confirmation.
Expand All @@ -396,7 +400,7 @@ func runPut(env *command.Env, name string) error {
if err != nil {
return err
}
if len(value) == 0 {
if len(value) == 0 && !putArgs.EmptyOK {
return errors.New("no secret provided, aborting")
}
io.WriteString(os.Stdout, "Confirm secret: ")
Expand All @@ -414,7 +418,7 @@ func runPut(env *command.Env, name string) error {
value, err = io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("read from stdin: %w", err)
} else if len(value) == 0 {
} else if len(value) == 0 && !putArgs.EmptyOK {
return errors.New("empty secret value")
}
fmt.Fprintf(env, "Read %d bytes from stdin\n", len(value))
Expand Down