/
configmgmtservice.go
executable file
·300 lines (252 loc) · 8.88 KB
/
configmgmtservice.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package service
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"sync"
"time"
"math/rand"
"encoding/json"
"crypto/sha256"
"encoding/base64"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/peer"
"github.com/securekey/fabric-snaps/configmanager/api"
"github.com/securekey/fabric-snaps/configmanager/pkg/mgmt"
cfgsnapapi "github.com/securekey/fabric-snaps/configurationsnap/api"
"github.com/securekey/fabric-snaps/util/errors"
)
var logger = logging.NewLogger("configsnap")
type cache map[string][]byte
//ConfigServiceImpl used to create cache instance
type ConfigServiceImpl struct {
mtx sync.RWMutex
cacheMap map[string]cache
configHashes map[string]string
metrics *Metrics
}
var instance = newConfigService()
//GetInstance gets instance of cache for snaps
func GetInstance() api.ConfigService {
return instance
}
func newConfigService() *ConfigServiceImpl {
service := &ConfigServiceImpl{}
service.cacheMap = make(map[string]cache)
service.configHashes = make(map[string]string)
return service
}
//Initialize will be called from config snap
func Initialize(stub shim.ChaincodeStubInterface, mspID string) *ConfigServiceImpl {
instance.Refresh(stub, mspID)
instance.Refresh(stub, cfgsnapapi.GeneralMspID)
return instance
}
//Get items from cache
func (csi *ConfigServiceImpl) Get(channelID string, configKey api.ConfigKey) ([]byte, bool, errors.Error) {
if csi == nil {
return nil, false, errors.New(errors.SystemError, "ConfigServiceImpl was not initialized")
}
if configKey.AppVersion == "" {
configKey.AppVersion = api.VERSION
}
channelCache := csi.getCache(channelID, configKey.MspID)
if channelCache == nil {
logger.Debugf("Config cache is not initialized for channel [%s]. Getting config from ledger.\n", channelID)
return csi.GetConfigFromLedger(channelID, configKey)
}
keyStr, err := mgmt.ConfigKeyToString(configKey)
if err != nil {
return nil, false, err
}
val := channelCache[keyStr]
if len(val) == 0 {
logger.Debugf("Config cache does not contain config for key [%s] on channel [%s]. Getting config from ledger.\n", keyStr, channelID)
//not in cache get from ledger
return csi.GetConfigFromLedger(channelID, configKey)
}
return val, csi.isConfigDirty(keyStr, val), nil
}
//GetFromCache get items from cache
func (csi *ConfigServiceImpl) GetFromCache(channelID string, configKey api.ConfigKey) ([]byte, errors.Error) {
if csi == nil {
return nil, errors.New(errors.SystemError, "ConfigServiceImpl was not initialized")
}
if configKey.AppVersion == "" {
configKey.AppVersion = api.VERSION
}
channelCache := csi.getCache(channelID, configKey.MspID)
if channelCache == nil {
return nil, errors.Errorf(errors.SystemError, "Config cache is not initialized for channel [%s]", channelID)
}
keyStr, err := mgmt.ConfigKeyToString(configKey)
if err != nil {
return nil, err
}
val := channelCache[keyStr]
if len(val) == 0 {
return nil, errors.Errorf(errors.SystemError, "Config cache does not contain config for key [%s] on channel [%s]", keyStr, channelID)
}
return channelCache[keyStr], nil
}
//GetViper configuration as Viper
func (csi *ConfigServiceImpl) GetViper(channelID string, configKey api.ConfigKey, configType api.ConfigType) (*viper.Viper, bool, errors.Error) {
configData, dirty, err := csi.Get(channelID, configKey)
if err != nil {
return nil, false, err
}
if len(configData) == 0 {
// No config found for the key. Return nil instead of an error so that the caller can differentiate between the two cases
return nil, false, nil
}
v := viper.New()
v.SetConfigType(string(configType))
e := v.ReadConfig(bytes.NewBuffer(configData))
if e != nil {
return nil, false, errors.WithMessage(errors.InitializeConfigError, e, "snap_config_init_error")
}
return v, dirty, err
}
//Refresh adds new items into cache and refreshes existing ones
func (csi *ConfigServiceImpl) Refresh(stub shim.ChaincodeStubInterface, mspID string) errors.Error {
logger.Debugf("***Refreshing mspid %s at %v\n", mspID, time.Unix(time.Now().Unix(), 0))
if csi == nil {
return errors.New(errors.SystemError, "ConfigServiceImpl was not initialized")
}
if stub == nil {
return errors.New(errors.SystemError, "Stub is nil")
}
configManager := mgmt.NewConfigManager(stub)
//get all by mspID
configKey := api.ConfigKey{MspID: mspID}
configMessages, err := configManager.Get(configKey)
if err != nil {
return err
}
if len(configMessages) == 0 {
return errors.Errorf(errors.SystemError, "Cannot create criteria for search by mspID %v", configMessages)
}
return csi.refreshCache(stub.GetChannelID(), configMessages, mspID)
}
//GetConfigFromLedger - gets snaps configs from ledger
func (csi *ConfigServiceImpl) GetConfigFromLedger(channelID string, configKey api.ConfigKey) ([]byte, bool, errors.Error) {
logger.Debugf("Getting key [%#v] on channel [%s]", configKey, channelID)
lgr := peer.GetLedger(channelID)
if lgr != nil {
logger.Debugf("****Ledger is set for channelID %s\n", channelID)
r := generateRandomAlphaOnlyString(12)
txsim, err := lgr.NewTxSimulator(r)
if err != nil {
errObj := errors.WithMessage(errors.SystemError, err, "Cannot create transaction simulator")
logger.Errorf("Get config from ledger failed: %s", errObj.GenerateLogMsg())
return nil, false, errObj
}
defer txsim.Done()
keyStr, e := mgmt.ConfigKeyToString(configKey)
if e != nil {
return nil, false, e
}
config, err := txsim.GetState("configurationsnap", keyStr)
if err != nil {
errObj := errors.WithMessage(errors.SystemError, err, fmt.Sprintf("Error getting state for app %s %s", keyStr, err))
logger.Errorf("Get config from ledger failed: %s", errObj.GenerateLogMsg())
return nil, false, errObj
}
return config, csi.isConfigDirty(keyStr, config), nil
}
return nil, false, errors.Errorf(errors.SystemError, "Cannot obtain ledger for channel %s", channelID)
}
//isConfigDirty checks if config retrieved for given key string is updated since its last retrieval.
// it checks hash of config bytes previously to current one, if there is a mismatch then returns true
func (csi *ConfigServiceImpl) isConfigDirty(keyStr string, config []byte) bool {
if len(config) == 0 {
return false
}
var dirtyFlag bool
currentHash := csi.generateHash(config)
csi.mtx.RLock()
hash, ok := csi.configHashes[keyStr]
if ok {
dirtyFlag = !(currentHash == hash)
} else {
dirtyFlag = true
}
csi.mtx.RUnlock()
if dirtyFlag {
//if there is config update then update hash values in map
csi.mtx.Lock()
csi.configHashes[keyStr] = currentHash
csi.mtx.Unlock()
}
return dirtyFlag
}
// generateHash generates hash for give bytes
func (csi *ConfigServiceImpl) generateHash(bytes []byte) string {
digest := sha256.Sum256(bytes)
return base64.StdEncoding.EncodeToString(digest[:])
}
func (csi *ConfigServiceImpl) refreshCache(channelID string, configMessages []*api.ConfigKV, mspID string) errors.Error {
if csi == nil {
return errors.New(errors.SystemError, "ConfigServiceImpl was not initialized")
}
logger.Debugf("Updating cache for channel %s\n", channelID)
cache := make(map[string][]byte)
compCache := make(map[string][]*api.ComponentConfig)
for _, val := range configMessages {
keyStr, err := mgmt.ConfigKeyToString(val.Key)
if err != nil {
return err
}
logger.Debugf("Adding item for key [%s] and channel [%s] to cache\n", keyStr, channelID)
cache[keyStr] = val.Value
if val.Key.ComponentName != "" {
key := val.Key
key.ComponentVersion = ""
keyStr, err = mgmt.ConfigKeyToString(key)
if err != nil {
return err
}
compConfig := api.ComponentConfig{}
err := json.Unmarshal(val.Value, &compConfig)
if err != nil {
return errors.Wrap(errors.UnmarshalError, err, "Error occurred while un-marshalling")
}
if _, ok := compCache[keyStr]; !ok {
compCache[keyStr] = make([]*api.ComponentConfig, 0)
}
compCache[keyStr] = append(compCache[keyStr], &compConfig)
}
}
for key, comps := range compCache {
compsBytes, e := json.Marshal(comps)
if e != nil {
return errors.WithMessage(errors.SystemError, e, "Failed to marshal component")
}
cache[key] = compsBytes
}
csi.mtx.Lock()
defer csi.mtx.Unlock()
instance.cacheMap[channelID+"_"+mspID] = cache
logger.Debugf("Updated cache for channel %s\n", channelID)
return nil
}
func (csi *ConfigServiceImpl) getCache(channelID, mspID string) cache {
csi.mtx.RLock()
defer csi.mtx.RUnlock()
return csi.cacheMap[channelID+"_"+mspID]
}
// generateRandomAlphaOnlyString generates an alphabetical random string with length n.
func generateRandomAlphaOnlyString(n int) string {
// make a really long string
letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}