// Copyright The Linux Foundation and each contributor to CommunityBridge. // SPDX-License-Identifier: MIT package company import ( "context" "errors" "fmt" "github.com/sirupsen/logrus" v1Models "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/models" "github.com/linuxfoundation/easycla/cla-backend-go/gen/v2/models" log "github.com/linuxfoundation/easycla/cla-backend-go/logging" "github.com/linuxfoundation/easycla/cla-backend-go/orgimport" "github.com/linuxfoundation/easycla/cla-backend-go/utils" acsService "github.com/linuxfoundation/easycla/cla-backend-go/v2/acs-service" memberService "github.com/linuxfoundation/easycla/cla-backend-go/v2/member-service" orgService "github.com/linuxfoundation/easycla/cla-backend-go/v2/organization-service" "github.com/linuxfoundation/easycla/cla-backend-go/v2/organization-service/client/organizations" v2UserServiceModels "github.com/linuxfoundation/easycla/cla-backend-go/v2/user-service/models" ) // ErrMemberServiceNotConfigured reports the create-company switch on without a usable member-service client var ErrMemberServiceNotConfigured = errors.New("company creation through member-service is not configured") // ErrCompanySharedDomain reports a website on a shared domain (public e-mail or hosting provider) that identifies no organization (lfx-self-serve#3794) var ErrCompanySharedDomain = errors.New("the company website is on a shared domain") // ErrCompanyAmbiguousDomain reports several Salesforce accounts sharing the website domain var ErrCompanyAmbiguousDomain = errors.New("several organizations match the company website") // ErrCompanyExists reports an EasyCLA company already recorded for the matched Account, or another Account under the same name var ErrCompanyExists = errors.New("organization already exists") // ErrCompanyCreateInProgress reports another creation running for the same website domain (lfx-self-serve#5129) var ErrCompanyCreateInProgress = errors.New("a company for this website is being created - try again in a minute") // ErrCompanyNotEligible reports a Salesforce account that member-service refuses to register as an organization var ErrCompanyNotEligible = errors.New("the organization is not eligible for EasyCLA") // ErrCompanyDependencyFailed reports a member-service, repository, Organization Service or ACS failure - not a caller error var ErrCompanyDependencyFailed = errors.New("company creation is temporarily unavailable") var sharedDomains = defaultSharedDomains() func defaultSharedDomains() orgimport.SharedDomains { set := orgimport.SharedDomains{} for _, d := range orgimport.DefaultSharedDomains { set[d] = true } return set } // EnableMemberServiceCreate routes company creation through member-service find-or-create (lfx-self-serve#4992); // a nil client makes creation fail explicitly instead of falling back to the Organization Service func (s *service) EnableMemberServiceCreate(client *memberService.Client) { s.createViaMemberService = true s.members = client } // EnableCreateLock makes member-service creations for one website domain run one at a time func (s *service) EnableCreateLock(locks CreateLocks) { s.createLocks = locks } // lockCreate takes the create lock of the domain and returns its release; a lock that cannot be taken for another // reason than being held is a warning, and the creation goes on without it func (s *service) lockCreate(ctx context.Context, f logrus.Fields, domain string) (func(), error) { if s.createLocks == nil { return func() {}, nil } owner, err := s.createLocks.Acquire(ctx, domain) if errors.Is(err, ErrCreateLockHeld) { log.WithFields(f).Warn("another company creation is running for the website domain - refusing to start a second one") return nil, fmt.Errorf("%w: %s", ErrCompanyCreateInProgress, domain) } if err != nil { log.WithFields(f).WithError(err).Warn("unable to take the company create lock - creating the company without it") return func() {}, nil } return func() { if releaseErr := s.createLocks.Release(ctx, domain, owner); releaseErr != nil { log.WithFields(f).WithError(releaseErr).Warn("unable to release the company create lock - it expires on its own") } }, nil } // createCompanyViaMemberService matches or creates the Salesforce account through member-service and creates the // EasyCLA company on its 001 id; only a created account grants company-admin to the creator after the row exists. func (s *service) createCompanyViaMemberService(ctx context.Context, f logrus.Fields, companyName, signingEntityName, companyWebsite, userEmail, userID, note string, lfUser *v2UserServiceModels.User) (*models.CompanyOutput, error) { f = memberServiceLogFields(f) if s.members == nil { log.WithFields(f).Warn("the create-company switch is on but the member-service client is not configured - refusing to create the company") return nil, ErrMemberServiceNotConfigured } domain, gate := sharedDomains.Shared(companyWebsite) if gate != "" { log.WithFields(f).Warnf("website %q is %s - refusing to create the company", companyWebsite, gate) return nil, fmt.Errorf("%w: %s", ErrCompanySharedDomain, companyWebsite) } f["companyDomain"] = domain release, lockErr := s.lockCreate(ctx, f, domain) if lockErr != nil { return nil, lockErr } defer release() if takenErr := s.companyNameTaken(ctx, f, companyName); takenErr != nil { return nil, takenErr } b2b, action, err := s.members.FindOrCreateB2BOrg(ctx, companyName, companyWebsite) if err != nil { switch { case errors.Is(err, memberService.ErrAmbiguousDomain): log.WithFields(f).WithError(err).Warn("several Salesforce accounts match the website domain - refusing to create the company") return nil, fmt.Errorf("%w: %s", ErrCompanyAmbiguousDomain, companyWebsite) case errors.Is(err, memberService.ErrNotEligible): log.WithFields(f).WithError(err).Warn("member-service refused to register the account as an organization") return nil, fmt.Errorf("%w: %v", ErrCompanyNotEligible, err) case errors.Is(err, memberService.ErrInvalidRequest): log.WithFields(f).WithError(err).Warn("member-service rejected the company request") return nil, err default: log.WithFields(f).WithError(err).Warn("member-service find-or-create failed") return nil, fmt.Errorf("%w: %v", ErrCompanyDependencyFailed, err) } } f["companySFID"], f["memberServiceAction"] = b2b.UID, action if b2b.Name != "" && b2b.Name != companyName { log.WithFields(f).Debugf("the Salesforce account name differs from the request - using %s", b2b.Name) companyName = b2b.Name if takenErr := s.companyNameTaken(ctx, f, companyName); takenErr != nil { return nil, takenErr } } if signingEntityName == "" { signingEntityName = companyName } if action == memberService.ActionMatched { existing, lookupErr := s.companyBySFID(ctx, b2b.UID) if lookupErr != nil { log.WithFields(f).WithError(lookupErr).Warn("unable to check whether the matched account is already an EasyCLA company") return nil, fmt.Errorf("%w: %v", ErrCompanyDependencyFailed, lookupErr) } if existing != nil { log.WithFields(f).Warnf("the matched account is already EasyCLA company %s - refusing to create another", existing.CompanyID) return nil, fmt.Errorf("%w: %s", ErrCompanyExists, b2b.UID) } } row := &v1Models.Company{ CompanyExternalID: b2b.UID, CompanyName: companyName, SigningEntityName: signingEntityName, CompanyACL: []string{}, Note: note, IsSanctioned: false, } if action == memberService.ActionCreated { row.CompanyManagerID = userID row.CompanyACL = []string{lfUser.Username} } created, createErr := s.companyRepo.CreateCompany(ctx, row) if createErr != nil { log.WithFields(f).WithError(createErr).Warnf("failed to create EasyCLA company %s", companyName) return nil, fmt.Errorf("%w: %v", ErrCompanyDependencyFailed, createErr) } if created == nil || created.CompanyExternalID != b2b.UID { log.WithFields(f).Warnf("an EasyCLA company named %q already exists with a different Salesforce account - refusing to create another", companyName) return nil, fmt.Errorf("%w: %s", ErrCompanyExists, companyName) } // best effort: the creator owns the row already, and a refused or failed grant must not fail a company that // exists (lfx-self-serve#5916) if action == memberService.ActionCreated { s.grantCreatorIfOrgKnown(ctx, f, b2b.UID, userEmail, userID, lfUser.Username) } return &models.CompanyOutput{ CompanyName: created.CompanyName, SigningEntityName: created.SigningEntityName, CompanyWebsite: companyWebsite, CompanyID: created.CompanyExternalID, }, nil } // memberServiceLogFields copies the caller's fields without the e-mail address: this path logs the user id only func memberServiceLogFields(f logrus.Fields) logrus.Fields { out := make(logrus.Fields, len(f)) for k, v := range f { if k != "userEmail" { out[k] = v } } return out } // companyNameTaken answers ErrCompanyExists when an EasyCLA company already carries the name (the repository would // otherwise silently return that row), before any Salesforce write or grant func (s *service) companyNameTaken(ctx context.Context, f logrus.Fields, companyName string) error { existing, err := s.companyRepo.GetCompanyByName(ctx, companyName) if err != nil { log.WithFields(f).WithError(err).Warn("unable to check whether the company name is already taken") return fmt.Errorf("%w: %v", ErrCompanyDependencyFailed, err) } if existing != nil { log.WithFields(f).Warnf("an EasyCLA company named %q already exists - refusing to create another", companyName) return fmt.Errorf("%w: %s", ErrCompanyExists, companyName) } return nil } // companyBySFID finds the EasyCLA company recorded for the account: the canonical 18-character id first, then the // 15-character form a legacy row may carry; nil when neither is recorded func (s *service) companyBySFID(ctx context.Context, sfid string) (*v1Models.Company, error) { var notFound *utils.CompanyNotFound existing, err := s.companyRepo.GetCompanyByExternalID(ctx, sfid) if err == nil || !errors.As(err, ¬Found) || len(sfid) != 18 { return existing, err } existing, err = s.companyRepo.GetCompanyByExternalID(ctx, sfid[:15]) if err != nil && errors.As(err, ¬Found) { return nil, nil } return existing, err } // grantCreatorIfOrgKnown grants company-admin on the new account to the creator, best effort: every failure is a // warning and the company stays created without the grant. It skips the Organization Service readiness probe in // ACS-direct mode; there the ACS answer is not logged because it may echo the request, only its status is. func (s *service) grantCreatorIfOrgKnown(ctx context.Context, f logrus.Fields, sfid, userEmail, userID, username string) { orgClient := orgService.GetClient() direct := acsService.DirectCLARolesEnabled() if !direct { if _, orgErr := orgClient.GetOrganization(ctx, sfid); orgErr != nil { if isOrgNotFound(orgErr) { log.WithFields(f).Warnf("the Organization Service does not know account %s yet - creating the company without the company-admin grant for user %s", sfid, userID) } else { log.WithFields(f).WithError(orgErr).Warnf("unable to read account %s from the Organization Service - creating the company without the company-admin grant for user %s", sfid, userID) } return } } roleID, adminErr := acsService.GetClient().GetRoleID(utils.CompanyAdminRole) if adminErr != nil { entry := log.WithFields(f) if !direct { entry = entry.WithError(adminErr) } entry.Warnf("problem getting the company-admin role ID - creating the company without the company-admin grant for user %s", userID) return } scopeErr := orgClient.CreateCLAOrgUserRoleScope(ctx, userEmail, username, sfid, roleID) switch { case scopeErr == nil: case isOrgAssociated(scopeErr): // ACS allows one organization-scope association per user; the Org Service path has always tolerated this log.WithFields(f).Warnf("user %s already belongs to another organization - creating the company without the company-admin grant", userID) default: // refused (ACS 400: the account is not in the Organization Service yet, or no corporate e-mail) or an outage; // the creator keeps the EasyCLA company ACL either way entry := log.WithFields(f) if !direct { entry = entry.WithError(scopeErr) } var grantErr *acsService.UserGrantError if errors.As(scopeErr, &grantErr) { entry = entry.WithField("acsStatus", grantErr.Status) } entry.WithField("acsReason", acsGrantReason(scopeErr)).Warnf("problem creating the company-admin scope for user %s - creating the company without the grant", userID) } } func isOrgNotFound(err error) bool { var notFound *organizations.GetOrgNotFound return errors.As(err, ¬Found) }