-
Notifications
You must be signed in to change notification settings - Fork 45
/
signatures.go
616 lines (558 loc) · 25.7 KB
/
signatures.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package dynamo_events
import (
"context"
"errors"
"fmt"
"strings"
"github.com/communitybridge/easycla/cla-backend-go/utils"
acs_service "github.com/communitybridge/easycla/cla-backend-go/v2/acs-service"
organization_service "github.com/communitybridge/easycla/cla-backend-go/v2/organization-service"
user_service "github.com/communitybridge/easycla/cla-backend-go/v2/user-service"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"github.com/aws/aws-lambda-go/events"
log "github.com/communitybridge/easycla/cla-backend-go/logging"
)
// constants
const (
CLASignatureType = "cla"
CCLASignatureType = "ccla"
ICLASignatureType = "icla"
ECLASignatureType = "ecla"
AddCLAManager = "add"
DeleteCLAManager = "delete"
)
// ErrNoExternalID when company does not have externalID
var ErrNoExternalID = errors.New("company External ID does not exist")
// Signature database model
type Signature struct {
SignatureID string `json:"signature_id"`
DateCreated string `json:"date_created"`
DateModified string `json:"date_modified"`
SignatureApproved bool `json:"signature_approved"`
SignatureSigned bool `json:"signature_signed"`
SignatureDocumentMajorVersion string `json:"signature_document_major_version"`
SignatureDocumentMinorVersion string `json:"signature_document_minor_version"`
SignatureReferenceID string `json:"signature_reference_id"`
SignatureReferenceName string `json:"signature_reference_name"`
SignatureReferenceNameLower string `json:"signature_reference_name_lower"`
SignatureProjectID string `json:"signature_project_id"`
SignatureReferenceType string `json:"signature_reference_type"`
SignatureType string `json:"signature_type"`
SignatureUserCompanyID string `json:"signature_user_ccla_company_id"`
EmailWhitelist []string `json:"email_whitelist"`
DomainWhitelist []string `json:"domain_whitelist"`
GitHubWhitelist []string `json:"github_whitelist"`
GitHubOrgWhitelist []string `json:"github_org_whitelist"`
SignatureACL []string `json:"signature_acl"`
SigtypeSignedApprovedID string `json:"sigtype_signed_approved_id"`
UserGithubUsername string `json:"user_github_username"`
UserLFUsername string `json:"user_lf_username"`
UserName string `json:"user_name"`
UserEmail string `json:"user_email"`
SignedOn string `json:"signed_on"`
}
// Assign Contributor role upon CCLA or CCLA/ICLA signing
func (s *service) SignatureAssignContributorEvent(event events.DynamoDBEventRecord) error {
ctx := utils.NewContext()
f := logrus.Fields{
"functionName": "SignatureAssignContributorEvent",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
}
log.WithFields(f).Debug("processing signature event to assign contributor...")
// Decode the pre-update and post-update signature record details
var newSignature, oldSignature Signature
err := unmarshalStreamImage(event.Change.OldImage, &oldSignature)
if err != nil {
log.WithFields(f).Warnf("problem decoding pre-update signature, error: %+v", err)
return err
}
err = unmarshalStreamImage(event.Change.NewImage, &newSignature)
if err != nil {
log.WithFields(f).Warnf("problem decoding post-update signature, error: %+v", err)
return err
}
// Add some details for our logger
f["id"] = newSignature.SignatureID
f["type"] = newSignature.SignatureType
f["referenceID"] = newSignature.SignatureReferenceID
f["referenceName"] = newSignature.SignatureReferenceName
f["referenceType"] = newSignature.SignatureReferenceType
f["projectID"] = newSignature.SignatureProjectID
f["approved"] = newSignature.SignatureApproved
f["signed"] = newSignature.SignatureSigned
if !oldSignature.SignatureSigned && newSignature.SignatureSigned {
log.WithFields(f).Debug("signature is now signed - assigning contributor...")
err := s.assignContributor(ctx, newSignature, f)
if err != nil {
return err
}
}
return nil
}
// should be called when we modify signature
func (s *service) SignatureSignedEvent(event events.DynamoDBEventRecord) error {
ctx := utils.NewContext()
f := logrus.Fields{
"functionName": "SignatureSignedEvent",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
}
log.WithFields(f).Debug("Processing signature signed event to modify the ACL...")
// Decode the pre-update and post-update signature record details
var newSignature, oldSignature Signature
err := unmarshalStreamImage(event.Change.OldImage, &oldSignature)
if err != nil {
log.WithFields(f).Warnf("problem decoding pre-update signature, error: %+v", err)
return err
}
err = unmarshalStreamImage(event.Change.NewImage, &newSignature)
if err != nil {
log.WithFields(f).Warnf("problem decoding post-update signature, error: %+v", err)
return err
}
// Add some details for our logger
f["id"] = newSignature.SignatureID
f["type"] = newSignature.SignatureType
f["referenceID"] = newSignature.SignatureReferenceID
f["referenceName"] = newSignature.SignatureReferenceName
f["referenceType"] = newSignature.SignatureReferenceType
f["projectID"] = newSignature.SignatureProjectID
f["approved"] = newSignature.SignatureApproved
f["signed"] = newSignature.SignatureSigned
// check if signature signed event is received
if !oldSignature.SignatureSigned && newSignature.SignatureSigned {
log.WithFields(f).Debugf("processing signature signed event for signature type: %s...", newSignature.SignatureType)
// Update the signed on date
log.WithFields(f).Debug("updating signed on date...")
err = s.signatureRepo.AddSignedOn(ctx, newSignature.SignatureID)
if err != nil {
log.WithFields(f).Warnf("failed to add signed_on date/time to signature, error: %+v", err)
}
// If oldSigACL CCLA signature...
if newSignature.SignatureType == CCLASignatureType {
log.WithFields(f).Debugf("processing signature type: %s with %d CLA Managers...",
newSignature.SignatureType, len(newSignature.SignatureACL))
if len(newSignature.SignatureACL) == 0 {
log.WithFields(f).Warn("initial cla manager details not found")
return errors.New("initial cla manager details not found")
}
log.WithFields(f).Debugf("loading company from signature by companyID: %s...", newSignature.SignatureReferenceID)
companyModel, err := s.companyRepo.GetCompany(ctx, newSignature.SignatureReferenceID)
if err != nil {
log.WithFields(f).Warnf("failed to lookup company from signature by companyID: %s, error: %+v",
newSignature.SignatureReferenceID, err)
return err
}
if companyModel == nil {
msg := fmt.Sprintf("failed to lookup company from signature by companyID: %s, not found",
newSignature.SignatureReferenceID)
log.WithFields(f).Warn(msg)
return errors.New(msg)
}
log.WithFields(f).Debugf("loaded company '%s' from signature by companyID: %s with companySFID: %s...",
companyModel.CompanyName, companyModel.CompanyID, companyModel.CompanyExternalID)
// We should have the company SFID...
if companyModel.CompanyExternalID == "" {
msg := fmt.Sprintf("company %s (%s) does not have oldSigACL SF Organization ID - unable to update permissions",
companyModel.CompanyName, companyModel.CompanyID)
log.WithFields(f).Warn(msg)
return errors.New(msg)
}
// Load the list of SF projects associated with this CLA Group
log.WithFields(f).Debugf("querying SF projects for CLA Group: %s", newSignature.SignatureProjectID)
projectCLAGroups, err := s.projectsClaGroupRepo.GetProjectsIdsForClaGroup(ctx, newSignature.SignatureProjectID)
log.WithFields(f).Debugf("found %d SF projects for CLA Group: %s", len(projectCLAGroups), newSignature.SignatureProjectID)
// Only proceed if we have one or more SF projects - otherwise, we can't assign and cleanup/adjust roles
if len(projectCLAGroups) == 0 {
log.WithFields(f).Warnf("unable to assign initial %s role or cleanup existing %s roles - no SF projects assigned to CLA group",
utils.CLAManagerRole, utils.CLADesigneeRole)
return nil
}
// We have oldSigACL separate routine to help assign the CLA Manager Role - it's oldSigACL bit wasteful as it
// loads the signature and other details again.
// Kick off oldSigACL go routine to set the cla manager role
// Set the CLA manager permissions
log.WithFields(f).Debug("assigning the initial CLA manager")
err = s.SetInitialCLAManagerACSPermissions(ctx, newSignature.SignatureID)
if err != nil {
log.WithFields(f).Warnf("failed to set initial cla manager, error: %+v", err)
return err
}
var eg errgroup.Group
// Remove designee role based on project type(foundation or Project level)
foundationSFID := projectCLAGroups[0].FoundationSFID
signedAtFoundation, signedErr := s.projectService.SignedAtFoundationLevel(ctx, foundationSFID)
if signedErr != nil {
log.WithFields(f).Debugf("failed to check signed level for ID: %s ", foundationSFID)
return signedErr
}
if signedAtFoundation {
log.WithFields(f).Debugf("removing existing %s role for project: '%s' (%s) and company: '%s' (%s)",
utils.CLADesigneeRole, projectCLAGroups[0].ProjectName, foundationSFID, companyModel.CompanyName, companyModel.CompanyExternalID)
err = s.removeCLAPermissionsByProjectOrganizationRole(ctx, foundationSFID, companyModel.CompanyExternalID, []string{utils.CLADesigneeRole})
if err != nil {
log.WithFields(f).Warnf("failed to remove %s roles for project: '%s' (%s) and company: '%s' (%s), error: %+v",
utils.CLADesigneeRole, projectCLAGroups[0].ProjectName, foundationSFID, companyModel.CompanyName, companyModel.CompanyExternalID, err)
return err
}
} else {
for _, projectCLAGroup := range projectCLAGroups {
pcg := projectCLAGroup // make a copy of the loop variable to use in the closure, avoids the loopclosure: loop variable projectCLAGroup captured by func literal lint error
eg.Go(func() error {
// Remove any roles that were previously assigned for cla-manager-designee
log.WithFields(f).Debugf("removing existing %s role for project: '%s' (%s) and company: '%s' (%s)",
utils.CLADesigneeRole, pcg.ProjectName, pcg.ProjectSFID, companyModel.CompanyName, companyModel.CompanyExternalID)
err = s.removeCLAPermissionsByProjectOrganizationRole(ctx, pcg.ProjectSFID, companyModel.CompanyExternalID, []string{utils.CLADesigneeRole})
if err != nil {
log.WithFields(f).Warnf("failed to remove %s roles for project: '%s' (%s) and company: '%s' (%s), error: %+v",
utils.CLADesigneeRole, pcg.ProjectName, pcg.ProjectSFID, companyModel.CompanyName, companyModel.CompanyExternalID, err)
return err
}
return nil
})
}
// Wait for the go routines to finish
log.WithFields(f).Debug("waiting for role assignment and cleanup...")
var lastRoleErr error
if roleErr := eg.Wait(); roleErr != nil {
log.WithFields(f).Warnf("encountered error while processing roles: %+v", roleErr)
lastRoleErr = roleErr
}
// Could be nil or the last error encountered
return lastRoleErr
}
}
}
return nil
}
// SignatureAddSigTypeSignedApprovedID function should be called when new icla, ecla signature added
func (s *service) SignatureAddSigTypeSignedApprovedID(event events.DynamoDBEventRecord) error {
ctx := utils.NewContext()
f := logrus.Fields{
"functionName": "SignatureAddSigTypeSignedApprovedID",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
}
log.WithFields(f).Debug("processing signature event - adding signature type signed approved id...")
var newSig Signature
var sigType string
var id string
err := unmarshalStreamImage(event.Change.NewImage, &newSig)
if err != nil {
return err
}
switch {
case newSig.SignatureType == CCLASignatureType:
sigType = CCLASignatureType
id = newSig.SignatureReferenceID
case newSig.SignatureType == CLASignatureType && newSig.SignatureUserCompanyID == "":
sigType = ICLASignatureType
id = newSig.SignatureReferenceID
case newSig.SignatureType == CLASignatureType && newSig.SignatureUserCompanyID != "":
sigType = ECLASignatureType
id = newSig.SignatureUserCompanyID
log.WithFields(f).Debugf("assigning contributor role for signature: %s ", newSig.SignatureID)
err = s.assignContributor(ctx, newSig, f)
if err != nil {
return err
}
default:
log.WithFields(f).Warnf("setting sigtype_signed_approved_id for signature: %s failed", newSig.SignatureID)
return errors.New("invalid signature in SignatureAddSigTypeSignedApprovedID")
}
val := fmt.Sprintf("%s#%v#%v#%s", sigType, newSig.SignatureSigned, newSig.SignatureApproved, id)
if newSig.SigtypeSignedApprovedID == val {
return nil
}
log.WithFields(f).Debugf("setting sigtype_signed_approved_id for signature: %s", newSig.SignatureID)
err = s.signatureRepo.AddSigTypeSignedApprovedID(ctx, newSig.SignatureID, val)
if err != nil {
return err
}
return nil
}
func (s *service) SignatureAddUsersDetails(event events.DynamoDBEventRecord) error {
ctx := utils.NewContext()
f := logrus.Fields{
"functionName": "SignatureAddUsersDetails",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
}
var newSig Signature
err := unmarshalStreamImage(event.Change.NewImage, &newSig)
if err != nil {
return err
}
if newSig.SignatureReferenceType == "user" && newSig.UserLFUsername == "" && newSig.UserGithubUsername == "" {
log.WithFields(f).Debugf("adding users details in signature: %s", newSig.SignatureID)
err = s.signatureRepo.AddUsersDetails(ctx, newSig.SignatureID, newSig.SignatureReferenceID)
if err != nil {
log.WithFields(f).Debugf("adding users details in signature: %s failed. error = %s", newSig.SignatureID, err.Error())
}
}
return nil
}
// signature function should be invoked when signature ACL is updated
func (s *service) UpdateCLAPermissions(event events.DynamoDBEventRecord) error {
f := logrus.Fields{
"functionName": "v2.dynamo_events.UpdateCLAPermissions",
}
// Decode the pre-update and post-update signature record details
var newSignature, oldSignature Signature
err := unmarshalStreamImage(event.Change.OldImage, &oldSignature)
if err != nil {
log.WithFields(f).Warnf("problem decoding pre-update signature, error: %+v", err)
return err
}
err = unmarshalStreamImage(event.Change.NewImage, &newSignature)
if err != nil {
log.WithFields(f).Warnf("problem decoding post-update signature, error: %+v", err)
return err
}
f["id"] = newSignature.SignatureID
f["type"] = newSignature.SignatureType
f["referenceID"] = newSignature.SignatureReferenceID
f["referenceName"] = newSignature.SignatureReferenceName
f["referenceType"] = newSignature.SignatureReferenceType
f["projectID"] = newSignature.SignatureProjectID
f["acl"] = strings.Join(newSignature.SignatureACL, ",")
log.WithFields(f).Debug("processing signature ACL to identify added/removed managers...")
managers := utils.SliceDifference(oldSignature.SignatureACL, newSignature.SignatureACL)
if len(managers) > 0 {
log.WithFields(f).Debugf("managers to be added/deleted : %+v", managers)
for _, entry := range managers {
log.WithFields(f).Debugf("processing difference: %s", entry)
if utils.StringInSlice(entry, newSignature.SignatureACL) {
// Assign CLA Manager role
log.WithFields(f).Debugf("Assigning user %s to the %s role", entry, utils.CLAManagerRole)
updateErr := s.updateCLAManagerPermissions(newSignature, managers, AddCLAManager)
if updateErr != nil {
log.WithFields(f).WithError(updateErr).Warn("problem assigning CLA Manager role")
return updateErr
}
} else {
// Remove CLA Manager role
log.WithFields(f).Debugf("Unassigning user %s from the %s role", entry, utils.CLAManagerRole)
updateErr := s.updateCLAManagerPermissions(newSignature, managers, DeleteCLAManager)
if updateErr != nil {
log.WithFields(f).WithError(updateErr).Warn("problem removing CLA Manager role")
return updateErr
}
}
}
} else {
log.WithFields(f).Debugf("No changes in ACL : %+v versus %+v", oldSignature.SignatureACL, newSignature.SignatureACL)
}
return nil
}
func (s *service) assignContributor(ctx context.Context, newSignature Signature, f logrus.Fields) error {
var companyID string
// Assign company ID based on signature type (CCLA, CCLA|ICLA)
if newSignature.SignatureType == utils.SignatureTypeCLA && newSignature.SignatureUserCompanyID != "" {
companyID = newSignature.SignatureUserCompanyID
} else if newSignature.SignatureType == utils.SignatureTypeCCLA && newSignature.SignatureReferenceID != "" {
companyID = newSignature.SignatureReferenceID
}
if (newSignature.SignatureType == utils.SignatureTypeCLA) || (newSignature.SignatureType == utils.SignatureTypeCCLA) {
log.WithFields(f).Debugf("processing signature type: %s with %d CLA Managers...",
newSignature.SignatureType, len(newSignature.SignatureACL))
companyModel, err := s.companyRepo.GetCompany(ctx, companyID)
if err != nil {
log.WithFields(f).Warnf("failed to lookup company from signature by companyID: %s, error: %+v",
companyID, err)
return err
}
if companyModel == nil {
msg := fmt.Sprintf("failed to lookup company from signature by companyID: %s, not found",
companyID)
log.WithFields(f).Warn(msg)
return errors.New(msg)
}
log.WithFields(f).Debugf("loaded company '%s' from signature by companyID: %s with companySFID: %s...",
companyModel.CompanyName, companyModel.CompanyID, companyModel.CompanyExternalID)
log.WithFields(f).Debugf("loaded company '%s' from signature by companyID: %s with companySFID: %s...",
companyModel.CompanyName, companyModel.CompanyID, companyModel.CompanyExternalID)
// We should have the company SFID...
if companyModel.CompanyExternalID == "" {
msg := fmt.Sprintf("company %s (%s) does not have oldSigACL SF Organization ID - unable to update permissions",
companyModel.CompanyName, companyModel.CompanyID)
log.WithFields(f).Warn(msg)
return errors.New(msg)
}
// Load the list of SF projects associated with this CLA Group
log.WithFields(f).Debugf("querying SF projects for CLA Group: %s", newSignature.SignatureProjectID)
projectCLAGroups, err := s.projectsClaGroupRepo.GetProjectsIdsForClaGroup(ctx, newSignature.SignatureProjectID)
log.WithFields(f).Debugf("found %d SF projects for CLA Group: %s", len(projectCLAGroups), newSignature.SignatureProjectID)
if err != nil {
log.WithFields(f).Errorf("Unable to query projectCLA groups by claGroupID : %s, error: %+v ", newSignature.SignatureProjectID, err)
return err
}
// Only proceed if we have one or more SF projects - otherwise, we can't assign and cleanup/adjust roles
if len(projectCLAGroups) == 0 {
log.WithFields(f).Warnf("no SF projects assigned to CLA group: %s ",
newSignature.SignatureProjectID)
return nil
}
_, _, err = s.companyService.AssociateContributorByGroup(ctx, companyModel.CompanyExternalID, newSignature.UserEmail, projectCLAGroups, newSignature.SignatureProjectID)
if err != nil {
log.WithFields(f).Errorf("unable to create contributor association for user: %s and company: %s ", newSignature.UserEmail, companyModel.CompanyExternalID)
}
return nil
}
return nil
}
// helper function that assigns acl permissions(v2 specific)
func (s *service) updateCLAManagerPermissions(signature Signature, managers []string, action string) error {
ctx := utils.NewContext()
f := logrus.Fields{
"functionName": "updateCLAManagerPermissions",
"signatureID": signature.SignatureID,
"AddedManagers": managers,
}
userClient := user_service.GetClient()
orgClient := organization_service.GetClient()
var companyID string
// Get Salesforce company
log.WithFields(f).Debug("Getting Salesforce Company ...")
if signature.SignatureType == utils.ClaTypeCCLA {
companyID = signature.SignatureReferenceID
} else if signature.SignatureType == utils.ClaTypeCCLA && signature.SignatureUserCompanyID != "" {
companyID = signature.SignatureUserCompanyID
}
companyModel, compErr := s.companyService.GetCompanyByID(ctx, companyID)
if compErr != nil {
log.WithFields(f).WithError(compErr).Warnf("unable to fetch company %s", companyID)
return compErr
}
if companyModel.CompanyExternalID == "" {
log.WithFields(f).WithError(ErrNoExternalID).Warnf("ExternalCompany ID does not exist for company: %s", companyID)
return ErrNoExternalID
}
org, orgErr := orgClient.GetOrganization(ctx, companyModel.CompanyExternalID)
if orgErr != nil {
log.WithFields(f).WithError(orgErr).Warnf("failed to get organisation for ID: %s ", companyModel.CompanyExternalID)
return orgErr
}
projectCLAGroups, pcgErr := s.projectsClaGroupRepo.GetProjectsIdsForClaGroup(ctx, signature.SignatureProjectID)
if pcgErr != nil {
log.WithFields(f).WithError(pcgErr).Warnf("unable to get project mappings for claGroupID: %s ", signature.SignatureProjectID)
return pcgErr
}
// Get Role ID
acsClient := acs_service.GetClient()
roleID, roleErr := acsClient.GetRoleID(utils.CLAManagerRole)
if roleErr != nil {
log.WithFields(f).WithError(roleErr).Warnf("failed to get roleID for : %s ", utils.CLAManagerRole)
return roleErr
}
log.WithFields(f).Debugf("Role ID for cla-manager-role : %s", roleID)
foundationSFID := projectCLAGroups[0].FoundationSFID
signedAtFoundation, signedErr := s.projectService.SignedAtFoundationLevel(ctx, foundationSFID)
// Create ProjectListing that handles CLA Manager permissions
projectSFIDList := utils.NewStringSet()
for _, p := range projectCLAGroups {
projectSFIDList.Add(p.ProjectSFID)
}
if signedErr != nil {
log.WithFields(f).WithError(signedErr).Warnf("failed to check signed status for foundationID: %s ", foundationSFID)
return signedErr
}
for i := range managers {
// Get User
mgr := managers[i]
lfUser, userErr := userClient.GetUserByUsername(mgr)
if userErr != nil {
log.WithFields(f).WithError(userErr).Warnf("Failed to get salesforce user for user: %s", mgr)
continue
}
// Get User email
email := lfUser.Emails[0].EmailAddress
if signedAtFoundation {
scopeID, scopeErr := orgClient.GetScopeID(ctx, org.ID, foundationSFID, utils.CLAManagerRole, utils.ProjectOrgScope, mgr)
if scopeErr != nil {
log.WithFields(f).WithError(scopeErr).Warnf("failed to get scopeID for foundation: %s , organization: %s, manager: %s and role: %s ", foundationSFID, org.Name, mgr, utils.CLAManagerRole)
continue
}
if action == AddCLAManager {
log.WithFields(f).Debugf("Adding CLA Manager permissions...")
hasScope, scopeErr := orgClient.IsUserHaveRoleScope(ctx, utils.CLAManagerRole, lfUser.ID, org.ID, foundationSFID)
if scopeErr != nil {
log.WithFields(f).WithError(scopeErr).Warnf("Failed to get scope for role: %s , user:%s , foundation: %s and org: %s ", utils.CLAManagerRole, lfUser.ID, foundationSFID, org.ID)
continue
}
if hasScope {
log.WithFields(f).Warnf("User: %s already has scope ", mgr)
continue
}
createErr := orgClient.CreateOrgUserRoleOrgScopeProjectOrg(ctx, *email, foundationSFID, org.ID, roleID)
if createErr != nil {
log.WithFields(f).WithError(createErr).Warnf("failed to create CLA Manager role for user: %s ", *email)
return scopeErr
}
} else if action == DeleteCLAManager {
log.WithFields(f).Debugf("Deleting CLA Manager permissions...")
deleteErr := orgClient.DeleteOrgUserRoleOrgScopeProjectOrg(ctx, org.ID, roleID, scopeID, &mgr, email)
if deleteErr != nil {
log.WithFields(f).WithError(deleteErr).Warn("Failed to remove CLA Manager from user: &s ", mgr)
return deleteErr
}
}
} else {
if action == AddCLAManager {
log.WithFields(f).Debugf("Adding CLA Manager permissions...")
var eg errgroup.Group
// add user as cla-manager for all projects of cla-group
for _, projectSfid := range projectSFIDList.List() {
// ensure that following goroutine gets a copy of projectSFID
projectSFID := projectSfid
eg.Go(func() error {
log.WithFields(f).Debugf("Adding CLA Manager role...")
err := orgClient.CreateOrgUserRoleOrgScopeProjectOrg(ctx, *email, projectSFID, org.ID, roleID)
if err != nil {
msg := fmt.Sprintf("unable to add %s scope for project: %s, company: %s using roleID: %s for user email: %s error = %s",
utils.CLAManagerRole, projectSFID, org.ID, roleID, *email, err)
log.WithFields(f).Warn(msg)
return err
}
return nil
})
}
// Wait for the go routines to finish
log.WithFields(f).Debugf("waiting for create role assignment to complete for %d projects...", len(projectSFIDList.List()))
if loadErr := eg.Wait(); loadErr != nil {
log.WithFields(f).Warn(loadErr)
continue
}
} else if action == DeleteCLAManager {
log.WithFields(f).Debugf("Deleting CLA Manager permissions...")
var eg errgroup.Group
// remove user as cla-manager for all projects of cla-group
for _, projectSfid := range projectSFIDList.List() {
// ensure that following goroutine gets a copy of projectSFID
projectSFID := projectSfid
eg.Go(func() error {
scopeID, scopeErr := orgClient.GetScopeID(ctx, org.ID, projectSFID, utils.CLAManagerRole, utils.ProjectOrgScope, mgr)
if scopeErr != nil {
log.WithFields(f).Warn(scopeErr)
return scopeErr
}
log.WithFields(f).Debugf("Removing CLA Manager role...")
deleteErr := orgClient.DeleteOrgUserRoleOrgScopeProjectOrg(ctx, org.ID, roleID, scopeID, &mgr, email)
if deleteErr != nil {
log.WithFields(f).Warn(deleteErr)
return deleteErr
}
return nil
})
}
// Wait for the go routines to finish
log.WithFields(f).Debugf("waiting for delete role assignment to complete for %d projects...", len(projectSFIDList.List()))
if loadErr := eg.Wait(); loadErr != nil {
log.WithFields(f).Warn(loadErr)
continue
}
}
}
}
return nil
}