mirror of
https://github.com/coder/coder.git
synced 2026-10-30 18:12:29 +08:00
Adds group and user ACLs to org-scoped MCP server configs so
organizations can restrict specific MCP servers to subsets of members,
mirroring the template ACL pattern.
## Summary
- Migration adds `group_acl`/`user_acl` JSONB columns (nested `{"<id>":
{"permissions": [...]}}` shape) and seeds every existing config with its
organization's Everyone group read entry (the Everyone group ID equals
the org ID), so member access is unchanged by default. Creation seeds
the same entry.
- The blanket org-member read grant from the base PR is replaced by ACL
evaluation: Rego requires org membership for every ACL grant, and
`GetAuthorizedMCPServerConfigs` compiles ACL-aware SQL filters.
- New `ActionShare` (org admins) gates `GET|PATCH
/api/experimental/organizations/{organization}/mcp-servers/{mcpserverconfig}/acl`
(nested under the organization like the rest of the config surface).
PATCH validates principals against the config's organization, merges
sparse updates under a row lock, stamps `updated_by`/`updated_at`, and
is audited as a Write with `Old` captured before authorization. A config
deleted concurrently between the middleware fetch and the locked
re-fetch is concealed as 404, matching the update and delete handlers.
ACL columns are tracked in the audit table.
- ACL management is available in all editions (no enterprise
entitlement), documented in the MCP servers page. Revoking an ACL does
not retro-strip already-selected configs from existing chats; new
selection is blocked at chat create.
- Force On respects the ACL: the forced set is loaded as the chat owner,
so a `force_on` server whose ACL denies the owner never attaches at
create, send, or generation time.
- No rolling-upgrade machinery: upgrades run in scheduled maintenance
downtime, so the migration only backfills existing rows; the API sets
the Everyone read grant explicitly on every insert.
Stacked on #27943. Part of the MCP org-separation stack.
Closes https://linear.app/codercom/issue/CODAGT-712
UAT: verified on a dogfood instance with two members and a custom group:
Everyone-seed default visibility, group grant with Everyone removal
(non-member loses list/fetch/selection), foreign-principal rejection,
non-admin share denial (audited 403 / concealed 404), user_acl restore,
and audited ACL diffs.
> Mux (AI agent) authored this PR on Mike's behalf.
<!-- mux-attribution: model=claude-fable-5 thinking=high -->
131 lines
4.2 KiB
Go
131 lines
4.2 KiB
Go
package acl
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type UpdateValidator[Role codersdk.WorkspaceRole | codersdk.TemplateRole | codersdk.ChatRole | codersdk.MCPServerConfigRole] interface {
|
|
// Users should return a map from user UUIDs (as strings) to the role they
|
|
// are being assigned. Additionally, it should return a string that will be
|
|
// used as the field name for the ValidationErrors returned from Validate.
|
|
Users() (map[string]Role, string)
|
|
// Groups should return a map from group UUIDs (as strings) to the role they
|
|
// are being assigned. Additionally, it should return a string that will be
|
|
// used as the field name for the ValidationErrors returned from Validate.
|
|
Groups() (map[string]Role, string)
|
|
// ValidateRole should return an error that will be used in the
|
|
// ValidationError if the role is invalid for the corresponding resource type.
|
|
ValidateRole(role Role) error
|
|
}
|
|
|
|
func Validate[Role codersdk.WorkspaceRole | codersdk.TemplateRole | codersdk.ChatRole | codersdk.MCPServerConfigRole](
|
|
ctx context.Context,
|
|
db database.Store,
|
|
v UpdateValidator[Role],
|
|
) []codersdk.ValidationError {
|
|
// nolint:gocritic // Validate requires full read access to users and groups
|
|
ctx = dbauthz.AsSystemRestricted(ctx)
|
|
var validErrs []codersdk.ValidationError
|
|
|
|
groupRoles, groupsField := v.Groups()
|
|
groupIDs := make([]uuid.UUID, 0, len(groupRoles))
|
|
for idStr, role := range groupRoles {
|
|
// Validate the provided role names
|
|
if err := v.ValidateRole(role); err != nil {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: groupsField,
|
|
Detail: err.Error(),
|
|
})
|
|
}
|
|
// Validate that the IDs are UUIDs
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: groupsField,
|
|
Detail: fmt.Sprintf("%v is not a valid UUID.", idStr),
|
|
})
|
|
continue
|
|
}
|
|
// Don't check if the ID exists when setting the role to
|
|
// WorkspaceRoleDeleted or TemplateRoleDeleted. They might've existing at
|
|
// some point and got deleted. If we report that as an error here then they
|
|
// can't be removed.
|
|
if string(role) == "" {
|
|
continue
|
|
}
|
|
groupIDs = append(groupIDs, id)
|
|
}
|
|
|
|
// Validate that the groups exist
|
|
groupValidation, err := db.ValidateGroupIDs(ctx, groupIDs)
|
|
if err != nil {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: groupsField,
|
|
Detail: fmt.Sprintf("failed to validate group IDs: %v", err.Error()),
|
|
})
|
|
}
|
|
if !groupValidation.Ok {
|
|
for _, id := range groupValidation.InvalidGroupIds {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: groupsField,
|
|
Detail: fmt.Sprintf("group with ID %v does not exist", id),
|
|
})
|
|
}
|
|
}
|
|
|
|
userRoles, usersField := v.Users()
|
|
userIDs := make([]uuid.UUID, 0, len(userRoles))
|
|
for idStr, role := range userRoles {
|
|
// Validate the provided role names
|
|
if err := v.ValidateRole(role); err != nil {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: usersField,
|
|
Detail: err.Error(),
|
|
})
|
|
}
|
|
// Validate that the IDs are UUIDs
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: usersField,
|
|
Detail: fmt.Sprintf("%v is not a valid UUID.", idStr),
|
|
})
|
|
continue
|
|
}
|
|
// Don't check if the ID exists when setting the role to
|
|
// WorkspaceRoleDeleted or TemplateRoleDeleted. They might've existing at
|
|
// some point and got deleted. If we report that as an error here then they
|
|
// can't be removed.
|
|
if string(role) == "" {
|
|
continue
|
|
}
|
|
userIDs = append(userIDs, id)
|
|
}
|
|
|
|
// Validate that the groups exist
|
|
userValidation, err := db.ValidateUserIDs(ctx, userIDs)
|
|
if err != nil {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: usersField,
|
|
Detail: fmt.Sprintf("failed to validate user IDs: %v", err.Error()),
|
|
})
|
|
}
|
|
if !userValidation.Ok {
|
|
for _, id := range userValidation.InvalidUserIds {
|
|
validErrs = append(validErrs, codersdk.ValidationError{
|
|
Field: usersField,
|
|
Detail: fmt.Sprintf("user with ID %v does not exist", id),
|
|
})
|
|
}
|
|
}
|
|
|
|
return validErrs
|
|
}
|