---
policy: /ce/ca/aws/account/aws-support-role
logic: /ce/ca/aws/account/aws-support-role/prod.logic.yaml
executionTime: 2025-04-24T23:44:05.486465724Z
rows:
  - id: test1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10__AWS_IAM_Policies__r.has(COMPLIANT) && CA10__AWS_IAM_Role_Policy_Attachments__r.has(COMPLIANT)
      actual: CA10__AWS_IAM_Policies__r.has(COMPLIANT) && CA10__AWS_IAM_Role_Policy_Attachments__r.has(COMPLIANT)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: CA10__AWS_IAM_Policies__r.has(INCOMPLIANT)
      actual: CA10__AWS_IAM_Policies__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)
      actual: CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)
      actual: CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: test5
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 400
      actual: 400
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test6
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 400
      actual: 400
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/account/aws-support-role/policy.yaml
    md5Hash: 8B6AB7E29A41168D2FC9884CD4EEDB26
    content: |
      ---
      names:
        full: "AWS Support Role is not created"
        contextual: "AWS Support Role is not created"
      description: "AWS provides a support center that can be used for incident notification and response, as well as\
        \ technical support and customer services. Create an IAM Role, with the appropriate policy assigned, to allow\
        \ authorized users to manage incidents with AWS Support."
      type: "COMPLIANCE_POLICY"
      categories:
        - "SECURITY"
      frameworkMappings:
        - "/frameworks/cis-aws-v5.0.0/01/16"
        - "/frameworks/cloudaware/identity-and-access-governance/rbac-management"
      similarPolicies:
        internal:
          - dec-x-9146aafe
        cloudConformity:
          - url: "https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/aws/IAM/support-role.html"
            name: "IAM Support Role"
  - path: /ce/ca/aws/account/aws-support-role/prod.logic.yaml
    md5Hash: A4BCADD7E16F02338710B9A3225C4A36
    content: "---\ninputType: \"CA10__CaAwsAccount__c\"\ntestData:\n - file: test-data.json\n\
      conditions:\n  - status: \"COMPLIANT\"\n    currentStateMessage: \"The AWS Account\
      \ has a Support role for managing incidents with AWS Support.\"\n    check:\n\
      \      AND:\n        args:\n          - RELATED_LIST_HAS:\n              status:\
      \ \"COMPLIANT\"\n              relationshipName: \"CA10__AWS_IAM_Policies__r\"\
      \n          - RELATED_LIST_HAS:\n              status: \"COMPLIANT\"\n     \
      \         relationshipName: \"CA10__AWS_IAM_Role_Policy_Attachments__r\"\n \
      \ - status: \"INCOMPLIANT\"\n    currentStateMessage: \"AWSSupportAccess IAM\
      \ Policy for managing incidents with AWS Support does not exist.\"\n    remediationMessage:\
      \ \"Consider creating AWSSupportAccess IAM Policy.\"\n    check:\n      RELATED_LIST_HAS:\n\
      \          status: \"INCOMPLIANT\"\n          relationshipName: \"CA10__AWS_IAM_Policies__r\"\
      \n  - status: \"INCOMPLIANT\"\n    currentStateMessage: \"AWSSupportAccess IAM\
      \ Policy is not attached to any IAM role.\"\n    remediationMessage: \"Consider\
      \ attaching AWSSupportAccess IAM Policy to an IAM Role dedicated to managing\
      \ incidents with AWS Support.\"\n    check:\n      RELATED_LIST_HAS:\n     \
      \     status: \"INCOMPLIANT\"\n          relationshipName: \"CA10__AWS_IAM_Role_Policy_Attachments__r\"\
      \notherwise:\n  status: \"UNDETERMINED\"\n  currentStateMessage: \"There are\
      \ no active IAM Roles and IAM Policies.\"\nrelatedLists:\n  - relationshipName:\
      \ \"CA10__AWS_IAM_Policies__r\"\n    importExtracts:\n    - file: /types/CA10__CaAwsIamPolicy__c/object.extracts.yaml\n\
      \    conditions:\n    - status: \"COMPLIANT\"\n      currentStateMessage: \"\
      This is an AWSSupportAccess IAM Policy.\"\n      check:\n        IS_EQUAL:\n\
      \          left: \n            EXTRACT: \"CA10__arn__c\"\n          right: \n\
      \            TEXT: arn:aws:iam::aws:policy/AWSSupportAccess\n    otherwise:\n\
      \      status: \"INCOMPLIANT\"\n      currentStateMessage: \"AWSSupportAccess\
      \ IAM Policy is not found.\"\n  - relationshipName: \"CA10__AWS_IAM_Role_Policy_Attachments__r\"\
      \n    importExtracts:\n      - file: /types/CA10__CaAwsIamRolePolicyAttachment__c/object.extracts.yaml\n\
      \    conditions:\n      - status: \"COMPLIANT\"\n        currentStateMessage:\
      \ \"There is an AWS IAM Role attached to the AWSSupportAccess IAM Policy.\"\n\
      \        check:\n         AND:\n           args:\n            - NOT_EMPTY_LOOKUP:\
      \ \"CA10__role__r\"\n            - NOT_EMPTY_LOOKUP: \"CA10__policy__r\"\n \
      \           - IS_EQUAL:\n                left:\n                  EXTRACT: CA10__policyArn__c\n\
      \                right:\n                  TEXT: arn:aws:iam::aws:policy/AWSSupportAccess\n\
      \    otherwise:\n      status: \"INCOMPLIANT\"\n      currentStateMessage: \"\
      There is no AWS IAM Role attached to AWSSupportAccess IAM Policy.\"\n"
  - path: /types/CA10__CaAwsIamRolePolicyAttachment__c/object.extracts.yaml
    md5Hash: 3B1645AD49B1ED98D792A387F70B5AA5
    content: "# yaml-language-server: $schema=../../schema/Extracts.schema.json\n\
      ---\nextracts:\n- name: CA10__policyArn__c\n  value: \n    FIELD:\n      path:\
      \ CA10__policyArn__c\n- name: CA10__role__c\n  value: \n    FIELD:\n      path:\
      \ CA10__role__c\n- name: CA10__policy__c\n  value: \n    FIELD:\n      path:\
      \ CA10__policy__c"
  - path: /types/CA10__CaAwsIamPolicy__c/object.extracts.yaml
    md5Hash: C48327B50CE4EB54B3C6B2B464134BB3
    content: "# yaml-language-server: $schema=../../schema/Extracts.schema.json\n\
      ---\nextracts:\n# Not Nullable. Can't have no Access, retrieved via iam:ListPolicies\n\
      - name: \"CA10__name__c\"\n  value: \n    FIELD:\n      path: \"CA10__name__c\"\
      \n      undeterminedIf:\n        isEmpty: \"Corrupted data. Policy Name cannot\
      \ be empty.\"\n# Checkbox. Can't have no Access, retrieved via iam:ListPolicies\n\
      - name: \"CA10__attachable__c\"\n  value: \n    FIELD:\n      path: \"CA10__attachable__c\"\
      \n- name: \"CA10__attachmentCount__c\"\n  value: \n    FIELD:\n      path: \"\
      CA10__attachmentCount__c\"\n- name: \"CA10__policyDocument__c\"\n  value: \n\
      \    FIELD:\n      path: CA10__policyDocument__c\n      returnType: BYTES\n\
      \      undeterminedIf:\n        noAccessDelegate:\n          path: \"CA10__policyDocument__c\"\
      \n          currentStateMessage: \"Unable to determine the Policy Document.\
      \ Possible permission issue with iam:GetPolicyVersion\"\n- name: \"caJsonFrom_policyDocument__c\"\
      \n  value:\n    JSON_FROM:\n      arg:\n        EXTRACT: \"CA10__policyDocument__c\"\
      \n      undeterminedIf:\n        isInvalid: \"IAM policy document JSON is invalid\"\
      \n# Not Nullable. Can't have no Access, retrieved via iam:ListPolicies\n- name:\
      \ \"CA10__arn__c\"\n  value: \n    FIELD:\n      path: CA10__arn__c\n      undeterminedIf:\n\
      \        isEmpty: \"Corrupted data. Policy ARN cannot be empty.\"\n"
  - path: /ce/ca/aws/account/aws-support-role/test-data.json
    md5Hash: E56AD00C43DCB0E44A11562E7B007179
    content: |-
      [
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "CA10__AWS_IAM_Policies__r.has(COMPLIANT) && CA10__AWS_IAM_Role_Policy_Attachments__r.has(COMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-07-10T20:44:00Z"
              },
              "Id": "test1",
              "CA10__AWS_IAM_Policies__r": [
                  {
                      "Id": "policy1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__arn__c": "arn:aws:iam::aws:policy/AWSSupportAccess",
                      "CA10__account__c": "test1"
                  }
              ],
              "CA10__AWS_IAM_Role_Policy_Attachments__r": [
                  {
                      "Id": "test1_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__role__c": "role1",
                      "CA10__roleArn__c": "arn:aws:iam::1234567890:role/service-role/roleName1",
                      "CA10__policy__c": "policy1",
                      "CA10__policyArn__c": "arn:aws:iam::aws:policy/AWSSupportAccess",
                      "CA10__account__c": "test1",
                      "CA10__role__r": {
                          "Id": "role1",
                          "CA10__disappearanceTime__c": null
                      },
                      "CA10__policy__r": {
                          "Id": "policy1",
                          "CA10__disappearanceTime__c": null
                      }
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "CA10__AWS_IAM_Policies__r.has(INCOMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-07-10T20:44:00Z"
              },
              "Id": "test2",
              "CA10__AWS_IAM_Policies__r": [
                  {
                      "Id": "policy2",
                      "CA10__disappearanceTime__c": null,
                      "CA10__arn__c": "arn:aws:iam::aws:policy/policyName2",
                      "CA10__account__c": "test2"
                  }
              ],
              "CA10__AWS_IAM_Role_Policy_Attachments__r": [
                  {
                      "Id": "test2_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__role__c": "role2",
                      "CA10__roleArn__c": "arn:aws:iam::1234567890:role/service-role/roleName2",
                      "CA10__policy__c": "policy2",
                      "CA10__policyArn__c": "arn:aws:iam::aws:policy/policyName2",
                      "CA10__account__c": "test2",
                      "CA10__role__r": {
                          "Id": "role2",
                          "CA10__disappearanceTime__c": null
                      },
                      "CA10__policy__r": {
                          "Id": "policy2",
                          "CA10__disappearanceTime__c": null
                      }
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "399",
                  "conditionText": "CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-07-10T20:44:00Z"
              },
              "Id": "test3",
              "CA10__AWS_IAM_Policies__r": [
                  {
                      "Id": "policy3",
                      "CA10__disappearanceTime__c": null,
                      "CA10__arn__c": "arn:aws:iam::aws:policy/AWSSupportAccess",
                      "CA10__account__c": "test3"
                  }
              ],
              "CA10__AWS_IAM_Role_Policy_Attachments__r": [
                  {
                      "Id": "test3_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__role__c": "role3",
                      "CA10__roleArn__c": "arn:aws:iam::1234567890:role/service-role/roleName3",
                      "CA10__policy__c": "policy3",
                      "CA10__policyArn__c": "arn:aws:iam::aws:policy/policyName3",
                      "CA10__account__c": "test3",
                      "CA10__role__r": {
                          "Id": "role3",
                          "CA10__disappearanceTime__c": null
                      },
                      "CA10__policy__r": {
                          "Id": "policy3",
                          "CA10__disappearanceTime__c": null
                      }
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "399",
                  "conditionText": "CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-07-10T20:44:00Z"
              },
              "Id": "test4",
              "CA10__AWS_IAM_Policies__r": [
                  {
                      "Id": "policy4",
                      "CA10__disappearanceTime__c": "2024-07-10T20:44:00Z",
                      "CA10__arn__c": "arn:aws:iam::aws:policy/AWSSupportAccess",
                      "CA10__account__c": "test4"
                  }
              ],
              "CA10__AWS_IAM_Role_Policy_Attachments__r": [
                  {
                      "Id": "test4_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__role__c": "role4",
                      "CA10__roleArn__c": "arn:aws:iam::1234567890:role/service-role/roleName4",
                      "CA10__policy__c": "policy4",
                      "CA10__policyArn__c": "arn:aws:iam::aws:policy/policyName4",
                      "CA10__account__c": "test4",
                      "CA10__role__r": {
                          "Id": "role4",
                          "CA10__disappearanceTime__c": null
                      },
                      "CA10__policy__r": {
                          "Id": "policy4",
                          "CA10__disappearanceTime__c": null
                      }
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "UNDETERMINED",
                  "conditionIndex": "400",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-07-10T20:44:00Z"
              },
              "Id": "test5",
              "CA10__AWS_IAM_Policies__r": [
                  {
                      "Id": "policy5",
                      "CA10__disappearanceTime__c": null,
                      "CA10__arn__c": "arn:aws:iam::aws:policy/AWSSupportAccess",
                      "CA10__account__c": "test5"
                  }
              ],
              "CA10__AWS_IAM_Role_Policy_Attachments__r": [
                  {
                      "Id": "test5_1",
                      "CA10__disappearanceTime__c": "2024-07-10T20:44:00Z",
                      "CA10__role__c": "role5",
                      "CA10__roleArn__c": "arn:aws:iam::1234567890:role/service-role/roleName5",
                      "CA10__policy__c": "policy5",
                      "CA10__policyArn__c": "arn:aws:iam::aws:policy/policyName5",
                      "CA10__account__c": "test5",
                      "CA10__role__r": {
                          "Id": "role5",
                          "CA10__disappearanceTime__c": null
                      },
                      "CA10__policy__r": {
                          "Id": "policy5",
                          "CA10__disappearanceTime__c": null
                      }
                  }
              ]
          },
          {
              "expectedResult": {
                  "status": "UNDETERMINED",
                  "conditionIndex": "400",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-07-10T20:44:00Z"
              },
              "Id": "test6",
              "CA10__AWS_IAM_Policies__r": [
                  {
                      "Id": "policy6",
                      "CA10__disappearanceTime__c": "2024-07-10T20:44:00Z",
                      "CA10__arn__c": "arn:aws:iam::aws:policy/AWSSupportAccess",
                      "CA10__account__c": "test6"
                  }
              ],
              "CA10__AWS_IAM_Role_Policy_Attachments__r": [
                  {
                      "Id": "test6_1",
                      "CA10__disappearanceTime__c": "2024-07-10T20:44:00Z",
                      "CA10__role__c": "role6",
                      "CA10__roleArn__c": "arn:aws:iam::1234567890:role/service-role/roleName6",
                      "CA10__policy__c": "policy6",
                      "CA10__policyArn__c": "arn:aws:iam::aws:policy/policyName6",
                      "CA10__account__c": "test6",
                      "CA10__role__r": {
                          "Id": "role6",
                          "CA10__disappearanceTime__c": null
                      },
                      "CA10__policy__r": {
                          "Id": "policy6",
                          "CA10__disappearanceTime__c": null
                      }
                  }
              ]
          }
      ]
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" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "CA10__AWS_IAM_Policies__r.has(COMPLIANT) && CA10__AWS_IAM_Role_Policy_Attachments__r.has(COMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "CA10__AWS_IAM_Policies__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test5",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "400",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test6",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "400",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsAccount__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test6"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsIamPolicy__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__arn__c STRING,
      CA10__account__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "policy1",
    "CA10__arn__c" : "arn:aws:iam::aws:policy/AWSSupportAccess",
    "CA10__account__c" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "policy2",
    "CA10__arn__c" : "arn:aws:iam::aws:policy/policyName2",
    "CA10__account__c" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "policy3",
    "CA10__arn__c" : "arn:aws:iam::aws:policy/AWSSupportAccess",
    "CA10__account__c" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "policy4",
    "CA10__disappearanceTime__c" : new Date("2024-07-10T20:44:00Z"),
    "CA10__arn__c" : "arn:aws:iam::aws:policy/AWSSupportAccess",
    "CA10__account__c" : "test4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "policy5",
    "CA10__arn__c" : "arn:aws:iam::aws:policy/AWSSupportAccess",
    "CA10__account__c" : "test5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "policy6",
    "CA10__disappearanceTime__c" : new Date("2024-07-10T20:44:00Z"),
    "CA10__arn__c" : "arn:aws:iam::aws:policy/AWSSupportAccess",
    "CA10__account__c" : "test6"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsIamRolePolicyAttachment__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__role__c STRING,
      CA10__roleArn__c STRING,
      CA10__policy__c STRING,
      CA10__policyArn__c STRING,
      CA10__account__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test1_1",
    "CA10__role__c" : "role1",
    "CA10__roleArn__c" : "arn:aws:iam::1234567890:role/service-role/roleName1",
    "CA10__policy__c" : "policy1",
    "CA10__policyArn__c" : "arn:aws:iam::aws:policy/AWSSupportAccess",
    "CA10__account__c" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test2_1",
    "CA10__role__c" : "role2",
    "CA10__roleArn__c" : "arn:aws:iam::1234567890:role/service-role/roleName2",
    "CA10__policy__c" : "policy2",
    "CA10__policyArn__c" : "arn:aws:iam::aws:policy/policyName2",
    "CA10__account__c" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test3_1",
    "CA10__role__c" : "role3",
    "CA10__roleArn__c" : "arn:aws:iam::1234567890:role/service-role/roleName3",
    "CA10__policy__c" : "policy3",
    "CA10__policyArn__c" : "arn:aws:iam::aws:policy/policyName3",
    "CA10__account__c" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test4_1",
    "CA10__role__c" : "role4",
    "CA10__roleArn__c" : "arn:aws:iam::1234567890:role/service-role/roleName4",
    "CA10__policy__c" : "policy4",
    "CA10__policyArn__c" : "arn:aws:iam::aws:policy/policyName4",
    "CA10__account__c" : "test4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test5_1",
    "CA10__disappearanceTime__c" : new Date("2024-07-10T20:44:00Z"),
    "CA10__role__c" : "role5",
    "CA10__roleArn__c" : "arn:aws:iam::1234567890:role/service-role/roleName5",
    "CA10__policy__c" : "policy5",
    "CA10__policyArn__c" : "arn:aws:iam::aws:policy/policyName5",
    "CA10__account__c" : "test5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "test6_1",
    "CA10__disappearanceTime__c" : new Date("2024-07-10T20:44:00Z"),
    "CA10__role__c" : "role6",
    "CA10__roleArn__c" : "arn:aws:iam::1234567890:role/service-role/roleName6",
    "CA10__policy__c" : "policy6",
    "CA10__policyArn__c" : "arn:aws:iam::aws:policy/policyName6",
    "CA10__account__c" : "test6"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsRole__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-07-10T20:44:00Z")
    },
    "Id" : "role1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "role2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "role3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "role4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "role5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-07-10T20:44:00Z")
    },
    "Id" : "role6"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsAccount__c(
      obj STRUCT<
          Id STRING,
          CA10__AWS_IAM_Policies__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__arn__c STRING,
              CA10__account__c STRING,
              result STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
          >>,
          CA10__AWS_IAM_Role_Policy_Attachments__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__role__c STRING,
              CA10__roleArn__c STRING,
              CA10__policy__c STRING,
              CA10__policyArn__c STRING,
              CA10__account__c STRING,
              CA10__role__r STRUCT<
                  Id STRING,
                  CA10__disappearanceTime__c TIMESTAMP
              >,
              CA10__policy__r STRUCT<
                  Id STRING,
                  CA10__disappearanceTime__c TIMESTAMP
              >,
              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]
      if (false) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared()", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      var count_CA10__AWS_IAM_Role_Policy_Attachments__r_COMPLIANT3 = 0;
      if (obj.CA10__AWS_IAM_Role_Policy_Attachments__r != null) {
          for (var i4 = 0; i4 < obj.CA10__AWS_IAM_Role_Policy_Attachments__r.length; i4++) {
              if (obj.CA10__AWS_IAM_Role_Policy_Attachments__r[i4].result.status == 'COMPLIANT') {
                  count_CA10__AWS_IAM_Role_Policy_Attachments__r_COMPLIANT3 += 1;
              }
          }
      }
      var count_CA10__AWS_IAM_Policies__r_COMPLIANT2 = 0;
      if (obj.CA10__AWS_IAM_Policies__r != null) {
          for (var i5 = 0; i5 < obj.CA10__AWS_IAM_Policies__r.length; i5++) {
              if (obj.CA10__AWS_IAM_Policies__r[i5].result.status == 'COMPLIANT') {
                  count_CA10__AWS_IAM_Policies__r_COMPLIANT2 += 1;
              }
          }
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Related list [CA10__AWS_IAM_Policies__r] ' + (count_CA10__AWS_IAM_Policies__r_COMPLIANT2 > 0 ? 'has' : 'does not have') + ' objects in COMPLIANT status');
      references1.push('Related list [CA10__AWS_IAM_Role_Policy_Attachments__r] ' + (count_CA10__AWS_IAM_Role_Policy_Attachments__r_COMPLIANT3 > 0 ? 'has' : 'does not have') + ' objects in COMPLIANT status');
      if (count_CA10__AWS_IAM_Policies__r_COMPLIANT2 > 0 && count_CA10__AWS_IAM_Role_Policy_Attachments__r_COMPLIANT3 > 0) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "CA10__AWS_IAM_Policies__r.has(COMPLIANT) && CA10__AWS_IAM_Role_Policy_Attachments__r.has(COMPLIANT)", currentStateMessage: "The AWS Account has a Support role for managing incidents with AWS Support.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      var count_CA10__AWS_IAM_Policies__r_INCOMPLIANT6 = 0;
      if (obj.CA10__AWS_IAM_Policies__r != null) {
          for (var i7 = 0; i7 < obj.CA10__AWS_IAM_Policies__r.length; i7++) {
              if (obj.CA10__AWS_IAM_Policies__r[i7].result.status == 'INCOMPLIANT') {
                  count_CA10__AWS_IAM_Policies__r_INCOMPLIANT6 += 1;
              }
          }
      }
      // condition[2], conditionIndex:[200..299]
      if (count_CA10__AWS_IAM_Policies__r_INCOMPLIANT6 > 0) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "CA10__AWS_IAM_Policies__r.has(INCOMPLIANT)", currentStateMessage: "AWSSupportAccess IAM Policy for managing incidents with AWS Support does not exist.", currentStateReferences: references1.join('\n'), remediation: "Consider creating AWSSupportAccess IAM Policy.", runtimeError: null};
      }
      var count_CA10__AWS_IAM_Role_Policy_Attachments__r_INCOMPLIANT8 = 0;
      if (obj.CA10__AWS_IAM_Role_Policy_Attachments__r != null) {
          for (var i9 = 0; i9 < obj.CA10__AWS_IAM_Role_Policy_Attachments__r.length; i9++) {
              if (obj.CA10__AWS_IAM_Role_Policy_Attachments__r[i9].result.status == 'INCOMPLIANT') {
                  count_CA10__AWS_IAM_Role_Policy_Attachments__r_INCOMPLIANT8 += 1;
              }
          }
      }
      // condition[3], conditionIndex:[300..399]
      if (count_CA10__AWS_IAM_Role_Policy_Attachments__r_INCOMPLIANT8 > 0) {
          return {status: 'INCOMPLIANT', conditionIndex: 399, conditionText: "CA10__AWS_IAM_Role_Policy_Attachments__r.has(INCOMPLIANT)", currentStateMessage: "AWSSupportAccess IAM Policy is not attached to any IAM role.", currentStateReferences: references1.join('\n'), remediation: "Consider attaching AWSSupportAccess IAM Policy to an IAM Role dedicated to managing incidents with AWS Support.", runtimeError: null};
      }
      return {status: 'UNDETERMINED', conditionIndex: 400, conditionText: "otherwise", currentStateMessage: "There are no active IAM Roles and IAM Policies.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__AWS_IAM_Role_Policy_Attachments__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__role__c STRING,
          CA10__roleArn__c STRING,
          CA10__policy__c STRING,
          CA10__policyArn__c STRING,
          CA10__account__c STRING,
          CA10__role__r STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP
          >,
          CA10__policy__r STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP
          >
      >,
      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 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 extract3() { if (!this.out) { this.out = obj.CA10__policyArn__c; } return this.out; };
      references1.push('Policy ARN [obj.CA10__policyArn__c]: ' + obj.CA10__policyArn__c);
      if ((TextLib.isNotEmpty(obj.CA10__roleArn__c) && TextLib.isNotEmpty(obj.CA10__role__c) && obj.CA10__role__r.CA10__disappearanceTime__c == null) && (TextLib.isNotEmpty(obj.CA10__policyArn__c) && TextLib.isNotEmpty(obj.CA10__policy__c) && obj.CA10__policy__r.CA10__disappearanceTime__c == null) && TextLib.equal(extract3.call(extract3), 'arn:aws:iam::aws:policy/AWSSupportAccess')) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "notEmptyLookup('CA10__role__r') && notEmptyLookup('CA10__policy__r') && extract('CA10__policyArn__c') == 'arn:aws:iam::aws:policy/AWSSupportAccess'", currentStateMessage: "There is an AWS IAM Role attached to the AWSSupportAccess IAM Policy.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "There is no AWS IAM Role attached to AWSSupportAccess IAM Policy.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__AWS_IAM_Policies__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__arn__c STRING,
          CA10__account__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 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__arn__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__arn__c.isEmpty()", currentStateMessage: "Corrupted data. Policy ARN cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__arn__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('ARN [obj.CA10__arn__c]: ' + obj.CA10__arn__c);
      try {
          if (TextLib.equal(extract3.call(extract3), 'arn:aws:iam::aws:policy/AWSSupportAccess')) {
              return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__arn__c') == 'arn:aws:iam::aws:policy/AWSSupportAccess'", currentStateMessage: "This is an AWSSupportAccess IAM Policy.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "AWSSupportAccess IAM Policy is not found.", 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,
          `CA10__AWS_IAM_Policies__r`.arr AS CA10__AWS_IAM_Policies__r,
          `CA10__AWS_IAM_Role_Policy_Attachments__r`.arr AS CA10__AWS_IAM_Role_Policy_Attachments__r,
          process_CA10__CaAwsAccount__c(
              STRUCT(
                  sObject.Id AS Id,
                  `CA10__AWS_IAM_Policies__r`.arr AS CA10__AWS_IAM_Policies__r,
                  `CA10__AWS_IAM_Role_Policy_Attachments__r`.arr AS CA10__AWS_IAM_Role_Policy_Attachments__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsAccount__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10__account__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__arn__c AS CA10__arn__c,
                      sObject.CA10__account__c AS CA10__account__c,
                      process_CA10__AWS_IAM_Policies__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__arn__c AS CA10__arn__c,
                              sObject.CA10__account__c AS CA10__account__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaAwsIamPolicy__c()) AS sObject
          GROUP BY sObject.CA10__account__c
      ) AS `CA10__AWS_IAM_Policies__r`
          ON sObject.Id = `CA10__AWS_IAM_Policies__r`.CA10__account__c
      LEFT JOIN (
          SELECT
              sObject.CA10__account__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__role__c AS CA10__role__c,
                      sObject.CA10__roleArn__c AS CA10__roleArn__c,
                      sObject.CA10__policy__c AS CA10__policy__c,
                      sObject.CA10__policyArn__c AS CA10__policyArn__c,
                      sObject.CA10__account__c AS CA10__account__c,
                      STRUCT (
                          `CA10__role__r`.Id AS Id,
                          `CA10__role__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
                      ) AS CA10__role__r,
                      STRUCT (
                          `CA10__policy__r`.Id AS Id,
                          `CA10__policy__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
                      ) AS CA10__policy__r,
                      process_CA10__AWS_IAM_Role_Policy_Attachments__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__role__c AS CA10__role__c,
                              sObject.CA10__roleArn__c AS CA10__roleArn__c,
                              sObject.CA10__policy__c AS CA10__policy__c,
                              sObject.CA10__policyArn__c AS CA10__policyArn__c,
                              sObject.CA10__account__c AS CA10__account__c,
                              STRUCT (
                                  `CA10__role__r`.Id AS Id,
                                  `CA10__role__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
                              ) AS CA10__role__r,
                              STRUCT (
                                  `CA10__policy__r`.Id AS Id,
                                  `CA10__policy__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
                              ) AS CA10__policy__r
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaAwsIamRolePolicyAttachment__c()) AS sObject
          LEFT JOIN UNNEST(mock_CA10__CaAwsRole__c()) AS `CA10__role__r`
              ON sObject.CA10__role__c = `CA10__role__r`.Id
          LEFT JOIN UNNEST(mock_CA10__CaAwsIamPolicy__c()) AS `CA10__policy__r`
              ON sObject.CA10__policy__c = `CA10__policy__r`.Id
          GROUP BY sObject.CA10__account__c
      ) AS `CA10__AWS_IAM_Role_Policy_Attachments__r`
          ON sObject.Id = `CA10__AWS_IAM_Role_Policy_Attachments__r`.CA10__account__c
  ) sObject ON sObject.Id = expectedResult.Id;
