All files / lib firewall-datasource-construct.ts

84.62% Statements 22/26
25% Branches 1/4
66.67% Functions 2/3
84.62% Lines 22/26

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                              1x 1x           1x 1x 1x 1x 1x           1x               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 dynamodb from "aws-cdk-lib/aws-dynamodb";
import {
  AttributeType,
  BillingMode,
  StreamViewType,
  TableEncryption,
} from "aws-cdk-lib/aws-dynamodb";
import * as kms from "aws-cdk-lib/aws-kms";
import * as sns from "aws-cdk-lib/aws-sns";
import { Annotations, CfnOutput, RemovalPolicy } from "aws-cdk-lib";
import * as subscriptions from "aws-cdk-lib/aws-sns-subscriptions";
import { Construct } from "constructs";
 
export interface AutConfigConstructProps {
  pointInTimeRecovery: boolean;
}
 
export class AutConfigDataSourceConstructConstruct extends Construct {
  readonly rulesTable: dynamodb.Table;
  readonly objectsTable: dynamodb.Table;
  readonly rulebundlesTable: dynamodb.Table;
  readonly auditsTable: dynamodb.Table;
  readonly notificationTopic: sns.Topic;
  readonly snsEncryptionKey: kms.Key;
  constructor(scope: Construct, id: string, props: AutConfigConstructProps) {
    super(scope, id);
 
    const dataBaseTableEncryption = new kms.Key(
      this,
      `auto-config-encryption-key`,
      {
        removalPolicy: RemovalPolicy.RETAIN,
        enableKeyRotation: true,
        alias: "AutoConfigTablesEncryptionKey",
      }
    );
 
    this.rulesTable = new dynamodb.Table(this, "RulesTable", {
      billingMode: BillingMode.PAY_PER_REQUEST,
      encryption: TableEncryption.CUSTOMER_MANAGED,
      encryptionKey: dataBaseTableEncryption,
      stream: StreamViewType.NEW_AND_OLD_IMAGES,
      tableName: "RuleExtensionsRuleTable",
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
      pointInTimeRecovery: props.pointInTimeRecovery,
    });
    this.rulesTable.addGlobalSecondaryIndex({
      indexName: "ruleBundleId",
      partitionKey: { name: "ruleBundleId", type: AttributeType.STRING },
    });
 
    const dataBaseAuditTableEncryption = new kms.Key(
      this,
      `auto-config-audit-encryption-key`,
      {
        removalPolicy: RemovalPolicy.RETAIN,
        enableKeyRotation: true,
        alias: "AutoConfigAuditTablesEncryptionKey",
      }
    );
 
    this.objectsTable = new dynamodb.Table(this, "ObjectsTable", {
      encryption: TableEncryption.CUSTOMER_MANAGED,
      encryptionKey: dataBaseTableEncryption,
      billingMode: BillingMode.PAY_PER_REQUEST,
      stream: StreamViewType.NEW_AND_OLD_IMAGES,
      tableName: "RuleExtensionsObjectTable",
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
      // sortKey: {name: 'namespace', type: dynamodb.AttributeType.STRING },
      pointInTimeRecovery: props.pointInTimeRecovery,
    });
 
    this.rulebundlesTable = new dynamodb.Table(this, "RuleBundlesTable", {
      encryption: TableEncryption.CUSTOMER_MANAGED,
      encryptionKey: dataBaseTableEncryption,
      billingMode: BillingMode.PAY_PER_REQUEST,
      tableName: "RuleExtensionsRuleBundleTable",
      stream: StreamViewType.NEW_AND_OLD_IMAGES,
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
      pointInTimeRecovery: props.pointInTimeRecovery,
    });
 
    this.auditsTable = new dynamodb.Table(this, "AuditsTable", {
      encryption: TableEncryption.CUSTOMER_MANAGED,
      encryptionKey: dataBaseAuditTableEncryption,
      tableName: "RuleExtensionsAuditTable",
      billingMode: BillingMode.PAY_PER_REQUEST,
      stream: StreamViewType.NEW_AND_OLD_IMAGES,
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
      pointInTimeRecovery: props.pointInTimeRecovery,
    });
 
    this.snsEncryptionKey = new kms.Key(
      this,
      `notification-sns-encryption-key`,
      {
        removalPolicy: RemovalPolicy.DESTROY,
        enableKeyRotation: true,
        alias: "RuleEvaluationResultTopicEncryptionKey",
      }
    );
 
    this.notificationTopic = new sns.Topic(this, "RuleEvaluationResultTopic", {
      masterKey: this.snsEncryptionKey,
    });
    this.subscribeToTopic();
 
    new CfnOutput(this, "RuleEvaluationResultTopicARN", {
      value: this.notificationTopic.topicArn,
    });
  }
 
  private subscribeToTopic() {
    const emails = this.node.tryGetContext("failureNotificationTargetEmails");
    Iif (emails) {
      if (!Array.isArray(emails)) {
        Annotations.of(this).addWarning(
          "failureNotificationTargetEmails contains invalid value it should be a list of emails, skip subscription"
        );
      } else {
        (<Array<string>>emails).forEach((email) =>
          this.notificationTopic.addSubscription(
            new subscriptions.EmailSubscription(email)
          )
        );
      }
    }
  }
}