diff options
author | Ben Burwell <ben@benburwell.com> | 2020-04-13 23:57:13 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2020-04-13 23:57:13 -0400 |
commit | 2ce3b86e0ff69538935db3149d1ed2f24aea09a3 (patch) | |
tree | 1c0329a5c1191690e57e7160bd3150c9a2851866 /server/helpers/vault | |
parent | 8b1ee3e95010681d98d1b31af98f0ce0832cedd2 (diff) |
Simplify
Diffstat (limited to 'server/helpers/vault')
-rw-r--r-- | server/helpers/vault/vault.go | 62 |
1 files changed, 0 insertions, 62 deletions
diff --git a/server/helpers/vault/vault.go b/server/helpers/vault/vault.go deleted file mode 100644 index e522d51..0000000 --- a/server/helpers/vault/vault.go +++ /dev/null @@ -1,62 +0,0 @@ -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 -} |