aboutsummaryrefslogtreecommitdiff
path: root/server/helpers/vault/vault.go
blob: e522d51d0bbea6424383bdd9a108ded016522731 (plain)
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
package vault

import (
	"fmt"
	"strings"

	"github.com/hashicorp/vault/api"
)

// NewClient returns a new vault client.
func NewClient(address, token string) (*Client, error) {
	config := &api.Config{
		Address: address,
	}
	client, err := api.NewClient(config)
	if err != nil {
		return nil, err
	}
	client.SetToken(token)
	return &Client{
		vault: client,
	}, nil
}

func parseName(name string) (path, key string) {
	name = strings.TrimPrefix(name, "/vault/")
	i := strings.LastIndex(name, "/")
	if i < 0 {
		return name, ""
	}
	return name[:i], name[i+1:]
}

// Client is a simple client for vault.
type Client struct {
	vault *api.Client
}

// Read returns a secret for a given path and key of the form `/vault/secret/path/key`.
// If the requested key cannot be read the original string is returned along with an error.
func (c *Client) Read(value string) (string, error) {
	p, k := parseName(value)
	data, err := c.vault.Logical().Read(p)
	if err != nil {
		return value, err
	}
	if data == nil {
		return value, fmt.Errorf("no such key %s", k)
	}
	secret, ok := data.Data[k]
	if !ok {
		return value, fmt.Errorf("no such key %s", k)
	}
	return secret.(string), nil
}

// Delete deletes the secret from vault.
func (c *Client) Delete(value string) error {
	p, _ := parseName(value)
	_, err := c.vault.Logical().Delete(p)
	return err
}