Skip to content
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

Introduce GuardedPrebuild to be used in #10696 #10940

Merged
merged 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions components/server/src/auth/resource-access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,76 @@ class TestResourceAccess {
teamRole: "owner",
expectation: true,
},
// prebuild
{
name: "prebuild get owner",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: true,
teamRole: undefined,
expectation: true,
},
{
name: "prebuild get other",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: false,
teamRole: undefined,
expectation: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geropl, out of curiosity, what the expected effect of this change for a non-team-member to open a workspace on a configured repository? e.g. is this rule also enforced on workspace startup?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does just introduce the GuardedPrebuild, but it is not used yet.

A follow up PR will introduce prebuild APIs (get/findByWorkspaceId), and it felt wrong to re-/ab-use workspace or workspaceinstance for it.

},
{
name: "prebuild get team member",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: false,
teamRole: "member",
expectation: true,
},
{
name: "prebuild get team owner (same as member)",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: false,
teamRole: "owner",
expectation: true,
},
// prebuild with repo access
{
name: "prebuild get owner",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: true,
teamRole: undefined,
repositoryAccess: true,
expectation: true,
},
{
name: "prebuild get other",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: false,
teamRole: undefined,
repositoryAccess: true,
expectation: true,
},
{
name: "prebuild get team member",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: false,
teamRole: "member",
repositoryAccess: true,
expectation: true,
},
{
name: "prebuild get team owner (same as member)",
resourceKind: "prebuild",
workspaceType: "prebuild",
isOwner: false,
teamRole: "owner",
repositoryAccess: true,
expectation: true,
},
];

for (const t of tests) {
Expand Down
25 changes: 23 additions & 2 deletions components/server/src/auth/resource-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {
CommitContext,
GitpodToken,
PrebuiltWorkspace,
Repository,
Snapshot,
Team,
Expand Down Expand Up @@ -36,7 +37,8 @@ export type GuardedResource =
| GuardedContentBlob
| GuardEnvVar
| GuardedTeam
| GuardedWorkspaceLog;
| GuardedWorkspaceLog
| GuardedPrebuild;

const ALL_GUARDED_RESOURCE_KINDS = new Set<GuardedResourceKind>([
"workspace",
Expand Down Expand Up @@ -119,6 +121,13 @@ export interface GuardedWorkspaceLog {
teamMembers?: TeamMemberInfo[];
}

export interface GuardedPrebuild {
kind: "prebuild";
subject: PrebuiltWorkspace;
workspace: Workspace;
teamMembers?: TeamMemberInfo[];
}

export type ResourceAccessOp = "create" | "update" | "get" | "delete";

export const ResourceAccessGuard = Symbol("ResourceAccessGuard");
Expand Down Expand Up @@ -208,7 +217,17 @@ export class OwnerResourceGuard implements ResourceAccessGuard {
return resource.members.some((m) => m.userId === this.userId && m.role === "owner");
}
case "workspaceLog":
return resource.subject.ownerId === this.userId;
// Owners may do everything, team members can "get"
return (
resource.subject.ownerId === this.userId ||
(operation === "get" && !!resource.teamMembers?.some((m) => m.userId === this.userId))
);
case "prebuild":
// Owners may do everything, team members can "get"
return (
resource.workspace.ownerId === this.userId ||
(operation === "get" && !!resource.teamMembers?.some((m) => m.userId === this.userId))
);
}
}
}
Expand Down Expand Up @@ -477,6 +496,8 @@ export class RepositoryResourceGuard implements ResourceAccessGuard {
case "snapshot":
workspace = resource.workspace;
break;
case "prebuild":
workspace = resource.workspace;
default:
// We do not handle resource kinds here!
return false;
Expand Down