Simon Marsh
6117bcd7bf
All checks were successful
continuous-integration/drone/push Build is passing
96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
//////////////////////////////////////////////////////////////////////////
|
|
|
|
package main
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
import (
|
|
// log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
vault "git.burble.dn42/burble.dn42/libvault"
|
|
)
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
var (
|
|
TokenFile string
|
|
TokenTTL string
|
|
)
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// helper funcs
|
|
|
|
func loadToken() *vault.Token {
|
|
|
|
var token *vault.Token
|
|
|
|
if os.Getenv("VAULT_TOKEN") == "" {
|
|
// if no env set, read token from a file
|
|
|
|
filename := TokenFile
|
|
if filename == "" {
|
|
filename = vault.VAULT_TOKEN_FILE
|
|
}
|
|
fmt.Printf("Reading token from file: %s\n", filename)
|
|
|
|
var err error
|
|
token, err = vault.NewTokenFromFile(filename)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: Failed to read token from file: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
} else {
|
|
// obtain token from environment
|
|
|
|
token = &vault.Token{}
|
|
token.Token = os.Getenv("VAULT_TOKEN")
|
|
|
|
}
|
|
|
|
return token
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
func CmdTokenRenew(cmd *cobra.Command, args []string) {
|
|
|
|
token := loadToken()
|
|
|
|
// set the renewal duration
|
|
var ttl time.Duration
|
|
if TokenTTL == "" {
|
|
ttl = vault.VAULT_TTL
|
|
} else {
|
|
var err error
|
|
ttl, err = time.ParseDuration(TokenTTL)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: failed to parse TTL: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("Renewing token for %s\n", ttl.String())
|
|
if err := token.Renew(ttl); err != nil {
|
|
fmt.Printf("ERROR: Failed to renew token: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
expiry, err := token.Expires()
|
|
if err != nil {
|
|
fmt.Printf("ERROR: renewed token, but couldn't get new expiry date: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("New token expiry date: %s\n", expiry.String())
|
|
os.Exit(0)
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// end of code
|