Skip to content

chore(coderd/rbac): add Action{Create,Delete}Agent to ResourceWorkspace #17932

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 2 commits into from
May 20, 2025

Conversation

DanielleMaywood
Copy link
Contributor

This PR adds two new actions: ActionCreateAgent and ActionDeleteAgent. The former is used in this PR for InsertWorkspaceAgent, with the latter to be used in a follow-up PR for Dev Container Agents.

A note has been left in dbauthz.go for InsertWorkspaceAgent detailing why it is allowed to insert a workspace agent when no workspace could be found.

…space`

This PR adds two new actions: `ActionCreateAgent` and
`ActionDeleteAgent`. The former is used in this PR for
`InsertWorkspaceAgent`, with the latter to be used in a follow-up PR for
Dev Container Agents.

A note has been left in `dbauthz.go` for `InsertWorkspaceAgent`
detailing why it is allowed to insert a workspace agent when no
workspace could be found.
@DanielleMaywood DanielleMaywood marked this pull request as ready for review May 20, 2025 10:14
@DanielleMaywood DanielleMaywood requested review from Emyrk and johnstcn and removed request for Emyrk May 20, 2025 10:14
@@ -4016,12 +4032,25 @@ func (s *MethodTestSuite) TestSystemFunctions() {
Returns(slice.New(a, b))
}))
s.Run("InsertWorkspaceAgent", s.Subtest(func(db database.Store, check *expects) {
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
u := dbgen.User(s.T(), db, database.User{})
Copy link
Member

Choose a reason for hiding this comment

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

Can we also add a test to validate:

  • A user in org A cannot create or delete an agent in org B?
  • A user in org A cannot delete an agent owned by a different user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing!

Comment on lines +229 to +237
{
Name: "CreateDeleteWorkspaceAgent",
Actions: []policy.Action{policy.ActionCreateAgent, policy.ActionDeleteAgent},
Resource: rbac.ResourceWorkspace.WithID(workspaceID).InOrg(orgID).WithOwner(currentUser.String()),
AuthorizeMap: map[bool][]hasAuthSubjects{
true: {owner, orgMemberMe, orgAdmin},
false: {setOtherOrg, memberMe, userAdmin, templateAdmin, orgTemplateAdmin, orgUserAdmin, orgAuditor, orgMemberMeBanWorkspace},
},
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@johnstcn Does this not already cover the tests you've suggested?

Copy link
Member

Choose a reason for hiding this comment

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

I think you're right actually!

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Approving, but would like a second opinion from @Emyrk

Comment on lines +3721 to +3723
if err := q.authorizeContext(ctx, policy.ActionCreateAgent, workspace); err != nil {
return database.WorkspaceAgent{}, err
}
Copy link
Member

Choose a reason for hiding this comment

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

If the err is sql.ErrNoRows, then this requires site wide permission. So only owner, system, and provisioner can do it. Is that ok?

What does it mean to insert a workspace agent without a workspace build?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With regards to our current test infrastructure, this happens quite often:

  • A workspace is created
  • A provisioner job is created
  • A workspace resource is created
  • A workspace agent is created
  • Maybe A workspace build is created

Unfortunately there is no way we can link back the resource to the workspace. We could always rewrite these tests but that might be a big change.

Another situation where this goes wrong is with workspace templates:

When attempting to update a workspace template, it will fail the job because it cannot insert a workspace agent. I'm not entirely sure why InsertWorkspaceAgent is invoked for the "Build" template flow but that is an example of where I don't think we have an actual workspace to link back to.

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

My comment is not blocking

@DanielleMaywood DanielleMaywood merged commit 3e7ff9d into main May 20, 2025
36 checks passed
@DanielleMaywood DanielleMaywood deleted the dm-workspace-agent-rbac-actions branch May 20, 2025 20:20
@github-actions github-actions bot locked and limited conversation to collaborators May 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants