|
| 1 | +import iam = require('@aws-cdk/aws-iam'); |
| 2 | +import sns = require('@aws-cdk/aws-sns'); |
| 3 | +import { Construct, Token } from '@aws-cdk/cdk'; |
| 4 | +import { ManagedRule, RuleProps } from './rule'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Construction properties for a AccessKeysRotated |
| 8 | + */ |
| 9 | +export interface AccessKeysRotatedProps extends RuleProps { |
| 10 | + /** |
| 11 | + * The maximum number of days within which the access keys must be rotated. |
| 12 | + * |
| 13 | + * @default 90 days |
| 14 | + */ |
| 15 | + readonly maxDays?: number; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Checks whether the active access keys are rotated within the number of days |
| 20 | + * specified in `maxDays`. |
| 21 | + * |
| 22 | + * @see https://docs.aws.amazon.com/config/latest/developerguide/access-keys-rotated.html |
| 23 | + * |
| 24 | + * @resource AWS::Config::ConfigRule |
| 25 | + */ |
| 26 | +export class AccessKeysRotated extends ManagedRule { |
| 27 | + constructor(scope: Construct, id: string, props: AccessKeysRotatedProps = {}) { |
| 28 | + super(scope, id, { |
| 29 | + ...props, |
| 30 | + identifier: 'ACCESS_KEYS_ROTATED', |
| 31 | + inputParameters: { |
| 32 | + ...props.maxDays |
| 33 | + ? { |
| 34 | + maxAccessKeyAge: props.maxDays |
| 35 | + } |
| 36 | + : {} |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Construction properties for a CloudFormationStackDriftDetectionCheck |
| 44 | + */ |
| 45 | +export interface CloudFormationStackDriftDetectionCheckProps extends RuleProps { |
| 46 | + /** |
| 47 | + * Whether to check only the stack where this rule is deployed. |
| 48 | + * |
| 49 | + * @default false |
| 50 | + */ |
| 51 | + readonly ownStackOnly?: boolean; |
| 52 | + |
| 53 | + /** |
| 54 | + * The IAM role to use for this rule. It must have permissions to detect drift |
| 55 | + * for AWS CloudFormation stacks. Ensure to attach `config.amazonaws.com` trusted |
| 56 | + * permissions and `ReadOnlyAccess` policy permissions. For specific policy permissions, |
| 57 | + * refer to https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html. |
| 58 | + * |
| 59 | + * @default a role will be created |
| 60 | + */ |
| 61 | + readonly role?: iam.IRole; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Checks whether your CloudFormation stacks' actual configuration differs, or |
| 66 | + * has drifted, from its expected configuration. |
| 67 | + * |
| 68 | + * @see https://docs.aws.amazon.com/config/latest/developerguide/cloudformation-stack-drift-detection-check.html |
| 69 | + * |
| 70 | + * @resource AWS::Config::ConfigRule |
| 71 | + */ |
| 72 | +export class CloudFormationStackDriftDetectionCheck extends ManagedRule { |
| 73 | + private readonly role: iam.IRole; |
| 74 | + |
| 75 | + constructor(scope: Construct, id: string, props: CloudFormationStackDriftDetectionCheckProps = {}) { |
| 76 | + super(scope, id, { |
| 77 | + ...props, |
| 78 | + identifier: 'CLOUDFORMATION_STACK_DRIFT_DETECTION_CHECK', |
| 79 | + inputParameters: { |
| 80 | + cloudformationRoleArn: new Token(() => this.role.roleArn) |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + this.scopeToResource('AWS::CloudFormation::Stack', props.ownStackOnly ? this.node.stack.stackId : undefined); |
| 85 | + |
| 86 | + this.role = props.role || new iam.Role(this, 'Role', { |
| 87 | + assumedBy: new iam.ServicePrincipal('config.amazonaws.com'), |
| 88 | + managedPolicyArns: [ |
| 89 | + new iam.AwsManagedPolicy('ReadOnlyAccess', this).policyArn, |
| 90 | + ] |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Construction properties for a CloudFormationStackNotificationCheck. |
| 97 | + */ |
| 98 | +export interface CloudFormationStackNotificationCheckProps extends RuleProps { |
| 99 | + /** |
| 100 | + * A list of allowed topics. At most 5 topics. |
| 101 | + */ |
| 102 | + readonly topics?: sns.ITopic[]; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Checks whether your CloudFormation stacks are sending event notifications to |
| 107 | + * a SNS topic. Optionally checks whether specified SNS topics are used. |
| 108 | + * |
| 109 | + * @see https://docs.aws.amazon.com/config/latest/developerguide/cloudformation-stack-notification-check.html |
| 110 | + * |
| 111 | + * @resource AWS::Config::ConfigRule |
| 112 | + */ |
| 113 | +export class CloudFormationStackNotificationCheck extends ManagedRule { |
| 114 | + constructor(scope: Construct, id: string, props: CloudFormationStackNotificationCheckProps = {}) { |
| 115 | + if (props.topics && props.topics.length > 5) { |
| 116 | + throw new Error('At most 5 topics can be specified.'); |
| 117 | + } |
| 118 | + |
| 119 | + super(scope, id, { |
| 120 | + ...props, |
| 121 | + identifier: 'CLOUDFORMATION_STACK_NOTIFICATION_CHECK', |
| 122 | + inputParameters: props.topics && props.topics.reduce( |
| 123 | + (params, topic, idx) => ({ ...params, [`snsTopic${idx + 1}`]: topic.topicArn }), |
| 124 | + {} |
| 125 | + ) |
| 126 | + }); |
| 127 | + |
| 128 | + this.scopeToResource('AWS::CloudFormation::Stack'); |
| 129 | + } |
| 130 | +} |
0 commit comments