summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/xordataexchange/crypt/backend
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/xordataexchange/crypt/backend')
-rw-r--r--vendor/github.com/xordataexchange/crypt/backend/LICENSE9
-rw-r--r--vendor/github.com/xordataexchange/crypt/backend/backend.go32
-rw-r--r--vendor/github.com/xordataexchange/crypt/backend/consul/consul.go87
-rw-r--r--vendor/github.com/xordataexchange/crypt/backend/etcd/etcd.go116
-rw-r--r--vendor/github.com/xordataexchange/crypt/backend/mock/mock.go61
5 files changed, 305 insertions, 0 deletions
diff --git a/vendor/github.com/xordataexchange/crypt/backend/LICENSE b/vendor/github.com/xordataexchange/crypt/backend/LICENSE
new file mode 100644
index 00000000..43846317
--- /dev/null
+++ b/vendor/github.com/xordataexchange/crypt/backend/LICENSE
@@ -0,0 +1,9 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 XOR Data Exchange, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/github.com/xordataexchange/crypt/backend/backend.go b/vendor/github.com/xordataexchange/crypt/backend/backend.go
new file mode 100644
index 00000000..bfe894dc
--- /dev/null
+++ b/vendor/github.com/xordataexchange/crypt/backend/backend.go
@@ -0,0 +1,32 @@
+// Package backend provides the K/V store interface for crypt backends.
+package backend
+
+// Response represents a response from a backend store.
+type Response struct {
+ Value []byte
+ Error error
+}
+
+// KVPair holds both a key and value when reading a list.
+type KVPair struct {
+ Key string
+ Value []byte
+}
+
+type KVPairs []*KVPair
+
+// A Store is a K/V store backend that retrieves and sets, and monitors
+// data in a K/V store.
+type Store interface {
+ // Get retrieves a value from a K/V store for the provided key.
+ Get(key string) ([]byte, error)
+
+ // List retrieves all keys and values under a provided key.
+ List(key string) (KVPairs, error)
+
+ // Set sets the provided key to value.
+ Set(key string, value []byte) error
+
+ // Watch monitors a K/V store for changes to key.
+ Watch(key string, stop chan bool) <-chan *Response
+}
diff --git a/vendor/github.com/xordataexchange/crypt/backend/consul/consul.go b/vendor/github.com/xordataexchange/crypt/backend/consul/consul.go
new file mode 100644
index 00000000..cf85ed53
--- /dev/null
+++ b/vendor/github.com/xordataexchange/crypt/backend/consul/consul.go
@@ -0,0 +1,87 @@
+package consul
+
+import (
+ "fmt"
+ "strings"
+ "time"
+
+ "github.com/xordataexchange/crypt/backend"
+
+ "github.com/armon/consul-api"
+)
+
+type Client struct {
+ client *consulapi.KV
+ waitIndex uint64
+}
+
+func New(machines []string) (*Client, error) {
+ conf := consulapi.DefaultConfig()
+ if len(machines) > 0 {
+ conf.Address = machines[0]
+ }
+ client, err := consulapi.NewClient(conf)
+ if err != nil {
+ return nil, err
+ }
+ return &Client{client.KV(), 0}, nil
+}
+
+func (c *Client) Get(key string) ([]byte, error) {
+ kv, _, err := c.client.Get(key, nil)
+ if err != nil {
+ return nil, err
+ }
+ if kv == nil {
+ return nil, fmt.Errorf("Key ( %s ) was not found.", key)
+ }
+ return kv.Value, nil
+}
+
+func (c *Client) List(key string) (backend.KVPairs, error) {
+ pairs, _, err := c.client.List(key, nil)
+ if err != nil {
+ return nil, err
+ }
+ if err != nil {
+ return nil, err
+ }
+ ret := make(backend.KVPairs, len(pairs), len(pairs))
+ for i, kv := range pairs {
+ ret[i] = &backend.KVPair{Key: kv.Key, Value: kv.Value}
+ }
+ return ret, nil
+}
+
+func (c *Client) Set(key string, value []byte) error {
+ key = strings.TrimPrefix(key, "/")
+ kv := &consulapi.KVPair{
+ Key: key,
+ Value: value,
+ }
+ _, err := c.client.Put(kv, nil)
+ return err
+}
+
+func (c *Client) Watch(key string, stop chan bool) <-chan *backend.Response {
+ respChan := make(chan *backend.Response, 0)
+ go func() {
+ for {
+ opts := consulapi.QueryOptions{
+ WaitIndex: c.waitIndex,
+ }
+ keypair, meta, err := c.client.Get(key, &opts)
+ if keypair == nil && err == nil {
+ err = fmt.Errorf("Key ( %s ) was not found.", key)
+ }
+ if err != nil {
+ respChan <- &backend.Response{nil, err}
+ time.Sleep(time.Second * 5)
+ continue
+ }
+ c.waitIndex = meta.LastIndex
+ respChan <- &backend.Response{keypair.Value, nil}
+ }
+ }()
+ return respChan
+}
diff --git a/vendor/github.com/xordataexchange/crypt/backend/etcd/etcd.go b/vendor/github.com/xordataexchange/crypt/backend/etcd/etcd.go
new file mode 100644
index 00000000..18f35510
--- /dev/null
+++ b/vendor/github.com/xordataexchange/crypt/backend/etcd/etcd.go
@@ -0,0 +1,116 @@
+package etcd
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "time"
+
+ "github.com/xordataexchange/crypt/backend"
+
+ goetcd "github.com/coreos/etcd/client"
+)
+
+type Client struct {
+ client goetcd.Client
+ keysAPI goetcd.KeysAPI
+ waitIndex uint64
+}
+
+func New(machines []string) (*Client, error) {
+ newClient, err := goetcd.New(goetcd.Config{
+ Endpoints: machines,
+ })
+ if err != nil {
+ return nil, fmt.Errorf("creating new etcd client for crypt.backend.Client: %v", err)
+ }
+ keysAPI := goetcd.NewKeysAPI(newClient)
+ return &Client{client: newClient, keysAPI: keysAPI, waitIndex: 0}, nil
+}
+
+func (c *Client) Get(key string) ([]byte, error) {
+ return c.GetWithContext(context.TODO(), key)
+}
+
+func (c *Client) GetWithContext(ctx context.Context, key string) ([]byte, error) {
+ resp, err := c.keysAPI.Get(ctx, key, nil)
+ if err != nil {
+ return nil, err
+ }
+ return []byte(resp.Node.Value), nil
+}
+
+func addKVPairs(node *goetcd.Node, list backend.KVPairs) backend.KVPairs {
+ if node.Dir {
+ for _, n := range node.Nodes {
+ list = addKVPairs(n, list)
+ }
+ return list
+ }
+ return append(list, &backend.KVPair{Key: node.Key, Value: []byte(node.Value)})
+}
+
+func (c *Client) List(key string) (backend.KVPairs, error) {
+ return c.ListWithContext(context.TODO(), key)
+}
+
+func (c *Client) ListWithContext(ctx context.Context, key string) (backend.KVPairs, error) {
+ resp, err := c.keysAPI.Get(ctx, key, nil)
+ if err != nil {
+ return nil, err
+ }
+ if !resp.Node.Dir {
+ return nil, errors.New("key is not a directory")
+ }
+ list := addKVPairs(resp.Node, nil)
+ return list, nil
+}
+
+func (c *Client) Set(key string, value []byte) error {
+ return c.SetWithContext(context.TODO(), key, value)
+}
+
+func (c *Client) SetWithContext(ctx context.Context, key string, value []byte) error {
+ _, err := c.keysAPI.Set(ctx, key, string(value), nil)
+ return err
+}
+
+func (c *Client) Watch(key string, stop chan bool) <-chan *backend.Response {
+ return c.WatchWithContext(context.Background(), key, stop)
+}
+
+func (c *Client) WatchWithContext(ctx context.Context, key string, stop chan bool) <-chan *backend.Response {
+ respChan := make(chan *backend.Response, 0)
+ go func() {
+ watcher := c.keysAPI.Watcher(key, nil)
+ ctx, cancel := context.WithCancel(ctx)
+ go func() {
+ <-stop
+ cancel()
+ }()
+ for {
+ var resp *goetcd.Response
+ var err error
+ // if c.waitIndex == 0 {
+ // resp, err = c.client.Get(key, false, false)
+ // if err != nil {
+ // respChan <- &backend.Response{nil, err}
+ // time.Sleep(time.Second * 5)
+ // continue
+ // }
+ // c.waitIndex = resp.EtcdIndex
+ // respChan <- &backend.Response{[]byte(resp.Node.Value), nil}
+ // }
+ // resp, err = c.client.Watch(key, c.waitIndex+1, false, nil, stop)
+ resp, err = watcher.Next(ctx)
+ if err != nil {
+ respChan <- &backend.Response{nil, err}
+ time.Sleep(time.Second * 5)
+ continue
+ }
+ c.waitIndex = resp.Node.ModifiedIndex
+ respChan <- &backend.Response{[]byte(resp.Node.Value), nil}
+ }
+ }()
+ return respChan
+}
diff --git a/vendor/github.com/xordataexchange/crypt/backend/mock/mock.go b/vendor/github.com/xordataexchange/crypt/backend/mock/mock.go
new file mode 100644
index 00000000..68a9b1c7
--- /dev/null
+++ b/vendor/github.com/xordataexchange/crypt/backend/mock/mock.go
@@ -0,0 +1,61 @@
+package mock
+
+import (
+ "errors"
+ "path"
+ "strings"
+ "time"
+
+ "github.com/xordataexchange/crypt/backend"
+)
+
+var mockedStore map[string][]byte
+
+type Client struct{}
+
+func New(machines []string) (*Client, error) {
+ if mockedStore == nil {
+ mockedStore = make(map[string][]byte, 2)
+ }
+ return &Client{}, nil
+}
+
+func (c *Client) Get(key string) ([]byte, error) {
+ if v, ok := mockedStore[key]; ok {
+ return v, nil
+ }
+ err := errors.New("Could not find key: " + key)
+ return nil, err
+}
+
+func (c *Client) List(key string) (backend.KVPairs, error) {
+ var list backend.KVPairs
+ dir := path.Clean(key) + "/"
+ for k, v := range mockedStore {
+ if strings.HasPrefix(k, dir) {
+ list = append(list, &backend.KVPair{Key: k, Value: v})
+ }
+ }
+ return list, nil
+}
+
+func (c *Client) Set(key string, value []byte) error {
+ mockedStore[key] = value
+ return nil
+}
+
+func (c *Client) Watch(key string, stop chan bool) <-chan *backend.Response {
+ respChan := make(chan *backend.Response, 0)
+ go func() {
+ for {
+ b, err := c.Get(key)
+ if err != nil {
+ respChan <- &backend.Response{nil, err}
+ time.Sleep(time.Second * 5)
+ continue
+ }
+ respChan <- &backend.Response{b, nil}
+ }
+ }()
+ return respChan
+}