-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (67 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"os"
"fmt"
"github.com/hashicorp/vault/api"
)
type VaultCryptoki struct {
DerivedRootTokenTTL string
DerivedRootTokenMaxTTL string
DerivedRootTokenWrapTTL string
DerivedRootTokenNumUses int
}
func (v *VaultCryptoki) DeriveRootToken(vaultAddr, rootToken string) (derivedToken string, err error) {
vaultConfig := api.DefaultConfig()
vaultConfig.Address = vaultAddr
client, err := api.NewClient(vaultConfig)
if err != nil {
return derivedToken, err
}
tokenCreateRequest := &api.TokenCreateRequest {
TTL: v.DerivedRootTokenTTL,
ExplicitMaxTTL: v.DerivedRootTokenMaxTTL,
DisplayName: "VCDerivedRootToken",
NumUses: 10,
}
client.SetToken(rootToken)
if v.DerivedRootTokenWrapTTL != "" {
client.SetWrappingLookupFunc(func(op, path string) string {
fmt.Printf("DeriveRootToken op=%s path=%s wrapTTL=%s\n", op, path, v.DerivedRootTokenWrapTTL)
// expect op=POST path=auth/token/create
return v.DerivedRootTokenWrapTTL
})
}
auth := client.Auth()
tokenAuth := auth.Token()
secret, err := tokenAuth.Create(tokenCreateRequest)
if err != nil {
fmt.Printf("Error: Failure creating derived root token: %v\n", err)
return derivedToken, err
}
fmt.Printf("Created token secret: %+v\n", secret)
derivedToken, err = secret.TokenID()
return derivedToken, err
}
func main() {
v := &VaultCryptoki{
DerivedRootTokenTTL: "5m",
DerivedRootTokenMaxTTL: "30m",
DerivedRootTokenWrapTTL: "1m",
DerivedRootTokenNumUses: 5,
}
vaultAddr := os.Getenv("VAULT_ADDR")
rootToken := os.Getenv("VAULT_TOKEN")
token2, err := v.DeriveRootToken(vaultAddr, rootToken)
if err != nil {
fmt.Printf("Error from DeriveRootToken; %v\n", err)
os.Exit(1)
}
fmt.Printf("Wrapped token: %+v\n", token2)
v.DerivedRootTokenWrapTTL = ""
token2, err = v.DeriveRootToken(vaultAddr, rootToken)
if err != nil {
fmt.Printf("Error from DeriveRootToken; %v\n", err)
os.Exit(1)
}
fmt.Printf("Non wrapped token: %+v\n", token2)
}