---
policy: /ce/ca/aws/apigateway/rest-api-stage-waf-web-acl
logic: /ce/ca/aws/apigateway/rest-api-stage-waf-web-acl/prod.logic.yaml
executionTime: 2025-04-24T23:44:21.775851205Z
rows:
  - id: test1
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: "not(['restApiStage'].contains(RecordType.DeveloperName))"
      actual: "not(['restApiStage'].contains(RecordType.DeveloperName))"
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__webAclArn__c').isEmpty()
      actual: extract('CA10__webAclArn__c').isEmpty()
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/apigateway/rest-api-stage-waf-web-acl/policy.yaml
    md5Hash: 286AF65A6336EBDE5D6AF9767625E939
    content: "---\nnames:\n  full: AWS API Gateway REST API Stage is not associated\
      \ with a WAF Web ACL\n  contextual: REST API Stage is not associated with a\
      \ WAF Web ACL\ndescription: >\n  Ensure that each AWS API Gateway REST API stage\
      \ is associated with a WAF Web \n  ACL to enhance its protection against malicious\
      \ web traffic. AWS WAF enables \n  configuration of rules to block, allow, or\
      \ count web requests based on custom \n  security criteria.\ncategories:\n \
      \ - \"SECURITY\"\ntype: \"COMPLIANCE_POLICY\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/resource-security/threat-protection\"\
      \n  - \"/frameworks/aws-fsbp-v1.0.0/api-gateway/04\"\nsimilarPolicies:\n  internal:\n\
      \    - \"dec-x-bfdadcc4\""
  - path: /ce/ca/aws/apigateway/rest-api-stage-waf-web-acl/prod.logic.yaml
    md5Hash: 4D08B96ECB0A6C69151BDAB3D7E42196
    content: |
      ---
      inputType: "CA10__CaAwsApiGatewayStage__c"
      importExtracts:
        - file: "/types/CA10__CaAwsApiGatewayStage__c/object.extracts.yaml"
      testData:
        - file: test-data.json
      recordTypes:
        - "restApiStage"
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "The stage is not associated with a WAF Web ACL."
          remediationMessage: "Consider associating a WAF Web ACL with an API Gateway Stage"
          check:
            IS_EMPTY:
              arg:
                EXTRACT: "CA10__webAclArn__c"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The stage is associated with a WAF Web ACL."
  - path: /types/CA10__CaAwsApiGatewayStage__c/object.extracts.yaml
    md5Hash: BFC9531C779AD876E602BE4C7AFCAE82
    content: "---\nextracts:\n# Values: OFF | INFO | ERROR. Nullable. Can't have no\
      \ access, retrieved via apigateway:GetStages.\n  - name: CA10__cloudWatchLogLevel__c\n\
      \    value: \n      FIELD:\n        path: CA10__cloudWatchLogLevel__c\n# Values:\
      \ Yes | No. Not nullable. Can't have no access, retrieved via apigateway:GetStages.\n\
      \  - name: CA10__xrayTracingEnabled__c\n    value: \n      FIELD:\n        path:\
      \ CA10__xrayTracingEnabled__c\n# Nullable. Can't have no access, retrieved via\
      \ apigateway:GetStages.\n  - name: CA10__webAclArn__c\n    value: \n      FIELD:\n\
      \        path: CA10__webAclArn__c"
  - path: /ce/ca/aws/apigateway/rest-api-stage-waf-web-acl/test-data.json
    md5Hash: 8B83F1AE712E9D2F7F9D7BB8103DFA6D
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-12T17:27:38Z"
              },
              "Id": "test1",
              "RecordTypeId": "RecordTypeId",
              "CA10__disappearanceTime__c": "2024-12-12T17:27:38Z",
              "CA10__webAclArn__c": "",
              "RecordType": {
                  "DeveloperName": "restApiStage",
                  "Id": "RecordTypeId"
              }
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "not(['restApiStage'].contains(RecordType.DeveloperName))",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-12T17:27:38Z"
              },
              "Id": "test2",
              "RecordTypeId": "RecordTypeId2",
              "CA10__disappearanceTime__c": null,
              "CA10__webAclArn__c": "",
              "RecordType": {
                  "DeveloperName": "httpApiStage",
                  "Id": "RecordTypeId2"
              }
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__webAclArn__c').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-12T17:27:38Z"
              },
              "Id": "test3",
              "RecordTypeId": "RecordTypeId",
              "CA10__disappearanceTime__c": null,
              "CA10__webAclArn__c": "",
              "RecordType": {
                  "DeveloperName": "restApiStage",
                  "Id": "RecordTypeId"
              }
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-12T17:27:38Z"
              },
              "Id": "test4",
              "RecordTypeId": "RecordTypeId",
              "CA10__disappearanceTime__c": null,
              "CA10__webAclArn__c": "WebAclArn",
              "RecordType": {
                  "DeveloperName": "restApiStage",
                  "Id": "RecordTypeId"
              }
          }
      ]
