Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import * as iam from "aws-cdk-lib/aws-iam"; import * as s3 from "aws-cdk-lib/aws-s3"; import * as ec2 from "aws-cdk-lib/aws-ec2"; import * as synthetics from "@aws-cdk/aws-synthetics-alpha"; import { Construct } from "constructs"; import { Aspects, Duration, RemovalPolicy, Stack } from "aws-cdk-lib"; import { AWSSecureBucket } from "./aws-secure-bucket"; import { SubnetType } from "aws-cdk-lib/aws-ec2"; import { CfnNagResourcePathEndingWithSuppressionAspect } from "../cfn-nag-suppression"; export interface AWSSyntheticsCanaryProps { /** * Name of the canary, must match ^[0-9a-z_\-]+$ * * @required */ readonly canaryName: string; /** * Specify the runtime version to use for the canary. * * @required */ readonly runtime: synthetics.Runtime; /** * The type of test that you want your canary to run. * * @required */ readonly test: synthetics.Test; /** * Specify the schedule for how often the canary runs. * * @optional * @default Once every 5 minutes (rate(5 minutes)) */ readonly schedule?: synthetics.Schedule; /** * Whether or not the canary should start after creation. * * @optional * @default true */ readonly startAfterCreation?: boolean; /** * Environment variables to be passed into canary test script * * @optional */ readonly environmentVariables?: Record<string, string>; /** * Canary test timeout in seconds * * @optional * @default 15 seconds */ readonly timeoutInSeconds?: number; readonly canaryRole?: iam.Role; /** * VPC configuration if canary will run inside the VPC * * @optional * @default Canary will run without VPC */ readonly vpc?: ec2.IVpc; /** * The S3 bucket prefix * * @optional - Specify this if you want a more specific path within the artifacts bucket. * @default No prefix */ readonly s3BucketPrefix?: string; /** * Specify if the artifact bucket should be removed when canary is destroyed * * Available option is in cdk.RemovalPolicy * * @optional * @default cdk.RemovalPolicy.DESTROY */ readonly removalPolicy?: RemovalPolicy; /** * The canary's bucket encryption key arn * * @optional - If a key arn is specified, the corresponding KMS key will be used to encrypt canary S3 bucket. * @default None - A new key is provisioned for the canary S3 bucket. */ readonly s3BucketEncryptionKeyArn?: string; /** The canary log bucket * * @optional - All canary logs will be stored in the provided bucket. * @default None - A new bucket is provisioned for the canary. */ readonly canaryLogBucket?: s3.IBucket; /** The number of days to retain data about failed runs of this canary * * @optional * @default None - If none of provided, cdk automatically applies the default value of 31 days. */ readonly failureLogRetentionPeriod?: number; /** The number of days to retain data about successful runs of this canary * * @optional * @default None - If none of provided, cdk automatically applies the default value of 31 days. */ readonly successLogRetentionPeriod?: number; } const canaryNameReg = /^[0-9a-z_-]+$/; export class AWSSyntheticsCanary extends Construct { public readonly canaryRole: iam.Role; public constructor( scope: Construct, id: string, props: AWSSyntheticsCanaryProps ) { super(scope, id); Iif (props.canaryName.length > 21) { throw new Error("Canary name must be less than 21 characters in length."); } Iif (!canaryNameReg.test(props.canaryName)) { throw new Error(`Invalid canary name, must match /^[0-9a-z_-]+$/`); } const removePolicy = props.removalPolicy ?? RemovalPolicy.DESTROY; // create canary artifacts bucket const artifactsBucket = props.canaryLogBucket ? props.canaryLogBucket : new AWSSecureBucket(this, "CanaryArtifactBucket", { autoDeleteObjects: removePolicy === RemovalPolicy.DESTROY, removalPolicy: removePolicy, encryptionKeyArn: props.s3BucketEncryptionKeyArn, }).bucket; const prefix = props.s3BucketPrefix || ""; Eif (props.canaryRole) { const policyDoc = this.getCanaryRolePolicyDoc(artifactsBucket, prefix); this.canaryRole = props.canaryRole; this.canaryRole.addManagedPolicy( iam.ManagedPolicy.fromAwsManagedPolicyName( "service-role/AWSLambdaBasicExecutionRole" ) ); this.canaryRole.addManagedPolicy( iam.ManagedPolicy.fromAwsManagedPolicyName( "service-role/AWSLambdaVPCAccessExecutionRole" ) ); this.canaryRole.attachInlinePolicy( new iam.Policy(this, "canaryPolicy", { document: policyDoc }) ); } else { // create canary execution role this.canaryRole = new iam.Role(this, `CanaryExecutionRole`, { assumedBy: new iam.ServicePrincipal("lambda"), managedPolicies: [ iam.ManagedPolicy.fromAwsManagedPolicyName( "service-role/AWSLambdaBasicExecutionRole" ), // must to have this one for lambda to run in VPC iam.ManagedPolicy.fromAwsManagedPolicyName( "service-role/AWSLambdaVPCAccessExecutionRole" ), ], inlinePolicies: { // eslint-disable-next-line @typescript-eslint/naming-convention CanaryPolicy: this.getCanaryRolePolicyDoc(artifactsBucket, prefix), }, description: "Execution Role for CloudWatch Synthetics Canary", }); const scheduleExpressString = props.schedule?.expressionString ?? "rate(5 minutes)"; // create synthetics canary const canary = new synthetics.Canary(this, "Canary", { artifactsBucketLocation: { bucket: artifactsBucket, prefix, }, role: this.canaryRole, runtime: props.runtime, canaryName: props.canaryName, schedule: synthetics.Schedule.expression(scheduleExpressString), startAfterCreation: props.startAfterCreation ?? true, test: props.test, environmentVariables: props.environmentVariables, vpc: props.vpc, vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS, }, failureRetentionPeriod: Duration.days( props.failureLogRetentionPeriod ?? 31 ), successRetentionPeriod: Duration.days( props.successLogRetentionPeriod ?? 31 ), }); Aspects.of(canary).add( new CfnNagResourcePathEndingWithSuppressionAspect( "/Canary/SecurityGroup/Resource", [ { id: "W40", reason: "Security group is created by CDK and is egress only", }, { id: "W5", reason: "Security group is created by CDK and is egress only", }, ] ) ); } } private getCanaryRolePolicyDoc( artifactsBucket: s3.IBucket, prefix: string ): iam.PolicyDocument { const { partition } = Stack.of(this); const policy = new iam.PolicyDocument({ statements: [ new iam.PolicyStatement({ resources: ["arn:aws:s3:::*"], actions: ["s3:ListAllMyBuckets"], }), new iam.PolicyStatement({ resources: [ artifactsBucket.arnForObjects(`${prefix ? prefix + "/*" : "*"}`), ], actions: ["s3:PutObject", "s3:GetBucketLocation"], }), new iam.PolicyStatement({ resources: [artifactsBucket.bucketArn], actions: ["s3:GetBucketLocation"], }), new iam.PolicyStatement({ resources: ["*"], actions: ["cloudwatch:PutMetricData"], conditions: { // eslint-disable-next-line @typescript-eslint/naming-convention StringEquals: { "cloudwatch:namespace": "CloudWatchSynthetics" }, }, }), new iam.PolicyStatement({ resources: ["*"], actions: ["xray:PutTraceSegments"], }), new iam.PolicyStatement({ resources: [`arn:${partition}:logs:::*`], actions: [ "logs:CreateLogStream", "logs:CreateLogGroup", "logs:PutLogEvents", ], }), ], }); return policy; } } |