Moves the chat model ACL handlers onto the organization ACL helpers in `coderd/mcp_acl.go`, which MCP server configs and organization skills already share. It deletes the model copies of the user and group resolvers, the member count query, the list of available principals, the organization and duplicate ID checks, and the role update loop: 30 lines added, 321 removed. Two shared helpers change: - `resolveACLUsers` and `resolveACLGroups` pass each stored ACL entry to the callback, so the model ACL reads each role from it. - `resolveACLGroups` takes an organization ID. The model ACL passes its organization, so groups from other organizations stay hidden. MCP server configs and organization skills pass `uuid.Nil`, which keeps every organization, as today. **What changes:** successful responses stay the same. On error paths, a stored key that is not a UUID is skipped without a warning log (`PATCH` validation rejects such keys), `500` details use the shared texts (`get ACL users` instead of `hydrate chat model ACL users`), duplicate ID errors come back in the same `400` as the other validation errors because the check now runs inside the write transaction, and a failed membership lookup skips the group check. **Why it is safe:** `TestChatModelACL` checks that groups from other organizations stay hidden, and `TestChatModelACLValidationIsAtomic` covers the duplicate check. I ran 9 mutants of the changed lines with `go test -overlay`: ignore the organization in `resolveACLGroups` or pass `uuid.Nil` from each handler, drop the stored entry given to either callback, report zero members, and apply group roles to users or user roles to groups. The tests fail on each. **Review focus:** `uuid.Nil` means every organization in `resolveACLGroups`, as it does in `GetGroups`. Validation: on `main` and on this branch, `go test ./coderd -run '(?i)acl|mcp|skill' -count=1` passes (24 tests, 62 with subtests) and `go test ./enterprise/coderd -run '(?i)acl|mcp|skill|TestEnterpriseAuditLogs' -count=1` passes (9 tests, 46 with subtests). The pre-commit hook passed (`make gen`, formatting, golangci-lint, slim build). ## Stack This stack moves the chat, chat project, and chat model ACL handlers onto the organization ACL helpers that MCP server configs and organization skills share, and deletes their copies. Successful ACL responses do not change. 1. #31014 Pin member counts and user details in chat and project ACL reads 2. #31015 Move chat model ACL handlers onto the shared ACL helpers 3. #31016 Move chat and chat project ACL reads onto the shared helpers > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.
215 lines
7.1 KiB
Go
215 lines
7.1 KiB
Go
package coderd
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"maps"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/audit"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/coderd/httpmw"
|
|
"github.com/coder/coder/v2/coderd/pubsub"
|
|
"github.com/coder/coder/v2/coderd/rbac/acl"
|
|
"github.com/coder/coder/v2/coderd/rbac/policy"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// @Summary Get an AI model ACL
|
|
// @ID get-an-ai-model-acl
|
|
// @Security CoderSessionToken
|
|
// @Tags Chats
|
|
// @Produce json
|
|
// @Param organization path string true "Organization name or ID"
|
|
// @Param model path string true "Model ID" format(uuid)
|
|
// @Success 200 {object} codersdk.ChatModelACL
|
|
// @Router /api/v2/organizations/{organization}/chats/models/{model}/acl [get]
|
|
// @x-apidocgen {"skip": true}
|
|
func (api *API) chatModelConfigACLHandler(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
config := httpmw.ChatModelConfigParam(r)
|
|
if !api.Authorize(r, policy.ActionShare, chatModelConfigRBACObject(config)) {
|
|
httpapi.ResourceNotFound(rw)
|
|
return
|
|
}
|
|
|
|
users, err := resolveACLUsers(ctx, api.Database, config.UserACL, chatACLUser)
|
|
if err != nil {
|
|
httpapi.InternalServerError(rw, err)
|
|
return
|
|
}
|
|
groups, err := resolveACLGroups(ctx, api.Database, config.OrganizationID, config.GroupACL, chatACLGroup)
|
|
if err != nil {
|
|
httpapi.InternalServerError(rw, err)
|
|
return
|
|
}
|
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.ChatModelACL{
|
|
Users: users,
|
|
Groups: groups,
|
|
})
|
|
}
|
|
|
|
// @Summary Get available AI model ACL users and groups
|
|
// @ID get-available-ai-model-acl-users-and-groups
|
|
// @Security CoderSessionToken
|
|
// @Tags Chats
|
|
// @Produce json
|
|
// @Param organization path string true "Organization name or ID"
|
|
// @Param model path string true "Model ID" format(uuid)
|
|
// @Param q query string false "User search query; free-text search also applies to groups"
|
|
// @Param after_id query string false "User after ID" format(uuid)
|
|
// @Param limit query int false "Page limit for users and groups, if 0 returns all candidates"
|
|
// @Param offset query int false "User page offset"
|
|
// @Success 200 {object} codersdk.ACLAvailable
|
|
// @Router /api/v2/organizations/{organization}/chats/models/{model}/acl/available [get]
|
|
// @x-apidocgen {"skip": true}
|
|
func (api *API) chatModelConfigACLAvailable(rw http.ResponseWriter, r *http.Request) {
|
|
config := httpmw.ChatModelConfigParam(r)
|
|
if !api.Authorize(r, policy.ActionShare, chatModelConfigRBACObject(config)) {
|
|
httpapi.ResourceNotFound(rw)
|
|
return
|
|
}
|
|
|
|
api.writeOrganizationACLAvailable(rw, r, config.OrganizationID)
|
|
}
|
|
|
|
type chatModelACLValidationError struct {
|
|
Validations []codersdk.ValidationError
|
|
}
|
|
|
|
func (*chatModelACLValidationError) Error() string {
|
|
return "invalid chat model ACL"
|
|
}
|
|
|
|
// @Summary Update an AI model ACL
|
|
// @ID update-an-ai-model-acl
|
|
// @Security CoderSessionToken
|
|
// @Tags Chats
|
|
// @Accept json
|
|
// @Param organization path string true "Organization name or ID"
|
|
// @Param model path string true "Model ID" format(uuid)
|
|
// @Param request body codersdk.UpdateChatModelACLRequest true "Sparse model ACL update"
|
|
// @Success 204
|
|
// @Router /api/v2/organizations/{organization}/chats/models/{model}/acl [patch]
|
|
// @x-apidocgen {"skip": true}
|
|
func (api *API) updateChatModelConfigACL(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
apiKey := httpmw.APIKey(r)
|
|
config := httpmw.ChatModelConfigParam(r)
|
|
if !api.Authorize(r, policy.ActionShare, chatModelConfigRBACObject(config)) {
|
|
httpapi.ResourceNotFound(rw)
|
|
return
|
|
}
|
|
|
|
auditor := api.Auditor.Load()
|
|
aReq, commitAudit := audit.InitRequest[database.ChatModelConfig](rw, &audit.RequestParams{
|
|
Audit: *auditor,
|
|
Log: api.Logger,
|
|
Request: r,
|
|
Action: database.AuditActionWrite,
|
|
OrganizationID: config.OrganizationID,
|
|
})
|
|
defer commitAudit()
|
|
aReq.Old = config
|
|
|
|
var req codersdk.UpdateChatModelACLRequest
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
|
return
|
|
}
|
|
|
|
var updated database.ChatModelConfig
|
|
err := api.inChatModelConfigWriteTx(ctx, config.OrganizationID, func(tx database.Store) error {
|
|
//nolint:gocritic // The ACL write below reauthorizes the locked row for share.
|
|
current, err := tx.GetChatModelConfigByID(dbauthz.AsSystemRestricted(ctx), config.ID)
|
|
if err != nil {
|
|
return xerrors.Errorf("get chat model config for ACL update: %w", err)
|
|
}
|
|
aReq.Old = current
|
|
|
|
userRoles, groupRoles, validations := validateOrganizationACLUpdate(ctx, tx, config.OrganizationID, ChatModelACLUpdateValidator(req))
|
|
if len(validations) > 0 {
|
|
return &chatModelACLValidationError{Validations: validations}
|
|
}
|
|
|
|
userACL := maps.Clone(current.UserACL)
|
|
groupACL := maps.Clone(current.GroupACL)
|
|
applyACLReadRoles(userACL, userRoles)
|
|
applyACLReadRoles(groupACL, groupRoles)
|
|
|
|
updated, err = tx.UpdateChatModelConfigACLByID(ctx, database.UpdateChatModelConfigACLByIDParams{
|
|
ID: config.ID,
|
|
UserACL: userACL,
|
|
GroupACL: groupACL,
|
|
UpdatedBy: uuid.NullUUID{UUID: apiKey.UserID, Valid: apiKey.UserID != uuid.Nil},
|
|
})
|
|
return err
|
|
})
|
|
if err != nil {
|
|
var validationErr *chatModelACLValidationError
|
|
switch {
|
|
case errors.As(err, &validationErr):
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Invalid request to update chat model ACL.",
|
|
Validations: validationErr.Validations,
|
|
})
|
|
case dbauthz.IsNotAuthorizedError(err):
|
|
httpapi.Forbidden(rw)
|
|
case xerrors.Is(err, sql.ErrNoRows), xerrors.Is(err, errChatModelConfigNotFound), httpapi.Is404Error(err):
|
|
httpapi.ResourceNotFound(rw)
|
|
default:
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
Message: "Failed to update chat model ACL.",
|
|
Detail: err.Error(),
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
aReq.New = updated
|
|
publishChatConfigEvent(api.Logger, api.Pubsub, pubsub.ChatConfigEventModelConfig, updated.ID)
|
|
rw.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func reducedUserFromPaginatedOrganizationMember(member database.PaginatedOrganizationMembersRow) codersdk.ReducedUser {
|
|
return codersdk.ReducedUser{
|
|
MinimalUser: codersdk.MinimalUser{
|
|
ID: member.OrganizationMember.UserID,
|
|
Username: member.Username,
|
|
Name: member.Name,
|
|
AvatarURL: member.AvatarURL,
|
|
},
|
|
Email: member.Email,
|
|
CreatedAt: member.UserCreatedAt,
|
|
UpdatedAt: member.UserUpdatedAt,
|
|
LastSeenAt: member.LastSeenAt,
|
|
Status: codersdk.UserStatus(member.Status),
|
|
LoginType: codersdk.LoginType(member.LoginType),
|
|
IsServiceAccount: member.IsServiceAccount,
|
|
}
|
|
}
|
|
|
|
type ChatModelACLUpdateValidator codersdk.UpdateChatModelACLRequest
|
|
|
|
var _ acl.UpdateValidator[codersdk.ChatRole] = ChatModelACLUpdateValidator{}
|
|
|
|
func (c ChatModelACLUpdateValidator) Users() (map[string]codersdk.ChatRole, string) {
|
|
return c.UserRoles, "user_roles"
|
|
}
|
|
|
|
func (c ChatModelACLUpdateValidator) Groups() (map[string]codersdk.ChatRole, string) {
|
|
return c.GroupRoles, "group_roles"
|
|
}
|
|
|
|
func (ChatModelACLUpdateValidator) ValidateRole(role codersdk.ChatRole) error {
|
|
if role == codersdk.ChatRoleRead || role == codersdk.ChatRoleDeleted {
|
|
return nil
|
|
}
|
|
return xerrors.Errorf("role %q is not a valid chat model role", role)
|
|
}
|