> [!NOTE] > This PR was generated by Coder Agents on behalf of @ksrinija. ## Ticket Link Fixes [CORE-780](https://linear.app/codercom/issue/CORE-780/bug-list-endpoints-return-500-column-reference-id-is-ambiguous-for-api) ## Description List endpoints returned 500 with `pq: column reference "id" is ambiguous` for API keys whose scope has an ID-scoped allow list (for example `--allow template:<uuid>`). The SQL authorization filters for workspaces, templates, audit logs and connection logs used the shared `resourceIDMatcher()`, which emits an unqualified `id`. Their list and count queries join other tables that also have an `id` column. The ID check is only generated for specific IDs, so wildcard allow lists (`template:*`) and tokens without allow lists were not affected. This PR qualifies the column in each affected converter in `coderd/rbac/regosql/configs.go`: `workspaces.id`, `t.id`, `audit_logs.id` and `connection_logs.id`. This follows the existing pattern used by `chatResourceIDMatcher()` (`chats_expanded.id`) and `ChatModelConfigConverter` (`cmc.id`). <details> <summary>Decision log</summary> - **Inline qualified matchers instead of changing `resourceIDMatcher()`.** Other converters share the helper and query different tables and aliases, so no single prefix inside it is correct for all of them. - **Remaining converters checked; none affected.** `UserConverter`, `MCPServerConfigConverter` and `OrganizationSkillConverter` filter single-table queries. `AIBridgeInterceptionConverter`'s queries either read one table or join subqueries that do not expose an `id` column. `NoACLConverter` and `DefaultVariableConverter` are only used in tests. This was checked against the query SQL, not run against Postgres. - **`AIBridgeInterceptionConverter` must keep a bare `id`.** Its queries refer to the table both as `aibridge_interceptions` and by the alias `ai`, so qualifying it with either name would break the others. - **Possible follow-up, not in scope:** change `resourceIDMatcher()` to take a column argument so each converter names its column explicitly. Pending team discussion. - **Tests:** string-match cases in `compile_test.go` document the exact SQL each converter generates, and need no database fixtures. A Postgres-backed test (`TestAllowListIDScopedListEndpoints`) also runs all 4 affected queries, so a later alias or join change that breaks the qualified column is caught. Both were kept after review discussion. </details> ## Testing Done - **SQL generation tests:** added `WorkspaceAllowList`, `TemplateAllowList`, `AuditLogAllowList` and `ConnectionLogAllowList` to `TestRegoQueries` in `coderd/rbac/regosql/compile_test.go`. Each compiles an ID-scoped allow list with the affected converter and checks the exact generated SQL. - **Postgres regression test:** added `TestAllowListIDScopedListEndpoints` in `coderd/database/dbauthz/allowlist_test.go`. It runs `GetWorkspaces`, `GetTemplatesWithFilter`, `GetAuditLogsOffset` and `GetConnectionLogsOffset` against Postgres with the real RBAC authorizer and an allow list naming one row of each type. Each subtest checks that the query succeeds and returns only the allow-listed row, out of two. This catches future alias or join changes that the string tests cannot. - **Confirmed both tests catch the bug:** with `configs.go` reverted to `main`, all 4 `TestRegoQueries` cases fail because the generated SQL contains the bare `id`, and all 4 Postgres subtests fail with `pq: column reference "id" is ambiguous`. With the fix, all pass. - **Existing tests:** `go test ./coderd/rbac/... ./coderd/database/dbauthz/...` passes. - **Lint:** the repo's pre-commit checks (`make pre-commit`, including `golangci-lint`) pass. - **CI:** <fill in once checks finish on `e64c7f69c9`>
102 lines
3.7 KiB
Go
102 lines
3.7 KiB
Go
package dbauthz_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cdr.dev/slog/v3/sloggers/slogtest"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/database/dbfake"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
// TestAllowListIDScopedListEndpoints ensures list queries compiled from an
|
|
// ID-scoped allow list produce valid SQL. The queries join tables that also
|
|
// have an "id" column, so the ID matcher must be table qualified.
|
|
func TestAllowListIDScopedListEndpoints(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
store, _ := dbtestutil.NewDB(t)
|
|
org := dbgen.Organization(t, store, database.Organization{})
|
|
user := dbgen.User(t, store, database.User{})
|
|
// Each build creates its own template, so the allow list below admits one
|
|
// workspace and one template and excludes the others.
|
|
ws := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{
|
|
OrganizationID: org.ID,
|
|
OwnerID: user.ID,
|
|
}).Do().Workspace
|
|
_ = dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{
|
|
OrganizationID: org.ID,
|
|
OwnerID: user.ID,
|
|
}).Do()
|
|
auditLog := dbgen.AuditLog(t, store, database.AuditLog{OrganizationID: org.ID, UserID: user.ID})
|
|
_ = dbgen.AuditLog(t, store, database.AuditLog{OrganizationID: org.ID, UserID: user.ID})
|
|
connLogParams := database.UpsertConnectionLogParams{
|
|
OrganizationID: org.ID,
|
|
WorkspaceOwnerID: user.ID,
|
|
WorkspaceID: ws.ID,
|
|
}
|
|
connLog := dbgen.ConnectionLog(t, store, connLogParams)
|
|
_ = dbgen.ConnectionLog(t, store, connLogParams)
|
|
|
|
scope, err := rbac.ScopeAll.Expand()
|
|
require.NoError(t, err)
|
|
scope.AllowIDList = []rbac.AllowListElement{
|
|
{Type: rbac.ResourceWorkspace.Type, ID: ws.ID.String()},
|
|
{Type: rbac.ResourceTemplate.Type, ID: ws.TemplateID.String()},
|
|
{Type: rbac.ResourceAuditLog.Type, ID: auditLog.ID.String()},
|
|
{Type: rbac.ResourceConnectionLog.Type, ID: connLog.ID.String()},
|
|
}
|
|
|
|
db := dbauthz.New(store, rbac.NewAuthorizer(prometheus.NewRegistry()), slogtest.Make(t, nil), coderdtest.AccessControlStorePointer())
|
|
subject := rbac.Subject{
|
|
ID: user.ID.String(),
|
|
Roles: rbac.Roles(must(rbac.RoleIdentifiers{rbac.RoleOwner()}.Expand())),
|
|
Groups: []string{},
|
|
Scope: scope,
|
|
}
|
|
|
|
t.Run("GetWorkspaces", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := dbauthz.As(testutil.Context(t, testutil.WaitMedium), subject)
|
|
rows, err := db.GetWorkspaces(ctx, database.GetWorkspacesParams{})
|
|
require.NoError(t, err)
|
|
require.Len(t, rows, 1)
|
|
require.Equal(t, ws.ID, rows[0].ID)
|
|
})
|
|
|
|
t.Run("GetTemplatesWithFilter", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := dbauthz.As(testutil.Context(t, testutil.WaitMedium), subject)
|
|
rows, err := db.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{})
|
|
require.NoError(t, err)
|
|
require.Len(t, rows, 1)
|
|
require.Equal(t, ws.TemplateID, rows[0].ID)
|
|
})
|
|
|
|
t.Run("GetAuditLogsOffset", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := dbauthz.As(testutil.Context(t, testutil.WaitMedium), subject)
|
|
rows, err := db.GetAuditLogsOffset(ctx, database.GetAuditLogsOffsetParams{})
|
|
require.NoError(t, err)
|
|
require.Len(t, rows, 1)
|
|
require.Equal(t, auditLog.ID, rows[0].AuditLog.ID)
|
|
})
|
|
|
|
t.Run("GetConnectionLogsOffset", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := dbauthz.As(testutil.Context(t, testutil.WaitMedium), subject)
|
|
rows, err := db.GetConnectionLogsOffset(ctx, database.GetConnectionLogsOffsetParams{})
|
|
require.NoError(t, err)
|
|
require.Len(t, rows, 1)
|
|
require.Equal(t, connLog.ID, rows[0].ConnectionLog.ID)
|
|
})
|
|
}
|