Repository navigation
feat: implement API key scopes database migration #19861
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
ThomasK33
merged 1 commit into
main
from
thomask33/19845-database-migrations-scopes-array-typed-allow-list
Sep 22, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
coderd/database/migrations/000371_api_key_scopes_array_allow_list.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| -- Recreate single-scope column and collapse arrays | ||
| ALTER TABLE api_keys ADD COLUMN scope api_key_scope DEFAULT 'all'::api_key_scope NOT NULL; | ||
|
|
||
| -- Collapse logic: prefer 'all', else 'application_connect', else 'all' | ||
| UPDATE api_keys SET scope = | ||
| CASE | ||
| WHEN 'all'::api_key_scope = ANY(scopes) THEN 'all'::api_key_scope | ||
| WHEN 'application_connect'::api_key_scope = ANY(scopes) THEN 'application_connect'::api_key_scope | ||
| ELSE 'all'::api_key_scope | ||
| END; | ||
|
|
||
| -- Drop new columns | ||
| ALTER TABLE api_keys DROP COLUMN allow_list; | ||
| ALTER TABLE api_keys DROP COLUMN scopes; | ||
|
|
||
| -- Note: We intentionally keep the expanded enum values to avoid dependency churn. | ||
| -- If strict narrowing is required, create a new type with only ('all','application_connect'), | ||
| -- cast column, drop the new type, and rename. |
157 changes: 157 additions & 0 deletions
157
coderd/database/migrations/000371_api_key_scopes_array_allow_list.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| -- Extend api_key_scope enum with low-level <resource>:<action> values derived from RBACPermissions | ||
| -- Generated via: go run ./scripts/generate_api_key_scope_enum | ||
| -- Begin enum extensions | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'aibridge_interception:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'aibridge_interception:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'aibridge_interception:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'api_key:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'api_key:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'api_key:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'api_key:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_org_role:assign'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_org_role:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_org_role:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_org_role:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_org_role:unassign'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_org_role:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_role:assign'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_role:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'assign_role:unassign'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'audit_log:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'audit_log:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'connection_log:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'connection_log:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'crypto_key:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'crypto_key:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'crypto_key:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'crypto_key:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'debug_info:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'deployment_config:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'deployment_config:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'deployment_stats:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'file:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'file:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'group:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'group:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'group:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'group:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'group_member:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'idpsync_settings:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'idpsync_settings:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'inbox_notification:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'inbox_notification:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'inbox_notification:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'license:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'license:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'license:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_message:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_message:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_message:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_message:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_preference:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_preference:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_template:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'notification_template:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_code_token:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_code_token:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_code_token:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_secret:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_secret:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_secret:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'oauth2_app_secret:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization_member:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization_member:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization_member:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'organization_member:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'prebuilt_workspace:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'prebuilt_workspace:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_daemon:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_daemon:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_daemon:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_daemon:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_jobs:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_jobs:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'provisioner_jobs:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'replicas:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'system:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'system:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'system:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'system:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'tailnet_coordinator:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'tailnet_coordinator:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'tailnet_coordinator:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'tailnet_coordinator:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:use'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'template:view_insights'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'usage_event:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'usage_event:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'usage_event:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user:read_personal'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user:update_personal'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user_secret:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user_secret:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user_secret:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'user_secret:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'webpush_subscription:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'webpush_subscription:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'webpush_subscription:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:application_connect'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:create_agent'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:delete_agent'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:ssh'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:start'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:stop'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_agent_devcontainers:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_agent_resource_monitor:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_agent_resource_monitor:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_agent_resource_monitor:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:application_connect'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:create_agent'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:delete_agent'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:ssh'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:start'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:stop'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_dormant:update'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_proxy:create'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_proxy:delete'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_proxy:read'; | ||
| ALTER TYPE api_key_scope ADD VALUE IF NOT EXISTS 'workspace_proxy:update'; | ||
| -- End enum extensions | ||
|
|
||
| -- Add new columns without defaults; backfill; then enforce NOT NULL | ||
| ALTER TABLE api_keys ADD COLUMN scopes api_key_scope[]; | ||
| ALTER TABLE api_keys ADD COLUMN allow_list text[]; | ||
|
|
||
| -- Backfill existing rows for compatibility | ||
| UPDATE api_keys SET scopes = ARRAY[scope::api_key_scope]; | ||
| UPDATE api_keys SET allow_list = ARRAY['*:*']; | ||
|
|
||
| -- Enforce NOT NULL | ||
| ALTER TABLE api_keys ALTER COLUMN scopes SET NOT NULL; | ||
| ALTER TABLE api_keys ALTER COLUMN allow_list SET NOT NULL; | ||
|
|
||
| -- Drop legacy single-scope column | ||
| ALTER TABLE api_keys DROP COLUMN scope; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.