---
policy: /ce/ca/aws/ec2/security-group-allows-unrestricted-traffic-to-all-ports
logic: /ce/ca/aws/ec2/security-group-allows-unrestricted-traffic-to-all-ports/prod.logic.yaml
executionTime: 2025-04-24T23:44:44.310459483Z
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: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)
      actual: CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)
      actual: CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: test5
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test6
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test7
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test8
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/ec2/security-group-allows-unrestricted-traffic-to-all-ports/policy.yaml
    md5Hash: 6A0899EEE38E249A32C47C0B6B1090E5
    content: "---\nnames:\n  full: AWS EC2 Security Group allows unrestricted traffic\
      \ to all ports\n  contextual: Security Group allows unrestricted traffic to\
      \ all ports\ndescription: >\n  Ensure that AWS EC2 Security Groups are configured\
      \ to avoid unrestricted \n  traffic to all ports. This involves reviewing and\
      \ limiting overly permissive \n  inbound rules that expose all ports (0-65535)\
      \ to unrestricted IP ranges (0.0.0.0/0).\ntype: \"COMPLIANCE_POLICY\"\ncategories:\n\
      \  - \"SECURITY\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/resource-security/threat-protection\"\
      \nsimilarPolicies:\n  internal:\n    - dec-x-3e95721c"
  - path: /ce/ca/aws/ec2/security-group-allows-unrestricted-traffic-to-all-ports/prod.logic.yaml
    md5Hash: 4802471D8E310F22E5A826FD4EF7EDD1
    content: |-
      ---
      inputType: "CA10__CaAwsSecurityGroup__c"
      testData:
        - file: "test-data.json"
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "There are rules that open common ports to the inbound connections"
          remediationMessage: "Remove the rules or adjust them to accept connections from\
            \ narrower CIDRs"
          check:
            RELATED_LIST_HAS:
              relationshipName: "CA10__AWS_EC2_Security_Group_Rules__r"
              status: "INCOMPLIANT"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "Group has no rules that open common ports to the internet"
      relatedLists:
        - relationshipName: "CA10__AWS_EC2_Security_Group_Rules__r"
          importExtracts:
          - file: "/types/CA10__CaAwsSecurityGroupRule2__c/object.extracts.yaml"
          conditions:
            - status: "INAPPLICABLE"
              currentStateMessage: "This is an outbound security group rule."
              check:
                NOT_EQUAL:
                  left:
                    EXTRACT: "CA10__direction__c"
                  right:
                    TEXT: "Inbound"
            - status: "INAPPLICABLE"
              currentStateMessage: "This is not an IP source based security group rule."
              check:
                NOT_EQUAL:
                  left:
                    EXTRACT: "CA10__source__c"
                  right:
                    TEXT: "IP"
            - status: "INAPPLICABLE"
              currentStateMessage: "This security group rule does not allow unrestricted access."
              check:
                AND:
                  args:
                    - NOT_EQUAL:
                        left:
                          EXTRACT: "CA10__sourceIpRange__c"
                        right:
                          TEXT: "0.0.0.0/0"
                    - NOT_EQUAL:
                        left:
                          EXTRACT: "CA10__sourceIpRange__c"
                        right:
                          TEXT: "::/0"
            - status: "INCOMPLIANT"
              currentStateMessage: "Rule opens all protocols and all ports to the internet"
              remediationMessage: "Remove the rule or narrow it down to a specific port,\
                \ protocol and CIDR"
              check:
                AND:
                  args:
                    - IS_EQUAL:
                        left:
                          EXTRACT: "CA10__protocol__c"
                        right:
                          TEXT: "All"
                    - IS_EMPTY:
                        arg:
                          EXTRACT: "CA10__fromPort__c"
                    - IS_EMPTY:
                        arg:
                          EXTRACT: "CA10__toPort__c"
            - status: "INCOMPLIANT"
              currentStateMessage: "Rule opens all protocols and all ports to the internet"
              remediationMessage: "Remove the rule or narrow it down to a specific port,\
                \ protocol and CIDR"
              check:
                AND:
                  args:
                    - IS_EQUAL:
                        left:
                          EXTRACT: "CA10__protocol__c"
                        right:
                          TEXT: "tcp"
                    - IS_EQUAL:
                        left:
                          EXTRACT: "CA10__fromPort__c"
                        right:
                          NUMBER: 0.0
                    - IS_EQUAL:
                        left:
                          EXTRACT: "CA10__toPort__c"
                        right:
                          NUMBER: 65535.0
          otherwise:
            status: "COMPLIANT"
            currentStateMessage: "This Security Group doesn't allow unrestricted access."
  - path: /ce/ca/aws/ec2/security-group-allows-unrestricted-traffic-to-all-ports/test-data.json
    md5Hash: 1FE3DA87009DD3D0A523336A9A37C862
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2024-05-27T17:37:28Z",
              "CA10__AWS_EC2_Security_Group_Rules__r": []
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__AWS_EC2_Security_Group_Rules__r": [
                  {
                      "Id": "test2_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__direction__c": "Inbound",
                      "CA10__source__c": "IP",
                      "CA10__sourceIpRange__c": "0.0.0.0/0",
                      "CA10__protocol__c": "tcp",
                      "CA10__fromPort__c": 0,
                      "CA10__toPort__c": 65535,
                      "CA10__securityGroup__c": "test2"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__AWS_EC2_Security_Group_Rules__r": [
                  {
                      "Id": "test3_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__direction__c": "Inbound",
                      "CA10__sourceIpRange__c": "::/0",
                      "CA10__source__c": "IP",
                      "CA10__protocol__c": "All",
                      "CA10__fromPort__c": null,
                      "CA10__toPort__c": null,
                      "CA10__securityGroup__c": "test3"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test5",
              "CA10__disappearanceTime__c": null,
              "CA10__AWS_EC2_Security_Group_Rules__r": [
                  {
                      "Id": "test5_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__direction__c": "Outbound",
                      "CA10__sourceIpRange__c": "::/0",
                      "CA10__source__c": "IP",
                      "CA10__protocol__c": "tcp",
                      "CA10__fromPort__c": null,
                      "CA10__toPort__c": null,
                      "CA10__securityGroup__c": "test5"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test6",
              "CA10__disappearanceTime__c": null,
              "CA10__AWS_EC2_Security_Group_Rules__r": [
                  {
                      "Id": "test6_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__direction__c": "Inbound",
                      "CA10__source__c": "Group",
                      "CA10__sourceIpRange__c": "0.0.0.0/0",
                      "CA10__protocol__c": "tcp",
                      "CA10__fromPort__c": null,
                      "CA10__toPort__c": null,
                      "CA10__securityGroup__c": "test6"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test7",
              "CA10__disappearanceTime__c": null,
              "CA10__AWS_EC2_Security_Group_Rules__r": [
                  {
                      "Id": "test7_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__direction__c": "Inbound",
                      "CA10__source__c": "IP",
                      "CA10__sourceIpRange__c": "0.0.0.0/0",
                      "CA10__protocol__c": "tcp",
                      "CA10__fromPort__c": 1111,
                      "CA10__toPort__c": 1111,
                      "CA10__securityGroup__c": "test7"
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-30T02:23:36Z"
              },
              "Id": "test8",
              "CA10__disappearanceTime__c": null,
              "CA10__AWS_EC2_Security_Group_Rules__r": []
          }
      ]
  - path: /types/CA10__CaAwsSecurityGroupRule2__c/object.extracts.yaml
    md5Hash: 58DC1872D7462985D50972C65C61C237
    content: "---\nextracts:\n# Values: IP, Group, PrefixList. Nullable, it can be\
      \ prefix list if empty. Can't have no access, retrieved via ec2:DescribeSecurityGroups\n\
      \  - name: \"CA10__source__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10__source__c\"\n# Values: IPv4, IPv6. Nullable it is not the IP source if\
      \ empty. Can't have no access, retrieved via ec2:DescribeSecurityGroups\n  -\
      \ name: \"CA10__sourceIpVersion__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__sourceIpVersion__c\"\n# Nullable. Can't have no access, retrieved\
      \ via ec2:DescribeSecurityGroups\n  - name: \"CA10__sourceIpRange__c\"\n   \
      \ value: \n      FIELD:\n        path: \"CA10__sourceIpRange__c\"\n# Values:\
      \ Inbound, Outbound. Not nullable. Can't have no access, retrieved via ec2:DescribeSecurityGroups\n\
      \  - name: \"CA10__direction__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__direction__c\"\n        undeterminedIf:\n          isEmpty: \"Corrupted\
      \ data. Rule Action cannot be empty.\"\n# Not nullable. Can't have no access,\
      \ retrieved via ec2:DescribeSecurityGroups\n  - name: \"CA10__protocol__c\"\n\
      \    value: \n      FIELD:\n        path: \"CA10__protocol__c\"\n        undeterminedIf:\n\
      \          isEmpty: \"Corrupted data. Protocol cannot be empty.\"\n# Nullable.\
      \ Can't have no access, retrieved via ec2:DescribeSecurityGroups\n  - name:\
      \ \"CA10__fromPort__c\"\n    value: \n      FIELD:\n        path: \"CA10__fromPort__c\"\
      \n# Nullable. Can't have no access, retrieved via ec2:DescribeSecurityGroups\n\
      \  - name: \"CA10__toPort__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10__toPort__c\"\n"
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" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test5",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test6",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test7",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test8",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsSecurityGroup__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2024-05-27T17:37:28Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test6"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test7"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test8"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsSecurityGroupRule2__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__direction__c STRING,
      CA10__source__c STRING,
      CA10__sourceIpRange__c STRING,
      CA10__protocol__c STRING,
      CA10__fromPort__c FLOAT64,
      CA10__toPort__c FLOAT64,
      CA10__securityGroup__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test2_1",
    "CA10__direction__c" : "Inbound",
    "CA10__source__c" : "IP",
    "CA10__sourceIpRange__c" : "0.0.0.0/0",
    "CA10__protocol__c" : "tcp",
    "CA10__fromPort__c" : 0,
    "CA10__toPort__c" : 65535,
    "CA10__securityGroup__c" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test3_1",
    "CA10__direction__c" : "Inbound",
    "CA10__source__c" : "IP",
    "CA10__sourceIpRange__c" : "::/0",
    "CA10__protocol__c" : "All",
    "CA10__fromPort__c" : null,
    "CA10__toPort__c" : null,
    "CA10__securityGroup__c" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test5_1",
    "CA10__direction__c" : "Outbound",
    "CA10__source__c" : "IP",
    "CA10__sourceIpRange__c" : "::/0",
    "CA10__protocol__c" : "tcp",
    "CA10__fromPort__c" : null,
    "CA10__toPort__c" : null,
    "CA10__securityGroup__c" : "test5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test6_1",
    "CA10__direction__c" : "Inbound",
    "CA10__source__c" : "Group",
    "CA10__sourceIpRange__c" : "0.0.0.0/0",
    "CA10__protocol__c" : "tcp",
    "CA10__fromPort__c" : null,
    "CA10__toPort__c" : null,
    "CA10__securityGroup__c" : "test6"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-30T02:23:36Z")
    },
    "Id" : "test7_1",
    "CA10__direction__c" : "Inbound",
    "CA10__source__c" : "IP",
    "CA10__sourceIpRange__c" : "0.0.0.0/0",
    "CA10__protocol__c" : "tcp",
    "CA10__fromPort__c" : 1111,
    "CA10__toPort__c" : 1111,
    "CA10__securityGroup__c" : "test7"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsSecurityGroup__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__AWS_EC2_Security_Group_Rules__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__direction__c STRING,
              CA10__source__c STRING,
              CA10__sourceIpRange__c STRING,
              CA10__protocol__c STRING,
              CA10__fromPort__c FLOAT64,
              CA10__toPort__c FLOAT64,
              CA10__securityGroup__c STRING,
              result STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError 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 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};
      }
      var count_CA10__AWS_EC2_Security_Group_Rules__r_INCOMPLIANT2 = 0;
      if (obj.CA10__AWS_EC2_Security_Group_Rules__r != null) {
          for (var i3 = 0; i3 < obj.CA10__AWS_EC2_Security_Group_Rules__r.length; i3++) {
              if (obj.CA10__AWS_EC2_Security_Group_Rules__r[i3].result.status == 'INCOMPLIANT') {
                  count_CA10__AWS_EC2_Security_Group_Rules__r_INCOMPLIANT2 += 1;
              }
          }
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Related list [CA10__AWS_EC2_Security_Group_Rules__r] ' + (count_CA10__AWS_EC2_Security_Group_Rules__r_INCOMPLIANT2 > 0 ? 'has' : 'does not have') + ' objects in INCOMPLIANT status');
      if (count_CA10__AWS_EC2_Security_Group_Rules__r_INCOMPLIANT2 > 0) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "CA10__AWS_EC2_Security_Group_Rules__r.has(INCOMPLIANT)", currentStateMessage: "There are rules that open common ports to the inbound connections", currentStateReferences: references1.join('\n'), remediation: "Remove the rules or adjust them to accept connections from narrower CIDRs", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "Group has no rules that open common ports to the internet", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__AWS_EC2_Security_Group_Rules__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__direction__c STRING,
          CA10__source__c STRING,
          CA10__sourceIpRange__c STRING,
          CA10__protocol__c STRING,
          CA10__fromPort__c FLOAT64,
          CA10__toPort__c FLOAT64,
          CA10__securityGroup__c 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 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 IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      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]
      function fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__direction__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__direction__c.isEmpty()", currentStateMessage: "Corrupted data. Rule Action cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__direction__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Direction [obj.CA10__direction__c]: ' + obj.CA10__direction__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'Inbound')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__direction__c') != 'Inbound'", currentStateMessage: "This is an outbound security group rule.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__source__c; } return this.out; };
      references1.push('Source [obj.CA10__source__c]: ' + obj.CA10__source__c);
      if (TextLib.notEqual(extract6.call(extract6), 'IP')) {
          return {status: 'INAPPLICABLE', conditionIndex: 299, conditionText: "extract('CA10__source__c') != 'IP'", currentStateMessage: "This is not an IP source based security group rule.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      function extract9() { if (!this.out) { this.out = obj.CA10__sourceIpRange__c; } return this.out; };
      references1.push('Source IP Range [obj.CA10__sourceIpRange__c]: ' + obj.CA10__sourceIpRange__c);
      if (TextLib.notEqual(extract9.call(extract9), '0.0.0.0/0') && TextLib.notEqual(extract9.call(extract9), '::/0')) {
          return {status: 'INAPPLICABLE', conditionIndex: 399, conditionText: "extract('CA10__sourceIpRange__c') != '0.0.0.0/0' && extract('CA10__sourceIpRange__c') != '::/0'", currentStateMessage: "This security group rule does not allow unrestricted access.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[4], conditionIndex:[400..499]
      function fieldChecked14() {
          if (TextLib.isEmpty(obj.CA10__protocol__c)) {
              throw new Error("UNDETERMINED condition:401", {cause: {status: 'UNDETERMINED', conditionIndex: 401, conditionText: "CA10__protocol__c.isEmpty()", currentStateMessage: "Corrupted data. Protocol cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__protocol__c;
      }
      function extract13() { if (!this.out) { this.out = fieldChecked14(); } return this.out; };
      function extract16() { if (!this.out) { this.out = obj.CA10__fromPort__c; } return this.out; };
      function extract19() { if (!this.out) { this.out = obj.CA10__toPort__c; } return this.out; };
      references1.push('Protocol [obj.CA10__protocol__c]: ' + obj.CA10__protocol__c);
      references1.push('From Port [obj.CA10__fromPort__c]: ' + obj.CA10__fromPort__c);
      references1.push('To Port [obj.CA10__toPort__c]: ' + obj.CA10__toPort__c);
      try {
          if (TextLib.equal(extract13.call(extract13), 'All') && IsEmptyLib.simpleIsEmpty(extract16.call(extract16)) && IsEmptyLib.simpleIsEmpty(extract19.call(extract19))) {
              return {status: 'INCOMPLIANT', conditionIndex: 499, conditionText: "extract('CA10__protocol__c') == 'All' && extract('CA10__fromPort__c').isEmpty() && extract('CA10__toPort__c').isEmpty()", currentStateMessage: "Rule opens all protocols and all ports to the internet", currentStateReferences: references1.join('\n'), remediation: "Remove the rule or narrow it down to a specific port, protocol and CIDR", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[5], conditionIndex:[500..599]
      function fieldChecked23() {
          if (TextLib.isEmpty(obj.CA10__protocol__c)) {
              throw new Error("UNDETERMINED condition:501", {cause: {status: 'UNDETERMINED', conditionIndex: 501, conditionText: "CA10__protocol__c.isEmpty()", currentStateMessage: "Corrupted data. Protocol cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__protocol__c;
      }
      function extract22() { if (!this.out) { this.out = fieldChecked23(); } return this.out; };
      function extract25() { if (!this.out) { this.out = obj.CA10__fromPort__c; } return this.out; };
      function extract28() { if (!this.out) { this.out = obj.CA10__toPort__c; } return this.out; };
      try {
          if (TextLib.equal(extract22.call(extract22), 'tcp') && extract25.call(extract25) == 0.0 && extract28.call(extract28) == 65535.0) {
              return {status: 'INCOMPLIANT', conditionIndex: 599, conditionText: "extract('CA10__protocol__c') == 'tcp' && extract('CA10__fromPort__c') == number(0.0) && extract('CA10__toPort__c') == number(65535.0)", currentStateMessage: "Rule opens all protocols and all ports to the internet", currentStateReferences: references1.join('\n'), remediation: "Remove the rule or narrow it down to a specific port, protocol and CIDR", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 600, conditionText: "otherwise", currentStateMessage: "This Security Group doesn't allow unrestricted access.", 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.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          `CA10__AWS_EC2_Security_Group_Rules__r`.arr AS CA10__AWS_EC2_Security_Group_Rules__r,
          process_CA10__CaAwsSecurityGroup__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  `CA10__AWS_EC2_Security_Group_Rules__r`.arr AS CA10__AWS_EC2_Security_Group_Rules__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsSecurityGroup__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10__securityGroup__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__direction__c AS CA10__direction__c,
                      sObject.CA10__source__c AS CA10__source__c,
                      sObject.CA10__sourceIpRange__c AS CA10__sourceIpRange__c,
                      sObject.CA10__protocol__c AS CA10__protocol__c,
                      sObject.CA10__fromPort__c AS CA10__fromPort__c,
                      sObject.CA10__toPort__c AS CA10__toPort__c,
                      sObject.CA10__securityGroup__c AS CA10__securityGroup__c,
                      process_CA10__AWS_EC2_Security_Group_Rules__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__direction__c AS CA10__direction__c,
                              sObject.CA10__source__c AS CA10__source__c,
                              sObject.CA10__sourceIpRange__c AS CA10__sourceIpRange__c,
                              sObject.CA10__protocol__c AS CA10__protocol__c,
                              sObject.CA10__fromPort__c AS CA10__fromPort__c,
                              sObject.CA10__toPort__c AS CA10__toPort__c,
                              sObject.CA10__securityGroup__c AS CA10__securityGroup__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaAwsSecurityGroupRule2__c()) AS sObject
          GROUP BY sObject.CA10__securityGroup__c
      ) AS `CA10__AWS_EC2_Security_Group_Rules__r`
          ON sObject.Id = `CA10__AWS_EC2_Security_Group_Rules__r`.CA10__securityGroup__c
  ) sObject ON sObject.Id = expectedResult.Id;
