---
policy: /ce/ca/aws/cloudtrail/s3-bucket-access-logging
logic: /ce/ca/aws/cloudtrail/s3-bucket-access-logging/prod.logic.yaml
executionTime: 2025-04-24T23:44:27.974517949Z
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: isEmptyLookup('CA10__bucket__r')
      actual: isEmptyLookup('CA10__bucket__r')
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__bucket__r.CA10__loggingDestinationBucketName__c').isEmpty()
      actual: extract('CA10__bucket__r.CA10__loggingDestinationBucketName__c').isEmpty()
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__bucket__r.CA10__arn__c') == extract('CA10__bucket__r.CA10__loggingDestinationBucketArn__c')
      actual: extract('CA10__bucket__r.CA10__arn__c') == extract('CA10__bucket__r.CA10__loggingDestinationBucketArn__c')
    runtimeError: {}
  - id: test5
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 499
      actual: 499
    conditionText:
      expected: isEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')
      actual: isEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')
    runtimeError: {}
  - id: test6
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 599
      actual: 599
    conditionText:
      expected: notEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')
      actual: notEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/cloudtrail/s3-bucket-access-logging/policy.yaml
    md5Hash: BA1715F8771329FFE9EF31E598F3E580
    content: |
      ---
      names:
        full: AWS CloudTrail S3 Bucket Access Logging is not enabled.
        contextual: S3 Bucket Access Logging is not enabled.
      description: "S3 Bucket Access Logging generates a log that contains access records\
        \ for each request made to your S3 bucket. An access log record contains details\
        \ about the request, such as the request type, the resources specified in the request\
        \ worked, and the time and date the request was processed. It is recommended that\
        \ bucket access logging be enabled on the CloudTrail S3 bucket."
      type: COMPLIANCE_POLICY
      categories:
        - SECURITY
        - RELIABILITY
      frameworkMappings:
        - "/frameworks/cis-aws-v5.0.0/03/04"
        - "/frameworks/cloudaware/logging-and-monitoring/logging-and-monitoring-configuration"
        - "/frameworks/aws-fsbp-v1.0.0/s3/09"
      similarPolicies:
        internal:
          - dec-x-e0014333
        cloudConformity:
          - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/aws/CloudTrail/cloudtrail-s3-bucket-logging-enabled.html
            name: CloudTrail S3 Bucket Logging Enabled
  - path: /ce/ca/aws/cloudtrail/s3-bucket-access-logging/prod.logic.yaml
    md5Hash: 2D0186B601BEFB95769DBF26AA935F23
    content: |-
      ---
      inputType: "CA10__CaAwsCloudTrailTrail__c"
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10__CaAwsCloudTrailTrail__c/object.extracts.yaml"
        - file: "/types/CA10__CaAwsBucket__c/object.extracts.yaml"
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "AWS CloudTrail references a missing bucket."
          remediationMessage: "Ensure that CloudTrail is referencing an active S3 bucket."
          check:
            IS_EMPTY_LOOKUP: "CA10__bucket__r"
        - status: "INCOMPLIANT"
          currentStateMessage: "Server access logging is not enabled."
          remediationMessage: "Consider enabling server access logging."
          check:
              IS_EMPTY:
                arg:
                  EXTRACT: "CA10__bucket__r.CA10__loggingDestinationBucketName__c"
      # When source bucket is its own destination bucket,
      # then ARNs will be the same but CA10__loggingDestinationBucket__c will be empty
        - status: "COMPLIANT"
          currentStateMessage: "Server access logging is enabled. The bucket uses itself as the logging destination."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__bucket__r.CA10__arn__c"
              right:
                EXTRACT: "CA10__bucket__r.CA10__loggingDestinationBucketArn__c"
        - status: "INCOMPLIANT"
          currentStateMessage: "The destination bucket is missing."
          remediationMessage: "Ensure that bucket access logging is referencing an active S3 bucket."
          check:
            IS_EMPTY_LOOKUP: "CA10__bucket__r.CA10__loggingDestinationBucket__r"
        - status: "COMPLIANT"
          currentStateMessage: "Server access logging is enabled."
          check:
            NOT_EMPTY_LOOKUP: "CA10__bucket__r.CA10__loggingDestinationBucket__r"
      otherwise:
        status: "UNDETERMINED"
        currentStateMessage: "Unexpected values in the fields."
  - path: /types/CA10__CaAwsBucket__c/object.extracts.yaml
    md5Hash: D7F01723B629E0F1265F06B9CE2EDE1F
    content: "---\nextracts:\n# Values: yes, no. Not Nullable.\n  - name: \"CA10__objectLockEnabled__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__objectLockEnabled__c\"\n\
      \        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__objectLockEnabled__c\"\n            currentStateMessage: \"Unable to determine\
      \ Object Lock status. Possible permission issue with s3:GetObjectLockConfiguration\"\
      \n          isEmpty: \"Object Lock status is not populated yet\"\n# Nullable.\n\
      \  - name: \"CA10__lifecycleRulesJson__c\"\n    value: \n      FIELD:\n    \
      \    path: \"CA10__lifecycleRulesJson__c\"\n        returnType: BYTES\n    \
      \    # undeterminedIf:\n        #   noAccessDelegate:\n        #     path: \"\
      CA10__lifecycleRulesJson__c\"\n        #     currentStateMessage: \"Unable to\
      \ determine Lifecycle Configuration. Possible permission issue with s3:GetLifecycleConfiguration\"\
      \n# Values: enabled, suspended, off. Not Nullable.\n  - name: \"CA10__versioningStatus__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__versioningStatus__c\"\n \
      \       undeterminedIf:\n          noAccessDelegate:\n            path: \"CA10__versioningStatus__c\"\
      \n            currentStateMessage: \"Unable to determine versioning status.\
      \ Possible permission issue with s3:GetBucketVersioning\"\n          isEmpty:\
      \ \"Status is not populated yet\"\n# Checkbox.\n  - name: \"CA10__versioningMfaDeleteEnabled__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__versioningMfaDeleteEnabled__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__versioningStatus__c\"\n            currentStateMessage: \"Unable to determine\
      \ versioning status. Possible permission issue with s3:GetBucketVersioning\"\
      \n# The field can be empty if server access logging is not enabled\n  - name:\
      \ \"CA10__loggingDestinationBucketName__c\"\n    value: \n      FIELD:\n   \
      \     path: \"CA10__loggingDestinationBucketName__c\"\n        # undeterminedIf:\n\
      \        #   noAccessDelegate:\n        #     path: \"CA10__loggingDestinationBucketName__c\"\
      \n        #     currentStateMessage: \"Unable to determine if server access\
      \ logging is enabled. Possible permission issue with s3:GetBucketLogging\"\n\
      # Cloudaware derives this field from CA10__loggingDestinationBucketName__c\n\
      \  - name: \"CA10__loggingDestinationBucketArn__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__loggingDestinationBucketArn__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__loggingDestinationBucketName__c\"\
      \n            currentStateMessage: \"Unable to determine if server access logging\
      \ is enabled. Possible permission issue with s3:GetBucketLogging\"\n# This is\
      \ a look up on CA10__CaAwsBucket__c derived from CA10__loggingDestinationBucketName__c\n\
      \  - name: \"CA10__loggingDestinationBucket__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__loggingDestinationBucket__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__loggingDestinationBucketName__c\"\
      \n            currentStateMessage: \"Unable to determine if server access logging\
      \ is enabled. Possible permission issue with s3:GetBucketLogging\"\n  - name:\
      \ \"CA10__arn__c\"\n    value: \n      FIELD:\n        path: \"CA10__arn__c\"\
      \n        undeterminedIf:\n          isEmpty: \"Bucket ARN cannot be empty.\
      \ Potential data corruption\"\n  - name: \"caJsonFrom__lifecycleRulesJson__c\"\
      \n    value: \n      JSON_FROM:\n        arg: \n          EXTRACT: \"CA10__lifecycleRulesJson__c\"\
      \n        undeterminedIf:\n          isInvalid: \"S3 Bucket Lifecycle Rules\
      \ JSON is invalid.\"\n# Returns BOOLEAN true if number of enabled lifecycle\
      \ rules more than 0 otherwise returns false\n  - name: \"caJsonQueryBoolean__lifecycleRulesJson_status_enabled__c\"\
      \n    value: \n      JSON_QUERY_BOOLEAN:\n        arg: \n          EXTRACT:\
      \ \"caJsonFrom__lifecycleRulesJson__c\"\n        expression: \"length([?status=='Enabled'])\
      \ > `0`\"\n        undeterminedIf: \n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ a boolean.\"\n  - name: \"CA10__blockPublicAcls__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__blockPublicAcls__c\"\n        undeterminedIf:\n     \
      \     noAccessDelegate:\n            path: \"CA10__blockPublicAcls__c\"\n  \
      \          currentStateMessage: \"Unable to determine the bucket policy. Possible\
      \ permission issue with s3:GetBucketPublicAccessBlock\"\n  - name: \"CA10__blockPublicPolicy__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__blockPublicPolicy__c\"\n\
      \        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__blockPublicPolicy__c\"\n            currentStateMessage: \"Unable to determine\
      \ the bucket policy. Possible permission issue with s3:GetBucketPublicAccessBlock\"\
      \n  - name: \"CA10__ignorePublicAcls__c\"\n    value: \n      FIELD:\n     \
      \   path: \"CA10__ignorePublicAcls__c\"\n        undeterminedIf:\n         \
      \ noAccessDelegate:\n            path: \"CA10__ignorePublicAcls__c\"\n     \
      \       currentStateMessage: \"Unable to determine the bucket policy. Possible\
      \ permission issue with s3:GetBucketPublicAccessBlock\"\n  - name: \"CA10__restrictPublicBuckets__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__restrictPublicBuckets__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__restrictPublicBuckets__c\"\n            currentStateMessage: \"Unable\
      \ to determine the bucket policy. Possible permission issue with s3:GetBucketPublicAccessBlock\"\
      \n# Nullable.\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 bucket policy. Possible permission issue with s3:GetBucketPolicy\"\n \
      \ - name: \"caJsonFrom__policyDocument__c\"\n    value: \n      JSON_FROM:\n\
      \        arg: \n          EXTRACT: \"CA10__policyDocument__c\"\n        undeterminedIf:\n\
      \          isInvalid: \"S3 Bucket Policy Document JSON is invalid.\"\n"
  - path: /ce/ca/aws/cloudtrail/s3-bucket-access-logging/test-data.json
    md5Hash: 92620BF0E6E7675C775FC2DDB9FC16CB
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-29T00:23:39Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2023-10-02T10:49:08Z",
              "CA10__bucket__c": "test1_1",
              "CA10__bucketArn__c": "arn:aws:s3:::test-bucket-1",
              "CA10__bucket__r": {
                  "Id": "test1_1",
                  "CA10__disappearanceTime__c": null,
                  "CA10__loggingDestinationBucketName__c": "",
                  "CA10__arn__c": "arn:aws:s3:::test-bucket-1",
                  "CA10__loggingDestinationBucketArn__c": "",
                  "CA10__loggingDestinationBucket__c": "",
                  "CA10__loggingDestinationBucket__r": {
                      "Id": null,
                      "CA10__disappearanceTime__c": null
                  }
              }
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "isEmptyLookup('CA10__bucket__r')",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-29T00:23:39Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__bucket__c": "",
              "CA10__bucketArn__c": "arn:aws:s3:::test-bucket-2",
              "CA10__bucket__r": {
                  "Id": null,
                  "CA10__disappearanceTime__c": null,
                  "CA10__loggingDestinationBucketName__c": null,
                  "CA10__arn__c": null,
                  "CA10__loggingDestinationBucketArn__c": null,
                  "CA10__loggingDestinationBucket__c": null,
                  "CA10__loggingDestinationBucket__r": {
                      "Id": null,
                      "CA10__disappearanceTime__c": null
                  }
              }
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__bucket__r.CA10__loggingDestinationBucketName__c').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-29T00:23:39Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__bucket__c": "test3_1",
              "CA10__bucketArn__c": "arn:aws:s3:::test-bucket-3",
              "CA10__bucket__r": {
                  "Id": "test3_1",
                  "CA10__disappearanceTime__c": null,
                  "CA10__loggingDestinationBucketName__c": "",
                  "CA10__arn__c": "arn:aws:s3:::test-bucket-3",
                  "CA10__loggingDestinationBucketArn__c": "",
                  "CA10__loggingDestinationBucket__c": "",
                  "CA10__loggingDestinationBucket__r": {
                      "Id": null,
                      "CA10__disappearanceTime__c": null
                  }
              }
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "399",
                  "conditionText": "extract('CA10__bucket__r.CA10__arn__c') == extract('CA10__bucket__r.CA10__loggingDestinationBucketArn__c')",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-29T00:23:39Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__bucket__c": "test4_1",
              "CA10__bucketArn__c": "arn:aws:s3:::test-bucket-4",
              "CA10__bucket__r": {
                  "Id": "test4_1",
                  "CA10__disappearanceTime__c": null,
                  "CA10__loggingDestinationBucketName__c": "test-bucket-4",
                  "CA10__arn__c": "arn:aws:s3:::test-bucket-4",
                  "CA10__loggingDestinationBucketArn__c": "arn:aws:s3:::test-bucket-4",
                  "CA10__loggingDestinationBucket__c": "",
                  "CA10__loggingDestinationBucket__r": {
                      "Id": null,
                      "CA10__disappearanceTime__c": null
                  }
              }
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "499",
                  "conditionText": "isEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-29T00:23:39Z"
              },
              "Id": "test5",
              "CA10__disappearanceTime__c": null,
              "CA10__bucket__c": "test5_1",
              "CA10__bucketArn__c": "arn:aws:s3:::test-bucket-1",
              "CA10__bucket__r": {
                  "Id": "test5_1",
                  "CA10__disappearanceTime__c": null,
                  "CA10__loggingDestinationBucketName__c": "test-bucket-2",
                  "CA10__arn__c": "arn:aws:s3:::test-bucket-5",
                  "CA10__loggingDestinationBucketArn__c": "arn:aws:s3:::test-bucket",
                  "CA10__loggingDestinationBucket__c": "",
                  "CA10__loggingDestinationBucket__r": {
                      "Id": null,
                      "CA10__disappearanceTime__c": null
                  }
              }
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "599",
                  "conditionText": "notEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-05-29T00:23:39Z"
              },
              "Id": "test6",
              "CA10__disappearanceTime__c": null,
              "CA10__bucket__c": "test6_1",
              "CA10__bucketArn__c": "arn:aws:s3:::test-bucket-1",
              "CA10__bucket__r": {
                  "Id": "test6_1",
                  "CA10__disappearanceTime__c": null,
                  "CA10__loggingDestinationBucketName__c": "test-bucket-2",
                  "CA10__arn__c": "arn:aws:s3:::test-bucket-6",
                  "CA10__loggingDestinationBucketArn__c": "arn:aws:s3:::test-bucket",
                  "CA10__loggingDestinationBucket__c": "test6_1_1",
                  "CA10__loggingDestinationBucket__r": {
                      "Id": "test6_1_1",
                      "CA10__disappearanceTime__c": null,
                      "CA10__bucket__c": "test6_1_1"
                  }
              }
          }
      ]
  - path: /types/CA10__CaAwsCloudTrailTrail__c/object.extracts.yaml
    md5Hash: 3DD3C63AB8EE3DD1379ED26A102A68C4
    content: "---\nextracts:\n# Checkbox. Can't have no access, retrieved via cloudtrail:DescribeTrails\n\
      \  - name: \"CA10__multiRegionTrail__c\"\n    value: \n      FIELD:\n      \
      \  path: \"CA10__multiRegionTrail__c\"\n# Checkbox. \n  - name: \"CA10__isLogging__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__isLogging__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__isLogging__c\"\n   \
      \         currentStateMessage: \"Unable to determine Logging status. Possible\
      \ permission issue with cloudtrail:GetTrailStatus.\"\n# Checkbox. Can't have\
      \ no access, retrieved via cloudtrail:DescribeTrails\n  - name: \"CA10__logFileValidationEnabled__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__logFileValidationEnabled__c\"\
      \n# Nullable\n  - name: \"CA10__eventSelectorsJson__c\"\n    value: \n     \
      \ FIELD:\n        path: \"CA10__eventSelectorsJson__c\"\n        returnType:\
      \ BYTES\n        undeterminedIf:\n          noAccessDelegate:\n            path:\
      \ \"CA10__eventSelectorsJson__c\"\n            currentStateMessage: \"Unable\
      \ to determine Logging status. Possible permission issue with cloudtrail:GetEventSelectors.\"\
      \n  - name: \"caJsonFrom__eventSelectorsJson__c\"\n    value: \n      JSON_FROM:\n\
      \        arg:\n          EXTRACT: \"CA10__eventSelectorsJson__c\"\n        undeterminedIf:\n\
      \          isInvalid: \"Provided CloudTrail Event Selector has invalid JSON.\"\
      \n# Returns TEXT. Values: All, WriteOnly, ReadOnly\n  - name: \"caJsonText__eventSelectorsJson_readWriteType__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg: \n          EXTRACT: \"\
      caJsonFrom__eventSelectorsJson__c\"\n        expression: \"[*].readWriteType\
      \ | [0]\"\n        undeterminedIf:\n          evaluationError: \"The JSON text\
      \ query has failed.\"\n          resultTypeMismatch: \"The JSON query did not\
      \ return a text type.\"\n# Returns BOOLEAN true or false\n  - name: \"caJsonBoolean__eventSelectorsJson_includeManagementEvents__c\"\
      \n    value: \n      JSON_QUERY_BOOLEAN:\n        arg: \n          EXTRACT:\
      \ \"caJsonFrom__eventSelectorsJson__c\"\n        expression: \"[*].includeManagementEvents\
      \ | [0]\"\n        undeterminedIf:\n          evaluationError: \"The JSON boolean\
      \ query has failed.\"\n          resultTypeMismatch: \"The JSON query did not\
      \ return a boolean type.\"\n# Nullable. Can't have no access, retrieved via\
      \ cloudtrail:DescribeTrails\n  - name: \"CA10__kmsKey__c\"\n    value: \n  \
      \    FIELD:\n        path: \"CA10__kmsKey__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" : "isEmptyLookup('CA10__bucket__r')",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__bucket__r.CA10__loggingDestinationBucketName__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10__bucket__r.CA10__arn__c') == extract('CA10__bucket__r.CA10__loggingDestinationBucketArn__c')",
      "runtimeError" : null
    }
  }, {
    "Id" : "test5",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "499",
      "conditionText" : "isEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')",
      "runtimeError" : null
    }
  }, {
    "Id" : "test6",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "599",
      "conditionText" : "notEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsCloudTrailTrail__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__bucket__c STRING,
      CA10__bucketArn__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2023-10-02T10:49:08Z"),
    "CA10__bucket__c" : "test1_1",
    "CA10__bucketArn__c" : "arn:aws:s3:::test-bucket-1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test2",
    "CA10__bucket__c" : "",
    "CA10__bucketArn__c" : "arn:aws:s3:::test-bucket-2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test3",
    "CA10__bucket__c" : "test3_1",
    "CA10__bucketArn__c" : "arn:aws:s3:::test-bucket-3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test4",
    "CA10__bucket__c" : "test4_1",
    "CA10__bucketArn__c" : "arn:aws:s3:::test-bucket-4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test5",
    "CA10__bucket__c" : "test5_1",
    "CA10__bucketArn__c" : "arn:aws:s3:::test-bucket-1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test6",
    "CA10__bucket__c" : "test6_1",
    "CA10__bucketArn__c" : "arn:aws:s3:::test-bucket-1"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsBucket__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__loggingDestinationBucketName__c STRING,
      CA10__arn__c STRING,
      CA10__loggingDestinationBucketArn__c STRING,
      CA10__loggingDestinationBucket__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test1_1",
    "CA10__loggingDestinationBucketName__c" : "",
    "CA10__arn__c" : "arn:aws:s3:::test-bucket-1",
    "CA10__loggingDestinationBucketArn__c" : "",
    "CA10__loggingDestinationBucket__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : null,
    "CA10__loggingDestinationBucketName__c" : null,
    "CA10__arn__c" : null,
    "CA10__loggingDestinationBucketArn__c" : null,
    "CA10__loggingDestinationBucket__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test3_1",
    "CA10__loggingDestinationBucketName__c" : "",
    "CA10__arn__c" : "arn:aws:s3:::test-bucket-3",
    "CA10__loggingDestinationBucketArn__c" : "",
    "CA10__loggingDestinationBucket__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test4_1",
    "CA10__loggingDestinationBucketName__c" : "test-bucket-4",
    "CA10__arn__c" : "arn:aws:s3:::test-bucket-4",
    "CA10__loggingDestinationBucketArn__c" : "arn:aws:s3:::test-bucket-4",
    "CA10__loggingDestinationBucket__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test5_1",
    "CA10__loggingDestinationBucketName__c" : "test-bucket-2",
    "CA10__arn__c" : "arn:aws:s3:::test-bucket-5",
    "CA10__loggingDestinationBucketArn__c" : "arn:aws:s3:::test-bucket",
    "CA10__loggingDestinationBucket__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test6_1",
    "CA10__loggingDestinationBucketName__c" : "test-bucket-2",
    "CA10__arn__c" : "arn:aws:s3:::test-bucket-6",
    "CA10__loggingDestinationBucketArn__c" : "arn:aws:s3:::test-bucket",
    "CA10__loggingDestinationBucket__c" : "test6_1_1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-29T00:23:39Z")
    },
    "Id" : "test6_1_1"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsCloudTrailTrail__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__bucket__c STRING,
          CA10__bucketArn__c STRING,
          CA10__bucket__r STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__loggingDestinationBucketName__c STRING,
              CA10__arn__c STRING,
              CA10__loggingDestinationBucketArn__c STRING,
              CA10__loggingDestinationBucket__c STRING,
              CA10__loggingDestinationBucket__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]
      if (TextLib.isEmpty(obj.CA10__bucketArn__c) || TextLib.isEmpty(obj.CA10__bucket__c) || obj.CA10__bucket__r.CA10__disappearanceTime__c != null) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "isEmptyLookup('CA10__bucket__r')", currentStateMessage: "AWS CloudTrail references a missing bucket.", currentStateReferences: references1.join('\n'), remediation: "Ensure that CloudTrail is referencing an active S3 bucket.", runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extractChecked2() {
          if (TextLib.isEmpty(obj.CA10__bucketArn__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "extractCheck(obj.CA10__bucketArn__c)", currentStateMessage: "Related object via CA10__bucketArn__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__bucket__c)) {
              throw new Error("UNDETERMINED condition:202", {cause: {status: 'UNDETERMINED', conditionIndex: 202, conditionText: "extractCheck(obj.CA10__bucket__c)", currentStateMessage: "Related object via CA10__bucket__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10__bucket__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:203", {cause: {status: 'UNDETERMINED', conditionIndex: 203, conditionText: "extractCheck(obj.CA10__bucket__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10__bucket__c is present, but deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return extract3.call(extract3);
      }
      function extract3() { if (!this.out) { this.out = obj.CA10__bucket__r.CA10__loggingDestinationBucketName__c; } return this.out; };
      references1.push('Logging Destination Bucket Name [obj.CA10__bucket__r.CA10__loggingDestinationBucketName__c]: ' + obj.CA10__bucket__r.CA10__loggingDestinationBucketName__c);
      try {
          if (TextLib.isEmpty(extractChecked2())) {
              return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__bucket__r.CA10__loggingDestinationBucketName__c').isEmpty()", currentStateMessage: "Server access logging is not enabled.", currentStateReferences: references1.join('\n'), remediation: "Consider enabling server access logging.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[3], conditionIndex:[300..399]
      function extractChecked5() {
          if (TextLib.isEmpty(obj.CA10__bucketArn__c)) {
              throw new Error("UNDETERMINED condition:301", {cause: {status: 'UNDETERMINED', conditionIndex: 301, conditionText: "extractCheck(obj.CA10__bucketArn__c)", currentStateMessage: "Related object via CA10__bucketArn__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__bucket__c)) {
              throw new Error("UNDETERMINED condition:302", {cause: {status: 'UNDETERMINED', conditionIndex: 302, conditionText: "extractCheck(obj.CA10__bucket__c)", currentStateMessage: "Related object via CA10__bucket__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10__bucket__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:303", {cause: {status: 'UNDETERMINED', conditionIndex: 303, conditionText: "extractCheck(obj.CA10__bucket__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10__bucket__c is present, but deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return extract6.call(extract6);
      }
      function fieldChecked7() {
          if (TextLib.isEmpty(obj.CA10__bucket__r.CA10__arn__c)) {
              throw new Error("UNDETERMINED condition:304", {cause: {status: 'UNDETERMINED', conditionIndex: 304, conditionText: "CA10__arn__c.isEmpty()", currentStateMessage: "Bucket ARN cannot be empty. Potential data corruption", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__bucket__r.CA10__arn__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      function extractChecked8() {
          if (TextLib.isEmpty(obj.CA10__bucketArn__c)) {
              throw new Error("UNDETERMINED condition:305", {cause: {status: 'UNDETERMINED', conditionIndex: 305, conditionText: "extractCheck(obj.CA10__bucketArn__c)", currentStateMessage: "Related object via CA10__bucketArn__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__bucket__c)) {
              throw new Error("UNDETERMINED condition:306", {cause: {status: 'UNDETERMINED', conditionIndex: 306, conditionText: "extractCheck(obj.CA10__bucket__c)", currentStateMessage: "Related object via CA10__bucket__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10__bucket__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:307", {cause: {status: 'UNDETERMINED', conditionIndex: 307, conditionText: "extractCheck(obj.CA10__bucket__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10__bucket__c is present, but deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return extract9.call(extract9);
      }
      function fieldChecked10() {
          if (TextLib.isEmpty(obj.CA10__bucket__r.CA10__loggingDestinationBucketName__c)) {
              throw new Error("UNDETERMINED condition:308", {cause: {status: 'UNDETERMINED', conditionIndex: 308, conditionText: "CA10__loggingDestinationBucketArn__c.delegatedTo(CA10__loggingDestinationBucketName__c).isEmpty()", currentStateMessage: "Unable to determine if server access logging is enabled. Possible permission issue with s3:GetBucketLogging", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__bucket__r.CA10__loggingDestinationBucketArn__c;
      }
      function extract9() { if (!this.out) { this.out = fieldChecked10(); } return this.out; };
      references1.push('ARN [obj.CA10__bucket__r.CA10__arn__c]: ' + obj.CA10__bucket__r.CA10__arn__c);
      references1.push('Logging Destination Bucket ARN [obj.CA10__bucket__r.CA10__loggingDestinationBucketArn__c]: ' + obj.CA10__bucket__r.CA10__loggingDestinationBucketArn__c);
      try {
          if (TextLib.equal(extractChecked5(), extractChecked8())) {
              return {status: 'COMPLIANT', conditionIndex: 399, conditionText: "extract('CA10__bucket__r.CA10__arn__c') == extract('CA10__bucket__r.CA10__loggingDestinationBucketArn__c')", currentStateMessage: "Server access logging is enabled. The bucket uses itself as the logging destination.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[4], conditionIndex:[400..499]
      if (TextLib.isEmpty(obj.CA10__bucketArn__c) || TextLib.isEmpty(obj.CA10__bucket__c) || obj.CA10__bucket__r.CA10__disappearanceTime__c != null || TextLib.isEmpty(obj.CA10__bucket__r.CA10__loggingDestinationBucketArn__c) || TextLib.isEmpty(obj.CA10__bucket__r.CA10__loggingDestinationBucket__c) || obj.CA10__bucket__r.CA10__loggingDestinationBucket__r.CA10__disappearanceTime__c != null) {
          return {status: 'INCOMPLIANT', conditionIndex: 499, conditionText: "isEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')", currentStateMessage: "The destination bucket is missing.", currentStateReferences: references1.join('\n'), remediation: "Ensure that bucket access logging is referencing an active S3 bucket.", runtimeError: null};
      }
      // condition[5], conditionIndex:[500..599]
      if (TextLib.isNotEmpty(obj.CA10__bucketArn__c) && TextLib.isNotEmpty(obj.CA10__bucket__c) && obj.CA10__bucket__r.CA10__disappearanceTime__c == null && TextLib.isNotEmpty(obj.CA10__bucket__r.CA10__loggingDestinationBucketArn__c) && TextLib.isNotEmpty(obj.CA10__bucket__r.CA10__loggingDestinationBucket__c) && obj.CA10__bucket__r.CA10__loggingDestinationBucket__r.CA10__disappearanceTime__c == null) {
          return {status: 'COMPLIANT', conditionIndex: 599, conditionText: "notEmptyLookup('CA10__bucket__r.CA10__loggingDestinationBucket__r')", currentStateMessage: "Server access logging is enabled.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'UNDETERMINED', conditionIndex: 600, conditionText: "otherwise", currentStateMessage: "Unexpected values in the fields.", 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,
          sObject.CA10__bucket__c AS CA10__bucket__c,
          sObject.CA10__bucketArn__c AS CA10__bucketArn__c,
          STRUCT (
              `CA10__bucket__r`.Id AS Id,
              `CA10__bucket__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
              `CA10__bucket__r`.CA10__loggingDestinationBucketName__c AS CA10__loggingDestinationBucketName__c,
              `CA10__bucket__r`.CA10__arn__c AS CA10__arn__c,
              `CA10__bucket__r`.CA10__loggingDestinationBucketArn__c AS CA10__loggingDestinationBucketArn__c,
              `CA10__bucket__r`.CA10__loggingDestinationBucket__c AS CA10__loggingDestinationBucket__c,
              STRUCT (
                  `CA10__bucket__r.CA10__loggingDestinationBucket__r`.Id AS Id,
                  `CA10__bucket__r.CA10__loggingDestinationBucket__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
              ) AS CA10__loggingDestinationBucket__r
          ) AS CA10__bucket__r,
          process_CA10__CaAwsCloudTrailTrail__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__bucket__c AS CA10__bucket__c,
                  sObject.CA10__bucketArn__c AS CA10__bucketArn__c,
                  STRUCT (
                      `CA10__bucket__r`.Id AS Id,
                      `CA10__bucket__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      `CA10__bucket__r`.CA10__loggingDestinationBucketName__c AS CA10__loggingDestinationBucketName__c,
                      `CA10__bucket__r`.CA10__arn__c AS CA10__arn__c,
                      `CA10__bucket__r`.CA10__loggingDestinationBucketArn__c AS CA10__loggingDestinationBucketArn__c,
                      `CA10__bucket__r`.CA10__loggingDestinationBucket__c AS CA10__loggingDestinationBucket__c,
                      STRUCT (
                          `CA10__bucket__r.CA10__loggingDestinationBucket__r`.Id AS Id,
                          `CA10__bucket__r.CA10__loggingDestinationBucket__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
                      ) AS CA10__loggingDestinationBucket__r
                  ) AS CA10__bucket__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsCloudTrailTrail__c()) AS sObject
      LEFT JOIN UNNEST(mock_CA10__CaAwsBucket__c()) AS `CA10__bucket__r`
          ON sObject.CA10__bucket__c = `CA10__bucket__r`.Id
      LEFT JOIN UNNEST(mock_CA10__CaAwsBucket__c()) AS `CA10__bucket__r.CA10__loggingDestinationBucket__r`
          ON `CA10__bucket__r`.CA10__loggingDestinationBucket__c = `CA10__bucket__r.CA10__loggingDestinationBucket__r`.Id
  ) sObject ON sObject.Id = expectedResult.Id;
