---
policy: /ce/ca/google/big-query/dataset-cmek-encryption
logic: /ce/ca/google/big-query/dataset-cmek-encryption/prod.logic.yaml
executionTime: 2025-04-24T23:46:47.289724685Z
rows:
  - id: a7m00
    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: a7m09
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: a7m08
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__kmsKeyName__c').isNotEmpty()
      actual: extract('CA10__kmsKeyName__c').isNotEmpty()
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/big-query/dataset-cmek-encryption/policy.yaml
    md5Hash: 241603B30AFE5A018C9FF1B411F374A9
    content: "---\nnames: \n  full: \"Google BigQuery Dataset is not encrypted with\
      \ Customer-Managed Encryption Key (CMEK)\"\n  contextual: \"Dataset is not encrypted\
      \ with Customer-Managed Encryption Key (CMEK)\"\ndescription: \"BigQuery by\
      \ default encrypts the data as rest by employing Envelope Encryption using \\\
      \n Google managed cryptographic keys. The data is encrypted using the data encryption\
      \ keys \\ \n and data encryption keys themselves are further encrypted using\
      \ key encryption keys. \\ \n This is seamless and do not require any additional\
      \ input from the user. However, if you want \\ \n to have greater control, Customer-managed\
      \ encryption keys (CMEK) can be used as encryption key \\ \n management solution\
      \ for BigQuery Data Sets.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  - \"SECURITY\"\
      \nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/07/03\n  - /frameworks/cloudaware/resource-security/data-encryption\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity/knowledge-base/gcp/BigQuery/dataset-encryption-cmek.html\"\
      \n      name: \"Enable BigQuery Dataset Encryption with Customer-Managed Encryption\
      \ Keys\"\n"
  - path: /ce/ca/google/big-query/dataset-cmek-encryption/prod.logic.yaml
    md5Hash: 1A1530D0B9AEAFBE1C4811826842A59F
    content: |-
      ---

      inputType: "CA10__CaGoogleBigQueryDataset__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaGoogleBigQueryDataset__c/object.extracts.yaml
      conditions:
        - status: "COMPLIANT"
          currentStateMessage: "BigQuery Dataset is encrypted with a KMS Key."
          check:
            NOT_EMPTY:
              arg:
                EXTRACT: "CA10__kmsKeyName__c"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "BigQuery Dataset is not encrypted with a KMS Key."
        remediationMessage: "Set a KMS key for BigQuery Dataset."
  - path: /ce/ca/google/big-query/dataset-cmek-encryption/test-data.json
    md5Hash: 566470E7982122469E17962CC15ACDAD
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a7m00",
          "CA10__disappearanceTime__c": "2022-09-03T13:25:44Z",
          "CA10__kmsKeyName__c": ""
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a7m09",
          "CA10__disappearanceTime__c": null,
          "CA10__kmsKeyName__c": ""
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__kmsKeyName__c').isNotEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a7m08",
          "CA10__disappearanceTime__c": null,
          "CA10__kmsKeyName__c": "keyName"
        }
      ]
  - path: /types/CA10__CaGoogleBigQueryDataset__c/object.extracts.yaml
    md5Hash: B8DC026BE7E9C3A134486F67E1661FE7
    content: "---\nextracts:\n  - name: \"CA10__access__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__access__c\"\n        undeterminedIf:\n          isEmpty:\
      \  \"Access field should not be empty\"\n  - name: \"CA10__kmsKeyName__c\"\n\
      \    value: \n      FIELD:\n        path: \"CA10__kmsKeyName__c\""
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" : "a7m00",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "a7m09",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "a7m08",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__kmsKeyName__c').isNotEmpty()",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleBigQueryDataset__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__kmsKeyName__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a7m00",
    "CA10__disappearanceTime__c" : new Date("2022-09-03T13:25:44Z"),
    "CA10__kmsKeyName__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a7m09",
    "CA10__kmsKeyName__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a7m08",
    "CA10__kmsKeyName__c" : "keyName"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleBigQueryDataset__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__kmsKeyName__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 Google [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__kmsKeyName__c; } return this.out; };
      references1.push('KMS Key Name [obj.CA10__kmsKeyName__c]: ' + obj.CA10__kmsKeyName__c);
      if (TextLib.isNotEmpty(extract3.call(extract3))) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__kmsKeyName__c').isNotEmpty()", currentStateMessage: "BigQuery Dataset is encrypted with a KMS Key.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "BigQuery Dataset is not encrypted with a KMS Key.", currentStateReferences: references1.join('\n'), remediation: "Set a KMS key for BigQuery Dataset.", 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__kmsKeyName__c AS CA10__kmsKeyName__c,
          process_CA10__CaGoogleBigQueryDataset__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__kmsKeyName__c AS CA10__kmsKeyName__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleBigQueryDataset__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
