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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 30x 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 apigateway from "aws-cdk-lib/aws-apigateway"; import { LambdaIntegration } from "aws-cdk-lib/aws-apigateway"; import * as dynamodb from "aws-cdk-lib/aws-dynamodb"; import * as iam from "aws-cdk-lib/aws-iam"; import * as lambda from "aws-cdk-lib/aws-lambda"; import { Tracing } from "aws-cdk-lib/aws-lambda"; import { CfnOutput, Duration, Names, Stack } from "aws-cdk-lib"; import * as ec2 from "aws-cdk-lib/aws-ec2"; import * as path from "path"; import * as logs from "aws-cdk-lib/aws-logs"; import * as kms from "aws-cdk-lib/aws-kms"; import * as cdk from "aws-cdk-lib"; import * as sqs from "aws-cdk-lib/aws-sqs"; import * as cloudtrail from "aws-cdk-lib/aws-cloudtrail"; import { Construct } from "constructs"; export interface AutConfigAPIConstructProps { ruleBundlesTable: dynamodb.Table; rulesTable: dynamodb.Table; objectsTable: dynamodb.Table; auditsTable: dynamodb.Table; allowTestInvoke: boolean; opaClusterAlbName?: string; vpc: ec2.IVpc; additionalRoles?: iam.Role[]; solutionId: string; version: string; trail?: cloudtrail.Trail; networkFirewallRuleGroupNamePattern?: string; loglevel?: string; defaultAggregatorName?: string; crossAccountConfigReadOnlyRole?: string; crossAccountNetworkFirewallReadWriteRole?: string; apiGatewayType?: string; canaryRole?: iam.Role; secOpsAdminRole?: iam.IRole; } export interface ApiEndpoint { resourcePath: string; httpMethod: string; } export interface APIUserPermission { endpoints: ApiEndpoint[]; allowedPersona: PersonaType[]; exactMatch?: boolean; } type PersonaType = "SEC_OPS" | "APP_OWNER"; export interface APIRolePersona { personaType: PersonaType; roleArn: string; } const permissionDefinitions: APIUserPermission[] = [ { endpoints: [ { httpMethod: "GET", resourcePath: "audits" }, { httpMethod: "POST", resourcePath: "objects" }, { httpMethod: "GET", resourcePath: "objects" }, { httpMethod: "GET", resourcePath: "objects/{id}" }, { httpMethod: "PUT", resourcePath: "objects/{id}" }, { httpMethod: "DELETE", resourcePath: "objects/{id}" }, { httpMethod: "POST", resourcePath: "rulebundles" }, { httpMethod: "GET", resourcePath: "rulebundles" }, { httpMethod: "PUT", resourcePath: "rulebundles/{id}" }, { httpMethod: "GET", resourcePath: "rulebundles/{id}" }, { httpMethod: "POST", resourcePath: "rulebundles/{id}/rules" }, { httpMethod: "GET", resourcePath: "rulebundles/{id}/rules" }, { httpMethod: "GET", resourcePath: "rulebundles/{id}/rules/{ruleId}" }, { httpMethod: "PUT", resourcePath: "rulebundles/{id}/rules/{ruleId}" }, { httpMethod: "DELETE", resourcePath: "rulebundles/{id}/rules/{ruleId}" }, ], allowedPersona: ["SEC_OPS"], }, ]; export class AutConfigAPIConstructConstruct extends Construct { public readonly api: apigateway.RestApi; public readonly apiFunction: lambda.Function; public readonly defaultSecurityGroup: ec2.SecurityGroup; private apiGatewayType: string; private readonly canaryRole?: iam.IRole; constructor(scope: Construct, id: string, props: AutConfigAPIConstructProps) { super(scope, id); this.canaryRole = props.canaryRole; const functionName = "AutoConfigAPIFunction"; const defaultPolicyIds = this.node.tryGetContext("defaultPolicyIds") ?? "forbidden_cross_object_reference,forbidden_create_modify_deny_rules_for_non_admin"; const loglevel = props.loglevel ?? "DEBUG"; const defaultAggregatorName = props.defaultAggregatorName ?? "org-replicator"; const ruleGroupNamePattern = props.networkFirewallRuleGroupNamePattern ?? "default-anfwconfig-rule-*"; this.apiGatewayType = props.apiGatewayType ?? "private"; const functionRole = new iam.Role(this, "ExecutionRole", { assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"), ...(props.crossAccountConfigReadOnlyRole && { roleName: `${functionName}ExecutionRole`, }), description: `Lambda execution role for function`, managedPolicies: [ iam.ManagedPolicy.fromAwsManagedPolicyName( "service-role/AWSLambdaBasicExecutionRole" ), // must to have this one for lambda to run in VPC iam.ManagedPolicy.fromAwsManagedPolicyName( "service-role/AWSLambdaVPCAccessExecutionRole" ), ], }); // Check ApiGateway type to decide if use VpcEndpoint const useVpcEndpoint = this.apiGatewayType === "private"; // compose VpcEndpoint setting const endpointConfig = this.createAPIgateWayEndpointConfig( useVpcEndpoint, props ); this.defaultSecurityGroup = new ec2.SecurityGroup(this, "SecurityGroup", { vpc: props.vpc, description: "Security group for fire fly Lambda Function " + Names.uniqueId(this), allowAllOutbound: true, }); const additionalPolicies = [ new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: [ "network-firewall:ListRuleGroups", "network-firewall:DescribeRuleGroup", "network-firewall:UpdateRuleGroup", ], resources: [ `arn:aws:network-firewall:${Stack.of(this).region}:${ Stack.of(this).account }:stateful-rulegroup/${ruleGroupNamePattern}`, ], }), new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: [ "config:SelectAggregateResourceConfig", "config:DescribeConfigurationAggregators", ], // https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsconfig.html only * can be added resources: [`*`], }), ]; // Allow cross account assume role if cross account role provided this.attachAcrossAccountAssumeRolePermission( additionalPolicies, props.crossAccountConfigReadOnlyRole ); this.attachAcrossAccountAssumeRolePermission( additionalPolicies, props.crossAccountNetworkFirewallReadWriteRole ); const apiFunctionDLQ = new sqs.Queue(this, "apiFunctionDLQ", { encryption: sqs.QueueEncryption.KMS_MANAGED, }); this.apiFunction = new lambda.Function( this, "autoConfig", { handler: "app.lambdaHandler", code: lambda.Code.fromAsset( path.resolve( __dirname, `../lambda/firewall-config-api/.aws-sam/build/${functionName}` ) ), timeout: Duration.seconds(30), initialPolicy: [...additionalPolicies], runtime: lambda.Runtime.NODEJS_14_X, role: functionRole, deadLetterQueue: apiFunctionDLQ, memorySize: 3008, tracing: Tracing.ACTIVE, vpc: props.vpc, securityGroups: [this.defaultSecurityGroup], environment: { RULES_TABLE_NAME: props.rulesTable.tableName, OBJECTS_TABLE_NAME: props.objectsTable.tableName, RULEBUNDLES_TABLE_NAME: props.ruleBundlesTable.tableName, AUDITS_TABLE_NAME: props.auditsTable.tableName, LOGLEVEL: loglevel, DEFAULT_AGGREGATOR_NAME: defaultAggregatorName, ...(props.opaClusterAlbName && { OPA_URL: props.opaClusterAlbName }), OPA_POLICY_LIST: defaultPolicyIds, CROSS_ACCOUNT_CONFIG_ROLE: props.crossAccountConfigReadOnlyRole ?? "", CROSS_ACCOUNT_ANFW_ROLE: props.crossAccountNetworkFirewallReadWriteRole ?? "", SOLUTION_ID: props.solutionId, VERSION: props.version, }, } ); Eif (props.trail) { props.trail.addLambdaEventSelector([this.apiFunction]); } props.objectsTable.grantReadWriteData(this.apiFunction); props.ruleBundlesTable.grantReadWriteData(this.apiFunction); props.rulesTable.grantReadWriteData(this.apiFunction); props.auditsTable.grantReadWriteData(this.apiFunction); const adminUserRole = props.secOpsAdminRole ?? new iam.Role(this, "api-admin-role", { assumedBy: new iam.AccountRootPrincipal(), roleName: `ObjectExtensionSecOpsAdminRole-${Stack.of(this).region}`, }); const appOwnerApiAccessRole = new iam.Role(this, "api-app-owner-role", { assumedBy: new iam.AccountRootPrincipal(), }); const serviceName = "NetworkFirewallObjectExtension"; const personas: APIRolePersona[] = [ { personaType: "SEC_OPS", roleArn: adminUserRole.roleArn }, { personaType: "APP_OWNER", roleArn: appOwnerApiAccessRole.roleArn }, ]; const policyDoc = this.createAPIgatewayResourcePolicy(personas, props); const accessLogGroup = this.createAccessLogGroup(); this.api = new apigateway.RestApi(this, `API`, { description: `Rest Api for Firewall config`, defaultIntegration: new LambdaIntegration(this.apiFunction, { proxy: true, //lambda proxy should be always on allowTestInvoke: props.allowTestInvoke, }), defaultCorsPreflightOptions: { allowOrigins: apigateway.Cors.ALL_ORIGINS, }, defaultMethodOptions: { authorizationType: apigateway.AuthorizationType.IAM, }, policy: policyDoc, endpointConfiguration: endpointConfig, deployOptions: { metricsEnabled: true, tracingEnabled: true, accessLogDestination: new apigateway.LogGroupLogDestination( accessLogGroup ), accessLogFormat: apigateway.AccessLogFormat.jsonWithStandardFields(), }, restApiName: `${serviceName}-API`, }); this.api.addUsagePlan("API-usage-plan").addApiStage({ stage: this.api.deploymentStage, }); adminUserRole.addToPrincipalPolicy( new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ["execute-api:Invoke"], resources: [ `arn:aws:execute-api:${Stack.of(this).region}:${ Stack.of(this).account }:${this.api.restApiId}/*/*/*`, ], }) ); const rulebundlesResource = this.api.root.addResource("rulebundles"); rulebundlesResource.addMethod("GET"); rulebundlesResource.addMethod("POST"); this.apiFunction.addEnvironment( "ADMINISTRATOR_ROLE", adminUserRole.roleArn ); this.apiFunction.addEnvironment( "APPLICATION_OWNER_ROLES", appOwnerApiAccessRole.roleArn ); new CfnOutput(this, "adminRoleArn", { value: adminUserRole.roleArn, }); const rulebundlesIdResource = rulebundlesResource.addResource("{id}"); rulebundlesIdResource.addMethod("GET"); rulebundlesIdResource.addMethod("PUT"); rulebundlesIdResource.addMethod("DELETE"); const rulesResource = rulebundlesIdResource.addResource("rules"); rulesResource.addMethod("POST"); rulesResource.addMethod("GET"); const ruleIdResource = rulesResource.addResource("{ruleId}"); ruleIdResource.addMethod("PUT"); ruleIdResource.addMethod("GET"); ruleIdResource.addMethod("DELETE"); const objectsResource = this.api.root.addResource("objects"); objectsResource.addMethod("GET"); objectsResource.addMethod("POST"); const objectsIdResource = objectsResource.addResource("{id}"); objectsIdResource.addMethod("GET"); objectsIdResource.addMethod("PUT"); objectsIdResource.addMethod("DELETE"); const auditsResource = this.api.root.addResource("audits"); auditsResource.addMethod("GET"); } private attachAcrossAccountAssumeRolePermission( additionalPolicies: iam.PolicyStatement[], crossAccountConfigReadonlyRole?: string ) { Iif (crossAccountConfigReadonlyRole) { additionalPolicies.push( new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ["sts:AssumeRole"], resources: [crossAccountConfigReadonlyRole], }) ); } } private createAccessLogGroup() { const encryptionKey = new kms.Key(this, "VpcFlowLogsKey", { removalPolicy: cdk.RemovalPolicy.DESTROY, enableKeyRotation: true, }); encryptionKey.addToResourcePolicy( new iam.PolicyStatement({ effect: iam.Effect.ALLOW, sid: "Allow Access Logs to use the key", principals: [ new iam.ServicePrincipal( `logs.${Stack.of(this).region}.amazonaws.com` ), ], actions: [ "kms:ReEncrypt", "kms:GenerateDataKey", "kms:Encrypt", "kms:DescribeKey", "kms:Decrypt", ], // This is a resource policy resources: ["*"], }) ); const accessLogGroup = new logs.LogGroup(this, "ApiGatewayAccessLogs", { encryptionKey: encryptionKey, }); return accessLogGroup; } private createAPIgatewayResourcePolicy( personas: APIRolePersona[], props: AutConfigAPIConstructProps ) { const policyDoc = this.composeApiResourcePolicy( permissionDefinitions, personas ); Eif (this.apiGatewayType === "private") { policyDoc.addStatements( iam.PolicyStatement.fromJson({ Effect: iam.Effect.DENY, Principal: { AWS: "*", }, Action: "execute-api:Invoke", Resource: `arn:aws:execute-api:${Stack.of(this).region}:${ Stack.of(this).account }:*/*/*/*`, Condition: { StringNotEquals: { "aws:sourceVpc": props.vpc.vpcId, }, }, }) ); } return policyDoc; } composeApiResourcePolicy( permissionDefinitions: APIUserPermission[], personas: APIRolePersona[] ): iam.PolicyDocument { const policyDoc = new iam.PolicyDocument(); const apiResourceStatements: iam.PolicyStatement[] = []; permissionDefinitions.forEach((permission: APIUserPermission) => { const personaRoles = personas.filter((p) => permission.allowedPersona.includes(p.personaType) ); // generate resource list of this statement const resources = permission.endpoints.map( ({ resourcePath, httpMethod }) => `execute-api:/*/${httpMethod.toUpperCase()}/${resourcePath}` ); // allow statement const allowStatements = personaRoles.map((pr) => { return iam.PolicyStatement.fromJson({ Effect: iam.Effect.ALLOW, Principal: { AWS: "*", }, Action: "execute-api:Invoke", Resource: resources, Condition: { StringEquals: { "aws:PrincipalArn": pr.roleArn, }, }, }); }); // deny statement const denyIfNotGivenPersonalStatements = personaRoles.map((pr) => { return iam.PolicyStatement.fromJson({ Effect: iam.Effect.DENY, Principal: { AWS: "*", }, Action: "execute-api:Invoke", Resource: `arn:aws:execute-api:${Stack.of(this).region}:${ Stack.of(this).account }:*/*/*`, Condition: { "ForAllValues:StringNotEquals": { "aws:PrincipalArn": [pr.roleArn, this.canaryRole?.roleArn], }, }, }); }); apiResourceStatements.push( ...allowStatements, ...denyIfNotGivenPersonalStatements ); }); policyDoc.addStatements(...apiResourceStatements); return policyDoc; } private createAPIgateWayEndpointConfig( useVpcEndpoint: boolean, props: AutConfigAPIConstructProps ) { let endpointConfig = undefined; Eif (useVpcEndpoint) { const vpcEndpointSecurityGroup = new ec2.SecurityGroup( this, `apigw-vpc-endpoint-sg`, { vpc: props.vpc, allowAllOutbound: false, } ); vpcEndpointSecurityGroup.addIngressRule( ec2.Peer.ipv4(props.vpc.vpcCidrBlock), ec2.Port.tcp(443) ); vpcEndpointSecurityGroup.addEgressRule( ec2.Peer.ipv4(props.vpc.vpcCidrBlock), ec2.Port.tcp(443) ); const vpcEndPoint = new ec2.InterfaceVpcEndpoint( this, `api-vpc-endpoint`, { vpc: props.vpc, service: ec2.InterfaceVpcEndpointAwsService.APIGATEWAY, privateDnsEnabled: true, securityGroups: [vpcEndpointSecurityGroup], subnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS, }, } ); endpointConfig = { types: [apigateway.EndpointType.PRIVATE], vpcEndpoints: [vpcEndPoint], }; } else { endpointConfig = { types: [apigateway.EndpointType.EDGE], }; } return endpointConfig; } } |