Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add API key scope to restrict access to user data#17692

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

ThomasK33
Copy link
Member

@ThomasK33ThomasK33 commentedMay 6, 2025
edited
Loading

Part of#17649

Related to:coder/terraform-provider-coder#391


Add API Key Scope Restriction for Workspace Agents

This PR adds a new security feature that allows workspace agents to have restricted API key scopes. It introduces a newapi_key_scope field to workspace agents with two possible values:

  • all: Full access to all endpoints (existing behavior)
  • no_user_data: Restricts access to user-specific data endpoints

The implementation includes:

  1. A new database migration that adds theapi_key_scope column to theworkspace_agents table
  2. Updated middleware to extract workspace agent information and check API key scope
  3. Support for specifying the scope in provisioners via theapi_key_scope attribute

The scope restrictions have been applied to several endpoints that expose user data:

  • /external-auth endpoints
  • Git SSH key endpoints

This change allows administrators to create workspaces with agents that have limited access to sensitive user data, improving security in multi-agent environments where some agents may need to be more restricted than others.

@ThomasK33Graphite App
Copy link
MemberAuthor

ThomasK33 commentedMay 6, 2025
edited
Loading

@ThomasK33ThomasK33 changed the titlefeat(agent): add API key scope to restrict access to user datafeat: add API key scope to restrict access to user dataMay 6, 2025
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch from6ee1c64 tofa4810bCompareMay 6, 2025 16:17
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch from5ffda26 tof43c610CompareMay 6, 2025 16:18
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch fromfa4810b to51a0361CompareMay 6, 2025 16:18
@ThomasK33ThomasK33 requested a review fromCopilotMay 6, 2025 16:19
Copy link
Contributor

@CopilotCopilotAI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds the new API key scope feature for workspace agents to restrict access to user-specific data. Key changes include:

  • Adding an api_key_scope field in the Agent proto, database model, and terraform resource.
  • Integrating the new field into middleware (AgentAPIKeyScopeCheckMW) for access enforcement.
  • Updating audit logs and documentation to reflect the new API key scope attribute.

Reviewed Changes

Copilot reviewed 15 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
site/e2e/provisionerGenerated.tsAdds the apiKeyScope field in the Agent interface and updates its encoding.
provisionersdk/proto/provisioner.protoIntroduces the new api_key_scope field with field number 26.
provisioner/terraform/resources.goUpdates agentAttributes and ConvertState to include APIKeyScope and improves slice allocation.
enterprise/audit/table.go & docs/admin/security/audit-logs.mdUpdates audit log configurations to include the api_key_scope field.
coderd/...Integrates api_key_scope handling in database queries, models, middleware, and tests, plus a minor improvement in tz_darwin.go using strings.ReplaceAll.
Files not reviewed (4)
  • coderd/database/dump.sql: Language not supported
  • coderd/database/migrations/000320_add_api_key_scope_to_workspace_agents.down.sql: Language not supported
  • coderd/database/migrations/000320_add_api_key_scope_to_workspace_agents.up.sql: Language not supported
  • coderd/database/queries/workspaceagents.sql: Language not supported

