open-vault/command/token_renew.go

115 lines
3.0 KiB
Go
Raw Normal View History

2015-04-20 01:04:01 +00:00
package command
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/vault/api"
2016-04-01 17:16:05 +00:00
"github.com/hashicorp/vault/meta"
2015-04-20 01:04:01 +00:00
)
// TokenRenewCommand is a Command that mounts a new mount.
type TokenRenewCommand struct {
2016-04-01 17:16:05 +00:00
meta.Meta
2015-04-20 01:04:01 +00:00
}
func (c *TokenRenewCommand) Run(args []string) int {
var format, increment string
2016-04-01 17:16:05 +00:00
flags := c.Meta.FlagSet("token-renew", meta.FlagSetDefault)
2015-04-20 01:04:01 +00:00
flags.StringVar(&format, "format", "table", "")
flags.StringVar(&increment, "increment", "", "")
2015-04-20 01:04:01 +00:00
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) > 2 {
2015-04-20 01:04:01 +00:00
flags.Usage()
c.Ui.Error(fmt.Sprintf(
"\ntoken-renew expects at most two arguments"))
2015-04-20 01:04:01 +00:00
return 1
}
var token string
if len(args) > 0 {
token = args[0]
}
var inc int
2016-03-02 01:25:40 +00:00
// If both are specified prefer the argument
if len(args) == 2 {
2016-03-02 01:25:40 +00:00
increment = args[1]
}
if increment != "" {
dur, err := time.ParseDuration(increment)
2015-04-20 01:04:01 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Invalid increment: %s", err))
return 1
}
inc = int(dur / time.Second)
2015-04-20 01:04:01 +00:00
}
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
// If the given token is the same as the client's, use renew-self instead
// as this is far more likely to be allowed via policy
var secret *api.Secret
if token == "" {
secret, err = client.Auth().Token().RenewSelf(inc)
} else {
secret, err = client.Auth().Token().Renew(token, inc)
}
2015-04-20 01:04:01 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error renewing token: %s", err))
return 1
}
return OutputSecret(c.Ui, format, secret)
2015-04-20 01:04:01 +00:00
}
func (c *TokenRenewCommand) Synopsis() string {
2015-09-12 01:08:32 +00:00
return "Renew an auth token if there is an associated lease"
2015-04-20 01:04:01 +00:00
}
func (c *TokenRenewCommand) Help() string {
helpText := `
Usage: vault token-renew [options] [token] [increment]
2015-04-20 01:04:01 +00:00
Renew an auth token, extending the amount of time it can be used. If a token
is given to the command, '/auth/token/renew' will be called with the given
token; otherwise, '/auth/token/renew-self' will be called with the client
token.
2015-04-20 01:04:01 +00:00
This command is similar to "renew", but "renew" is only for leases; this
command is only for tokens.
2015-04-20 01:04:01 +00:00
An optional increment can be given to request a certain number of seconds to
increment the lease. This request is advisory; Vault may not adhere to it at
all. If a token is being passed in on the command line, the increment can as
well; otherwise it must be passed in via the '-increment' flag.
2015-04-20 01:04:01 +00:00
General Options:
` + meta.GeneralOptionsUsage() + `
2015-04-20 01:04:01 +00:00
Token Renew Options:
-increment=3600 The desired increment. If not supplied, Vault will
use the default TTL. If supplied, it may still be
ignored. This can be submitted as an integer number
of seconds or a string duration (e.g. "72h").
2015-04-20 01:04:01 +00:00
-format=table The format for output. By default it is a whitespace-
delimited table. This can also be json or yaml.
2015-04-20 01:04:01 +00:00
`
return strings.TrimSpace(helpText)
}