Skip to content

Commit f74974f

Browse files
feat: added encryption config override (#669)
* added encryption config override * added tests --------- Co-authored-by: Dan Behrman <166764905+DanBehrman-CR@users.noreply.github.com>
1 parent 530da13 commit f74974f

5 files changed

Lines changed: 103 additions & 1 deletion

File tree

lib/deploy/stepFunctions/compileIamRole.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,13 @@ module.exports = {
915915
});
916916
}
917917

918+
if (stateMachineObj.encryptionConfig && stateMachineObj.encryptionConfig.KmsKeyId) {
919+
iamPermissions.push({
920+
action: 'kms:Decrypt,kms:Encrypt',
921+
resource: { 'Fn::Sub': stateMachineObj.encryptionConfig.KmsKeyId },
922+
});
923+
}
924+
918925
iamPermissions = consolidatePermissionsByAction(iamPermissions);
919926
iamPermissions = consolidatePermissionsByResource(iamPermissions);
920927
const iamStatements = getIamStatements(iamPermissions, stateMachineObj);

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4425,7 +4425,6 @@ describe('#compileIamRole', () => {
44254425
expect(boundary).to.equal('arn:aws:iam::myAccount:policy/permission_boundary');
44264426
});
44274427

4428-
44294428
it('should handle permissions listObjectsV2', () => {
44304429
const myBucket = 'myBucket';
44314430
serverless.service.stepFunctions = {
@@ -4477,4 +4476,29 @@ describe('#compileIamRole', () => {
44774476
expect(statements[3].Resource[0]).to.equal(`arn:aws:s3:::${myBucket}`);
44784477
expect(statements[3].Resource[1]).to.equal(`arn:aws:s3:::${myBucket}/*`);
44794478
});
4479+
4480+
it('should add permissions for KMS key if present', () => {
4481+
serverless.service.stepFunctions = {
4482+
stateMachines: {
4483+
myStateMachine1: {
4484+
id: 'StateMachine1',
4485+
encryptionConfig: {
4486+
KmsKeyId: 'arn:kms:....',
4487+
},
4488+
definition: {},
4489+
},
4490+
},
4491+
};
4492+
4493+
serverlessStepFunctions.compileIamRole();
4494+
const statements = serverlessStepFunctions.serverless.service.provider
4495+
.compiledCloudFormationTemplate.Resources.StateMachine1Role.Properties.Policies[0]
4496+
.PolicyDocument.Statement;
4497+
4498+
expect(statements).to.have.lengthOf(1);
4499+
expect(statements[0].Effect).to.equal('Allow');
4500+
expect(statements[0].Action[0]).to.equal('kms:Decrypt');
4501+
expect(statements[0].Action[1]).to.equal('kms:Encrypt');
4502+
expect(statements[0].Resource[0]['Fn::Sub']).to.equal('arn:kms:....');
4503+
});
44804504
});

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module.exports = {
100100
let DependsOn = [];
101101
let LoggingConfiguration;
102102
let TracingConfiguration;
103+
let EncryptionConfiguration;
103104
let Tags;
104105
if (stateMachineObj.inheritGlobalTags === false) {
105106
Tags = [];
@@ -219,6 +220,16 @@ module.exports = {
219220
};
220221
}
221222

223+
if (value.encryptionConfig) {
224+
EncryptionConfiguration = {
225+
KmsDataKeyReusePeriodSeconds: value.encryptionConfig.KmsDataKeyReusePeriodSeconds,
226+
KmsKeyId: {
227+
'Fn::Sub': value.encryptionConfig.KmsKeyId,
228+
},
229+
Type: value.encryptionConfig.Type,
230+
};
231+
}
232+
222233
const stateMachineOutputLogicalId = this
223234
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
224235

@@ -230,6 +241,7 @@ module.exports = {
230241
StateMachineType: stateMachineObj.type,
231242
LoggingConfiguration,
232243
TracingConfiguration,
244+
EncryptionConfiguration,
233245
},
234246
DependsOn,
235247
};

lib/deploy/stepFunctions/compileStateMachines.schema.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ const tracingConfig = Joi.object().keys({
4949
enabled: Joi.boolean().default(false),
5050
});
5151

52+
const encryptionConfig = Joi.object().keys({
53+
KmsDataKeyReusePeriodSeconds: Joi.number().default(900),
54+
KmsKeyId: Joi.string().default(''),
55+
Type: Joi.string().default('AWS_OWNED_KEY'),
56+
});
57+
5258
const iamRoleStatements = Joi.array().items(
5359
Joi.object({
5460
Effect: Joi.string().valid('Allow', 'Deny'),
@@ -82,6 +88,7 @@ const schema = Joi.object().keys({
8288
retain,
8389
loggingConfig,
8490
tracingConfig,
91+
encryptionConfig,
8592
inheritGlobalTags,
8693
iamRoleStatements,
8794
}).oxor('role', 'iamRoleStatements');

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,4 +1808,56 @@ describe('#compileStateMachines', () => {
18081808
orderValue: '{% $states.input.order.total %}',
18091809
});
18101810
});
1811+
1812+
it('should compile with a specified KMS key', () => {
1813+
serverless.service.stepFunctions = {
1814+
stateMachines: {
1815+
myStateMachine1: {
1816+
id: 'Test',
1817+
name: 'Test',
1818+
definition: {},
1819+
encryptionConfig: {
1820+
KmsKeyId: 'arn:kms:...',
1821+
},
1822+
},
1823+
},
1824+
};
1825+
1826+
serverlessStepFunctions.compileStateMachines();
1827+
1828+
const encryptionConfiguration = serverlessStepFunctions.serverless.service
1829+
.provider.compiledCloudFormationTemplate.Resources
1830+
.Test.Properties.EncryptionConfiguration;
1831+
1832+
expect(encryptionConfiguration.KmsKeyId['Fn::Sub']).to.equal('arn:kms:...');
1833+
expect(encryptionConfiguration.KmsDataKeyReusePeriodSeconds).to.equal(900);
1834+
expect(encryptionConfiguration.Type).to.equal('AWS_OWNED_KEY');
1835+
});
1836+
1837+
it('should compile with a specified KMS key, type and reuse period', () => {
1838+
serverless.service.stepFunctions = {
1839+
stateMachines: {
1840+
myStateMachine1: {
1841+
id: 'Test',
1842+
name: 'Test',
1843+
definition: {},
1844+
encryptionConfig: {
1845+
KmsKeyId: 'arn:kms:...',
1846+
KmsDataKeyReusePeriodSeconds: 10,
1847+
Type: 'MANAGED',
1848+
},
1849+
},
1850+
},
1851+
};
1852+
1853+
serverlessStepFunctions.compileStateMachines();
1854+
1855+
const encryptionConfiguration = serverlessStepFunctions.serverless.service
1856+
.provider.compiledCloudFormationTemplate.Resources
1857+
.Test.Properties.EncryptionConfiguration;
1858+
1859+
expect(encryptionConfiguration.KmsKeyId['Fn::Sub']).to.equal('arn:kms:...');
1860+
expect(encryptionConfiguration.KmsDataKeyReusePeriodSeconds).to.equal(10);
1861+
expect(encryptionConfiguration.Type).to.equal('MANAGED');
1862+
});
18111863
});

0 commit comments

Comments
 (0)