Skip to content

Commit

Permalink
Add Prefetching to Vault backend
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Apr 19, 2018
1 parent 14e27fa commit c0289b9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_install:
- export PATH="$GOPATH/bin:$PATH"
- docker run -d -p 2379:2379 quay.io/coreos/etcd /usr/local/bin/etcd -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379
- docker run -d -p 8500:8500 --name consul consul
- docker run -d -p 8200:8200 --cap-add=IPC_LOCK -e 'VAULT_DEV_ROOT_TOKEN_ID=root' -e 'VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200' vault
- docker run -d -p 8200:8200 --cap-add=IPC_LOCK -e 'VAULT_DEV_ROOT_TOKEN_ID=root' -e 'VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200' vault:0.9.6

env:
- VAULT_ADDR=http://127.0.0.1:8200
Expand Down
35 changes: 15 additions & 20 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

[[constraint]]
name = "github.com/hashicorp/vault"
version = "0.9.3"
version = "0.10.0"

[prune]
go-tests = true
Expand Down
28 changes: 17 additions & 11 deletions backend/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,34 @@ import (
type Backend struct {
client *api.Logical
path string
secret *api.Secret
}

// NewBackend creates a configuration loader that loads from Vault
func NewBackend(c *api.Logical, p string) *Backend {
// all the keys from the given path and holds them in memory.
func NewBackend(client *api.Logical, path string) *Backend {
return &Backend{
client: c,
path: p,
client: client,
path: path,
}
}

// Get loads the given key from Vault
// Get loads the given key from Vault.
func (b *Backend) Get(ctx context.Context, key string) ([]byte, error) {
secret, err := b.client.Read(b.path)
if err != nil {
return nil, err
}
var err error

if b.secret == nil {
b.secret, err = b.client.Read(b.path)
if err != nil {
return nil, err
}

if secret == nil {
return nil, fmt.Errorf("secret not found at the following path: %s", b.path)
if b.secret == nil {
return nil, fmt.Errorf("secret not found at the following path: %s", b.path)
}
}

if v, ok := secret.Data[key]; ok {
if v, ok := b.secret.Data[key]; ok {
return []byte(v.(string)), nil
}

Expand Down
7 changes: 6 additions & 1 deletion backend/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vault

import (
"context"
"os"
"testing"

"github.com/hashicorp/vault/api"
Expand All @@ -11,21 +12,24 @@ import (
)

func TestVaultBackend(t *testing.T) {
os.Setenv("VAULT_ADDR", "http://127.0.0.1:8200")
client, err := api.NewClient(api.DefaultConfig())
require.NoError(t, err)

client.SetToken("root")
c := client.Logical()

path := "secret/test"
b := NewBackend(c, path)

t.Run("SecretPathNotFound", func(t *testing.T) {
b := NewBackend(c, path)
_, err := b.Get(context.Background(), "foo")
require.EqualError(t, err, "secret not found at the following path: secret/test")
})

t.Run("OK", func(t *testing.T) {
b := NewBackend(c, path)

_, err = c.Write(path,
map[string]interface{}{
"foo": "bar",
Expand All @@ -43,6 +47,7 @@ func TestVaultBackend(t *testing.T) {
})

t.Run("NotFound", func(t *testing.T) {
b := NewBackend(c, path)
_, err := b.Get(context.Background(), "badKey")
require.EqualError(t, err, backend.ErrNotFound.Error())
})
Expand Down

0 comments on commit c0289b9

Please sign in to comment.