2022-09-23 08:30:32 +10:00
|
|
|
package coderd_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-08-06 09:18:46 -04:00
|
|
|
"fmt"
|
2022-09-23 08:30:32 +10:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
|
|
|
"github.com/coder/coder/v2/testutil"
|
2022-09-23 08:30:32 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCheckPermissions(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
2022-10-10 15:37:06 -05:00
|
|
|
adminClient := coderdtest.New(t, &coderdtest.Options{
|
|
|
|
|
IncludeProvisionerDaemon: true,
|
|
|
|
|
})
|
2022-09-23 08:30:32 +10:00
|
|
|
// Create adminClient, member, and org adminClient
|
|
|
|
|
adminUser := coderdtest.CreateFirstUser(t, adminClient)
|
2023-02-06 17:48:21 -06:00
|
|
|
memberClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
|
2022-09-23 08:30:32 +10:00
|
|
|
memberUser, err := memberClient.User(ctx, codersdk.Me)
|
|
|
|
|
require.NoError(t, err)
|
2024-06-05 11:25:02 -05:00
|
|
|
orgAdminClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID, rbac.ScopedRoleOrgAdmin(adminUser.OrganizationID))
|
2022-09-23 08:30:32 +10:00
|
|
|
orgAdminUser, err := orgAdminClient.User(ctx, codersdk.Me)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2022-10-10 15:37:06 -05:00
|
|
|
version := coderdtest.CreateTemplateVersion(t, adminClient, adminUser.OrganizationID, nil)
|
2023-10-03 11:02:56 -06:00
|
|
|
coderdtest.AwaitTemplateVersionJobCompleted(t, adminClient, version.ID)
|
2022-10-10 15:37:06 -05:00
|
|
|
template := coderdtest.CreateTemplate(t, adminClient, adminUser.OrganizationID, version.ID)
|
|
|
|
|
|
2022-09-23 08:30:32 +10:00
|
|
|
// With admin, member, and org admin
|
|
|
|
|
const (
|
2022-10-10 15:37:06 -05:00
|
|
|
readAllUsers = "read-all-users"
|
|
|
|
|
readOrgWorkspaces = "read-org-workspaces"
|
|
|
|
|
readMyself = "read-myself"
|
|
|
|
|
readOwnWorkspaces = "read-own-workspaces"
|
|
|
|
|
updateSpecificTemplate = "update-specific-template"
|
2022-09-23 08:30:32 +10:00
|
|
|
)
|
|
|
|
|
params := map[string]codersdk.AuthorizationCheck{
|
|
|
|
|
readAllUsers: {
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
2023-04-11 08:57:23 -05:00
|
|
|
ResourceType: codersdk.ResourceUser,
|
2022-09-23 08:30:32 +10:00
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
},
|
2025-10-27 17:14:16 -06:00
|
|
|
readOrgWorkspaces: {
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
|
|
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
|
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
},
|
2022-09-23 08:30:32 +10:00
|
|
|
readMyself: {
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
2023-04-11 08:57:23 -05:00
|
|
|
ResourceType: codersdk.ResourceUser,
|
2022-09-23 08:30:32 +10:00
|
|
|
OwnerID: "me",
|
|
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
},
|
|
|
|
|
readOwnWorkspaces: {
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
2023-04-11 08:57:23 -05:00
|
|
|
ResourceType: codersdk.ResourceWorkspace,
|
2022-09-23 08:30:32 +10:00
|
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
2025-10-27 17:14:16 -06:00
|
|
|
OwnerID: "me",
|
2022-09-23 08:30:32 +10:00
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
},
|
2022-10-10 15:37:06 -05:00
|
|
|
updateSpecificTemplate: {
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
2023-04-11 08:57:23 -05:00
|
|
|
ResourceType: codersdk.ResourceTemplate,
|
2022-10-10 15:37:06 -05:00
|
|
|
ResourceID: template.ID.String(),
|
|
|
|
|
},
|
|
|
|
|
Action: "update",
|
|
|
|
|
},
|
2022-09-23 08:30:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
Name string
|
|
|
|
|
Client *codersdk.Client
|
|
|
|
|
UserID uuid.UUID
|
|
|
|
|
Check codersdk.AuthorizationResponse
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
Name: "Admin",
|
|
|
|
|
Client: adminClient,
|
|
|
|
|
UserID: adminUser.UserID,
|
|
|
|
|
Check: map[string]bool{
|
2022-10-10 15:37:06 -05:00
|
|
|
readAllUsers: true,
|
2025-10-27 17:14:16 -06:00
|
|
|
readOrgWorkspaces: true,
|
2022-10-10 15:37:06 -05:00
|
|
|
readMyself: true,
|
|
|
|
|
readOwnWorkspaces: true,
|
|
|
|
|
updateSpecificTemplate: true,
|
2022-09-23 08:30:32 +10:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "OrgAdmin",
|
|
|
|
|
Client: orgAdminClient,
|
|
|
|
|
UserID: orgAdminUser.ID,
|
|
|
|
|
Check: map[string]bool{
|
2024-07-19 10:44:18 -10:00
|
|
|
readAllUsers: true,
|
2025-10-27 17:14:16 -06:00
|
|
|
readOrgWorkspaces: true,
|
2022-10-10 15:37:06 -05:00
|
|
|
readMyself: true,
|
|
|
|
|
readOwnWorkspaces: true,
|
|
|
|
|
updateSpecificTemplate: true,
|
2022-09-23 08:30:32 +10:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "Member",
|
|
|
|
|
Client: memberClient,
|
|
|
|
|
UserID: memberUser.ID,
|
|
|
|
|
Check: map[string]bool{
|
2023-07-26 10:33:48 -04:00
|
|
|
readAllUsers: false,
|
2025-10-27 17:14:16 -06:00
|
|
|
readOrgWorkspaces: false,
|
2022-10-10 15:37:06 -05:00
|
|
|
readMyself: true,
|
|
|
|
|
readOwnWorkspaces: true,
|
|
|
|
|
updateSpecificTemplate: false,
|
2022-09-23 08:30:32 +10:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range testCases {
|
|
|
|
|
t.Run("CheckAuthorization/"+c.Name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
2023-01-29 15:47:24 -06:00
|
|
|
resp, err := c.Client.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: params})
|
2022-09-23 08:30:32 +10:00
|
|
|
require.NoError(t, err, "check perms")
|
|
|
|
|
require.Equal(t, c.Check, resp)
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-08-06 09:18:46 -04:00
|
|
|
|
|
|
|
|
// Enough same-typed checks in one request to push a group past the batching
|
|
|
|
|
// threshold, exercising the grouped partial-evaluation path and the key
|
|
|
|
|
// mapping. Reading org members is allowed for any member; updating them is
|
|
|
|
|
// admin-only, so the two actions must map back to their keys distinctly.
|
|
|
|
|
t.Run("CheckAuthorization/Grouped", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
|
|
grouped := make(map[string]codersdk.AuthorizationCheck)
|
|
|
|
|
adminExpected := make(map[string]bool)
|
|
|
|
|
memberExpected := make(map[string]bool)
|
|
|
|
|
for i := 0; i < 55; i++ {
|
|
|
|
|
readKey := fmt.Sprintf("read-members-%d", i)
|
|
|
|
|
grouped[readKey] = codersdk.AuthorizationCheck{
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
|
|
|
|
ResourceType: codersdk.ResourceOrganizationMember,
|
|
|
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
}
|
|
|
|
|
adminExpected[readKey] = true
|
|
|
|
|
memberExpected[readKey] = true
|
|
|
|
|
|
|
|
|
|
updateKey := fmt.Sprintf("update-members-%d", i)
|
|
|
|
|
grouped[updateKey] = codersdk.AuthorizationCheck{
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
|
|
|
|
ResourceType: codersdk.ResourceOrganizationMember,
|
|
|
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
|
|
|
},
|
|
|
|
|
Action: "update",
|
|
|
|
|
}
|
|
|
|
|
adminExpected[updateKey] = true
|
|
|
|
|
memberExpected[updateKey] = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
adminResp, err := adminClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, adminExpected, map[string]bool(adminResp))
|
|
|
|
|
|
|
|
|
|
memberResp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, memberExpected, map[string]bool(memberResp))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// A member may create a workspace in an org it belongs to, so an
|
|
|
|
|
// any_org=true check is allowed. Repeating it past the batching threshold
|
|
|
|
|
// must not change the answer: AnyOrgOwner objects have no verified partial-
|
|
|
|
|
// evaluation semantics, so they must stay on the per-object path rather than
|
|
|
|
|
// being grouped into rbac.Filter's prepared query, which denies them.
|
|
|
|
|
t.Run("CheckAuthorization/AnyOrg", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
|
|
check := codersdk.AuthorizationCheck{
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
|
|
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
|
|
|
OwnerID: "me",
|
|
|
|
|
AnyOrgOwner: true,
|
|
|
|
|
},
|
|
|
|
|
Action: "create",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Below the threshold: a single check is evaluated in full and allowed.
|
|
|
|
|
single, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{
|
|
|
|
|
Checks: map[string]codersdk.AuthorizationCheck{"create-any-org": check},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.True(t, single["create-any-org"], "single any_org check should be allowed")
|
|
|
|
|
|
|
|
|
|
// The same check repeated past the threshold must stay allowed.
|
|
|
|
|
grouped := make(map[string]codersdk.AuthorizationCheck)
|
|
|
|
|
expected := make(map[string]bool)
|
|
|
|
|
for i := 0; i < 55; i++ {
|
|
|
|
|
key := fmt.Sprintf("create-any-org-%d", i)
|
|
|
|
|
grouped[key] = check
|
|
|
|
|
expected[key] = true
|
|
|
|
|
}
|
|
|
|
|
resp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, expected, map[string]bool(resp))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// A single (read, workspace) group past the batching threshold mixing
|
|
|
|
|
// resource_id checks (concrete workspaces fetched via the maxFetch path)
|
|
|
|
|
// with org-scoped checks (synthetic org-level objects). The member owns the
|
|
|
|
|
// fetched workspace so those keys are allowed, but cannot read all org
|
|
|
|
|
// workspaces so the org-scoped keys are denied. Both object shapes must map
|
|
|
|
|
// back to the correct per-key verdict through the one prepared query.
|
|
|
|
|
t.Run("CheckAuthorization/MixedGroup", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
|
|
workspace := coderdtest.CreateWorkspace(t, memberClient, template.ID)
|
|
|
|
|
coderdtest.AwaitWorkspaceBuildJobCompleted(t, memberClient, workspace.LatestBuild.ID)
|
|
|
|
|
|
|
|
|
|
grouped := make(map[string]codersdk.AuthorizationCheck)
|
|
|
|
|
expected := make(map[string]bool)
|
|
|
|
|
// resource_id checks are capped at maxFetch (10); the member owns the
|
|
|
|
|
// workspace, so reading it is allowed.
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
key := fmt.Sprintf("read-own-workspace-%d", i)
|
|
|
|
|
grouped[key] = codersdk.AuthorizationCheck{
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
|
|
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
|
|
|
ResourceID: workspace.ID.String(),
|
|
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
}
|
|
|
|
|
expected[key] = true
|
|
|
|
|
}
|
|
|
|
|
// Org-scoped checks push the (read, workspace) group past the threshold.
|
|
|
|
|
// A member cannot read every workspace in the org, so these are denied.
|
|
|
|
|
for i := 0; i < 45; i++ {
|
|
|
|
|
key := fmt.Sprintf("read-org-workspace-%d", i)
|
|
|
|
|
grouped[key] = codersdk.AuthorizationCheck{
|
|
|
|
|
Object: codersdk.AuthorizationObject{
|
|
|
|
|
ResourceType: codersdk.ResourceWorkspace,
|
|
|
|
|
OrganizationID: adminUser.OrganizationID.String(),
|
|
|
|
|
},
|
|
|
|
|
Action: "read",
|
|
|
|
|
}
|
|
|
|
|
expected[key] = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, expected, map[string]bool(resp))
|
|
|
|
|
})
|
2022-09-23 08:30:32 +10:00
|
|
|
}
|