Skip to content

Commit

Permalink
CLI: add an option for renew command fail on non-fullfillable request…
Browse files Browse the repository at this point in the history
… to allow command chaining

Signed-off-by: saiaunghlyanhtet <[email protected]>
  • Loading branch information
saiaunghlyanhtet committed Jan 11, 2025
1 parent fd00bbf commit 028f058
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/29060.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
CLI: adds an optional flag (--fail-if-not-fulfilled) to the renew command, which lets the renew command fail on unfulfillable requests and allows command chaining to allow further executions.
```
19 changes: 17 additions & 2 deletions command/token_renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var (
type TokenRenewCommand struct {
*BaseCommand

flagAccessor bool
flagIncrement time.Duration
flagAccessor bool
flagIncrement time.Duration
flagFailIfNotFulfilled bool
}

func (c *TokenRenewCommand) Synopsis() string {
Expand Down Expand Up @@ -86,6 +87,15 @@ func (c *TokenRenewCommand) Flags() *FlagSets {
"numeric string with suffix like \"30s\" or \"5m\".",
})

f.BoolVar(&BoolVar{
Name: "fail-if-not-fulfilled",
Target: &c.flagFailIfNotFulfilled,
Default: false,
EnvVar: "",
Completion: complete.PredictNothing,
Usage: "Fail if the requested TTL increment cannot be fully fulfilled.",
})

return set
}

Expand Down Expand Up @@ -140,5 +150,10 @@ func (c *TokenRenewCommand) Run(args []string) int {
return 2
}

if c.flagFailIfNotFulfilled && secret.Auth.LeaseDuration < int(increment.Seconds()) {
c.UI.Info("Token renewal completed with capped duration, failing the command because of --fail-if-not-fulfilled")
return 1
}

return OutputSecret(c.UI, secret)
}
12 changes: 12 additions & 0 deletions command/token_renew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ func TestTokenRenewCommand_Run(t *testing.T) {
"",
0,
},
{
"fail_if_not_fulfilled_exceeds_max_ttl",
[]string{"-increment", "33d", "--fail-if-not-fulfilled"},
"Token renewal completed with capped duration, failing the command because of --fail-if-not-fulfilled",
1,
},
{
"fail_if_not_fulfilled_within_max_ttl",
[]string{"-increment", "30m", "--fail-if-not-fulfilled"},
"",
0,
},
}

t.Run("validations", func(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions website/content/docs/commands/token/renew.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Renew a token requesting a specific increment value:
$ vault token renew -increment=30m 96ddf4bc-d217-f3ba-f9bd-017055595017
```

Fail if the requested TTL increment cannot be fully fulfilled:

```shell-session
$ vault token renew -increment=30m 96ddf4bc-d217-f3ba-f9bd-017055595017 --fail-if-not-fulfilled || vault login
```

## Usage

The following flags are available in addition to the [standard set of
Expand All @@ -53,3 +59,7 @@ flags](/vault/docs/commands) included on all commands.
Vault will not honor this request for periodic tokens. If not supplied, Vault will use
the default TTL. This is specified as a numeric string with suffix like "30s"
or "5m". This is aliased as "-i".

- `--fail-if-not-fulfilled` - Fail if the requested TTL increment cannot be fully fulfilled.
Vault will allow token renewal request completion with capped duration even if renew request fails.
And Vault will also allow command chaining after renew command.

0 comments on commit 028f058

Please sign in to comment.