@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch from51a0361 tof685750CompareMay 6, 2025 19:07
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch fromf43c610 to4fe32d3CompareMay 6, 2025 19:40
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch 2 times, most recently from39814d8 to07de6c8CompareMay 7, 2025 09:42
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch 2 times, most recently from5734989 to937e55aCompareMay 7, 2025 09:45
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch 2 times, most recently from67f2973 tod9b3dbcCompareMay 7, 2025 09:56
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch from937e55a to4deeaf3CompareMay 7, 2025 09:56
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch fromd9b3dbc to9220437CompareMay 7, 2025 11:19
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch from4deeaf3 to5582625CompareMay 7, 2025 12:34
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch 2 times, most recently from171c263 toafa0215CompareMay 7, 2025 13:37
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch from5582625 to6cc32e3CompareMay 7, 2025 13:37
@ThomasK33ThomasK33 marked this pull request as ready for reviewMay 7, 2025 13:58
@ThomasK33ThomasK33 requested a review fromEmyrkMay 7, 2025 14:07
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch fromafa0215 to795b6b5CompareMay 7, 2025 19:36
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication branch from6cc32e3 tobc33d16CompareMay 7, 2025 19:36
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch from795b6b5 tod0766e9CompareMay 7, 2025 19:46
@ThomasK33ThomasK33 changed the base branch fromthomask33/05-06-feat_mcp_add_support_for_running_mcp_server_without_user_authentication tographite-base/17692May 7, 2025 19:53
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch fromd0766e9 to2d58a59CompareMay 7, 2025 19:53
@graphite-appgraphite-appbot changed the base branch fromgraphite-base/17692 tomainMay 7, 2025 19:53
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch 2 times, most recently from7440aa8 to2bdc470CompareMay 7, 2025 20:01
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch 2 times, most recently from57f1735 to50943c1CompareMay 7, 2025 22:47
spikecurtis
spikecurtis previously requested changesMay 8, 2025
Copy link
Contributor

@spikecurtisspikecurtis left a comment

Choose a reason for hiding this comment

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

Needs to bump the provisionerd API version, since it adds a new (backward compatible) field.

@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch from50943c1 to31e4751CompareMay 8, 2025 07:51
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch from31e4751 toc453560CompareMay 8, 2025 07:59
@spikecurtisspikecurtis dismissed theirstale reviewMay 8, 2025 09:43

fixed; but I haven't reviewed the whole thing.

@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch fromc453560 tobc9cfb8CompareMay 8, 2025 19:55
@graphite-appGraphite App
Copy link

graphite-appbot commentedMay 8, 2025
edited
Loading

Merge activity

  • May 8, 6:14 PM EDT:Graphite disabled "merge when ready" on this PR due to: a merge conflict with the target branch; resolve the conflict and try again..
  • May 12, 10:33 AM EDT:Graphite disabled "merge when ready" on this PR due to: a merge conflict with the target branch; resolve the conflict and try again..

@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch frombc9cfb8 toefbe50aCompareMay 9, 2025 07:52
@ThomasK33ThomasK33 requested a review fromEmyrkMay 12, 2025 09:31
Copy link
Member

@johnstcnjohnstcn left a comment

Choose a reason for hiding this comment

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

I would like to see some additional tests around the RBAC scope, but I don't need to review again after that.

Comment on lines 16 to 17
// API v1.5:
// - Add new field named `api_key_scope` in the Agent.
Copy link
Member

Choose a reason for hiding this comment

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

heads-up: there are a number of in-flight PRs that will be bumping this field.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Since the 1.5 version has yet to be released, I don't think I need to bump it again (?).

Copy link
Member

Choose a reason for hiding this comment

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

No, just giving a heads-up of a potential need to rebase.

ThomasK33 reacted with thumbs up emoji
Copy link
MemberAuthor

@ThomasK33ThomasK33May 15, 2025
edited
Loading

Choose a reason for hiding this comment

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

I had to rebase anyway since six new migrations happened, and new fields were added to the agent, too.

