Skip to content

Commit 4798911

Browse files
committed
add unit test for legacy behavior
1 parent ea7b3f1 commit 4798911

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

coderd/idpsync/group.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ type GroupSyncSettings struct {
206206
// group IDs are used to account for group renames.
207207
// For legacy configurations, this config option has to remain.
208208
// Deprecated: Use GroupMapping instead.
209-
LegacyGroupNameMapping map[string]string
209+
LegacyGroupNameMapping map[string]string `json:"legacy_group_name_mapping,omitempty"`
210210
}
211211

212212
func (s *GroupSyncSettings) Set(v string) error {
@@ -251,6 +251,12 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
251251

252252
groups := make([]ExpectedGroup, 0)
253253
for _, group := range parsedGroups {
254+
// Legacy group mappings happen before the regex filter.
255+
mappedGroupName, ok := s.LegacyGroupNameMapping[group]
256+
if ok {
257+
group = mappedGroupName
258+
}
259+
254260
// Only allow through groups that pass the regex
255261
if s.RegexFilter != nil {
256262
if !s.RegexFilter.MatchString(group) {
@@ -267,11 +273,6 @@ func (s GroupSyncSettings) ParseClaims(mergedClaims jwt.MapClaims) ([]ExpectedGr
267273
continue
268274
}
269275

270-
mappedGroupName, ok := s.LegacyGroupNameMapping[group]
271-
if ok {
272-
groups = append(groups, ExpectedGroup{GroupName: &mappedGroupName})
273-
continue
274-
}
275276
group := group
276277
groups = append(groups, ExpectedGroup{GroupName: &group})
277278
}
@@ -332,6 +333,23 @@ func (s GroupSyncSettings) HandleMissingGroups(ctx context.Context, tx database.
332333
if err != nil {
333334
return nil, xerrors.Errorf("insert missing groups: %w", err)
334335
}
336+
337+
if len(missingGroups) != len(createdMissingGroups) {
338+
// This is unfortunate, but if legacy params are used, then some existing groups
339+
// can come as params. So we need to fetch them
340+
allGroups, err := tx.GetGroups(ctx, database.GetGroupsParams{
341+
OrganizationID: orgID,
342+
GroupNames: missingGroups,
343+
})
344+
if err != nil {
345+
return nil, xerrors.Errorf("get groups by names: %w", err)
346+
}
347+
348+
createdMissingGroups = db2sdk.List(allGroups, func(g database.GetGroupsRow) database.Group {
349+
return g.Group
350+
})
351+
}
352+
335353
for _, created := range createdMissingGroups {
336354
addIDs = append(addIDs, created.ID)
337355
}

coderd/idpsync/group_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ func TestGroupSyncTable(t *testing.T) {
197197
Settings: nil,
198198
Groups: map[uuid.UUID]bool{},
199199
},
200+
{
201+
Name: "LegacyMapping",
202+
Settings: &idpsync.GroupSyncSettings{
203+
GroupField: "groups",
204+
RegexFilter: regexp.MustCompile("^legacy"),
205+
LegacyGroupNameMapping: map[string]string{
206+
"create-bar": "legacy-bar",
207+
"foo": "legacy-foo",
208+
},
209+
AutoCreateMissingGroups: true,
210+
},
211+
Groups: map[uuid.UUID]bool{},
212+
GroupNames: map[string]bool{
213+
"legacy-foo": false,
214+
},
215+
ExpectedGroupNames: []string{
216+
"legacy-bar",
217+
"legacy-foo",
218+
},
219+
},
200220
}
201221

202222
for _, tc := range testCases {

0 commit comments

Comments
 (0)