/
endpoint.go
601 lines (544 loc) · 20.5 KB
/
endpoint.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package kv
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/neuvector/neuvector/controller/access"
"github.com/neuvector/neuvector/controller/api"
"github.com/neuvector/neuvector/share"
"github.com/neuvector/neuvector/share/cluster"
"github.com/neuvector/neuvector/share/utils"
)
type purgeFilterFunc func(epName, key string) bool
type cfgEndpoint struct {
name string
key string
section string
lock string
isStore bool
purgeFilter purgeFilterFunc
}
const (
_filterFedPolicyObjects = iota + 1
)
type fedKeyInfo struct {
filterFedObjectType int // the fed policy objects to filter on non-master clusters under an endpoint
fedMasterOnlyKeys []string // the keys to filter on non-master clusters under an endpoint, like "object/config/admission_control/fed/"
filterSubKeyPrefix []string // the subkey prefix to filter on non-master clusters under an endpoint, like "fed.xyz"
alwaysFilterKeys []string // the keys to always skip under an endpoint, like "object/config/federation/clusters_status/"
}
// for backup/restore filtering of joint-clusters in a fed
var _fedKeyInfo = map[string]fedKeyInfo{
share.CFGEndpointGroup: fedKeyInfo{filterSubKeyPrefix: []string{api.FederalGroupPrefix}}, // filter keys like object/config/group/fed.group-1
share.CFGEndpointPolicy: fedKeyInfo{filterFedObjectType: _filterFedPolicyObjects},
share.CFGEndpointProcessProfile: fedKeyInfo{filterSubKeyPrefix: []string{api.FederalGroupPrefix}},
share.CFGEndpointFileMonitor: fedKeyInfo{filterSubKeyPrefix: []string{api.FederalGroupPrefix}},
share.CFGEndpointFileAccessRule: fedKeyInfo{filterSubKeyPrefix: []string{api.FederalGroupPrefix}},
share.CFGEndpointResponseRule: fedKeyInfo{fedMasterOnlyKeys: []string{share.CLUSConfigFedResponseRuleKey}},
share.CFGEndpointAdmissionControl: fedKeyInfo{fedMasterOnlyKeys: []string{share.CLUSConfigFedAdmCtrlKey}},
share.CFGEndpointFederation: fedKeyInfo{
alwaysFilterKeys: []string{share.CLUSFedClustersStatusKey, share.CLUSFedToPingPollKey},
fedMasterOnlyKeys: []string{share.CLUSFedSystemKey},
filterSubKeyPrefix: []string{share.CLUSFedRulesRevisionSubKey},
},
}
// for import/purge filtering
var _skipKeyInfo = map[string][]string{
share.CFGEndpointAdmissionControl: []string{share.CLUSAdmissionCertKey(share.CLUSConfigAdmissionControlStore, share.DefaultPolicyName)},
share.CFGEndpointCrd: []string{share.CLUSAdmissionCertKey(share.CLUSConfigCrdStore, share.DefaultPolicyName)},
}
var groupCfgEndpoint *cfgEndpoint = &cfgEndpoint{name: share.CFGEndpointGroup, key: share.CLUSConfigGroupStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey, purgeFilter: purgeGroupFilter} // group cfgEndpoint
var pprofileCfgEndpoint *cfgEndpoint = &cfgEndpoint{name: share.CFGEndpointProcessProfile, key: share.CLUSConfigProcessProfileStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey, purgeFilter: purgeGroupFilter} // process profile cfgEndpoint
var fmonitorCfgEndpoint *cfgEndpoint = &cfgEndpoint{name: share.CFGEndpointFileMonitor, key: share.CLUSConfigFileMonitorStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey, purgeFilter: purgeGroupFilter} // file monitor cfgEndpoint
var faccessCfgEndpoint *cfgEndpoint = &cfgEndpoint{name: share.CFGEndpointFileAccessRule, key: share.CLUSConfigFileAccessRuleStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey, purgeFilter: purgeGroupFilter} // file access cfgEndpoint
var registryCfgEndpoint *cfgEndpoint = &cfgEndpoint{name: share.CFGEndpointRegistry, key: share.CLUSConfigRegistryStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey}
// Order is important
var cfgEndpoints []*cfgEndpoint = []*cfgEndpoint{
&cfgEndpoint{name: share.CFGEndpointFederation, key: share.CLUSConfigFederationStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockFedKey, purgeFilter: purgeFedFilter},
&cfgEndpoint{name: share.CFGEndpointUserRole, key: share.CLUSConfigUserRoleStore, isStore: true,
section: api.ConfSectionUser, lock: share.CLUSLockUserKey},
&cfgEndpoint{name: share.CFGEndpointPwdProfile, key: share.CLUSConfigPwdProfileStore, isStore: true,
section: api.ConfSectionUser, lock: share.CLUSLockUserKey},
&cfgEndpoint{name: share.CFGEndpointUser, key: share.CLUSConfigUserStore, isStore: true,
section: api.ConfSectionUser, lock: share.CLUSLockUserKey},
&cfgEndpoint{name: share.CFGEndpointLicense, key: share.CLUSConfigLicenseKey, isStore: false,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
// &cfgEndpoint{name: CFGEndpointEULA, key: share.CLUSConfigEULAKey, isStore: false,
// section: api.ConfEndpointConfig, lock: share.CLUSLockConfigKey},
// Not to export uniconf, if the exported config is going to be used on other systems,
// uniconf settings are not portable; if the export config is used on the system itself,
// the current state is kept, no refresh of uniconf keys.
// &cfgEndpoint{name: "uniconf", key: share.CLUSUniconfStore, isStore: true,
// section: api.ConfEndpointIDConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointServer, key: share.CLUSConfigServerStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointSystem, key: share.CLUSConfigSystemKey, isStore: false,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointScan, key: share.CLUSConfigScanKey, isStore: false,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
registryCfgEndpoint,
&cfgEndpoint{name: share.CFGEndpointAdmissionControl, key: share.CLUSConfigAdmissionControlStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockAdmCtrlKey},
groupCfgEndpoint,
&cfgEndpoint{name: share.CFGEndpointPolicy, key: share.CLUSConfigPolicyStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey},
pprofileCfgEndpoint,
fmonitorCfgEndpoint,
faccessCfgEndpoint,
&cfgEndpoint{name: share.CFGEndpointResponseRule, key: share.CLUSConfigResponseRuleStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey},
&cfgEndpoint{name: share.CFGEndpointCrd, key: share.CLUSConfigCrdStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockPolicyKey},
&cfgEndpoint{name: share.CFGEndpointDlpRule, key: share.CLUSConfigDlpRuleStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey},
&cfgEndpoint{name: share.CFGEndpointDlpGroup, key: share.CLUSConfigDlpGroupStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey},
&cfgEndpoint{name: share.CFGEndpointWafRule, key: share.CLUSConfigWafRuleStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey},
&cfgEndpoint{name: share.CFGEndpointWafGroup, key: share.CLUSConfigWafGroupStore, isStore: true,
section: api.ConfSectionPolicy, lock: share.CLUSLockPolicyKey},
&cfgEndpoint{name: share.CFGEndpointScript, key: share.CLUSConfigScriptStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointCompliance, key: share.CLUSConfigComplianceStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointVulnerability, key: share.CLUSConfigVulnerabilityStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointDomain, key: share.CLUSConfigDomainStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
&cfgEndpoint{name: share.CFGEndpointCloud, key: share.CLUSConfigCloudStore, isStore: true,
section: api.ConfSectionConfig, lock: share.CLUSLockConfigKey},
}
// Endpoint name to endping
var cfgEndpointMap map[string]*cfgEndpoint = make(map[string]*cfgEndpoint)
var restoredFedRole string
func purgeFedFilter(epName, key string) bool {
return false // no purge
}
func purgeGroupFilter(epName, key string) bool {
accAdmin := access.NewFedAdminAccessControl()
name := share.CLUSGroupKey2Name(key)
group, _, _ := clusHelper.GetGroup(name, accAdmin)
// Keep the learned, ground group & reserved local/fed groups
return group == nil || ((group.CfgType == share.FederalCfg || group.CfgType == share.UserCreated) && !group.Reserved)
}
func skipCertFilter(epName, key string) bool { // return true means to skip the key
if skipKeys, ok := _skipKeyInfo[epName]; ok {
for _, skipKeyPath := range skipKeys {
if key == skipKeyPath { // this key should be skipped
return true
}
}
}
return false
}
func readKeyValue(reader *bufio.Reader) (string, string, error) {
for {
key, err := reader.ReadString('\n')
if err == io.EOF || err != nil {
return "", "", err
} else if key == "\n" {
continue
}
value, err := reader.ReadString('\n')
if err == io.EOF || err != nil {
return "", "", err
}
if !strings.HasPrefix(key, "#") && !strings.HasPrefix(value, "#") {
// Remove trailing \n !!
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
return key, value, nil
}
}
return "", "", io.EOF
}
func isFedObject(filterFedObjectType int, key string, value []byte, restore bool) (bool, []byte) {
switch filterFedObjectType {
case _filterFedPolicyObjects:
policyRulePrefix := share.CLUSConfigPolicyStore + "default/rule/"
if strings.HasPrefix(key, policyRulePrefix) {
idRaw, _ := strconv.Atoi(key[len(policyRulePrefix):])
id := uint32(idRaw)
if id > api.PolicyFedRuleIDBase && id < api.PolicyFedRuleIDMax {
return true, nil
}
} else {
if key == share.CLUSPolicyZipRuleListKey(share.DefaultPolicyName) {
var rhs []*share.CLUSRuleHead
var uzb []byte
var tmpvalue []byte = value
//for backup case unzip rulelist before save to file
if !restore {
//do unzip
uzb = utils.GunzipBytes(value)
if uzb == nil {
log.Error("Failed to unzip data")
return true, nil
}
tmpvalue = uzb
}
if err := json.Unmarshal(tmpvalue, &rhs); err == nil {
// because fed policies are always in top, we can simply iterate thru rhs
firstNonFedIdx := len(rhs)
for idx, rh := range rhs {
if rh != nil && rh.CfgType != share.FederalCfg {
firstNonFedIdx = idx
break
}
}
if firstNonFedIdx > 0 {
newValue, _ := json.Marshal(rhs[firstNonFedIdx:])
return false, newValue
} else {
//in case there are no fed policies
if !restore {
return false, uzb
}
}
} else {
if !restore {
return false, uzb
}
}
}
}
default:
//for FedRoleMaster unzip policy rulelist before save to storage
if !restore && key == share.CLUSPolicyZipRuleListKey(share.DefaultPolicyName) {
var uzb []byte
//do unzip
uzb = utils.GunzipBytes(value)
if uzb == nil {
log.Error("Failed to unzip policy rulelist")
return true, nil
}
return false, uzb
}
}
if key == share.CLUSPolicyRuleListKey(share.DefaultPolicyName) {
//since 3.2.1 rulelist key is changed to CLUSPolicyZipRuleListKey
//so skip backup/export using CLUSPolicyRuleListKey
//for restore we still need to restore older version backup file that only
//has CLUSPolicyRuleListKey
return !restore, nil
}
return false, nil
}
func (ep cfgEndpoint) getBackupFilename() string {
return fmt.Sprintf("%s%s.backup", configBackupDir, ep.name)
}
func (ep cfgEndpoint) getTempFilePrefix() string {
return fmt.Sprintf("nvcfg.%s", ep.name)
}
func (ep cfgEndpoint) backup(fedRole string) error {
log.WithFields(log.Fields{"endpoint": ep.name}).Debug()
if _, err := os.Stat(configBackupDir); os.IsNotExist(err) {
if err = os.MkdirAll(configBackupDir, 0755); err != nil {
log.WithFields(log.Fields{"error": err, "dir": configBackupDir}).Error("Failed to make directory")
return err
}
}
prefix := ep.getTempFilePrefix()
// Cannot be in /tmp, because /tmp is of different partition of /var/neuvector, os.rename() will fail
err := filepath.Walk(configBackupDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.WithFields(log.Fields{"path": path, "error": err.Error()}).Error()
return err
}
if info.IsDir() {
if path == configBackupDir {
return nil
} else {
return filepath.SkipDir
}
}
if strings.HasPrefix(info.Name(), prefix) {
log.WithFields(log.Fields{"path": path}).Error("Remove leftover temp file")
os.Remove(path)
}
return nil
})
if err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Error("Failed to walk directory")
}
tmpfile, err := ioutil.TempFile(configBackupDir, prefix)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to create temp. file")
return err
}
defer os.Remove(tmpfile.Name())
// Write key/value to file
wfp := bufio.NewWriter(tmpfile)
if err = ep.write(wfp, fedRole); err != nil {
log.WithFields(log.Fields{"error": err, "file": tmpfile.Name()}).Error("Failed to write temp. file")
tmpfile.Close()
return err
}
wfp.Flush()
if err = tmpfile.Close(); err != nil {
log.WithFields(log.Fields{"error": err, "file": tmpfile.Name()}).Error("Failed to close temp. file")
return err
}
stat, err := os.Stat(tmpfile.Name())
if err != nil {
log.WithFields(log.Fields{"error": err, "file": tmpfile.Name()}).Error("Failed to stat temp. file")
return err
}
if stat.Size() == 0 && ep.name == share.CFGEndpointUser {
log.WithFields(log.Fields{"endpoint": ep.name}).Error("Ingore empty backup file")
return fmt.Errorf("Empty backup file")
}
target := ep.getBackupFilename()
if err = os.Rename(tmpfile.Name(), target); err != nil {
log.WithFields(log.Fields{
"error": err, "file": tmpfile.Name(), "target": target,
}).Error("Failed to move temp. file")
return err
}
return nil
}
func (ep cfgEndpoint) restore(importInfo *fedRulesRevInfo, txn *cluster.ClusterTransact) error {
fedEndpointCfg := false
source := ep.getBackupFilename()
if ep.name == share.CFGEndpointFederation {
fedEndpointCfg = true
}
if _, err := os.Stat(source); os.IsNotExist(err) {
// Config files might not be persistent or directory might not be mapped.
log.WithFields(log.Fields{"error": err, "file": source}).Info("File not exist")
return err
}
f, err := os.Open(source)
if err != nil {
log.WithFields(log.Fields{"error": err, "file": source}).Error("Unable to open file to read")
return err
}
defer f.Close()
var filterFedObjectType int
var fedMasterOnlyKeys, filterSubKeyPrefix, alwaysFilterKeys []string
if keyInfo, ok := _fedKeyInfo[ep.name]; ok {
if restoredFedRole != api.FedRoleMaster { // need to filter fed rule keys on non-master cluster
filterFedObjectType = keyInfo.filterFedObjectType
fedMasterOnlyKeys = keyInfo.fedMasterOnlyKeys
filterSubKeyPrefix = keyInfo.filterSubKeyPrefix
}
alwaysFilterKeys = keyInfo.alwaysFilterKeys
}
// Restore key/value from files
count := 0
r := bufio.NewReader(f)
for {
key, value, err := readKeyValue(r)
if err == io.EOF {
return errDone
} else if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to read line")
return ErrInvalidFileFormat
}
if skipCertFilter(ep.name, key) {
continue
}
if fedEndpointCfg {
// special handling for federation backup file because fedRole is unknown when Restore() is called
if key == share.CLUSFedMembershipKey {
var m share.CLUSFedMembership
if err := json.Unmarshal([]byte(value), &m); err == nil {
restoredFedRole = m.FedRole
if keyInfo, ok := _fedKeyInfo[ep.name]; ok {
if restoredFedRole != api.FedRoleMaster { // need to filter fed rule keys on non-master cluster
filterFedObjectType = keyInfo.filterFedObjectType
fedMasterOnlyKeys = keyInfo.fedMasterOnlyKeys
filterSubKeyPrefix = keyInfo.filterSubKeyPrefix
} else {
filterFedObjectType = 0
fedMasterOnlyKeys = nil
filterSubKeyPrefix = nil
}
alwaysFilterKeys = keyInfo.alwaysFilterKeys
}
}
} else if key == share.CLUSFedRulesRevisionKey {
if restoredFedRole == api.FedRoleMaster {
importInfo.fedRulesRevValue = value
// do not write to kv now. postpone it at the last write
continue
}
}
}
if ep.name == share.CFGEndpointUser {
var u share.CLUSUser
if err := json.Unmarshal([]byte(value), &u); err == nil {
u.FailedLoginCount = 0
u.BlockLoginSince = time.Time{}
u.PwdResetTime = time.Now().UTC()
data, _ := json.Marshal(&u)
value = string(data)
}
}
skip := false
for _, prefix := range fedMasterOnlyKeys {
if strings.HasPrefix(key, prefix) {
skip = true
break
}
}
if !skip {
for _, filterKey := range alwaysFilterKeys {
if strings.HasPrefix(key, filterKey) {
skip = true
break
}
}
}
if !skip {
yes, newValue := isFedObject(filterFedObjectType, key, []byte(value), true)
if yes {
skip = true // filter fed policy objects on non-master cluster
} else if newValue != nil {
value = string(newValue)
}
}
if !skip {
ss := strings.Split(key, "/")
if len(ss) > 0 {
kLast := ss[len(ss)-1]
for _, prefix := range filterSubKeyPrefix {
if strings.HasPrefix(kLast, prefix) {
skip = true
break
}
}
}
}
// Value can be empty if a key was never been written when it's exported
if !skip && len(value) != 0 {
array, err := upgrade(key, []byte(value))
if err != nil {
log.WithFields(log.Fields{"error": err, "key": key, "value": value}).Error("Failed to upgrade key/value")
return ErrInvalidFileFormat
}
if key == share.CLUSPolicyZipRuleListKey(share.DefaultPolicyName) {
applyTransaction(txn, nil, false, 0)
//zip rulelist before put to cluster during restore
clusHelper.PutPolicyRuleListZip(key, array)
} else {
clusHelper.DuplicateNetworkKeyTxn(txn, key, array)
txn.Put(key, array)
if txn.Size() >= 64 {
applyTransaction(txn, nil, false, 0)
}
}
count++
}
}
log.WithFields(log.Fields{"endpoint": ep.name, "file": source, "keys": count}).Info()
return nil
}
func (ep cfgEndpoint) write(writer *bufio.Writer, fedRole string) error {
var filterFedObjectType int
var fedMasterOnlyKeys, filterSubKeyPrefix, alwaysFilterKeys []string
if keyInfo, ok := _fedKeyInfo[ep.name]; ok {
if fedRole != api.FedRoleMaster { // need to filter fed rule keys on non-master cluster
filterFedObjectType = keyInfo.filterFedObjectType
fedMasterOnlyKeys = keyInfo.fedMasterOnlyKeys
filterSubKeyPrefix = keyInfo.filterSubKeyPrefix
}
alwaysFilterKeys = keyInfo.alwaysFilterKeys
}
if ep.isStore {
if kvPairs, err := cluster.List(ep.key); err == nil || err == cluster.ErrEmptyStore {
for _, kvPair := range kvPairs {
key := kvPair.Key
skip := false
if skipCertFilter(ep.name, key) {
continue
}
for _, prefix := range fedMasterOnlyKeys {
if strings.HasPrefix(key, prefix) {
skip = true
break
}
}
if !skip {
for _, filterKey := range alwaysFilterKeys {
if strings.HasPrefix(key, filterKey) {
skip = true
break
}
}
}
if !skip {
ss := strings.Split(key, "/")
if len(ss) > 0 {
kLast := ss[len(ss)-1]
for _, prefix := range filterSubKeyPrefix {
if strings.HasPrefix(kLast, prefix) {
skip = true
break
}
}
}
}
if !skip {
value := kvPair.Value
yes, newValue := isFedObject(filterFedObjectType, key, value, false)
if yes {
continue // filter fed policy objects on non-master cluster
} else if newValue != nil {
value = newValue
}
line := fmt.Sprintf("%s\n%s\n", key, value)
if _, err = writer.WriteString(line); err != nil {
return err
}
}
}
} else {
return err
}
} else {
if value, err := cluster.Get(ep.key); err == nil || err == cluster.ErrKeyNotFound {
line := fmt.Sprintf("%s\n%s\n", ep.key, value)
if _, err = writer.WriteString(line); err != nil {
return err
}
} else {
return err
}
}
return nil
}
func (ep cfgEndpoint) purge(txn *cluster.ClusterTransact, importTask *share.CLUSImportTask) error {
if ep.isStore {
keys, _ := cluster.GetStoreKeys(ep.key)
if len(keys) > 0 {
for _, key := range keys {
if ep.purgeFilter == nil || ep.purgeFilter(ep.name, key) {
txn.Delete(key)
}
}
}
} else {
if ep.purgeFilter == nil || ep.purgeFilter(ep.name, ep.key) {
txn.Delete(ep.key)
}
}
if txn.Size() >= 64 {
applyTransaction(txn, importTask, true, 0)
}
return nil
}