|
| 1 | +jest.mock('@db', () => ({ |
| 2 | + db: { integrationConnection: { findFirst: jest.fn() } }, |
| 3 | + Prisma: {}, |
| 4 | +})); |
| 5 | + |
| 6 | +import { db } from '@db'; |
| 7 | +import { |
| 8 | + CloudSecurityService, |
| 9 | + ConnectionNotFoundError, |
| 10 | +} from './cloud-security.service'; |
| 11 | + |
| 12 | +type Ctor = ConstructorParameters<typeof CloudSecurityService>; |
| 13 | + |
| 14 | +const findFirst = (db as unknown as { |
| 15 | + integrationConnection: { findFirst: jest.Mock }; |
| 16 | +}).integrationConnection.findFirst; |
| 17 | + |
| 18 | +describe('CloudSecurityService.resolveAwsSession', () => { |
| 19 | + let credentialVault: { getDecryptedCredentials: jest.Mock }; |
| 20 | + let awsService: { resolveRoleSession: jest.Mock }; |
| 21 | + let service: CloudSecurityService; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + credentialVault = { getDecryptedCredentials: jest.fn() }; |
| 25 | + awsService = { resolveRoleSession: jest.fn() }; |
| 26 | + service = new CloudSecurityService( |
| 27 | + credentialVault as unknown as Ctor[0], |
| 28 | + {} as unknown as Ctor[1], |
| 29 | + {} as unknown as Ctor[2], |
| 30 | + awsService as unknown as Ctor[3], |
| 31 | + {} as unknown as Ctor[4], |
| 32 | + {} as unknown as Ctor[5], |
| 33 | + ); |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('scopes the connection lookup by organizationId (tenant isolation)', async () => { |
| 38 | + findFirst.mockResolvedValueOnce(null); |
| 39 | + |
| 40 | + await expect( |
| 41 | + service.resolveAwsSession('conn_1', 'org_1'), |
| 42 | + ).rejects.toBeInstanceOf(ConnectionNotFoundError); |
| 43 | + |
| 44 | + expect(findFirst).toHaveBeenCalledWith( |
| 45 | + expect.objectContaining({ |
| 46 | + where: { id: 'conn_1', organizationId: 'org_1', status: 'active' }, |
| 47 | + }), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns not_configured for a non-AWS provider (never assumes)', async () => { |
| 52 | + findFirst.mockResolvedValueOnce({ provider: { slug: 'gcp' } }); |
| 53 | + |
| 54 | + const result = await service.resolveAwsSession('conn_1', 'org_1'); |
| 55 | + |
| 56 | + expect(result).toEqual({ ok: false, reason: 'not_configured' }); |
| 57 | + expect(awsService.resolveRoleSession).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns not_configured when roleArn/externalId are missing', async () => { |
| 61 | + findFirst.mockResolvedValueOnce({ |
| 62 | + provider: { slug: 'aws' }, |
| 63 | + variables: {}, |
| 64 | + }); |
| 65 | + credentialVault.getDecryptedCredentials.mockResolvedValueOnce({ |
| 66 | + externalId: 'eid', // roleArn missing |
| 67 | + }); |
| 68 | + |
| 69 | + const result = await service.resolveAwsSession('conn_1', 'org_1'); |
| 70 | + |
| 71 | + expect(result).toEqual({ ok: false, reason: 'not_configured' }); |
| 72 | + expect(awsService.resolveRoleSession).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('returns the resolved session on success', async () => { |
| 76 | + findFirst.mockResolvedValueOnce({ |
| 77 | + provider: { slug: 'aws' }, |
| 78 | + variables: { regions: ['us-east-1'] }, |
| 79 | + }); |
| 80 | + credentialVault.getDecryptedCredentials.mockResolvedValueOnce({ |
| 81 | + roleArn: 'arn:aws:iam::123456789012:role/x', |
| 82 | + externalId: 'eid', |
| 83 | + regions: ['us-east-1'], |
| 84 | + }); |
| 85 | + awsService.resolveRoleSession.mockResolvedValueOnce({ |
| 86 | + accessKeyId: 'AKIA_TEMP', |
| 87 | + secretAccessKey: 'secret', |
| 88 | + sessionToken: 'token', |
| 89 | + }); |
| 90 | + |
| 91 | + const result = await service.resolveAwsSession('conn_1', 'org_1'); |
| 92 | + |
| 93 | + expect(result).toEqual({ |
| 94 | + ok: true, |
| 95 | + session: { |
| 96 | + accessKeyId: 'AKIA_TEMP', |
| 97 | + secretAccessKey: 'secret', |
| 98 | + sessionToken: 'token', |
| 99 | + }, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns assume_failed with the real reason when the assume throws', async () => { |
| 104 | + findFirst.mockResolvedValueOnce({ |
| 105 | + provider: { slug: 'aws' }, |
| 106 | + variables: {}, |
| 107 | + }); |
| 108 | + credentialVault.getDecryptedCredentials.mockResolvedValueOnce({ |
| 109 | + roleArn: 'arn:aws:iam::123456789012:role/x', |
| 110 | + externalId: 'eid', |
| 111 | + regions: ['us-east-1'], |
| 112 | + }); |
| 113 | + awsService.resolveRoleSession.mockRejectedValueOnce( |
| 114 | + new Error('not authorized to perform sts:AssumeRole'), |
| 115 | + ); |
| 116 | + |
| 117 | + const result = await service.resolveAwsSession('conn_1', 'org_1'); |
| 118 | + |
| 119 | + expect(result).toEqual({ |
| 120 | + ok: false, |
| 121 | + reason: 'assume_failed', |
| 122 | + error: 'not authorized to perform sts:AssumeRole', |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments