-
Notifications
You must be signed in to change notification settings - Fork 1
/
internal.go
386 lines (319 loc) · 11.1 KB
/
internal.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
package internal
import (
"encoding/json"
"errors"
"fmt"
"log"
"log/syslog"
"reflect"
"regexp"
"strings"
"time"
"github.com/silinternational/personnel-sync/v6/alert"
)
const (
DefaultConfigFile = "./config.json"
DefaultVerbosity = 5
DestinationTypeGoogleContacts = "GoogleContacts"
DestinationTypeGoogleGroups = "GoogleGroups"
DestinationTypeGoogleSheets = "GoogleSheets"
DestinationTypeGoogleUsers = "GoogleUsers"
DestinationTypeRestAPI = "RestAPI"
DestinationTypeWebHelpDesk = "WebHelpDesk"
SourceTypeGoogleSheets = "GoogleSheets"
SourceTypeRestAPI = "RestAPI"
)
// RemapToDestinationAttributes returns a slice of Person instances that each have
// only the desired attributes based on the destination attribute keys.
// If a required attribute is missing for a Person, then their disableChanges
// value is set to true.
func RemapToDestinationAttributes(logger *log.Logger, sourcePersons []Person, attributeMap []AttributeMap) ([]Person, error) {
var peopleForDestination []Person
for _, person := range sourcePersons {
attrs := map[string]string{}
// Build attrs with only attributes from destination map, disable changes on person missing a required attribute
disableChanges := false
for _, attrMap := range attributeMap {
if value, ok := person.Attributes[attrMap.Source]; ok {
attrs[attrMap.Destination] = value
} else if attrMap.Required {
jsonAttrs, _ := json.Marshal(attrs)
logger.Printf("user missing attribute %s. Rest of data: %s", attrMap.Source, jsonAttrs)
disableChanges = true
}
}
peopleForDestination = append(peopleForDestination, Person{
CompareValue: person.CompareValue,
Attributes: attrs,
DisableChanges: disableChanges,
})
}
return peopleForDestination, nil
}
// getPersonFromList returns the person if found in peopleList otherwise an empty Person{}
func getPersonFromList(compareValue string, peopleList []Person) Person {
lowerCompareValue := strings.ToLower(compareValue)
for _, person := range peopleList {
if strings.ToLower(person.CompareValue) == lowerCompareValue {
return person
}
}
return Person{}
}
func personAttributesAreEqual(logger *log.Logger, sp, dp Person, config Config) bool {
caseSensitivityList := getCaseSensitivitySourceAttributeList(config.AttributeMap)
equal := true
for key, val := range sp.Attributes {
if !stringsAreEqual(val, dp.Attributes[key], caseSensitivityList[key]) {
if config.Runtime.Verbosity >= VerbosityMedium {
logger.Printf(`User: "%s", "%s" not equal, CaseSensitive: "%t", Source: "%s", Dest: "%s"`+"\n",
sp.CompareValue, key, caseSensitivityList[key], val, dp.Attributes[key])
equal = false
} else {
logger.Printf(`User: "%s" not equal`+"\n", key)
return false
}
}
}
return equal
}
func stringsAreEqual(val1, val2 string, caseSensitive bool) bool {
if caseSensitive {
return val1 == val2
}
return strings.ToLower(val1) == strings.ToLower(val2)
}
func getCaseSensitivitySourceAttributeList(attributeMap []AttributeMap) map[string]bool {
results := map[string]bool{}
for _, attrMap := range attributeMap {
results[attrMap.Destination] = attrMap.CaseSensitive
}
return results
}
// GenerateChangeSet builds the three slice attributes of a ChangeSet (Create, Update and Delete) based on whether they
// are in the slice of destination Person instances.
//
// It skips all source Person instances that have DisableChanges set to true
func GenerateChangeSet(logger *log.Logger, sourcePeople, destinationPeople []Person, config Config) ChangeSet {
var changeSet ChangeSet
// Find users who need to be created or updated
for _, sp := range sourcePeople {
// If user was missing a required attribute, don't change their record
if sp.DisableChanges {
continue
}
sp := processExpressions(logger, config, sp)
destinationPerson := getPersonFromList(sp.CompareValue, destinationPeople)
if destinationPerson.CompareValue == "" {
changeSet.Create = append(changeSet.Create, sp)
continue
}
if !personAttributesAreEqual(logger, sp, destinationPerson, config) {
sp.ID = destinationPerson.Attributes["id"]
changeSet.Update = append(changeSet.Update, sp)
continue
}
}
// Find users who need to be deleted
for _, dp := range destinationPeople {
sourcePerson := getPersonFromList(dp.CompareValue, sourcePeople)
if sourcePerson.CompareValue == "" {
changeSet.Delete = append(changeSet.Delete, dp)
}
}
return changeSet
}
func processExpressions(logger *log.Logger, config Config, person Person) Person {
for _, attr := range config.AttributeMap {
if attr.Expression == "" {
continue
}
attrName := attr.Destination // the remap to destination attributes has happened already
attrValue, _ := person.Attributes[attrName]
re, err := regexp.Compile(attr.Expression)
if err != nil {
msg := fmt.Sprintf("invalid regular expression (%q) on attribute %s",
attr.Expression, attrName)
logger.Println(msg)
alert.SendEmail(config.Alert, msg)
continue
}
n := re.ReplaceAllString(attrValue, attr.Replace)
person.Attributes[attrName] = n
}
return person
}
// RunSyncSet calls a number of functions to do the following ...
// - it gets the list of people from the source
// - it remaps their attributes to match the keys used in the destination
// - it gets the list of people from the destination
// - it generates the lists of people to change, update and delete
// - if dryRun is true, it prints those lists, otherwise it makes the associated changes
func RunSyncSet(logger *log.Logger, source Source, destination Destination, config Config) error {
sourcePeople, err := source.ListUsers(GetSourceAttributes(config.AttributeMap))
if err != nil {
return err
}
if len(sourcePeople) == 0 {
return errors.New("no people found in source")
}
logger.Printf(" Found %v people in source", len(sourcePeople))
// remap source people to destination attributes for comparison
sourcePeople, err = RemapToDestinationAttributes(logger, sourcePeople, config.AttributeMap)
if err != nil {
return err
}
destinationPeople, err := destination.ListUsers(GetDestinationAttributes(config.AttributeMap))
if err != nil {
return err
}
logger.Printf(" Found %v people in destination", len(destinationPeople))
changeSet := GenerateChangeSet(logger, sourcePeople, destinationPeople, config)
logger.Printf("ChangeSet Plans: Create %d, Update %d, Delete %d\n",
len(changeSet.Create), len(changeSet.Update), len(changeSet.Delete))
// If in DryRun mode only print out ChangeSet plans and return mocked change results based on plans
if config.Runtime.DryRunMode {
logger.Println("Dry run mode enabled. Change set details follow:")
printChangeSet(logger, changeSet)
return nil
}
// Create a channel to pass activity logs for printing
eventLog := make(chan EventLogItem, 50)
go processEventLog(logger, config.Alert, eventLog)
results := destination.ApplyChangeSet(changeSet, eventLog)
logger.Printf("Sync results: %v users added, %v users updated, %v users removed\n",
results.Created, results.Updated, results.Deleted)
for i := 0; i < 100; i++ {
time.Sleep(time.Millisecond * 10)
if len(eventLog) == 0 {
break
}
}
close(eventLog)
return nil
}
func GetSourceAttributes(attrMap []AttributeMap) []string {
var keys []string
for _, attr := range attrMap {
if attr.Source != "" {
keys = append(keys, attr.Source)
}
}
return keys
}
func GetDestinationAttributes(attrMap []AttributeMap) []string {
var keys []string
for _, attr := range attrMap {
if attr.Destination != "" {
keys = append(keys, attr.Destination)
}
}
return keys
}
func processEventLog(logger *log.Logger, config alert.Config, eventLog <-chan EventLogItem) {
for msg := range eventLog {
logger.Println(msg)
if msg.Level == syslog.LOG_ALERT || msg.Level == syslog.LOG_EMERG {
alert.SendEmail(config, msg.String())
}
}
}
func printChangeSet(logger *log.Logger, changeSet ChangeSet) {
logger.Printf("Users to be created: %d ...", len(changeSet.Create))
for i, user := range changeSet.Create {
logger.Printf(" create %v) %s", i+1, user.CompareValue)
}
logger.Printf("Users to be updated: %d ...", len(changeSet.Update))
for i, user := range changeSet.Update {
logger.Printf(" update %v) %s", i+1, user.CompareValue)
}
logger.Printf("Users to be deleted: %d ...", len(changeSet.Delete))
for i, user := range changeSet.Delete {
logger.Printf(" delete %v) %s", i+1, user.CompareValue)
}
}
// This function will search element inside array with any type.
// Will return boolean and index for matched element.
// True and index more than 0 if element is exist.
// needle is element to search, haystack is slice of value to be search.
func InArray(needle, haystack any) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(haystack).Kind() {
case reflect.Slice:
s := reflect.ValueOf(haystack)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(needle, s.Index(i).Interface()) {
index = i
exists = true
return
}
}
}
return
}
type EmptyDestination struct{}
func (e *EmptyDestination) ForSet(syncSetJson json.RawMessage) error {
return nil
}
func (e *EmptyDestination) ListUsers(desiredAttrs []string) ([]Person, error) {
return []Person{}, nil
}
func (e *EmptyDestination) ApplyChangeSet(changes ChangeSet, eventLog chan<- EventLogItem) ChangeResults {
return ChangeResults{}
}
type EmptySource struct{}
func (e *EmptySource) ForSet(syncSetJson json.RawMessage) error {
return nil
}
func (e *EmptySource) ListUsers(desiredAttrs []string) ([]Person, error) {
return []Person{}, nil
}
// NewBatchTimer returns a new BatchTimer with the startTime set to the current time and the endTime set to
// secondsPerBatch from now
func NewBatchTimer(batchSize, secondsPerBatch int) BatchTimer {
b := BatchTimer{}
b.Init(batchSize, secondsPerBatch)
return b
}
// BatchTimer is intended as a time limited batch enforcer. To create one, call its Init method.
// Then, to use it call its WaitOnBatch method after every call to the associated go routine
type BatchTimer struct {
startTime time.Time
endTime time.Time
Counter int
SecondsPerBatch int
BatchSize int
}
// Init sets the startTime to the current time, sets the endTime based on secondsPerBatch into the future
func (b *BatchTimer) Init(batchSize, secondsPerBatch int) {
b.startTime = time.Now()
b.setEndTime()
b.SecondsPerBatch = secondsPerBatch
b.BatchSize = batchSize
b.Counter = 0
}
func (b *BatchTimer) setEndTime() {
var emptyTime time.Time
if b.startTime == emptyTime {
b.startTime = time.Now()
}
b.endTime = b.startTime.Add(time.Second * time.Duration(b.SecondsPerBatch))
}
// WaitOnBatch increments the Counter and then if fewer than BatchSize have been dealt with, just returns without doing
// anything Otherwise, sleeps until the batch time has expired (i.e. current time is past endTime). If this last process
// occurs, then it ends by resetting the batch's times and counter.
func (b *BatchTimer) WaitOnBatch() {
b.Counter++
if b.Counter < b.BatchSize {
return
}
for {
currTime := time.Now()
if currTime.After(b.endTime) {
break
}
time.Sleep(time.Second)
}
b.Init(b.BatchSize, b.SecondsPerBatch)
}