script: |-
  CREATE TEMP FUNCTION mock_ExpectedResult()
  RETURNS ARRAY<STRUCT<
          Id STRING,
          expectedResult STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, runtimeError STRING>
      >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "Id" : "test1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "not(['restApiStage'].contains(RecordType.DeveloperName))",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__webAclArn__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsApiGatewayStage__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      RecordTypeId STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__webAclArn__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-12-12T17:27:38Z")
    },
    "Id" : "test1",
    "RecordTypeId" : "RecordTypeId",
    "CA10__disappearanceTime__c" : new Date("2024-12-12T17:27:38Z"),
    "CA10__webAclArn__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-12T17:27:38Z")
    },
    "Id" : "test2",
    "RecordTypeId" : "RecordTypeId2",
    "CA10__webAclArn__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-12T17:27:38Z")
    },
    "Id" : "test3",
    "RecordTypeId" : "RecordTypeId",
    "CA10__webAclArn__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-12T17:27:38Z")
    },
    "Id" : "test4",
    "RecordTypeId" : "RecordTypeId",
    "CA10__webAclArn__c" : "WebAclArn"
  } ];
  """;

  CREATE TEMP FUNCTION mock_RecordType()
  RETURNS ARRAY<STRUCT<
      DeveloperName STRING,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-12-12T17:27:38Z")
    },
    "DeveloperName" : "restApiStage",
    "Id" : "RecordTypeId"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-12T17:27:38Z")
    },
    "DeveloperName" : "httpApiStage",
    "Id" : "RecordTypeId2"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsApiGatewayStage__c(
      obj STRUCT<
          Id STRING,
          RecordTypeId STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__webAclArn__c STRING,
          RecordType STRUCT<
              DeveloperName STRING,
              Id STRING
          >
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var CollectionLib = new function () {
          this.parse = function(arg, separator) {
              let out = new Set();
              if (arg == null) {
                  return out;
              }
              arg.split(separator).forEach(el => {if (TextLib.isNotEmpty(el)) {out.add(TextLib.normalize(el))}});
              return out;
          };
          this.fromArray = function(arr) {
              let out = new Set();
              if (arr == null) {
                  return out;
              }
              arr.forEach(el => {if (TextLib.isNotEmpty(el)) {out.add(TextLib.normalize(el))}});
              return out;
          };
          this.equal = function(left, right) {
              left = left == null ? new Set() : left;
              right = right == null ? new Set() : right;
              return left.size === right.size && new Set([...left, ...right]).size === left.size;
          };
          this.notEqual = function(left, right) {
              left = left == null ? new Set() : left;
              right = right == null ? new Set() : right;
              return left.size !== right.size || new Set([...left, ...right]).size !== left.size;
          };
          this.contains = function(collection, search) {
              if (collection == null || TextLib.isEmpty(search)) {
                  return false;
              }
              return collection.has(TextLib.normalize(search));
          };
          this.size = function(collection) {
              return collection == null ? 0 : collection.size;
          };
      }();
      var TextLib = new function () {
          this.normalize = function(arg) {
              return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase();
          };
          this.isEmpty = function(arg) {
              return this.normalize(arg) == '';
          };
          this.isNotEmpty = function(arg) {
              return this.normalize(arg) != '';
          };
          this.equal = function(left, right) {
              return this.normalize(left) == this.normalize(right);
          };
          this.notEqual = function(left, right) {
              return this.normalize(left) != this.normalize(right);
          };
          this.startsWith = function(arg, substring) {
              return this.normalize(arg).startsWith(this.normalize(substring));
          };
          this.endsWith = function(arg, substring) {
              return this.normalize(arg).endsWith(this.normalize(substring));
          };
          this.contains = function(arg, substring) {
              return this.normalize(arg).includes(this.normalize(substring));
          };
      }();
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From AWS [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c);
      if (obj.CA10__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Record Type Name [obj.RecordType.DeveloperName]: ' + obj.RecordType.DeveloperName);
      if (!CollectionLib.contains(CollectionLib.fromArray(['restApiStage']), obj.RecordType.DeveloperName)) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "not(['restApiStage'].contains(RecordType.DeveloperName))", currentStateMessage: "Object record type is not applicable", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract4() { if (!this.out) { this.out = obj.CA10__webAclArn__c; } return this.out; };
      references1.push('Web ACL ARN [obj.CA10__webAclArn__c]: ' + obj.CA10__webAclArn__c);
      if (TextLib.isEmpty(extract4.call(extract4))) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__webAclArn__c').isEmpty()", currentStateMessage: "The stage is not associated with a WAF Web ACL.", currentStateReferences: references1.join('\n'), remediation: "Consider associating a WAF Web ACL with an API Gateway Stage", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The stage is associated with a WAF Web ACL.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;


  SELECT
    expectedResult.Id as Id,
    IF (
      IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '')
      AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1)
      AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '')
      AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''),
      "MATCH",
      "FAIL"
    ) as match,
    expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus,
    expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex,
    expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText,
    expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError
  FROM UNNEST(mock_ExpectedResult()) expectedResult
  LEFT JOIN (
      SELECT
          sObject.Id AS Id,
          sObject.RecordTypeId AS RecordTypeId,
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__webAclArn__c AS CA10__webAclArn__c,
          STRUCT (
              `RecordType`.DeveloperName AS DeveloperName,
              `RecordType`.Id AS Id
          ) AS RecordType,
          process_CA10__CaAwsApiGatewayStage__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.RecordTypeId AS RecordTypeId,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__webAclArn__c AS CA10__webAclArn__c,
                  STRUCT (
                      `RecordType`.DeveloperName AS DeveloperName,
                      `RecordType`.Id AS Id
                  ) AS RecordType
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsApiGatewayStage__c()) AS sObject
      LEFT JOIN UNNEST(mock_RecordType()) AS `RecordType`
          ON sObject.RecordTypeId = `RecordType`.Id
  ) sObject ON sObject.Id = expectedResult.Id;
