|
1 | 1 | import type { AgentCoreProjectSpec } from '../../../../../schema'; |
2 | | -import { resolveRuntimeTargetNames } from '../resolve'; |
3 | | -import { describe, expect, it } from 'vitest'; |
| 2 | +import { getOrCreateABTestRole, resolveRuntimeTargetNames } from '../resolve'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +const { mockIamSend } = vi.hoisted(() => ({ |
| 6 | + mockIamSend: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock('@aws-sdk/client-iam', () => ({ |
| 10 | + IAMClient: class { |
| 11 | + send = mockIamSend; |
| 12 | + }, |
| 13 | + CreateRoleCommand: class { |
| 14 | + constructor(public input: unknown) {} |
| 15 | + }, |
| 16 | + GetRoleCommand: class { |
| 17 | + constructor(public input: unknown) {} |
| 18 | + }, |
| 19 | + PutRolePolicyCommand: class { |
| 20 | + constructor(public input: unknown) {} |
| 21 | + }, |
| 22 | + DeleteRolePolicyCommand: class { |
| 23 | + constructor(public input: unknown) {} |
| 24 | + }, |
| 25 | + DeleteRoleCommand: class { |
| 26 | + constructor(public input: unknown) {} |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock('../../../../aws/account', () => ({ |
| 31 | + getCredentialProvider: vi.fn().mockReturnValue({}), |
| 32 | +})); |
| 33 | + |
| 34 | +const accountId = '123456789012'; |
| 35 | +const roleArn = `arn:aws:iam::${accountId}:role/AgentCore-Test-ABTestExperiment`; |
| 36 | + |
| 37 | +interface TestTrustPolicy { |
| 38 | + Version: string; |
| 39 | + Statement: { |
| 40 | + Effect: string; |
| 41 | + Principal: Record<string, string>; |
| 42 | + Action: string; |
| 43 | + Condition?: { |
| 44 | + StringEquals: Record<string, string>; |
| 45 | + ArnLike: Record<string, string>; |
| 46 | + }; |
| 47 | + }[]; |
| 48 | +} |
| 49 | + |
| 50 | +function expectedTrustPolicy(): TestTrustPolicy { |
| 51 | + return { |
| 52 | + Version: '2012-10-17', |
| 53 | + Statement: [ |
| 54 | + { |
| 55 | + Effect: 'Allow', |
| 56 | + Principal: { Service: 'bedrock-agentcore.amazonaws.com' }, |
| 57 | + Action: 'sts:AssumeRole', |
| 58 | + Condition: { |
| 59 | + StringEquals: { 'aws:SourceAccount': accountId }, |
| 60 | + ArnLike: { 'aws:SourceArn': `arn:aws:bedrock-agentcore:*:${accountId}:ab-test/*` }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function roleOptions() { |
| 68 | + return { |
| 69 | + region: 'us-east-1', |
| 70 | + projectName: 'Test', |
| 71 | + testName: 'Experiment', |
| 72 | + gatewayArn: `arn:aws:bedrock-agentcore:us-east-1:${accountId}:gateway/test`, |
| 73 | + propagationDelayMs: 0, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +function entityAlreadyExistsError(): Error { |
| 78 | + return Object.assign(new Error('Role already exists'), { name: 'EntityAlreadyExistsException' }); |
| 79 | +} |
| 80 | + |
| 81 | +describe('getOrCreateABTestRole', () => { |
| 82 | + beforeEach(() => { |
| 83 | + vi.clearAllMocks(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('creates a new role and applies its inline permissions policy', async () => { |
| 87 | + mockIamSend.mockResolvedValueOnce({ Role: { Arn: roleArn } }).mockResolvedValueOnce({}); |
| 88 | + |
| 89 | + await expect(getOrCreateABTestRole(roleOptions())).resolves.toBe(roleArn); |
| 90 | + expect(mockIamSend).toHaveBeenCalledTimes(2); |
| 91 | + }); |
| 92 | + |
| 93 | + it('reuses an existing role when its trust policy matches', async () => { |
| 94 | + mockIamSend |
| 95 | + .mockRejectedValueOnce(entityAlreadyExistsError()) |
| 96 | + .mockResolvedValueOnce({ |
| 97 | + Role: { Arn: roleArn, AssumeRolePolicyDocument: expectedTrustPolicy() }, |
| 98 | + }) |
| 99 | + .mockResolvedValueOnce({}); |
| 100 | + |
| 101 | + await expect(getOrCreateABTestRole(roleOptions())).resolves.toBe(roleArn); |
| 102 | + expect(mockIamSend).toHaveBeenCalledTimes(3); |
| 103 | + }); |
| 104 | + |
| 105 | + it('reuses an existing role with a URL-encoded matching trust policy', async () => { |
| 106 | + const encodedPolicy = encodeURIComponent(JSON.stringify(expectedTrustPolicy())); |
| 107 | + mockIamSend |
| 108 | + .mockRejectedValueOnce(entityAlreadyExistsError()) |
| 109 | + .mockResolvedValueOnce({ |
| 110 | + Role: { Arn: roleArn, AssumeRolePolicyDocument: encodedPolicy }, |
| 111 | + }) |
| 112 | + .mockResolvedValueOnce({}); |
| 113 | + |
| 114 | + await expect(getOrCreateABTestRole(roleOptions())).resolves.toBe(roleArn); |
| 115 | + expect(mockIamSend).toHaveBeenCalledTimes(3); |
| 116 | + }); |
| 117 | + |
| 118 | + it('rejects an existing role with an additional trusted principal', async () => { |
| 119 | + const trustPolicy = expectedTrustPolicy(); |
| 120 | + trustPolicy.Statement.push({ |
| 121 | + Effect: 'Allow', |
| 122 | + Principal: { AWS: `arn:aws:iam::${accountId}:user/attacker` }, |
| 123 | + Action: 'sts:AssumeRole', |
| 124 | + }); |
| 125 | + mockIamSend.mockRejectedValueOnce(entityAlreadyExistsError()).mockResolvedValueOnce({ |
| 126 | + Role: { Arn: roleArn, AssumeRolePolicyDocument: trustPolicy }, |
| 127 | + }); |
| 128 | + |
| 129 | + await expect(getOrCreateABTestRole(roleOptions())).rejects.toThrow(/trust policy does not match/); |
| 130 | + expect(mockIamSend).toHaveBeenCalledTimes(2); |
| 131 | + }); |
| 132 | + |
| 133 | + const weakenedConditions: [string, (policy: TestTrustPolicy) => void][] = [ |
| 134 | + [ |
| 135 | + 'SourceAccount', |
| 136 | + policy => { |
| 137 | + policy.Statement[0]!.Condition!.StringEquals['aws:SourceAccount'] = '*'; |
| 138 | + }, |
| 139 | + ], |
| 140 | + [ |
| 141 | + 'SourceArn', |
| 142 | + policy => { |
| 143 | + policy.Statement[0]!.Condition!.ArnLike['aws:SourceArn'] = '*'; |
| 144 | + }, |
| 145 | + ], |
| 146 | + ]; |
| 147 | + |
| 148 | + it.each(weakenedConditions)( |
| 149 | + 'rejects an existing role with a weakened %s condition', |
| 150 | + async (_condition, weakenPolicy) => { |
| 151 | + const trustPolicy = expectedTrustPolicy(); |
| 152 | + weakenPolicy(trustPolicy); |
| 153 | + mockIamSend.mockRejectedValueOnce(entityAlreadyExistsError()).mockResolvedValueOnce({ |
| 154 | + Role: { Arn: roleArn, AssumeRolePolicyDocument: trustPolicy }, |
| 155 | + }); |
| 156 | + |
| 157 | + await expect(getOrCreateABTestRole(roleOptions())).rejects.toThrow(/trust policy does not match/); |
| 158 | + expect(mockIamSend).toHaveBeenCalledTimes(2); |
| 159 | + } |
| 160 | + ); |
| 161 | +}); |
4 | 162 |
|
5 | 163 | type GatewaysOnly = Pick<AgentCoreProjectSpec, 'agentCoreGateways'>; |
6 | 164 |
|
|
0 commit comments