Skip to content

Commit c267137

Browse files
committed
add some test noise
1 parent c1cc257 commit c267137

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

coderd/idpsync/organization.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
127127
// Find the difference in the expected and the existing orgs, and
128128
// correct the set of orgs the user is a member of.
129129
add, remove := slice.SymmetricDifference(existingOrgIDs, finalExpected)
130-
notExists := make([]uuid.UUID, 0)
130+
// notExists is purely for debugging. It logs when the settings want
131+
// a user in an organization, but the organization does not exist.
132+
notExists := slice.DifferenceFunc(expectedOrgIDs, finalExpected, func(a, b uuid.UUID) bool {
133+
return a == b
134+
})
131135
for _, orgID := range add {
132136
_, err := tx.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{
133137
OrganizationID: orgID,
@@ -138,9 +142,26 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
138142
})
139143
if err != nil {
140144
if xerrors.Is(err, sql.ErrNoRows) {
145+
// This should not happen because we check the org existance
146+
// beforehand.
141147
notExists = append(notExists, orgID)
142148
continue
143149
}
150+
151+
if database.IsUniqueViolation(err, database.UniqueOrganizationMembersPkey) {
152+
// If we hit this error we have a bug. The user already exists in the
153+
// organization, but was not detected to be at the start of this function.
154+
// Instead of failing the function, an error will be logged. This is to not bring
155+
// down the entire syncing behavior from a single failed org. Failing this can
156+
// prevent user logins, so only fatal non-recoverable errors should be returned.
157+
s.Logger.Error(ctx, "syncing user to organization failed as they are already a member, please report this failure to Coder",
158+
slog.F("user_id", user.ID),
159+
slog.F("username", user.Username),
160+
slog.F("organization_id", orgID),
161+
slog.Error(err),
162+
)
163+
continue
164+
}
144165
return xerrors.Errorf("add user to organization: %w", err)
145166
}
146167
}
@@ -156,6 +177,7 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
156177
}
157178

158179
if len(notExists) > 0 {
180+
notExists = slice.Unique(notExists) // Remove dupes
159181
s.Logger.Debug(ctx, "organizations do not exist but attempted to use in org sync",
160182
slog.F("not_found", notExists),
161183
slog.F("user_id", user.ID),

coderd/idpsync/organizations_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ func TestSyncOrganizations(t *testing.T) {
4949
t.Run("SyncUserToDeletedOrg", func(t *testing.T) {
5050
ctx := testutil.Context(t, testutil.WaitMedium)
5151
db, _ := dbtestutil.NewDB(t)
52+
user := dbgen.User(t, db, database.User{})
53+
extra := dbgen.Organization(t, db, database.Organization{})
54+
dbgen.OrganizationMember(t, db, database.OrganizationMember{
55+
UserID: user.ID,
56+
OrganizationID: extra.ID,
57+
})
5258

5359
// Create a new organization, add in the user as a member, then delete
5460
// the org.
5561
org := dbgen.Organization(t, db, database.Organization{})
56-
user := dbgen.User(t, db, database.User{})
5762
dbgen.OrganizationMember(t, db, database.OrganizationMember{
5863
UserID: user.ID,
5964
OrganizationID: org.ID,
60-
CreatedAt: dbtime.Now(),
61-
UpdatedAt: dbtime.Now(),
62-
Roles: nil,
6365
})
6466

6567
err := db.UpdateOrganizationDeletedByID(ctx, database.UpdateOrganizationDeletedByIDParams{
@@ -76,6 +78,7 @@ func TestSyncOrganizations(t *testing.T) {
7678
OrganizationField: "orgs",
7779
OrganizationMapping: map[string][]uuid.UUID{
7880
"random": {org.ID},
81+
"noise": {uuid.New()},
7982
},
8083
OrganizationAssignDefault: false,
8184
},
@@ -84,7 +87,7 @@ func TestSyncOrganizations(t *testing.T) {
8487
err = s.SyncOrganizations(ctx, db, user, idpsync.OrganizationParams{
8588
SyncEntitled: true,
8689
MergedClaims: map[string]interface{}{
87-
"orgs": []string{"random"},
90+
"orgs": []string{"random", "noise"},
8891
},
8992
})
9093
require.NoError(t, err)

0 commit comments

Comments
 (0)