-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvault.go
72 lines (55 loc) · 1.33 KB
/
vault.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
vaultapi "github.com/hashicorp/vault/api"
)
var (
secretjob *Secret
)
type Secret struct {
Data SecretToken `json:"data"`
}
type SecretToken struct {
Token string `json:"token"`
}
type VaultClient struct {
client *vaultapi.Client
}
func createVaultClient(tokenRef, vaultUrl string) (*VaultClient, error) {
config := &vaultapi.Config{
Address: vaultUrl,
}
// get secrets from token ref
resp, err := http.Get(apiHost + fmt.Sprintf("/api/v1/namespaces/default/secrets/"+tokenRef))
if err != nil {
log.Println(red("Error pulling secrets", err))
return nil, err
}
secretObj := json.NewDecoder(resp.Body)
err = secretObj.Decode(&secretjob)
if err != nil {
log.Println(err)
}
token := secretjob.Data.Token
decodedToken, err := base64.StdEncoding.DecodeString(token)
client, err := vaultapi.NewClient(config)
client.SetToken(string(decodedToken))
if err != nil {
log.Println("Error creating vault", err)
return nil, err
}
return &VaultClient{client}, nil
}
func (vaultClient *VaultClient) read(secretPath string) (map[string]interface{}, error) {
c := vaultClient.client.Logical()
secret, err := c.Read(secretPath)
if err != nil {
log.Println("VAULT Error getting secret", err)
return nil, err
}
return secret.Data, nil
}