-
Notifications
You must be signed in to change notification settings - Fork 9
/
etcd_storage.go
167 lines (144 loc) · 4.67 KB
/
etcd_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package etcd
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"time"
keyStorageLocal "github.com/madappgang/identifo/configuration/key_storage/local"
keyStorageS3 "github.com/madappgang/identifo/configuration/key_storage/s3"
configStorageFile "github.com/madappgang/identifo/configuration/storage/file"
ijwt "github.com/madappgang/identifo/jwt"
"github.com/madappgang/identifo/model"
"go.etcd.io/etcd/clientv3"
)
const (
defaultEtcdConnectionString = "http://127.0.0.1:2379"
timeoutPerRequest = 5 * time.Second
)
// ConfigurationStorage is an etcd-backed storage for server configuration.
type ConfigurationStorage struct {
Client *clientv3.Client
FileStorage *configStorageFile.ConfigurationStorage
settingsKey string
keyStorage model.KeyStorage
}
// NewConfigurationStorage creates new etcd-backed server config storage.
func NewConfigurationStorage(settings model.ConfigurationStorageSettings, serverConfigPath string) (*ConfigurationStorage, error) {
if settings.SettingsKey == "" {
return nil, fmt.Errorf("Empty server settings key for etcd")
}
if settings.Endpoints == nil {
settings.Endpoints = []string{defaultEtcdConnectionString}
}
c, err := clientv3.New(clientv3.Config{
Endpoints: settings.Endpoints,
DialTimeout: timeoutPerRequest,
})
if err != nil {
return nil, err
}
settingsKey := settings.SettingsKey
// Init file storage for config replication.
settings.SettingsKey = serverConfigPath
fileStorage, err := configStorageFile.NewConfigurationStorage(settings)
if err != nil {
return nil, err
}
var keyStorage model.KeyStorage
switch settings.KeyStorage.Type {
case model.KeyStorageTypeLocal:
keyStorage, err = keyStorageLocal.NewKeyStorage(settings.KeyStorage)
case model.KeyStorageTypeS3:
keyStorage, err = keyStorageS3.NewKeyStorage(settings.KeyStorage)
default:
return nil, fmt.Errorf("Unknown key storage type: %s", settings.KeyStorage.Type)
}
if err != nil {
return nil, err
}
return &ConfigurationStorage{
Client: c,
FileStorage: fileStorage,
settingsKey: settingsKey,
keyStorage: keyStorage,
}, nil
}
// InsertConfig inserts key-value pair to configuration storage.
func (cs *ConfigurationStorage) InsertConfig(key string, value interface{}) error {
var strVal string
var err error
switch reflect.TypeOf(value).Kind() {
case reflect.String:
strVal = value.(string)
case reflect.Ptr:
out, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("Cannot serialize pointer %v to string: %s", value, err)
}
strVal = string(out)
}
if key == "" && strVal == "" {
go cs.idleInsertConfig()
return nil
}
_, err = cs.Client.Put(context.Background(), key, strVal)
if err == nil {
// Also update file.
go func() {
if fileErr := cs.FileStorage.InsertConfig(cs.FileStorage.ServerConfigPath, value); fileErr != nil {
fmt.Println("Could not replicate settings in file: ", fileErr)
} else {
fmt.Println("Successfully replicated settings in file")
}
}()
}
return err
}
// LoadServerSettings loads server configuration from configuration storage.
func (cs *ConfigurationStorage) LoadServerSettings(settings *model.ServerSettings) error {
res, err := cs.Client.Get(context.Background(), cs.settingsKey)
if err != nil {
return fmt.Errorf("Cannot get value by key %s: %s", cs.settingsKey, err)
}
if len(res.Kvs) == 0 {
return fmt.Errorf("Etcd: No value for key %s", cs.settingsKey)
}
err = json.Unmarshal(res.Kvs[0].Value, settings)
return err
}
// InsertKeys inserts new public and private keys into the key storage.
func (cs *ConfigurationStorage) InsertKeys(keys *model.JWTKeys) error {
if err := cs.keyStorage.InsertKeys(keys); err != nil {
return err
}
return nil
}
// LoadKeys loads public and private keys from the key storage.
func (cs *ConfigurationStorage) LoadKeys(alg ijwt.TokenSignatureAlgorithm) (*model.JWTKeys, error) {
return cs.keyStorage.LoadKeys(alg)
}
// GetUpdateChan implements ConfigurationStorage interface.
func (cs *ConfigurationStorage) GetUpdateChan() chan interface{} {
return make(chan interface{}, 1)
}
// CloseUpdateChan implements ConfigurationStorage interface.
func (cs *ConfigurationStorage) CloseUpdateChan() {}
// idleInsertConfig inserts existing settings.
func (cs *ConfigurationStorage) idleInsertConfig() {
key := cs.settingsKey
settings := new(model.ServerSettings)
if err := cs.LoadServerSettings(settings); err != nil {
log.Println("Error while idle config insert: could not load server settings.", err)
return
}
if key == "" {
log.Println("Error while idle config insert: empty key.")
return
}
if err := cs.InsertConfig(key, settings); err != nil {
log.Println("Error while idle config insert.", err)
return
}
}