|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { generateKeyPairSync } from 'node:crypto' |
| 5 | +import { createSessionPrincipal } from '@sim/testing/factories/principal.factory' |
| 6 | +import { encryptionMock, encryptionMockFns } from '@sim/testing/mocks/encryption.mock' |
| 7 | +import { workspaceAuthzMock, workspaceAuthzMockFns } from '@sim/testing/mocks/workspace-authz.mock' |
| 8 | +import { |
| 9 | + workspaceContextMock, |
| 10 | + workspaceContextMockFns, |
| 11 | +} from '@sim/testing/mocks/workspace-context.mock' |
| 12 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +const hoisted = vi.hoisted(() => ({ actorContext: vi.fn() })) |
| 15 | + |
| 16 | +vi.mock('@/lib/workspaces/application/context', () => workspaceContextMock) |
| 17 | +vi.mock('@sim/platform-authz/workspace', () => workspaceAuthzMock) |
| 18 | +vi.mock('@/lib/core/security/encryption', () => encryptionMock) |
| 19 | +vi.mock('@/lib/credentials/access', () => ({ getCredentialActorContext: hoisted.actorContext })) |
| 20 | + |
| 21 | +import { env } from '@/lib/core/config/env' |
| 22 | +import { prepareWorkloadIdentity } from '@/lib/credentials/application/prepare-workload-identity' |
| 23 | + |
| 24 | +const principal = createSessionPrincipal() |
| 25 | +const keylessSecret = { |
| 26 | + type: 'snowflake_workload_identity_v1', |
| 27 | + config: { |
| 28 | + authType: 'workload_identity', |
| 29 | + host: 'myorg-myaccount.snowflakecomputing.com', |
| 30 | + subject: 'sim:workspace:workspace-1:credential:abcdef0123456789', |
| 31 | + }, |
| 32 | +} |
| 33 | +const tokenSecret = { |
| 34 | + type: 'token_service_account', |
| 35 | + providerId: 'snowflake-service-account', |
| 36 | + apiToken: 'pat-secret', |
| 37 | + domain: 'myorg-myaccount.snowflakecomputing.com', |
| 38 | +} |
| 39 | + |
| 40 | +/** The credential being reconnected, as the caller's access to it resolves. */ |
| 41 | +function reconnecting(secret: object, access: { isAdmin?: boolean; workspaceId?: string } = {}) { |
| 42 | + hoisted.actorContext.mockResolvedValue({ |
| 43 | + credential: { |
| 44 | + workspaceId: access.workspaceId ?? 'workspace-1', |
| 45 | + type: 'service_account', |
| 46 | + providerId: 'snowflake-service-account', |
| 47 | + encryptedServiceAccountKey: 'ciphertext', |
| 48 | + }, |
| 49 | + member: null, |
| 50 | + hasWorkspaceAccess: true, |
| 51 | + canWriteWorkspace: true, |
| 52 | + isAdmin: access.isAdmin ?? true, |
| 53 | + }) |
| 54 | + encryptionMockFns.mockDecryptSecret.mockResolvedValue({ decrypted: JSON.stringify(secret) }) |
| 55 | +} |
| 56 | + |
| 57 | +function prepare() { |
| 58 | + return prepareWorkloadIdentity.execute({ |
| 59 | + principal, |
| 60 | + input: { workspaceId: 'workspace-1', provider: 'snowflake', credentialId: 'cred-1' }, |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +beforeAll(() => { |
| 65 | + Reflect.set( |
| 66 | + env, |
| 67 | + 'WORKLOAD_IDENTITY_SIGNING_KEY', |
| 68 | + generateKeyPairSync('rsa', { modulusLength: 2048 }).privateKey.export({ |
| 69 | + type: 'pkcs8', |
| 70 | + format: 'pem', |
| 71 | + }) |
| 72 | + ) |
| 73 | + Reflect.set(env, 'WORKLOAD_IDENTITY_ISSUER', 'https://sim.example.com/oidc/workload') |
| 74 | +}) |
| 75 | + |
| 76 | +afterAll(() => { |
| 77 | + Reflect.set(env, 'WORKLOAD_IDENTITY_SIGNING_KEY', undefined) |
| 78 | + Reflect.set(env, 'WORKLOAD_IDENTITY_ISSUER', undefined) |
| 79 | +}) |
| 80 | + |
| 81 | +describe('preparing a Snowflake reconnect', () => { |
| 82 | + beforeEach(() => { |
| 83 | + vi.clearAllMocks() |
| 84 | + workspaceContextMockFns.mockLoadActiveWorkspaceApplicationContext.mockResolvedValue({ |
| 85 | + workspaceId: 'workspace-1', |
| 86 | + workspaceOrganizationId: null, |
| 87 | + allowPersonalApiKeys: true, |
| 88 | + billedAccountUserId: 'user-1', |
| 89 | + }) |
| 90 | + workspaceAuthzMockFns.mockResolveEffectiveWorkspacePermission.mockResolvedValue('write') |
| 91 | + }) |
| 92 | + |
| 93 | + it('opens a keyless credential on keyless', async () => { |
| 94 | + reconnecting(keylessSecret) |
| 95 | + await expect(prepare()).resolves.toMatchObject({ reconnectsKeyless: true }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('opens a token credential on the token', async () => { |
| 99 | + reconnecting(tokenSecret) |
| 100 | + expect(await prepare()).not.toHaveProperty('reconnectsKeyless') |
| 101 | + }) |
| 102 | + |
| 103 | + it('reveals nothing about a credential the caller does not administer', async () => { |
| 104 | + reconnecting(keylessSecret, { isAdmin: false }) |
| 105 | + expect(await prepare()).not.toHaveProperty('reconnectsKeyless') |
| 106 | + expect(encryptionMockFns.mockDecryptSecret).not.toHaveBeenCalled() |
| 107 | + }) |
| 108 | + |
| 109 | + it('reveals nothing about a credential from another workspace', async () => { |
| 110 | + reconnecting(keylessSecret, { workspaceId: 'workspace-2' }) |
| 111 | + expect(await prepare()).not.toHaveProperty('reconnectsKeyless') |
| 112 | + expect(encryptionMockFns.mockDecryptSecret).not.toHaveBeenCalled() |
| 113 | + }) |
| 114 | +}) |
0 commit comments