Skip to content

Commit 740557c

Browse files
committed
fix: correct test for ItemReader with JSONpath input
1 parent baf6764 commit 740557c

File tree

1 file changed

+79
-7
lines changed

1 file changed

+79
-7
lines changed

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,8 +1494,9 @@ describe('#compileIamRole', () => {
14941494
});
14951495

14961496
it('should give s3:GetObject permission for only objects referenced by state machine with ItemReader', () => {
1497+
const hello = 'hello.txt';
1498+
const world = 'world.txt';
14971499
const testBucket = 'test-bucket';
1498-
const testKey = 'test-key';
14991500

15001501
const genStateMachine = (id, lambdaArn, bucket, key) => ({
15011502
id,
@@ -1517,8 +1518,8 @@ describe('#compileIamRole', () => {
15171518
ItemReader: {
15181519
Resource: 'arn:aws:states:::s3:getObject',
15191520
Parameters: {
1520-
'Bucket.$': bucket,
1521-
'Key.$': key,
1521+
Bucket: bucket,
1522+
Key: key,
15221523
},
15231524
},
15241525
End: true,
@@ -1530,9 +1531,9 @@ describe('#compileIamRole', () => {
15301531
serverless.service.stepFunctions = {
15311532
stateMachines: {
15321533
myStateMachine1: genStateMachine('StateMachine1',
1533-
'arn:aws:lambda:us-west-2:1234567890:function:foo', '$.testBucket', '$.testKey'),
1534+
'arn:aws:lambda:us-west-2:1234567890:function:foo', testBucket, hello),
15341535
myStateMachine2: genStateMachine('StateMachine2',
1535-
'arn:aws:lambda:us-west-2:1234567890:function:foo', testBucket, testKey),
1536+
'arn:aws:lambda:us-west-2:1234567890:function:foo', testBucket, world),
15361537
},
15371538
};
15381539

@@ -1542,9 +1543,80 @@ describe('#compileIamRole', () => {
15421543
const policy1 = resources.StateMachine1Role.Properties.Policies[0];
15431544
const policy2 = resources.StateMachine2Role.Properties.Policies[0];
15441545
expect(policy1.PolicyDocument.Statement[1].Resource)
1545-
.to.be.deep.equal('*');
1546+
.to.be.deep.equal([`arn:aws:s3:::${testBucket}/${hello}`]);
15461547
expect(policy2.PolicyDocument.Statement[1].Resource)
1547-
.to.be.deep.equal([`arn:aws:s3:::${testBucket}/${testKey}`]);
1548+
.to.be.deep.equal([`arn:aws:s3:::${testBucket}/${world}`]);
1549+
});
1550+
1551+
it('should give s3:GetObject permission to * when Bucket.$ and Key.$ are seen on ItemReader', () => {
1552+
const genStateMachine = (id, lambdaArn) => ({
1553+
id,
1554+
definition: {
1555+
StartAt: 'A',
1556+
States: {
1557+
A: {
1558+
Type: 'Map',
1559+
ItemProcessor: {
1560+
StartAt: 'B',
1561+
States: {
1562+
B: {
1563+
Type: 'Task',
1564+
Resource: lambdaArn,
1565+
End: true,
1566+
},
1567+
},
1568+
},
1569+
ItemReader: {
1570+
Resource: 'arn:aws:states:::s3:getObject',
1571+
Parameters: {
1572+
Bucket: 'test-bucket',
1573+
Key: 'test-key',
1574+
},
1575+
},
1576+
Next: 'C',
1577+
},
1578+
C: {
1579+
Type: 'Map',
1580+
ItemProcessor: {
1581+
StartAt: 'D',
1582+
States: {
1583+
D: {
1584+
Type: 'Task',
1585+
Resource: lambdaArn,
1586+
End: true,
1587+
},
1588+
},
1589+
},
1590+
ItemReader: {
1591+
Resource: 'arn:aws:states:::s3:getObject',
1592+
Parameters: {
1593+
'Bucket.$': '$.testBucket',
1594+
'Key.$': '$.key',
1595+
},
1596+
},
1597+
End: true,
1598+
},
1599+
},
1600+
},
1601+
});
1602+
1603+
serverless.service.stepFunctions = {
1604+
stateMachines: {
1605+
myStateMachine1: genStateMachine('StateMachine1',
1606+
'arn:aws:lambda:us-west-2:1234567890:function:foo'),
1607+
},
1608+
};
1609+
1610+
serverlessStepFunctions.compileIamRole();
1611+
const resources = serverlessStepFunctions.serverless.service
1612+
.provider.compiledCloudFormationTemplate.Resources;
1613+
const policy1 = resources.StateMachine1Role.Properties.Policies[0];
1614+
1615+
// even though some tasks target specific topic ARNs, other states use Bucket.$
1616+
// and Key.$ so we need to give broad permissions to be able to get any
1617+
// table and key the input specifies
1618+
expect(policy1.PolicyDocument.Statement[1].Resource)
1619+
.to.be.deep.equal('*');
15481620
});
15491621

15501622
it('should not generate any permissions for Task states not yet supported', () => {

0 commit comments

Comments
 (0)