-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (55 loc) · 1.29 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
package main
import (
"os"
"time"
"github.com/hashicorp/vault/api"
"github.com/sirupsen/logrus"
)
func main() {
client, err := api.NewClient(&api.Config{
Address: os.Getenv("VAULT_ADDR"),
Timeout: time.Second * 60,
})
if err != nil {
logrus.Fatal(err)
}
initialized, err := client.Sys().InitStatus()
if err != nil {
logrus.Fatal(err)
}
if initialized {
logrus.Info("Vault is initialized")
status, err := client.Sys().SealStatus()
if err != nil {
logrus.Fatal(err)
}
if status.Sealed {
logrus.Info("Vault is sealed. Unsealing...")
_, err := client.Sys().Unseal(os.Getenv("UNSEAL_KEY"))
if err != nil {
logrus.Fatal(err)
}
} else {
logrus.Info("Vault is already unlocked")
}
logrus.Info("Sleeping for 1h now")
time.Sleep(1 * time.Hour)
return
}
logrus.Info("Not initialized. Initializing!")
init, err := client.Sys().Init(&api.InitRequest{
SecretShares: 1,
SecretThreshold: 1,
})
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Vault initialized successfully.")
logrus.Infof("Root token: %s", init.RootToken)
for _, key := range init.Keys {
logrus.Infof("Please set the following UNSEAL_KEY: %s", key)
}
logrus.Info("Sleeping forever now. Please kill this container when configured correctly.")
i := make(chan int)
<-i
}