Skip to content

Commit

Permalink
Implement Consul K/V storage backend
Browse files Browse the repository at this point in the history
Fixes #672
  • Loading branch information
dominikschulz committed Mar 4, 2018
1 parent 2826b33 commit 99dbe56
Show file tree
Hide file tree
Showing 51 changed files with 7,332 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions backend/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func WithStoreBackendString(ctx context.Context, sb string) context.Context {
return WithStoreBackend(ctx, KVMock)
case "fs":
return WithStoreBackend(ctx, FS)
case "consul":
return WithStoreBackend(ctx, Consul)
default:
return WithStoreBackend(ctx, FS)
}
Expand All @@ -147,6 +149,8 @@ func StoreBackendName(sb StoreBackend) string {
return "fs"
case KVMock:
return "kvmock"
case Consul:
return "consul"
default:
return ""
}
Expand Down
2 changes: 2 additions & 0 deletions backend/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
KVMock StoreBackend = iota
// FS is a filesystem-backend storage
FS
// Consul is a consul backend storage
Consul
)

// Store is an storage backend
Expand Down
91 changes: 91 additions & 0 deletions backend/store/kv/consul/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package consul

import (
"context"

"github.com/blang/semver"
api "github.com/hashicorp/consul/api"
)

type Store struct {
api *api.Client
}

func New(host, datacenter, token string) (*Store, error) {
client, err := api.NewClient(&api.Config{
Address: host,
Datacenter: datacenter,
Token: token,
})
if err != nil {
return nil, err
}
return &Store{
api: client,
}, nil
}

func (s *Store) Get(ctx context.Context, name string) ([]byte, error) {
p, _, err := s.api.KV().Get(name, nil)
if err != nil {
return nil, err
}
if p == nil || p.Value == nil {
return nil, nil
}
return p.Value, nil
}

func (s *Store) Set(ctx context.Context, name string, value []byte) error {
p := &api.KVPair{
Key: name,
Value: value,
}
_, err := s.api.KV().Put(p, nil)
return err
}

func (s *Store) Delete(ctx context.Context, name string) error {
_, err := s.api.KV().Delete(name, nil)
return err
}

func (s *Store) Exists(ctx context.Context, name string) bool {
v, err := s.Get(ctx, name)
if err == nil && v != nil {
return true
}
return false
}

func (s *Store) List(ctx context.Context, prefix string) ([]string, error) {
pairs, _, err := s.api.KV().List(prefix, nil)
if err != nil {
return nil, err
}
res := make([]string, len(pairs))
for _, kvp := range pairs {
res = append(res, kvp.Key)
}
return res, nil
}

func (s *Store) IsDir(ctx context.Context, name string) bool {
ls, err := s.List(ctx, name)
if err == nil && len(ls) > 1 {
return true
}
return false
}

func (s *Store) Prune(ctx context.Context, prefix string) error {
return s.Delete(ctx, prefix)
}

func (s *Store) Name() string {
return "consul"
}

func (s *Store) Version() semver.Version {
return semver.Version{Major: 1}
}
9 changes: 9 additions & 0 deletions store/sub/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/justwatchcom/gopass/backend/crypto/gpg/openpgp"
"github.com/justwatchcom/gopass/backend/crypto/xc"
"github.com/justwatchcom/gopass/backend/store/fs"
kvconsul "github.com/justwatchcom/gopass/backend/store/kv/consul"
kvmock "github.com/justwatchcom/gopass/backend/store/kv/mock"
gitcli "github.com/justwatchcom/gopass/backend/sync/git/cli"
"github.com/justwatchcom/gopass/backend/sync/git/gogit"
Expand Down Expand Up @@ -51,6 +52,14 @@ func New(ctx context.Context, alias, path string, cfgdir string) (*Store, error)
case backend.KVMock:
s.store = kvmock.New()
out.Debug(ctx, "Using Store Backend: kvmock")
case backend.Consul:
// TODO need #677 first
store, err := kvconsul.New("TODO", "TODO", "TODO")
if err != nil {
return nil, err
}
s.store = store
out.Debug(ctx, "Using Store Backend: consul")
default:
return nil, fmt.Errorf("Unknown store backend")
}
Expand Down
Loading

0 comments on commit 99dbe56

Please sign in to comment.