-
Notifications
You must be signed in to change notification settings - Fork 7
/
vault.go
53 lines (42 loc) · 1.21 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
package vault
import (
"fmt"
"github.com/hashicorp/vault/api"
)
// Client holds a Hashicorp Vault client
type Client struct {
*api.Client
authPath string
}
// NewClient create a new instance of *Client
func NewClient(authPath string) (*Client, error) {
c, err := api.NewClient(api.DefaultConfig())
if err != nil {
return nil, err
}
return &Client{c, authPath}, nil
}
// Authenticate authenticates hashicorp Vault using a given jwt and role
func (c *Client) Authenticate(jwt, role string) error {
if jwt == "" {
return fmt.Errorf("no jwt token provided to authenticate vault")
}
authData := map[string]interface{}{
"jwt": jwt,
"role": role,
}
secret, err := c.Logical().Write(fmt.Sprintf("auth/%s/login", c.authPath), authData)
if err != nil {
return fmt.Errorf("failed to login using jwt: %w", err)
}
c.SetToken(secret.Auth.ClientToken)
return nil
}
// GetSecret reads a secret from the given path and key
func (c *Client) GetSecret(path, key string) (map[string]interface{}, error) {
secret, err := c.Logical().Read(fmt.Sprintf("%s/data/%s", path, key))
if err != nil {
return nil, fmt.Errorf("failed to retrieve secret with key '%s' from '%s': %w", key, path, err)
}
return secret.Data, nil
}