2015-04-20 01:04:01 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-03-01 22:02:48 +00:00
|
|
|
"time"
|
2015-12-30 19:41:50 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2016-08-13 01:01:30 +00:00
|
|
|
"github.com/hashicorp/vault/helper/duration"
|
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 {
|
2016-03-01 22:02:48 +00:00
|
|
|
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", "")
|
2016-03-01 22:02:48 +00:00
|
|
|
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()
|
2016-03-01 22:02:48 +00:00
|
|
|
if len(args) > 2 {
|
2015-04-20 01:04:01 +00:00
|
|
|
flags.Usage()
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
2016-03-01 22:02:48 +00:00
|
|
|
"\ntoken-renew expects at most two arguments"))
|
2015-04-20 01:04:01 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-01 22:02:48 +00:00
|
|
|
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
|
2016-03-01 22:02:48 +00:00
|
|
|
if len(args) == 2 {
|
2016-03-02 01:25:40 +00:00
|
|
|
increment = args[1]
|
|
|
|
}
|
|
|
|
if increment != "" {
|
2016-08-13 01:01:30 +00:00
|
|
|
dur, err := duration.ParseDurationSecond(increment)
|
2015-04-20 01:04:01 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Invalid increment: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-01 22:02:48 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-12-30 19:41:50 +00:00
|
|
|
// 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
|
2016-03-01 22:02:48 +00:00
|
|
|
if token == "" {
|
|
|
|
secret, err = client.Auth().Token().RenewSelf(inc)
|
2015-12-30 19:41:50 +00:00
|
|
|
} else {
|
2016-03-01 22:02:48 +00:00
|
|
|
secret, err = client.Auth().Token().Renew(token, inc)
|
2015-12-30 19:41:50 +00:00
|
|
|
}
|
2015-04-20 01:04:01 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error renewing token: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:55:29 +00:00
|
|
|
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 := `
|
2016-03-01 22:02:48 +00:00
|
|
|
Usage: vault token-renew [options] [token] [increment]
|
2015-04-20 01:04:01 +00:00
|
|
|
|
2016-03-01 22:02:48 +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
|
|
|
|
2016-03-01 22:02:48 +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
|
|
|
|
2016-03-01 22:02:48 +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:
|
2016-04-01 20:50:12 +00:00
|
|
|
` + meta.GeneralOptionsUsage() + `
|
2015-04-20 01:04:01 +00:00
|
|
|
Token Renew Options:
|
|
|
|
|
2016-03-01 22:02:48 +00:00
|
|
|
-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-
|
2015-12-10 10:32:31 +00:00
|
|
|
delimited table. This can also be json or yaml.
|
2015-04-20 01:04:01 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|