Skip to content

Commit

Permalink
handle rotation in days with scheduleExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Dec 14, 2023
1 parent ada07d7 commit 524ea38
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@
]
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
},
"SecretId": {
"Ref": "DatabaseSecretAttachmentE5D1B020"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@
]
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
},
"SecretId": {
"Ref": "DatabaseSecretAttachmentE5D1B020"
Expand Down Expand Up @@ -920,7 +920,7 @@
]
},
"RotationRules": {
"AutomaticallyAfterDays": 7
"ScheduleExpression": "rate(7 days)"
},
"SecretId": {
"Ref": "CustomRotationOptionsSecretAttachment697A23BF"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@
]
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
},
"SecretId": {
"Ref": "FromSnapshotSnapshotSecretAttachmentA3F619B8"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@
]
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
},
"SecretId": {
"Ref": "InstanceSecretAttachment83BEE581"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"RotationType": "MySQLSingleUser"
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"RotationType": "MySQLSingleUser"
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"RotationType": "MySQLSingleUser"
},
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
}
}
},
Expand Down Expand Up @@ -84,7 +84,7 @@
},
"RotateImmediatelyOnUpdate": false,
"RotationRules": {
"AutomaticallyAfterDays": 30
"ScheduleExpression": "rate(30 days)"
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-docdb/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ describe('DatabaseCluster', () => {
'Fn::GetAtt': ['DatabaseRotationSingleUser65F55654', 'Outputs.RotationLambdaARN'],
},
RotationRules: {
AutomaticallyAfterDays: 5,
ScheduleExpression: 'rate(5 days)',
},
});
});
Expand Down Expand Up @@ -899,7 +899,7 @@ describe('DatabaseCluster', () => {
'Fn::GetAtt': ['DatabaseRotation6B6E1D86', 'Outputs.RotationLambdaARN'],
},
RotationRules: {
AutomaticallyAfterDays: 5,
ScheduleExpression: 'rate(5 days)',
},
});
});
Expand Down
16 changes: 8 additions & 8 deletions packages/aws-cdk-lib/aws-rds/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ describe('cluster', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
});
});
Expand Down Expand Up @@ -2006,7 +2006,7 @@ describe('cluster', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
});

Expand Down Expand Up @@ -2058,7 +2058,7 @@ describe('cluster', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::RotationSchedule', {
RotationRules: {
AutomaticallyAfterDays: 15,
ScheduleExpression: 'rate(15 days)',
},
});

Expand Down Expand Up @@ -2125,7 +2125,7 @@ describe('cluster', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::RotationSchedule', {
RotationRules: {
AutomaticallyAfterDays: 15,
ScheduleExpression: 'rate(15 days)',
},
});

Expand Down Expand Up @@ -2231,7 +2231,7 @@ describe('cluster', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
RotateImmediatelyOnUpdate: false,
});
Expand Down Expand Up @@ -2266,7 +2266,7 @@ describe('cluster', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
RotateImmediatelyOnUpdate: false,
});
Expand Down Expand Up @@ -3439,7 +3439,7 @@ describe('cluster', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::RotationSchedule', {
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
});
});
Expand Down Expand Up @@ -3493,7 +3493,7 @@ describe('cluster', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
});

Expand Down
8 changes: 4 additions & 4 deletions packages/aws-cdk-lib/aws-rds/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ describe('instance', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
});
});
Expand Down Expand Up @@ -810,7 +810,7 @@ describe('instance', () => {
],
},
RotationRules: {
AutomaticallyAfterDays: 30,
ScheduleExpression: 'rate(30 days)',
},
});

Expand Down Expand Up @@ -858,7 +858,7 @@ describe('instance', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::RotationSchedule', {
RotationRules: {
AutomaticallyAfterDays: 15,
ScheduleExpression: 'rate(15 days)',
},
});

Expand Down Expand Up @@ -921,7 +921,7 @@ describe('instance', () => {
// THEN
Template.fromStack(stack).hasResourceProperties('AWS::SecretsManager::RotationSchedule', {
RotationRules: {
AutomaticallyAfterDays: 15,
ScheduleExpression: 'rate(15 days)',
},
});

Expand Down
12 changes: 3 additions & 9 deletions packages/aws-cdk-lib/aws-secretsmanager/lib/rotation-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export class RotationSchedule extends Resource {
);
}

let automaticallyAfterDays: number | undefined;
let scheduleExpression: string | undefined;
if (props.automaticallyAfter) {
const automaticallyAfterMillis = props.automaticallyAfter.toMilliseconds();
Expand All @@ -139,20 +138,15 @@ export class RotationSchedule extends Resource {
if (automaticallyAfterMillis > Duration.days(1000).toMilliseconds()) {
throw new Error(`automaticallyAfter must not be greater than 1000 days, got ${props.automaticallyAfter.toDays()} days`);
}
if (props.automaticallyAfter.toHours() >= 24) {
automaticallyAfterDays = props.automaticallyAfter.toDays();
} else {
scheduleExpression = Schedule.rate(props.automaticallyAfter).expressionString;
}
scheduleExpression = Schedule.rate(props.automaticallyAfter).expressionString;
}
} else {
automaticallyAfterDays = 30;
scheduleExpression = Schedule.rate(Duration.days(30)).expressionString;
}

let rotationRules: CfnRotationSchedule.RotationRulesProperty | undefined;
if (automaticallyAfterDays !== undefined || scheduleExpression !== undefined) {
if (scheduleExpression) {
rotationRules = {
automaticallyAfterDays,
scheduleExpression,
};
}
Expand Down
Loading

0 comments on commit 524ea38

Please sign in to comment.