fix(enterprise/coderd): reject duplicate default_org_member_roles entries
The organization PATCH endpoint stored duplicate default role names verbatim, which made the Roles page render a role twice. The CLI already de-duplicates the flag values.
This commit is contained in:
1 parent
7931b250a5
commit
d7ba937a10
2 files changed
+44
No files matched your search
@@ -67,7 +67,20 @@ func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
|
||||
// for every member of the org. A future change can extend this to
|
||||
// custom org roles by routing through canAssignRoles in dbauthz.
|
||||
if req.DefaultOrgMemberRoles != nil {
|
||||
seen := make(map[string]struct{}, len(*req.DefaultOrgMemberRoles))
|
||||
for _, name := range *req.DefaultOrgMemberRoles {
|
||||
if _, ok := seen[name]; ok {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: "Invalid default_org_member_roles entry.",
|
||||
Detail: fmt.Sprintf("%q is listed more than once.", name),
|
||||
Validations: []codersdk.ValidationError{{
|
||||
Field: "default_org_member_roles",
|
||||
Detail: fmt.Sprintf("%q is listed more than once.", name),
|
||||
}},
|
||||
})
|
||||
return
|
||||
}
|
||||
seen[name] = struct{}{}
|
||||
if _, err := rbac.RoleByName(rbac.RoleIdentifier{Name: name, OrganizationID: organization.ID}); err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: "Invalid default_org_member_roles entry.",
|
||||
|
||||
@@ -519,6 +519,37 @@ func TestPatchOrganizationsByUser(t *testing.T) {
|
||||
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
|
||||
require.Contains(t, apiErr.Message, "Invalid default_org_member_roles entry")
|
||||
})
|
||||
|
||||
t.Run("DuplicateRoleRejected", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client, _ := coderdenttest.New(t, &coderdenttest.Options{
|
||||
LicenseOptions: &coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
codersdk.FeatureMultipleOrganizations: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
ctx := testutil.Context(t, testutil.WaitMedium)
|
||||
o := coderdenttest.CreateOrganization(t, client, coderdenttest.CreateOrganizationOptions{})
|
||||
|
||||
//nolint:gocritic // Only owners can update organization settings.
|
||||
_, err := client.UpdateOrganization(ctx, o.ID.String(), codersdk.UpdateOrganizationRequest{
|
||||
DefaultOrgMemberRoles: new([]string{
|
||||
codersdk.RoleOrganizationWorkspaceAccess,
|
||||
codersdk.RoleAgentsAccess,
|
||||
codersdk.RoleAgentsAccess,
|
||||
}),
|
||||
})
|
||||
var apiErr *codersdk.Error
|
||||
require.ErrorAs(t, err, &apiErr)
|
||||
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
|
||||
require.Contains(t, apiErr.Detail, "listed more than once")
|
||||
|
||||
//nolint:gocritic // The owner verifies the stored defaults are unchanged.
|
||||
got, err := client.Organization(ctx, o.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, rbac.DefaultOrgMemberRoles(), got.DefaultOrgMemberRoles)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user