Created
September 10, 2018 23:28
-
-
Save cpuspellcaster/1e46019a617c5253447dd8b0c560824d to your computer and use it in GitHub Desktop.
Example Vault token renewal operation in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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