Files
39b359a70e chore: keep db partner roles out of picker (#54019)
## Problem

Newly added trusted partner roles appear as user selectable /
manageable. Resolves DEPR-735.

## Solution

Trusted partner roles should not be selectable in temporary access. And
should not appear as modifiable in the database roles tab.

| Before | After |
| --- | --- |
| <img width="553" height="398" alt="CleanShot 2026-11-27 at 12 24 43"
src="https://github.com/user-attachments/assets/14affc16-2f00-496f-a124-d918acd93bf0"
/> | <img width="553" height="363" alt="CleanShot 2026-11-27 at 12 25
00"
src="https://github.com/user-attachments/assets/de06002f-d063-453e-a9da-00eee8e0616d"
/> |
| _This partner role `this_is_a_test` shouldn’t be here._ | _Partner
role `this_is_a_test` is no longer in the list of options._ |
| <img width="940" height="305" alt="CleanShot 2026-11-30 at 10 34 52"
src="https://github.com/user-attachments/assets/bdface9f-b3f4-4620-b567-cba8d829205e"
/> | <img width="935" height="299" alt="CleanShot 2026-11-30 at 10 37
40"
src="https://github.com/user-attachments/assets/da1abaa3-5a98-4837-9e8f-35be84b6be4c"
/> |
| _Previous definition._ | _Broadened definition to better encompass
roles we generally manage
([context](https://github.com/supabase/supabase/pull/54019#discussion_r4716757425))._
|

## How to test

1. Open the [Studio staging
preview](https://studio-staging-git-chore-depr-735-supabase.vercel.app)
and select a staging test project. If **Temporary access** is off in
**Feature previews**, enable it there. Then go to **Database → Settings
→ Temporary access** and make sure it is enabled for the test project.
2. Go to **Database → Roles**. Open Chrome DevTools, select **Network**,
and reload. Filter for `key=database-roles`. Right-click the matching
request and select **Override content**.
3. Append this test role to the roles array. Save the override and
reload.

```json
{
  "id": 99999,
  "name": "this_is_a_test",
  "isSuperuser": false,
  "canCreateDb": false,
  "canCreateRole": false,
  "inheritRole": true,
  "canLogin": true,
  "isReplicationRole": false,
  "canBypassRls": false,
  "activeConnections": 0,
  "connectionLimit": 90,
  "validUntil": null,
  "comment": "{\"_supabase\":{\"oauth_app_id\":\"app\"}}",
  "config": {}
}
```

4. In **Database → Roles**, make sure that `this_is_a_test` appears
under **Supabase managed roles** without edit or delete controls.
5. Go to **Database → Settings → Temporary access**, select **Add
rule**, and open the role picker. The test role should be absent.
6. Set the test role's `comment` to `null` in the override and reload.
Open **Add rule** again and make sure that the role is now selectable.
Close the form without saving and remove the override.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-11-30 15:35:30 +01:00

209 lines
5.8 KiB
TypeScript

import dayjs from 'dayjs'
import { describe, expect, it } from 'vitest'
import type { JitUserRuleDraft } from './JitDbAccess.types'
import {
computeStatusFromGrants,
createEmptyGrant,
getAssignableJitRoleOptions,
getInvalidIpRangeRows,
getJitMemberOptions,
getRelativeDatetimeByMode,
serializeDraftRolesForGrantMutation,
} from './JitDbAccess.utils'
import type { PgRole } from '@/data/database-roles/database-roles-query'
import type { OrganizationMembersData } from '@/data/organizations/organization-members-query'
describe('jitDbAccess.utils', () => {
it('returns empty expiry string for never/custom-only fallback modes', () => {
expect(getRelativeDatetimeByMode('never')).toBe('')
expect(getRelativeDatetimeByMode('custom')).toBe('')
})
it('creates future datetimes for preset expiry modes', () => {
const inOneHour = dayjs(getRelativeDatetimeByMode('1h'))
expect(inOneHour.isValid()).toBe(true)
expect(inOneHour.isAfter(dayjs())).toBe(true)
})
it('computes active and expired status counts including IP counts', () => {
const activeGrant = {
...createEmptyGrant('role_active'),
enabled: true,
hasExpiry: true,
expiry: dayjs().add(1, 'day').toISOString(),
ipRanges: [{ value: '192.0.2.0/24' }],
}
const expiredGrant = {
...createEmptyGrant('role_expired'),
enabled: true,
hasExpiry: true,
expiry: dayjs().subtract(1, 'day').toISOString(),
ipRanges: [{ value: '203.0.113.0/24' }],
}
const perpetualGrant = {
...createEmptyGrant('role_never'),
enabled: true,
hasExpiry: false,
expiryMode: 'never' as const,
expiry: '',
}
expect(computeStatusFromGrants([activeGrant, expiredGrant, perpetualGrant])).toEqual({
active: 2,
expired: 1,
activeIp: 1,
expiredIp: 1,
})
})
it('returns invalid CIDRs from repeated input rows', () => {
expect(
getInvalidIpRangeRows([
{ value: '192.0.2.0/24' },
{ value: 'not-a-cidr' },
{ value: '10.0.0.1/33' },
{ value: '2001:db8::/64' },
{ value: '2001:db8::/129' },
])
).toEqual(['not-a-cidr', '10.0.0.1/33', '2001:db8::/129'])
})
})
describe('serializeDraftRolesForGrantMutation', () => {
it('serializes role expiry and IP restrictions for grant mutation payload', () => {
const expiry = '2026-06-01T12:00:00.000Z'
const draft: JitUserRuleDraft = {
memberId: 'user-1',
grants: [
{
...createEmptyGrant('postgres'),
enabled: true,
branchesOnly: true,
hasExpiry: true,
expiryMode: 'custom',
expiry,
ipRanges: [{ value: '192.0.2.0/24' }, { value: ' ' }, { value: '2001:db8::/64' }],
},
{
...createEmptyGrant('supabase_read_only_user'),
enabled: true,
hasExpiry: false,
expiryMode: 'never',
expiry: '',
},
{
...createEmptyGrant('ignored_disabled'),
enabled: false,
},
],
}
expect(serializeDraftRolesForGrantMutation(draft)).toEqual([
{
role: 'postgres',
branches_only: true,
expires_at: dayjs(expiry).unix(),
allowed_networks: {
allowed_cidrs: [{ cidr: '192.0.2.0/24' }],
allowed_cidrs_v6: [{ cidr: '2001:db8::/64' }],
},
},
{
role: 'supabase_read_only_user',
},
])
})
})
describe('getJitMemberOptions', () => {
it('excludes invited org members without gotrue IDs from selectable options', () => {
const organizationMembers: OrganizationMembersData = [
{
gotrue_id: 'de305d54-75b4-431b-adb2-eb6b9e546014',
avatar_url: null,
primary_email: 'active@example.com',
username: 'Active User',
is_sso_user: false,
mfa_enabled: false,
metadata: {},
role_ids: [],
},
{
gotrue_id: '',
avatar_url: null,
invited_id: 123,
invited_at: '2026-03-01T00:00:00.000Z',
primary_email: 'expired-invite@example.com',
username: 'e',
is_sso_user: false,
mfa_enabled: false,
metadata: {},
role_ids: [],
},
]
expect(getJitMemberOptions(organizationMembers, [])).toEqual([
{
id: 'de305d54-75b4-431b-adb2-eb6b9e546014',
email: 'active@example.com',
name: 'Active User',
},
])
})
})
describe('getAssignableJitRoleOptions', () => {
const role = (name: string, overrides: Partial<PgRole> = {}): PgRole => ({
id: 1,
name,
isSuperuser: false,
canCreateDb: false,
canCreateRole: false,
inheritRole: true,
canLogin: true,
isReplicationRole: false,
canBypassRls: false,
activeConnections: 0,
connectionLimit: 100,
validUntil: null,
config: {},
comment: null,
...overrides,
})
it('leaves out Supabase support and Trusted Partner roles', () => {
const options = getAssignableJitRoleOptions([
role('postgres'),
role('supabase_read_only_user'),
role('support_ro_1a2b3c'),
role('acme_user', { comment: '{"_supabase":{"oauth_app_id":"app"}}' }),
role('reporting', { comment: 'Role for reporting' }),
])
expect(options.map((option) => option.id)).toEqual([
'postgres',
'reporting',
'supabase_read_only_user',
])
})
it('leaves out Supabase system roles, superusers, pg_ roles and roles that cannot log in', () => {
const options = getAssignableJitRoleOptions([
role('supabase_auth_admin'),
role('supabase_admin'),
role('pgbouncer'),
role('authenticator'),
role('anon'),
role('pg_monitor'),
role('root', { isSuperuser: true }),
role('no_login', { canLogin: false }),
role('reporting'),
])
expect(options.map((option) => option.id)).toEqual(['reporting'])
})
})