| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"io/ioutil" |
| 7 |
|
| 8 |
"github.com/xordataexchange/crypt/backend" |
| 9 |
"github.com/xordataexchange/crypt/backend/consul" |
| 10 |
"github.com/xordataexchange/crypt/backend/etcd" |
| 11 |
"github.com/xordataexchange/crypt/encoding/secconf" |
| 12 |
) |
| 13 |
|
| 14 |
type KVPair struct { |
| 15 |
backend.KVPair |
| 16 |
} |
| 17 |
|
| 18 |
type KVPairs []*KVPair |
| 19 |
|
| 20 |
type configManager struct { |
| 21 |
keystore []byte |
| 22 |
store backend.Store |
| 23 |
} |
| 24 |
|
| 25 |
// A ConfigManager retrieves and decrypts configuration from a key/value store. |
| 26 |
type ConfigManager interface { |
| 27 |
Get(key string) ([]byte, error) |
| 28 |
List(key string) (KVPairs, error) |
| 29 |
Set(key string, value []byte) error |
| 30 |
Watch(key string, stop chan bool) <-chan *Response |
| 31 |
} |
| 32 |
|
| 33 |
type standardConfigManager struct { |
| 34 |
store backend.Store |
| 35 |
} |
| 36 |
|
| 37 |
func NewStandardConfigManager(client backend.Store) (ConfigManager, error) { |
| 38 |
return standardConfigManager{client}, nil |
| 39 |
} |
| 40 |
|
| 41 |
func NewConfigManager(client backend.Store, keystore io.Reader) (ConfigManager, error) { |
| 42 |
bytes, err := ioutil.ReadAll(keystore) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
return configManager{bytes, client}, nil |
| 47 |
} |
| 48 |
|
| 49 |
// NewStandardEtcdConfigManager returns a new ConfigManager backed by etcd. |
| 50 |
func NewStandardEtcdConfigManager(machines []string) (ConfigManager, error) { |
| 51 |
store, err := etcd.New(machines) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
|
| 56 |
return NewStandardConfigManager(store) |
| 57 |
} |
| 58 |
|
| 59 |
// NewStandardConsulConfigManager returns a new ConfigManager backed by consul. |
| 60 |
func NewStandardConsulConfigManager(machines []string) (ConfigManager, error) { |
| 61 |
store, err := consul.New(machines) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
return NewStandardConfigManager(store) |
| 66 |
} |
| 67 |
|
| 68 |
// NewEtcdConfigManager returns a new ConfigManager backed by etcd. |
| 69 |
// Data will be encrypted. |
| 70 |
func NewEtcdConfigManager(machines []string, keystore io.Reader) (ConfigManager, error) { |
| 71 |
store, err := etcd.New(machines) |
| 72 |
if err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
return NewConfigManager(store, keystore) |
| 76 |
} |
| 77 |
|
| 78 |
// NewConsulConfigManager returns a new ConfigManager backed by consul. |
| 79 |
// Data will be encrypted. |
| 80 |
func NewConsulConfigManager(machines []string, keystore io.Reader) (ConfigManager, error) { |
| 81 |
store, err := consul.New(machines) |
| 82 |
if err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
return NewConfigManager(store, keystore) |
| 86 |
} |
| 87 |
|
| 88 |
// Get retrieves and decodes a secconf value stored at key. |
| 89 |
func (c configManager) Get(key string) ([]byte, error) { |
| 90 |
value, err := c.store.Get(key) |
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
return secconf.Decode(value, bytes.NewBuffer(c.keystore)) |
| 95 |
} |
| 96 |
|
| 97 |
// Get retrieves a value stored at key. |
| 98 |
// convenience function, no additional value provided over |
| 99 |
// `etcdctl` |
| 100 |
func (c standardConfigManager) Get(key string) ([]byte, error) { |
| 101 |
value, err := c.store.Get(key) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
return value, err |
| 106 |
} |
| 107 |
|
| 108 |
// List retrieves and decodes all secconf value stored under key. |
| 109 |
func (c configManager) List(key string) (KVPairs, error) { |
| 110 |
list, err := c.store.List(key) |
| 111 |
retList := make(KVPairs, len(list)) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
for i, kv := range list { |
| 116 |
retList[i].Key = kv.Key |
| 117 |
retList[i].Value, err = secconf.Decode(kv.Value, bytes.NewBuffer(c.keystore)) |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
} |
| 122 |
return retList, nil |
| 123 |
} |
| 124 |
|
| 125 |
// List retrieves all values under key. |
| 126 |
// convenience function, no additional value provided over |
| 127 |
// `etcdctl` |
| 128 |
func (c standardConfigManager) List(key string) (KVPairs, error) { |
| 129 |
list, err := c.store.List(key) |
| 130 |
retList := make(KVPairs, len(list)) |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
for i, kv := range list { |
| 135 |
retList[i].Key = kv.Key |
| 136 |
retList[i].Value = kv.Value |
| 137 |
} |
| 138 |
return retList, err |
| 139 |
} |
| 140 |
|
| 141 |
// Set will put a key/value into the data store |
| 142 |
// and encode it with secconf |
| 143 |
func (c configManager) Set(key string, value []byte) error { |
| 144 |
encodedValue, err := secconf.Encode(value, bytes.NewBuffer(c.keystore)) |
| 145 |
if err == nil { |
| 146 |
err = c.store.Set(key, encodedValue) |
| 147 |
} |
| 148 |
return err |
| 149 |
} |
| 150 |
|
| 151 |
// Set will put a key/value into the data store |
| 152 |
func (c standardConfigManager) Set(key string, value []byte) error { |
| 153 |
err := c.store.Set(key, value) |
| 154 |
return err |
| 155 |
} |
| 156 |
|
| 157 |
type Response struct { |
| 158 |
Value []byte |
| 159 |
Error error |
| 160 |
} |
| 161 |
|
| 162 |
func (c configManager) Watch(key string, stop chan bool) <-chan *Response { |
| 163 |
resp := make(chan *Response, 0) |
| 164 |
backendResp := c.store.Watch(key, stop) |
| 165 |
go func() { |
| 166 |
for { |
| 167 |
select { |
| 168 |
case <-stop: |
| 169 |
return |
| 170 |
case r := <-backendResp: |
| 171 |
if r.Error != nil { |
| 172 |
resp <- &Response{nil, r.Error} |
| 173 |
continue |
| 174 |
} |
| 175 |
value, err := secconf.Decode(r.Value, bytes.NewBuffer(c.keystore)) |
| 176 |
resp <- &Response{value, err} |
| 177 |
} |
| 178 |
} |
| 179 |
}() |
| 180 |
return resp |
| 181 |
} |
| 182 |
|
| 183 |
func (c standardConfigManager) Watch(key string, stop chan bool) <-chan *Response { |
| 184 |
resp := make(chan *Response, 0) |
| 185 |
backendResp := c.store.Watch(key, stop) |
| 186 |
go func() { |
| 187 |
for { |
| 188 |
select { |
| 189 |
case <-stop: |
| 190 |
return |
| 191 |
case r := <-backendResp: |
| 192 |
if r.Error != nil { |
| 193 |
resp <- &Response{nil, r.Error} |
| 194 |
continue |
| 195 |
} |
| 196 |
resp <- &Response{r.Value, nil} |
| 197 |
} |
| 198 |
} |
| 199 |
}() |
| 200 |
return resp |
| 201 |
} |