Skip to content

Commit 3bc538a

Browse files
committed
update iam role setting
1 parent 86b8994 commit 3bc538a

File tree

2 files changed

+95
-20
lines changed

2 files changed

+95
-20
lines changed

index.js

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class ServerlessStepFunctions {
1313
this.stage = this.provider.getStage();
1414
this.awsStateLanguage = {};
1515
this.functionArns = {};
16-
this.iamRoleName = `serverless-step-functions-executerole-${this.region}`;
17-
this.iamPolicyName = `serverless-step-functions-executepolicy-${this.region}`;
1816
this.iamPolicyStatement = `{
1917
"Version": "2012-10-17",
2018
"Statement": [
@@ -149,6 +147,7 @@ class ServerlessStepFunctions {
149147

150148
remove() {
151149
return BbPromise.bind(this)
150+
.then(this.deleteIamRole)
152151
.then(this.getStateMachineArn)
153152
.then(this.deleteStateMachine)
154153
.then(() => {
@@ -164,6 +163,18 @@ class ServerlessStepFunctions {
164163
.then(this.describeExecution);
165164
}
166165

166+
getIamRoleName() {
167+
let name = `${this.service}-${this.region}-${this.stage}-${this.options.state}-`;
168+
name += 'ssf-exerole';
169+
return name;
170+
}
171+
172+
getIamPolicyName() {
173+
let name = `${this.service}-${this.region}-${this.stage}-${this.options.state}-`;
174+
name += 'ssf-exepolicy';
175+
return name;
176+
}
177+
167178
getStateMachineName() {
168179
return `${this.service}-${this.stage}-${this.options.state}`;
169180
}
@@ -172,7 +183,7 @@ class ServerlessStepFunctions {
172183
return this.provider.request('IAM',
173184
'getRole',
174185
{
175-
RoleName: this.iamRoleName,
186+
RoleName: this.getIamRoleName(),
176187
},
177188
this.options.stage,
178189
this.options.region)
@@ -207,7 +218,7 @@ class ServerlessStepFunctions {
207218
'createRole',
208219
{
209220
AssumeRolePolicyDocument: this.assumeRolePolicyDocument,
210-
RoleName: this.iamRoleName,
221+
RoleName: this.getIamRoleName(),
211222
},
212223
this.options.stage,
213224
this.options.region)
@@ -217,7 +228,7 @@ class ServerlessStepFunctions {
217228
'createPolicy',
218229
{
219230
PolicyDocument: this.iamPolicyStatement,
220-
PolicyName: this.iamPolicyName,
231+
PolicyName: this.getIamPolicyName(),
221232
},
222233
this.options.stage,
223234
this.options.region);
@@ -226,7 +237,43 @@ class ServerlessStepFunctions {
226237
'attachRolePolicy',
227238
{
228239
PolicyArn: result.Policy.Arn,
229-
RoleName: this.iamRoleName,
240+
RoleName: this.getIamRoleName(),
241+
},
242+
this.options.stage,
243+
this.options.region)
244+
)
245+
.then(() => BbPromise.resolve());
246+
}
247+
248+
deleteIamRole() {
249+
let policyArn;
250+
return this.provider.request('STS',
251+
'getCallerIdentity',
252+
{},
253+
this.options.stage,
254+
this.options.region)
255+
.then((result) => {
256+
policyArn = `arn:aws:iam::${result.Account}:policy/${this.getIamPolicyName()}`;
257+
return this.provider.request('IAM',
258+
'detachRolePolicy',
259+
{
260+
PolicyArn: policyArn,
261+
RoleName: this.getIamRoleName(),
262+
},
263+
this.options.stage,
264+
this.options.region)})
265+
.then((result) => this.provider.request('IAM',
266+
'deletePolicy',
267+
{
268+
PolicyArn: policyArn,
269+
},
270+
this.options.stage,
271+
this.options.region)
272+
)
273+
.then((result) => this.provider.request('IAM',
274+
'deleteRole',
275+
{
276+
RoleName: this.getIamRoleName(),
230277
},
231278
this.options.stage,
232279
this.options.region)

index.test.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ describe('ServerlessStepFunctions', () => {
5555
it('should set the stage variable', () =>
5656
expect(serverlessStepFunctions.stage).to.be.equal(provider.getStage()));
5757

58-
it('should set the iamRoleName variable', () =>
59-
expect(serverlessStepFunctions.iamRoleName).to.be
60-
.equal('serverless-step-functions-executerole-us-east-1'));
61-
62-
it('should set the iamPolicyName variable', () =>
63-
expect(serverlessStepFunctions.iamPolicyName).to.be
64-
.equal('serverless-step-functions-executepolicy-us-east-1'));
65-
6658
it('should set the assumeRolePolicyDocument variable', () =>
6759
expect(serverlessStepFunctions.assumeRolePolicyDocument).to.be
6860
.equal(`{
@@ -155,14 +147,17 @@ describe('ServerlessStepFunctions', () => {
155147

156148
describe('#remove()', () => {
157149
it('should run promise chain in order', () => {
150+
const deleteIamRoleStub = sinon
151+
.stub(serverlessStepFunctions, 'deleteIamRole').returns(BbPromise.resolve());
158152
const getStateMachineArnStub = sinon
159153
.stub(serverlessStepFunctions, 'getStateMachineArn').returns(BbPromise.resolve());
160154
const deleteStateMachineStub = sinon
161155
.stub(serverlessStepFunctions, 'deleteStateMachine').returns(BbPromise.resolve());
162156

163157
return serverlessStepFunctions.remove()
164158
.then(() => {
165-
expect(getStateMachineArnStub.calledOnce).to.be.equal(true);
159+
expect(deleteIamRoleStub.calledOnce).to.be.equal(true);
160+
expect(getStateMachineArnStub.calledAfter(deleteIamRoleStub)).to.be.equal(true);
166161
expect(deleteStateMachineStub.calledAfter(getStateMachineArnStub)).to.be.equal(true);
167162

168163
serverlessStepFunctions.getStateMachineArn.restore();
@@ -193,10 +188,17 @@ describe('ServerlessStepFunctions', () => {
193188
});
194189
});
195190

196-
describe('#getStateMachineName', () => {
197-
it('should return stateMachineName', () => {
198-
expect(serverlessStepFunctions.getStateMachineName())
199-
.to.be.equal('step-functions-dev-stateMachine');
191+
describe('#getIamRoleName', () => {
192+
it('should return IamRoleName', () => {
193+
expect(serverlessStepFunctions.getIamRoleName())
194+
.to.be.equal('step-functions-us-east-1-dev-stateMachine-ssf-exerole');
195+
});
196+
});
197+
198+
describe('#getIamPolicyName', () => {
199+
it('should return IamPolicyName', () => {
200+
expect(serverlessStepFunctions.getIamPolicyName())
201+
.to.be.equal('step-functions-us-east-1-dev-stateMachine-ssf-exepolicy');
200202
});
201203
});
202204

@@ -214,7 +216,7 @@ describe('ServerlessStepFunctions', () => {
214216
'IAM',
215217
'getRole',
216218
{
217-
RoleName: 'serverless-step-functions-executerole-us-east-1',
219+
RoleName: 'step-functions-us-east-1-dev-stateMachine-ssf-exerole',
218220
},
219221
serverlessStepFunctions.options.stage,
220222
serverlessStepFunctions.options.region
@@ -299,6 +301,32 @@ describe('ServerlessStepFunctions', () => {
299301
);
300302
});
301303

304+
describe('#deleteIamRole()', () => {
305+
let deleteIamRoleStub;
306+
beforeEach(() => {
307+
deleteIamRoleStub = sinon.stub(serverlessStepFunctions.provider, 'request');
308+
deleteIamRoleStub.onFirstCall().returns(BbPromise.resolve({ Account: 1234 }));
309+
deleteIamRoleStub.onSecondCall().returns(BbPromise.resolve());
310+
deleteIamRoleStub.onThirdCall().returns(BbPromise.resolve());
311+
deleteIamRoleStub.onCall(4).returns(BbPromise.resolve());
312+
});
313+
314+
it('should deleteIamRole with correct params', () => serverlessStepFunctions.deleteIamRole()
315+
.then(() => {
316+
expect(deleteIamRoleStub.callCount).to.be.equal(4);
317+
expect(deleteIamRoleStub.args[0][0]).to.be.equal('STS');
318+
expect(deleteIamRoleStub.args[0][1]).to.be.equal('getCallerIdentity');
319+
expect(deleteIamRoleStub.args[1][0]).to.be.equal('IAM');
320+
expect(deleteIamRoleStub.args[1][1]).to.be.equal('detachRolePolicy');
321+
expect(deleteIamRoleStub.args[2][0]).to.be.equal('IAM');
322+
expect(deleteIamRoleStub.args[2][1]).to.be.equal('deletePolicy');
323+
expect(deleteIamRoleStub.args[3][0]).to.be.equal('IAM');
324+
expect(deleteIamRoleStub.args[3][1]).to.be.equal('deleteRole');
325+
serverlessStepFunctions.provider.request.restore();
326+
})
327+
);
328+
});
329+
302330
describe('#getStateMachineArn()', () => {
303331
let getStateMachineStub;
304332
beforeEach(() => {

0 commit comments

Comments
 (0)