Skip to content

chore: handle deleted organizations in idp sync #17405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 16, 2025
Next Next commit
test: add unit test to excercise bug when idp sync hits deleted orgs
Deleted organizations are still attempting to sync members.
This causes an error on inserting the member, and would likely
cause issues later in the sync process even if that member is
inserted. Deleted orgs should be skipped.
  • Loading branch information
Emyrk committed Apr 15, 2025
commit 6046b56de521b9affdca248a1ca084352997038d
60 changes: 60 additions & 0 deletions coderd/idpsync/organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/stretchr/testify/require"

"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/idpsync"
"github.com/coder/coder/v2/coderd/runtimeconfig"
"github.com/coder/coder/v2/testutil"
Expand Down Expand Up @@ -38,3 +42,59 @@ func TestParseOrganizationClaims(t *testing.T) {
require.False(t, params.SyncEntitled)
})
}

func TestSyncOrganizations(t *testing.T) {
t.Parallel()

t.Run("SyncUserToDeletedOrg", func(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitMedium)
db, _ := dbtestutil.NewDB(t)

// Create a new organization, add in the user as a member, then delete
// the org.
org := dbgen.Organization(t, db, database.Organization{})
user := dbgen.User(t, db, database.User{})
dbgen.OrganizationMember(t, db, database.OrganizationMember{
UserID: user.ID,
OrganizationID: org.ID,
CreatedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
Roles: nil,
})

err := db.UpdateOrganizationDeletedByID(ctx, database.UpdateOrganizationDeletedByIDParams{
UpdatedAt: dbtime.Now(),
ID: org.ID,
})
require.NoError(t, err)

// Now sync the user to the deleted organization
s := idpsync.NewAGPLSync(
slogtest.Make(t, &slogtest.Options{}),
runtimeconfig.NewManager(),
idpsync.DeploymentSyncSettings{
OrganizationField: "orgs",
OrganizationMapping: map[string][]uuid.UUID{
"random": {org.ID},
},
OrganizationAssignDefault: false,
},
)

err = s.SyncOrganizations(ctx, db, user, idpsync.OrganizationParams{
SyncEntitled: true,
MergedClaims: map[string]interface{}{
"orgs": []string{"random"},
},
})
require.NoError(t, err)

mems, err := db.OrganizationMembers(ctx, database.OrganizationMembersParams{
OrganizationID: org.ID,
UserID: user.ID,
IncludeSystem: false,
})
require.NoError(t, err)
require.Len(t, mems, 1)
})
}