---
policy: /ce/ca/aws/cloudtrail/encryption-with-kms-cmk
logic: /ce/ca/aws/cloudtrail/encryption-with-kms-cmk/prod.logic.yaml
executionTime: 2025-04-24T23:44:26.464357969Z
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__kmsKey__r')
      actual: isEmptyLookup('CA10__kmsKey__r')
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__kmsKey__r.CA10__state__c') != 'Enabled'
      actual: extract('CA10__kmsKey__r.CA10__state__c') != 'Enabled'
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__kmsKey__r.CA10__state__c') == 'Enabled'
      actual: extract('CA10__kmsKey__r.CA10__state__c') == 'Enabled'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/cloudtrail/encryption-with-kms-cmk/policy.yaml
    md5Hash: 8B29B12CB5ACFD1BB3BAA111485E6138
    content: |
      ---
      names:
        full: AWS CloudTrail is not encrypted with KMS CMK
        contextual: CloudTrail is not encrypted with KMS CMK
      description: "AWS CloudTrail is a web service that records AWS API calls for an account\
        \ and makes those logs available to users and resources in accordance with IAM policies.\
        \ AWS Key Management Service (KMS) is a managed service that helps create and control\
        \ the encryption keys used to encrypt account data, and uses Hardware Security Modules\
        \ (HSMs) to protect the security of encryption keys. CloudTrail logs can be configured\
        \ to leverage server side encryption (SSE) and KMS customer created master keys\
        \ (CMK) to further protect CloudTrail logs. It is recommended that CloudTrail be\
        \ configured to use SSE-KMS."
      type: COMPLIANCE_POLICY
      categories:
        - SECURITY
      frameworkMappings:
        - "/frameworks/cis-aws-v5.0.0/03/05"
        - "/frameworks/cloudaware/resource-security/data-encryption"
        - "/frameworks/aws-fsbp-v1.0.0/cloudtrail/02"
      similarPolicies:
        internal:
          - dec-x-d896d172
        cloudConformity:
          - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/aws/CloudTrail/cloudtrail-logs-encrypted.html
            name: CloudTrail Logs Encrypted
  - path: /ce/ca/aws/cloudtrail/encryption-with-kms-cmk/prod.logic.yaml
    md5Hash: A75437995A496E2A215EE0393ED1D137
    content: |-
      ---
      inputType: "CA10__CaAwsCloudTrailTrail__c"
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10__CaAwsCloudTrailTrail__c/object.extracts.yaml"
        - file: "/types/CA10__CaAwsKmsKey__c/object.extracts.yaml"
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "AWS CloudTrail is not encrypted with KMS CMK."
          remediationMessage: "Ensure that CloudTrail is referencing an active S3 bucket."
          check:
            IS_EMPTY_LOOKUP: "CA10__kmsKey__r"
        - status: "INCOMPLIANT"
          currentStateMessage: "AWS CloudTrail KMS CMK is not enabled."
          remediationMessage: "Ensure that CloudTrail is referencing an enabled KMS CMK."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__kmsKey__r.CA10__state__c"
              right:
                TEXT: "Enabled"
        - status: "COMPLIANT"
          currentStateMessage: "AWS CloudTrail is encrypted with KMS CMK."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__kmsKey__r.CA10__state__c"
              right:
                TEXT: "Enabled"
      otherwise:
        status: "UNDETERMINED"
        currentStateMessage: "Unexpected values in the fields."
  - 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"
  - path: /types/CA10__CaAwsKmsKey__c/object.extracts.yaml
    md5Hash: 0B059DB01E1D6F7518BF61709BB65A29
    content: "---\nextracts:\n# Values: Creating, Enabled, Disabled, PendingDeletion,\
      \ PendingImport, PendingReplicaDeletion, Unavailable, Updating. Not nullable.\n\
      \  - name: \"CA10__state__c\"\n    value: \n      FIELD:\n        path: \"CA10__state__c\"\
      \n        undeterminedIf:\n          noAccessDelegate: \n            path: \"\
      CA10__state__c\"\n            currentStateMessage: \"Unable to determine Key\
      \ state. Possible permission issue with kms:DescribeKey.\"\n          isEmpty:\
      \ \"KMS Key state is not populated yet.\"\n# Values: RSA_2048, RSA_3072, RSA_4096,\
      \ ECC_NIST_P256, ECC_NIST_P384, ECC_NIST_P521, ECC_SECG_P256K1, SYMMETRIC_DEFAULT,\
      \ HMAC_224, HMAC_256, HMAC_384, HMAC_512, SM2\n# Not nullable\n  - name: \"\
      CA10__keySpec__c\"\n    value: \n      FIELD:\n        path: \"CA10__keySpec__c\"\
      \n        undeterminedIf:\n          noAccessDelegate: \n            path: \"\
      CA10__keySpec__c\"\n            currentStateMessage: \"Unable to determine Key\
      \ state. Possible permission issue with kms:DescribeKey.\"\n          isEmpty:\
      \ \"KMS Key state is not populated yet.\"\n# Checkbox.\n  - name: \"CA10__rotationEnabled__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__rotationEnabled__c\"\n  \
      \      undeterminedIf:\n          noAccessDelegate:\n            path: \"CA10__rotationEnabled__c\"\
      \n            currentStateMessage: \"Unable to determine Key Rotation status.\
      \ Possible permission issue with kms:GetKeyRotationStatus.\"\n"
  - path: /ce/ca/aws/cloudtrail/encryption-with-kms-cmk/test-data.json
    md5Hash: 3B4C1B029B3F41BFEB844B6BB9A8AC0D
    content: "[\n    {\n        \"expectedResult\": {\n            \"status\": \"\
      DISAPPEARED\",\n            \"conditionIndex\": \"99\",\n            \"conditionText\"\
      : \"isDisappeared(CA10__disappearanceTime__c)\",\n            \"runtimeError\"\
      : null\n        },\n        \"context\": {\n            \"snapshotTime\": \"\
      2024-05-28T17:35:50Z\"\n        },\n        \"Id\": \"test1\",\n        \"CA10__disappearanceTime__c\"\
      : \"2024-05-28T17:35:50Z\",\n        \"CA10__kmsKey__c\": \"\",\n        \"\
      CA10__kmsKeyArn__c\": \"\",\n        \"CA10__kmsKey__r\": {\n            \"\
      Id\": null,\n            \"CA10__disappearanceTime__c\": null,\n           \
      \ \"CA10__state__c\": null\n        }\n    },\n    {\n        \"expectedResult\"\
      : {\n            \"status\": \"INCOMPLIANT\",\n            \"conditionIndex\"\
      : \"199\",\n            \"conditionText\": \"isEmptyLookup('CA10__kmsKey__r')\"\
      ,\n            \"runtimeError\": null\n        },\n        \"context\": {\n\
      \            \"snapshotTime\": \"2024-05-28T17:35:50Z\"\n        },\n      \
      \  \"Id\": \"test2\",\n        \"CA10__disappearanceTime__c\": null,\n     \
      \   \"CA10__kmsKey__c\": \"\",\n        \"CA10__kmsKeyArn__c\": \"\",\n    \
      \    \"CA10__kmsKey__r\": {\n            \"Id\": null,\n            \"CA10__disappearanceTime__c\"\
      : null,\n            \"CA10__state__c\": null\n        }\n    },\n    {\n  \
      \      \"expectedResult\": {\n            \"status\": \"INCOMPLIANT\",\n   \
      \         \"conditionIndex\": \"299\",\n            \"conditionText\": \"extract('CA10__kmsKey__r.CA10__state__c')\
      \ != 'Enabled'\",\n            \"runtimeError\": null\n        },\n        \"\
      context\": {\n            \"snapshotTime\": \"2024-05-28T17:35:50Z\"\n     \
      \   },\n        \"Id\": \"test3\",\n        \"CA10__disappearanceTime__c\":\
      \ null,\n        \"CA10__kmsKey__c\": \"test3_1\",                \n       \
      \ \"CA10__kmsKeyArn__c\": \"arn:aws:kms:us-east-1:123456789123:key/1-2-3-4-5\"\
      ,\n        \"CA10__kmsKey__r\": {\n            \"Id\": \"test3_1\",\n      \
      \      \"CA10__disappearanceTime__c\": null,\n            \"CA10__state__c\"\
      : \"Disabled\"\n        }\n    },\n    {\n        \"expectedResult\": {\n  \
      \          \"status\": \"COMPLIANT\",\n            \"conditionIndex\": \"399\"\
      ,\n            \"conditionText\": \"extract('CA10__kmsKey__r.CA10__state__c')\
      \ == 'Enabled'\",\n            \"runtimeError\": null\n        },\n        \"\
      context\": {\n            \"snapshotTime\": \"2024-05-28T17:35:50Z\"\n     \
      \   },\n        \"Id\": \"test4\",\n        \"CA10__disappearanceTime__c\":\
      \ null,\n        \"CA10__kmsKey__c\": \"test4_1\",\n        \"CA10__kmsKeyArn__c\"\
      : \"arn:aws:kms:us-east-1:123456789123:key/1-2-3-4-5\",\n        \"CA10__kmsKey__r\"\
      : {\n            \"Id\": \"test4_1\",\n            \"CA10__disappearanceTime__c\"\
      : null,\n            \"CA10__state__c\": \"Enabled\"\n        }\n    }\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__kmsKey__r')",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__kmsKey__r.CA10__state__c') != 'Enabled'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10__kmsKey__r.CA10__state__c') == 'Enabled'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsCloudTrailTrail__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__kmsKey__c STRING,
      CA10__kmsKeyArn__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2024-05-28T17:35:50Z"),
    "CA10__kmsKey__c" : "",
    "CA10__kmsKeyArn__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : "test2",
    "CA10__kmsKey__c" : "",
    "CA10__kmsKeyArn__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : "test3",
    "CA10__kmsKey__c" : "test3_1",
    "CA10__kmsKeyArn__c" : "arn:aws:kms:us-east-1:123456789123:key/1-2-3-4-5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : "test4",
    "CA10__kmsKey__c" : "test4_1",
    "CA10__kmsKeyArn__c" : "arn:aws:kms:us-east-1:123456789123:key/1-2-3-4-5"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsKmsKey__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__state__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : null,
    "CA10__state__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : "test3_1",
    "CA10__state__c" : "Disabled"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-05-28T17:35:50Z")
    },
    "Id" : "test4_1",
    "CA10__state__c" : "Enabled"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsCloudTrailTrail__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__kmsKey__c STRING,
          CA10__kmsKeyArn__c STRING,
          CA10__kmsKey__r STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__state__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]
      if (TextLib.isEmpty(obj.CA10__kmsKeyArn__c) || TextLib.isEmpty(obj.CA10__kmsKey__c) || obj.CA10__kmsKey__r.CA10__disappearanceTime__c != null) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "isEmptyLookup('CA10__kmsKey__r')", currentStateMessage: "AWS CloudTrail is not encrypted with KMS CMK.", 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__kmsKeyArn__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "extractCheck(obj.CA10__kmsKeyArn__c)", currentStateMessage: "Related object via CA10__kmsKeyArn__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__kmsKey__c)) {
              throw new Error("UNDETERMINED condition:202", {cause: {status: 'UNDETERMINED', conditionIndex: 202, conditionText: "extractCheck(obj.CA10__kmsKey__c)", currentStateMessage: "Related object via CA10__kmsKey__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10__kmsKey__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:203", {cause: {status: 'UNDETERMINED', conditionIndex: 203, conditionText: "extractCheck(obj.CA10__kmsKey__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10__kmsKey__c is present, but deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return extract3.call(extract3);
      }
      function fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__kmsKey__r.CA10__state__c)) {
              throw new Error("UNDETERMINED condition:204", {cause: {status: 'UNDETERMINED', conditionIndex: 204, conditionText: "CA10__state__c.delegatedTo(CA10__state__c).isEmpty()", currentStateMessage: "Unable to determine Key state. Possible permission issue with kms:DescribeKey.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__kmsKey__r.CA10__state__c)) {
              throw new Error("UNDETERMINED condition:205", {cause: {status: 'UNDETERMINED', conditionIndex: 205, conditionText: "CA10__state__c.isEmpty()", currentStateMessage: "KMS Key state is not populated yet.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__kmsKey__r.CA10__state__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('State [obj.CA10__kmsKey__r.CA10__state__c]: ' + obj.CA10__kmsKey__r.CA10__state__c);
      try {
          if (TextLib.notEqual(extractChecked2(), 'Enabled')) {
              return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__kmsKey__r.CA10__state__c') != 'Enabled'", currentStateMessage: "AWS CloudTrail KMS CMK is not enabled.", currentStateReferences: references1.join('\n'), remediation: "Ensure that CloudTrail is referencing an enabled KMS CMK.", 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__kmsKeyArn__c)) {
              throw new Error("UNDETERMINED condition:301", {cause: {status: 'UNDETERMINED', conditionIndex: 301, conditionText: "extractCheck(obj.CA10__kmsKeyArn__c)", currentStateMessage: "Related object via CA10__kmsKeyArn__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__kmsKey__c)) {
              throw new Error("UNDETERMINED condition:302", {cause: {status: 'UNDETERMINED', conditionIndex: 302, conditionText: "extractCheck(obj.CA10__kmsKey__c)", currentStateMessage: "Related object via CA10__kmsKey__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10__kmsKey__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:303", {cause: {status: 'UNDETERMINED', conditionIndex: 303, conditionText: "extractCheck(obj.CA10__kmsKey__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10__kmsKey__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__kmsKey__r.CA10__state__c)) {
              throw new Error("UNDETERMINED condition:304", {cause: {status: 'UNDETERMINED', conditionIndex: 304, conditionText: "CA10__state__c.delegatedTo(CA10__state__c).isEmpty()", currentStateMessage: "Unable to determine Key state. Possible permission issue with kms:DescribeKey.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__kmsKey__r.CA10__state__c)) {
              throw new Error("UNDETERMINED condition:305", {cause: {status: 'UNDETERMINED', conditionIndex: 305, conditionText: "CA10__state__c.isEmpty()", currentStateMessage: "KMS Key state is not populated yet.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__kmsKey__r.CA10__state__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      try {
          if (TextLib.equal(extractChecked5(), 'Enabled')) {
              return {status: 'COMPLIANT', conditionIndex: 399, conditionText: "extract('CA10__kmsKey__r.CA10__state__c') == 'Enabled'", currentStateMessage: "AWS CloudTrail is encrypted with KMS CMK.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'UNDETERMINED', conditionIndex: 400, 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__kmsKey__c AS CA10__kmsKey__c,
          sObject.CA10__kmsKeyArn__c AS CA10__kmsKeyArn__c,
          STRUCT (
              `CA10__kmsKey__r`.Id AS Id,
              `CA10__kmsKey__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
              `CA10__kmsKey__r`.CA10__state__c AS CA10__state__c
          ) AS CA10__kmsKey__r,
          process_CA10__CaAwsCloudTrailTrail__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__kmsKey__c AS CA10__kmsKey__c,
                  sObject.CA10__kmsKeyArn__c AS CA10__kmsKeyArn__c,
                  STRUCT (
                      `CA10__kmsKey__r`.Id AS Id,
                      `CA10__kmsKey__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      `CA10__kmsKey__r`.CA10__state__c AS CA10__state__c
                  ) AS CA10__kmsKey__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsCloudTrailTrail__c()) AS sObject
      LEFT JOIN UNNEST(mock_CA10__CaAwsKmsKey__c()) AS `CA10__kmsKey__r`
          ON sObject.CA10__kmsKey__c = `CA10__kmsKey__r`.Id
  ) sObject ON sObject.Id = expectedResult.Id;