Role: Role{
Identifier: RoleIdentifier{Name: fmt.Sprintf("Scope_%s", ScopeNoUserData)},
DisplayName: "Scope without access to user data",
Site: allPermsExcept(ResourceUser),
Copy link
Member

Choose a reason for hiding this comment

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

Thisseems like the right way to do this if I recall correctly but it does make me nervous that we're essentially saying "this scope can do anything at site level except read user data". I'd like to see a test case added that confirms that this scope can't do things that the actual auth subject can't do.

Copy link
Member

Choose a reason for hiding this comment

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

It is an intersection with the roles.

The default scope we use is*:

ScopeAll: {
Role:Role{
Identifier:RoleIdentifier{Name:fmt.Sprintf("Scope_%s",ScopeAll)},
DisplayName:"All operations",
Site:Permissions(map[string][]policy.Action{
ResourceWildcard.Type: {policy.WildcardSymbol},
}),
Org:map[string][]Permission{},
User: []Permission{},
},
AllowIDList: []string{policy.WildcardSymbol},
},

johnstcn reacted with thumbs up emoji
…ut user data accessChange-Id: Ia5a7085afea6ad6ab7fdba2ab738357f4c519966Signed-off-by: Thomas Kosiewski <tk@coder.com>
@ThomasK33ThomasK33force-pushed thethomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branch fromefbe50a tode14cfaCompareMay 15, 2025 08:44
Copy link
Member

@EmyrkEmyrk left a comment

Choose a reason for hiding this comment

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

LG, really like seeing the scopes have some utility!

Comment on lines +805 to +808
workspaceAgentInfo := httpmw.ExtractWorkspaceAgentAndLatestBuild(httpmw.ExtractWorkspaceAgentAndLatestBuildConfig{
DB: options.Database,
Optional: false,
})
Copy link
Member

Choose a reason for hiding this comment

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

If this is only used once, we can probably go back to doing it inline, rather than declaring it up here.

Just a nit

Comment on lines +1056 to +1113

meID := uuid.New()
user = Subject{
ID: meID.String(),
Roles: Roles{
must(RoleByName(RoleMember())),
must(RoleByName(ScopedRoleOrgMember(defOrg))),
},
Scope: must(ScopeNoUserData.Expand()),
}

// Test 1: Verify that no_user_data scope prevents accessing user data
testAuthorize(t, "ReadPersonalUser", user,
cases(func(c authTestCase) authTestCase {
c.actions = ResourceUser.AvailableActions()
c.allow = false
c.resource.ID = meID.String()
return c
}, []authTestCase{
{resource: ResourceUser.WithOwner(meID.String()).InOrg(defOrg).WithID(meID)},
}),
)

// Test 2: Verify token can still perform regular member actions that don't involve user data
testAuthorize(t, "NoUserData_CanStillUseRegularPermissions", user,
// Test workspace access - should still work
cases(func(c authTestCase) authTestCase {
c.actions = []policy.Action{policy.ActionRead}
c.allow = true
return c
}, []authTestCase{
// Can still read owned workspaces
{resource: ResourceWorkspace.InOrg(defOrg).WithOwner(user.ID)},
}),
// Test workspace create - should still work
cases(func(c authTestCase) authTestCase {
c.actions = []policy.Action{policy.ActionCreate}
c.allow = true
return c
}, []authTestCase{
// Can still create workspaces
{resource: ResourceWorkspace.InOrg(defOrg).WithOwner(user.ID)},
}),
)

// Test 3: Verify token cannot perform actions outside of member role
testAuthorize(t, "NoUserData_CannotExceedMemberRole", user,
cases(func(c authTestCase) authTestCase {
c.actions = []policy.Action{policy.ActionRead, policy.ActionUpdate, policy.ActionDelete}
c.allow = false
return c
}, []authTestCase{
// Cannot access other users' workspaces
{resource: ResourceWorkspace.InOrg(defOrg).WithOwner("other-user")},
// Cannot access admin resources
{resource: ResourceOrganization.WithID(defOrg)},
}),
)
Copy link
Member

Choose a reason for hiding this comment

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

This is great! Thank you! 👍

@ThomasK33ThomasK33 merged commit1bacd82 intomainMay 15, 2025
40 checks passed
@ThomasK33ThomasK33 deleted the thomask33/05-06-feat_add_api_key_scope_for_workspace_agents_to_support_running_without_user_data_access branchMay 15, 2025 14:32
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsMay 15, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@johnstcnjohnstcnjohnstcn approved these changes

Copilot code reviewCopilotCopilot left review comments

@EmyrkEmyrkEmyrk approved these changes

@spikecurtisspikecurtisAwaiting requested review from spikecurtisspikecurtis is a code owner

Assignees

@ThomasK33ThomasK33

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@ThomasK33@johnstcn@spikecurtis@Emyrk

[8]ページ先頭

©2009-2025 Movatter.jp