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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 2x 2x 8x 8x 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 { Annotations, RemovalPolicy, Stack, StackProps, Tags, } from "aws-cdk-lib"; import { IVpc, Port, SecurityGroup } from "aws-cdk-lib/aws-ec2"; import { Effect, IRole, PolicyStatement, Role, ServicePrincipal, } from "aws-cdk-lib/aws-iam"; import * as synthetics from "@aws-cdk/aws-synthetics-alpha"; import { CfnCanary } from "aws-cdk-lib/aws-synthetics"; import { Construct } from "constructs"; import * as fs from "fs"; import * as path from "path"; import { AutConfigConstructConstruct } from "./firewall-auto-config-construct"; import { AutConfigAPIConstructConstruct } from "./firewall-config-api-construct"; import { AutConfigDataSourceConstructConstruct } from "./firewall-datasource-construct"; import { AutConfigNetworkConstruct } from "./firewall-network-construct"; import { ApiServiceDashboard } from "./monitor/api-dashboard"; import { AWSSyntheticsCanary } from "./monitor/api-synthetics-canary"; import { SolutionMetricsCollectorConstruct } from "./monitor/solution-metrics-collector"; import { OpaECSCluster } from "./opa-cluster"; export interface FirewallObjectExtensionSolutionStackProperty extends StackProps { solutionId: string; version: string; } export class FirewallObjectExtensionSolutionStack extends Stack { SOLUTION_ID = "SO0196"; REG_IAM_ARN = /arn:aws:iam::\d{12}:role\/\w+/; public readonly apiGatewayId: string; constructor( scope: Construct, id: string, props: FirewallObjectExtensionSolutionStackProperty ) { super(scope, id, props); const importVpcDetails = this.node.tryGetContext("importVpcDetails") ?? undefined; const networkConstruct = new AutConfigNetworkConstruct( this, "network", importVpcDetails ); const vpc = networkConstruct.vpc; const trail = networkConstruct.trail; const dataSources = new AutConfigDataSourceConstructConstruct( this, "DataSources", { pointInTimeRecovery: true } ); const loglevel = this.node.tryGetContext("loglevel"); const defaultAggregatorName = this.node.tryGetContext( "defaultAggregatorName" ); const networkFirewallRuleGroupNamePattern = this.node.tryGetContext( "networkFirewallRuleGroupNamePattern" ); const secOpsAdminRole = this.importAdminRole(); const crossAccountConfigReadOnlyRole = this.node.tryGetContext( "crossAccountConfigReadOnlyRole" ); const apiGatewayType = this.node.tryGetContext("apiGatewayType"); const crossAccountNetworkFirewallReadWriteRole = this.node.tryGetContext( "crossAccountNetworkFirewallReadWriteRole" ); const enableOpa = !!this.node.tryGetContext("enableOpa"); this.validateRoleFormat( crossAccountNetworkFirewallReadWriteRole, "crossAccountNetworkFirewallReadWriteRole" ); this.validateRoleFormat( crossAccountConfigReadOnlyRole, "crossAccountConfigReadOnlyRole" ); const autoConfigConstruct = new AutConfigConstructConstruct( this, "auto-config", { rulesTable: dataSources.rulesTable, ruleBundlesTable: dataSources.rulebundlesTable, objectsTable: dataSources.objectsTable, notificationTopic: dataSources.notificationTopic, notificationEncryptionKey: dataSources.snsEncryptionKey, vpc: vpc, solutionId: props.solutionId, version: props.version, trail: trail, loglevel, networkFirewallRuleGroupNamePattern, defaultAggregatorName, crossAccountConfigReadOnlyRole, crossAccountNetworkFirewallReadWriteRole, } ); const opaCluster = this.createOPAClusterByConfig(enableOpa, vpc); const canaryRole = new Role(this, `CanaryExecutionRole`, { assumedBy: new ServicePrincipal("lambda"), }); const apiConstruct = new AutConfigAPIConstructConstruct( this, "auto-config-api", { rulesTable: dataSources.rulesTable, ruleBundlesTable: dataSources.rulebundlesTable, objectsTable: dataSources.objectsTable, auditsTable: dataSources.auditsTable, vpc: vpc, opaClusterAlbName: opaCluster?.opaALBDnsName, allowTestInvoke: false, trail: trail, solutionId: props.solutionId, version: props.version, loglevel, networkFirewallRuleGroupNamePattern, defaultAggregatorName, crossAccountConfigReadOnlyRole, crossAccountNetworkFirewallReadWriteRole, apiGatewayType, canaryRole, secOpsAdminRole, } ); // Has to do this after all the construct initialized otherwise it would be a circular dependency Iif (opaCluster) { const allowLambdaSg = new SecurityGroup(this, "SecurityGroup", { vpc: vpc, description: "Security group allowing lambda SG to access OPA ", allowAllOutbound: true, }); allowLambdaSg.addIngressRule( apiConstruct.defaultSecurityGroup, Port.tcp(443) ); opaCluster?.loadBalancedFargateService.loadBalancer.addSecurityGroup( allowLambdaSg ); } this.allowAccessToVpcEndpointOnlyFromLambdas( dataSources, networkConstruct, apiConstruct, autoConfigConstruct ); new ApiServiceDashboard(this, "auto-config-api-dashboard", { serviceName: "auto-config-api", apiName: apiConstruct.api.restApiName, functionName: apiConstruct.apiFunction.functionName, schedulerFunction: autoConfigConstruct.schedulerFunction, alarmsTriggerDuration: autoConfigConstruct.ruleResolutionInterval, auditsTableName: dataSources.auditsTable.tableName, objectsTableName: dataSources.objectsTable.tableName, rulesTableName: dataSources.rulesTable.tableName, ruleBundlesTableName: dataSources.rulebundlesTable.tableName, solutionOperationalTopic: dataSources.notificationTopic, }); this.apiGatewayId = apiConstruct.api.restApiId; // Check ApiGateway type to decide if use VpcEndpoint const useVpcEndpoint = apiGatewayType === "private"; const vpcConfig = useVpcEndpoint ? { vpcId: vpc.vpcId, subnetIds: vpc.privateSubnets.map((subnet) => subnet.subnetId), securityGroupIds: [apiConstruct.defaultSecurityGroup.securityGroupId], } : undefined; const canary = this.createCanary( apiConstruct.api.restApiId, RemovalPolicy.DESTROY, vpcConfig, canaryRole ); canary.canaryRole.addToPolicy( new PolicyStatement({ effect: Effect.ALLOW, actions: ["execute-api:Invoke"], resources: [ `arn:aws:execute-api:${Stack.of(this).region}:${ Stack.of(this).account }:${this.apiGatewayId}/*/GET/*`, ], }) ); const sendAnonymousMetric = this.node.tryGetContext("sendAnonymousMetric") ?? "Yes"; Iif (sendAnonymousMetric && !["Yes", "No"].includes(sendAnonymousMetric)) { Annotations.of(this).addError( "Configuration sendAnonymousMetric can only contain value Yes or No" ); } new SolutionMetricsCollectorConstruct(this, "metrics-collector-construct", { version: props.version, solutionId: props.solutionId, solutionDisplayName: "Dynamic Object and Rule Extensions for AWS Network Firewall", sendAnonymousMetric: sendAnonymousMetric, functionName: "SolutionMetricsCollectorFunction", metricsData: { enabledOpa: enableOpa, crossAccount: crossAccountNetworkFirewallReadWriteRole || crossAccountConfigReadOnlyRole, privateEndpoint: useVpcEndpoint, importedVpc: importVpcDetails ? true : false, }, }); Tags.of(this).add("SOLUTION-ID", props.solutionId); Tags.of(this).add("VERSION", props.version); } private importAdminRole(): IRole | undefined { const secOpsAdminRole = this.node.tryGetContext( "objectExtensionSecOpsAdminRole" ); this.validateRoleFormat(secOpsAdminRole, "objectExtensionSecOpsAdminRole"); Iif (secOpsAdminRole) { return Role.fromRoleArn( this, "objectExtensionSecOpsAdminRole", secOpsAdminRole ); } return undefined; } private validateRoleFormat(role: string, roleName: string) { Eif (!role) { return; } const match = role.match(this.REG_IAM_ARN); if (!match) { Annotations.of(this).addError( `Invalid configuration, ${roleName} is not a valid arn` ); } } private allowAccessToVpcEndpointOnlyFromLambdas( dataSources: AutConfigDataSourceConstructConstruct, networkConstruct: AutConfigNetworkConstruct, apiConstruct: AutConfigAPIConstructConstruct, autoConfigConstruct: AutConfigConstructConstruct ) { const targetTableArns = [ dataSources.auditsTable.tableArn, dataSources.objectsTable.tableArn, dataSources.rulebundlesTable.tableArn, dataSources.rulesTable.tableArn, ]; const targetResourceArn = targetTableArns .map((arn) => [arn ? arn + "/index/*" : "", arn]) .flatMap((i) => i); networkConstruct.ddbEndpoint?.addToPolicy( PolicyStatement.fromJson({ Effect: Effect.ALLOW, Principal: { AWS: "*", }, Action: "dynamodb:*", Resource: targetResourceArn, Condition: { ArnEquals: { "aws:PrincipalArn": apiConstruct.apiFunction.role?.roleArn, }, }, }) ); networkConstruct.ddbEndpoint?.addToPolicy( PolicyStatement.fromJson({ Effect: Effect.ALLOW, Principal: { AWS: "*", }, Action: "dynamodb:*", Resource: targetResourceArn, Condition: { ArnEquals: { "aws:PrincipalArn": autoConfigConstruct.schedulerFunction.role?.roleArn, }, }, }) ); networkConstruct.ddbEndpoint?.addToPolicy( PolicyStatement.fromJson({ Effect: Effect.ALLOW, Principal: { AWS: "*", }, Action: "dynamodb:*", Resource: targetResourceArn, Condition: { ArnEquals: { "aws:PrincipalArn": autoConfigConstruct.autoConfigFunction.role?.roleArn, }, }, }) ); } private createOPAClusterByConfig( enableOpa: boolean, vpc: IVpc ): OpaECSCluster | undefined { Iif (enableOpa) { const opaECSCluster = new OpaECSCluster(this, "opa-cluster", { vpc: vpc, }); return opaECSCluster; } return undefined; } private createCanary( restApiId: string, removePolicy: RemovalPolicy, vpcConfig?: CfnCanary.VPCConfigProperty, canaryRole?: Role ): AWSSyntheticsCanary { const apiCanary = new AWSSyntheticsCanary(this, "canary", { canaryName: "ff-canary", runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_3_0, schedule: synthetics.Schedule.expression("rate(5 minutes)"), test: synthetics.Test.custom({ code: synthetics.Code.fromInline( fs .readFileSync( path.join(__dirname, "../lambda/canary/build/canary/index.js") ) .toString() ), handler: "index.handler", }), environmentVariables: { TEST_TARGET_API: `https://${restApiId}.execute-api.${ Stack.of(this).region }.amazonaws.com/prod/`, }, startAfterCreation: true, removalPolicy: removePolicy, canaryRole: canaryRole, ...(vpcConfig && { vpcConfig: vpcConfig }), }); return apiCanary; } } |