Skip to content

Commit 7feba2a

Browse files
Address AI feedback
Signed-off-by: Lukasz Gryglicki <lgryglicki@cncf.io> Assisted by [OpenAI](https://platform.openai.com/) Assisted by [GitHub Copilot](https://github.com/features/copilot) Assisted by [Claude](https://claude.ai)
1 parent 8018705 commit 7feba2a

3 files changed

Lines changed: 52 additions & 26 deletions

File tree

cla-backend-go/company/mocks/mock_repo.go

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cla-backend-go/company/repository.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type IRepository interface { //nolint
5555
RejectCompanyAccessRequest(ctx context.Context, companyInviteID string) error
5656
UpdateCompanyAccessList(ctx context.Context, companyID string, companyACL []string) error
5757
UpdateCompanySanctionStatus(ctx context.Context, companyID string, sanctioned bool, origin string) error
58-
ClearCompanySanctionStatusIfSSS(ctx context.Context, companyID string) error
58+
ClearCompanySanctionStatusIfSSS(ctx context.Context, companyID string) (bool, error)
5959
IsCCLAEnabledForCompany(ctx context.Context, companyID string) (bool, error)
6060
}
6161

@@ -1279,6 +1279,9 @@ func (repo repository) UpdateCompanyAccessList(ctx context.Context, companyID st
12791279
return nil
12801280
}
12811281

1282+
// sanctionOriginSSS is the sanction_origin value written by the Sanctions Screening Service.
1283+
const sanctionOriginSSS = "sss"
1284+
12821285
// UpdateCompanySanctionStatus sets is_sanctioned and, when origin is non-empty, sanction_origin.
12831286
// Pass origin="sss" when flagging via SSS; pass origin="" for manual admin updates.
12841287
func (repo repository) UpdateCompanySanctionStatus(ctx context.Context, companyID string, sanctioned bool, origin string) error {
@@ -1327,7 +1330,7 @@ func (repo repository) UpdateCompanySanctionStatus(ctx context.Context, companyI
13271330
// with absent or non-"sss" origin). Only set the SSS flag when the company is
13281331
// currently unblocked or already SSS-blocked. A ConditionalCheckFailedException
13291332
// therefore means a manual/admin block is already in place and must be preserved.
1330-
sssSettingBlock := sanctioned && origin == "sss"
1333+
sssSettingBlock := sanctioned && origin == sanctionOriginSSS
13311334
if sssSettingBlock {
13321335
values[":false"] = &dynamodb.AttributeValue{BOOL: aws.Bool(false)}
13331336
input.ConditionExpression = aws.String("attribute_not_exists(#S) OR #S = :false OR #O = :o")
@@ -1347,8 +1350,10 @@ func (repo repository) UpdateCompanySanctionStatus(ctx context.Context, companyI
13471350
}
13481351

13491352
// ClearCompanySanctionStatusIfSSS clears is_sanctioned only when sanction_origin="sss".
1350-
// A ConditionalCheckFailedException (manual/absent origin) is silently ignored.
1351-
func (repo repository) ClearCompanySanctionStatusIfSSS(ctx context.Context, companyID string) error {
1353+
// It returns (cleared, err): cleared is true only when the conditional matched and the
1354+
// record was actually cleared; a ConditionalCheckFailedException (manual/absent origin)
1355+
// returns (false, nil) so callers can leave any manual/admin block in place.
1356+
func (repo repository) ClearCompanySanctionStatusIfSSS(ctx context.Context, companyID string) (bool, error) {
13521357
f := logrus.Fields{
13531358
"functionName": "company.repository.ClearCompanySanctionStatusIfSSS",
13541359
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
@@ -1372,19 +1377,19 @@ func (repo repository) ClearCompanySanctionStatusIfSSS(ctx context.Context, comp
13721377
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
13731378
":false": {BOOL: aws.Bool(false)},
13741379
":m": {S: aws.String(now)},
1375-
":sss": {S: aws.String("sss")},
1380+
":sss": {S: aws.String(sanctionOriginSSS)},
13761381
},
13771382
}
13781383

13791384
if _, err := repo.dynamoDBClient.UpdateItem(input); err != nil {
13801385
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
13811386
log.WithFields(f).Debugf("sanction_origin != sss for company %s; not auto-clearing (manual/admin block)", companyID)
1382-
return nil
1387+
return false, nil
13831388
}
13841389
log.WithFields(f).Warnf("error clearing company sanction status: %v", err)
1385-
return err
1390+
return false, err
13861391
}
1387-
return nil
1392+
return true, nil
13881393
}
13891394

13901395
func (repo repository) CreateCompany(ctx context.Context, in *models.Company) (*models.Company, error) {

cla-backend-go/v2/sign/service.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const (
6464
DocusignCompleted = "Completed"
6565
complianceCacheTTL = 5 * time.Minute
6666
maxComplianceCacheSize = 1000
67+
sanctionOriginSSS = "sss"
6768
)
6869

6970
// errors
@@ -2997,14 +2998,18 @@ func (s *service) checkCompanyCompliance(ctx context.Context, company *v1Models.
29972998

29982999
// Short-circuit for manually/admin-set blocks (sanction_origin != "sss" or no origin).
29993000
// SSS-origin blocks fall through so a now-clean result can clear them.
3000-
if company.IsSanctioned && company.SanctionOrigin != "sss" {
3001+
if company.IsSanctioned && company.SanctionOrigin != sanctionOriginSSS {
30013002
log.WithFields(f).Warnf("company has non-SSS sanction block (origin=%q), blocking without SSS call", company.SanctionOrigin)
30023003
return true, nil
30033004
}
30043005

30053006
cacheKey := s.complianceCacheKey(company)
30063007
if cached, ok := s.getComplianceCache(cacheKey); ok {
30073008
log.WithFields(f).Debugf("using cached compliance result for organization/company: %s", cacheKey)
3009+
// Mirror the cached decision onto the loaded model so downstream gates in this
3010+
// request stay consistent. Manual/admin blocks already short-circuited above, so a
3011+
// cached-clean result here cannot be masking such a block.
3012+
s.applyComplianceToModel(company, cached.sanctioned)
30083013
return cached.sanctioned, nil
30093014
}
30103015

@@ -3091,40 +3096,55 @@ func (s *service) checkCompanyCompliance(ctx context.Context, company *v1Models.
30913096
return false, fmt.Errorf("checkCompanyCompliance: unexpected SSS status %q for company %s (required mode blocks on ambiguous results)", result.Status, company.CompanyID)
30923097
}
30933098

3094-
// Persist result: set origin="sss" on flagged; conditionally clear on clean (only if sss-origin).
3099+
// Persist result and reflect it on the in-memory model so downstream gates in this
3100+
// same request (e.g. ProcessEmployeeSignature) see the just-updated state instead of
3101+
// the stale value loaded before this check ran.
30953102
if sanctioned {
30963103
log.WithFields(f).Warnf("SSS returned flagged status for company %s, persisting sanction with origin=sss", company.CompanyID)
3097-
if persistErr := s.companyRepo.UpdateCompanySanctionStatus(ctx, company.CompanyID, true, "sss"); persistErr != nil {
3104+
if persistErr := s.companyRepo.UpdateCompanySanctionStatus(ctx, company.CompanyID, true, sanctionOriginSSS); persistErr != nil {
30983105
log.WithFields(f).WithError(persistErr).Warnf("failed to persist sanction status for company %s", company.CompanyID)
30993106
return false, fmt.Errorf("failed to persist sanction status for company %s: %w", company.CompanyID, persistErr)
31003107
}
3108+
// Flagged always blocks downstream, so reflecting it is safe even if a concurrent
3109+
// manual/admin block now owns the persisted record (that also blocks).
3110+
s.applyComplianceToModel(company, true)
31013111
} else {
31023112
// Clear only when previously set by SSS; manual blocks are left untouched.
3103-
// ClearCompanySanctionStatusIfSSS treats a conditional-check failure (manual
3104-
// block / nothing to clear) as a no-op, so a non-nil error here is a real
3105-
// persistence failure. In required mode we fail closed rather than allow with a
3106-
// stale persisted sanction still in place.
3113+
// ClearCompanySanctionStatusIfSSS reports whether it actually cleared; a non-nil
3114+
// error is a real persistence failure (not a benign conditional-check no-op), so in
3115+
// required mode we fail closed rather than allow with a stale persisted sanction.
31073116
log.WithFields(f).Debugf("SSS returned clean status for company %s; attempting conditional clear", company.CompanyID)
3108-
if clearErr := s.companyRepo.ClearCompanySanctionStatusIfSSS(ctx, company.CompanyID); clearErr != nil {
3117+
cleared, clearErr := s.companyRepo.ClearCompanySanctionStatusIfSSS(ctx, company.CompanyID)
3118+
if clearErr != nil {
31093119
log.WithFields(f).WithError(clearErr).Warnf("failed to conditionally clear sanction status for company %s", company.CompanyID)
31103120
if s.sssRequired {
31113121
return false, fmt.Errorf("checkCompanyCompliance: SSS returned clean but clearing the persisted sanction failed for company %s: %w", company.CompanyID, clearErr)
31123122
}
31133123
}
3124+
// Only mirror the clear on the in-memory model when it actually applied. If the
3125+
// conditional did not match — e.g. a concurrent manual/admin block now owns the
3126+
// record — leave the loaded state so downstream gates still honor that block.
3127+
if cleared {
3128+
s.applyComplianceToModel(company, false)
3129+
}
31143130
}
31153131

3116-
// Reflect the live SSS decision on the in-memory model so any downstream gate in
3117-
// this same request (e.g. ProcessEmployeeSignature) sees the just-updated state
3118-
// rather than the now-stale value that was loaded before this check ran.
3132+
s.setComplianceCache(cacheKey, sanctioned)
3133+
return sanctioned, nil
3134+
}
3135+
3136+
// applyComplianceToModel updates the in-memory company model to reflect a compliance
3137+
// decision so downstream gates in the same request stay consistent with this check.
3138+
func (s *service) applyComplianceToModel(company *v1Models.Company, sanctioned bool) {
3139+
if company == nil {
3140+
return
3141+
}
31193142
company.IsSanctioned = sanctioned
31203143
if sanctioned {
3121-
company.SanctionOrigin = "sss"
3144+
company.SanctionOrigin = sanctionOriginSSS
31223145
} else {
31233146
company.SanctionOrigin = ""
31243147
}
3125-
3126-
s.setComplianceCache(cacheKey, sanctioned)
3127-
return sanctioned, nil
31283148
}
31293149

31303150
func (s *service) complianceCacheKey(company *v1Models.Company) string {

0 commit comments

Comments
 (0)