HandleMissingGroups ensures all ExpectedGroups convert to uuids. Groups can be referenced by name via legacy params or IDP group names. These group names are converted to IDs for easier assignment. Missing groups are created if AutoCreate is enabled. TODO: Batching this would be better, as this is 1
(ctx context.Context, tx database.Store, orgID uuid.UUID, add []ExpectedGroup)
| 409 | // Missing groups are created if AutoCreate is enabled. |
| 410 | // TODO: Batching this would be better, as this is 1 or 2 db calls per organization. |
| 411 | func (s GroupSyncSettings) HandleMissingGroups(ctx context.Context, tx database.Store, orgID uuid.UUID, add []ExpectedGroup) ([]uuid.UUID, error) { |
| 412 | // All expected that are missing IDs means the group does not exist |
| 413 | // in the database, or it is a legacy mapping, and we need to do a lookup. |
| 414 | var missingGroups []string |
| 415 | addIDs := make([]uuid.UUID, 0) |
| 416 | |
| 417 | for _, expected := range add { |
| 418 | if expected.GroupID == nil && expected.GroupName != nil { |
| 419 | missingGroups = append(missingGroups, *expected.GroupName) |
| 420 | } else if expected.GroupID != nil { |
| 421 | // Keep the IDs to sync the groups. |
| 422 | addIDs = append(addIDs, *expected.GroupID) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if s.AutoCreateMissing && len(missingGroups) > 0 { |
| 427 | // Insert any missing groups. If the groups already exist, this is a noop. |
| 428 | _, err := tx.InsertMissingGroups(ctx, database.InsertMissingGroupsParams{ |
| 429 | OrganizationID: orgID, |
| 430 | Source: database.GroupSourceOidc, |
| 431 | GroupNames: missingGroups, |
| 432 | }) |
| 433 | if err != nil { |
| 434 | return nil, xerrors.Errorf("insert missing groups: %w", err) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Fetch any missing groups by name. If they exist, their IDs will be |
| 439 | // matched and returned. |
| 440 | if len(missingGroups) > 0 { |
| 441 | // Do name lookups for all groups that are missing IDs. |
| 442 | newGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{ |
| 443 | OrganizationID: orgID, |
| 444 | HasMemberID: uuid.UUID{}, |
| 445 | GroupNames: missingGroups, |
| 446 | }) |
| 447 | if err != nil { |
| 448 | return nil, xerrors.Errorf("get groups by names: %w", err) |
| 449 | } |
| 450 | for _, g := range newGroups { |
| 451 | addIDs = append(addIDs, g.Group.ID) |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return addIDs, nil |
| 456 | } |
| 457 | |
| 458 | func ConvertAllowList(allowList []string) map[string]struct{} { |
| 459 | allowMap := make(map[string]struct{}, len(allowList)) |
no test coverage detected