Skip to content

Commit 4dfda6d

Browse files
author
Michael Bahr
committed
chore: refactor the getDynamoDBPermissions function to reduce complexity
1 parent 182e15c commit 4dfda6d

File tree

1 file changed

+16
-72
lines changed

1 file changed

+16
-72
lines changed

lib/deploy/stepFunctions/compileIamRole.js

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -130,67 +130,6 @@ function getDynamoDBArn(tableName) {
130130
};
131131
}
132132

133-
function getDynamoDBIndexArn(tableName, indexName) {
134-
if (isIntrinsic(tableName)) {
135-
// most likely we'll see a { Ref: LogicalId }, which we need to map to
136-
// { Fn::GetAtt: [ LogicalId, Arn ] } to get the ARN
137-
if (tableName.Ref) {
138-
return {
139-
'Fn::Join': [
140-
'/',
141-
[
142-
{ 'Fn::GetAtt': [tableName.Ref, 'Arn'] },
143-
'index',
144-
indexName,
145-
],
146-
],
147-
};
148-
}
149-
// but also support importing the table name from an external stack that exports it
150-
// as we still want to support direct state machine actions interacting with those tables
151-
if (tableName['Fn::ImportValue']) {
152-
return {
153-
'Fn::Join': [
154-
':',
155-
[
156-
'arn',
157-
{ Ref: 'AWS::Partition' },
158-
'dynamodb',
159-
{ Ref: 'AWS::Region' },
160-
{ Ref: 'AWS::AccountId' },
161-
{
162-
'Fn::Join': [
163-
'/',
164-
[
165-
'table',
166-
tableName,
167-
'index',
168-
indexName,
169-
],
170-
],
171-
},
172-
],
173-
],
174-
};
175-
}
176-
}
177-
178-
return {
179-
'Fn::Join': [
180-
':',
181-
[
182-
'arn',
183-
{ Ref: 'AWS::Partition' },
184-
'dynamodb',
185-
{ Ref: 'AWS::Region' },
186-
{ Ref: 'AWS::AccountId' },
187-
`table/${tableName}/index/${indexName}`,
188-
],
189-
],
190-
};
191-
}
192-
193-
194133
function getBatchPermissions() {
195134
return [{
196135
action: 'batch:SubmitJob,batch:DescribeJobs,batch:TerminateJob',
@@ -243,26 +182,31 @@ function getEcsPermissions() {
243182
}
244183

245184
function getDynamoDBPermissions(action, state) {
246-
const indexName = state.Parameters['IndexName.$']
247-
? '*'
248-
: state.Parameters.IndexName;
249-
250185
let resource;
251-
if (indexName) {
252-
resource = state.Parameters['TableName.$']
186+
187+
if (state.Parameters['TableName.$']) {
188+
// When the TableName is only known at runtime, we
189+
// have to provide * permissions during deployment.
190+
resource = '*';
191+
} else if (state.Parameters['IndexName.$'] || state.Parameters.IndexName) {
192+
// When the Parameters contain an IndexName, we have to build a
193+
// longer arn that includes the index.
194+
const indexName = state.Parameters['IndexName.$']
195+
// We must provide * here instead of state.Parameters['IndexName.$'], because we don't know
196+
// which index will be targeted when we the step function runs
253197
? '*'
254-
: getDynamoDBIndexArn(state.Parameters.TableName, indexName);
198+
: state.Parameters.IndexName;
199+
200+
resource = getDynamoDBArn(`${state.Parameters.TableName}/index/${indexName}`);
255201
} else {
256-
resource = state.Parameters['TableName.$']
257-
? '*'
258-
: getDynamoDBArn(state.Parameters.TableName);
202+
resource = getDynamoDBArn(state.Parameters.TableName);
259203
}
204+
260205
return [{
261206
action,
262207
resource,
263208
}];
264209
}
265-
266210
function getRedshiftDataPermissions(action, state) {
267211
if (['redshift-data:ExecuteStatement', 'redshift-data:BatchExecuteStatement'].includes(action)) {
268212
const clusterName = _.has(state, 'Parameters.ClusterIdentifier') ? state.Parameters.ClusterIdentifier : '*';

0 commit comments

Comments
 (0)