Skip to content

Instantly share code, notes, and snippets.

@cpuspellcaster
Created September 10, 2018 23:28
Show Gist options
  • Save cpuspellcaster/1e46019a617c5253447dd8b0c560824d to your computer and use it in GitHub Desktop.
Save cpuspellcaster/1e46019a617c5253447dd8b0c560824d to your computer and use it in GitHub Desktop.
Example Vault token renewal operation in Go
package main
import (
"fmt"
"time"
vault "github.com/hashicorp/vault/api"
)
func main() {
client, _ := vault.NewClient(nil)
ta := client.Auth().Token()
s, err := ta.LookupSelf()
if err != nil {
panic(err)
}
for {
renewable, _ := s.TokenIsRenewable()
if renewable {
d, _ := s.TokenTTL()
fmt.Printf("Token TTL: %d (Renewing in: %d)\n", d, d/2)
timer := time.NewTimer(d / 2)
select {
case <-timer.C:
break
}
fmt.Printf("Incrementing Token TTL for %ds\n", d)
s, _ = ta.RenewSelf(int(d / 2))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment