---
policy: /ce/ca/google/compute-engine/disk-csek-encryption
logic: /ce/ca/google/compute-engine/disk-csek-encryption/prod.logic.yaml
executionTime: 2025-04-24T23:46:50.237220913Z
rows:
  - id: 111
    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: 112
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: isEmptyLookup('CA10__gceDisk__r')
      actual: isEmptyLookup('CA10__gceDisk__r')
    runtimeError: {}
  - id: 113
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: 114
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c').isNotEmpty()
      actual: extract('CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c').isNotEmpty()
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/compute-engine/disk-csek-encryption/policy.yaml
    md5Hash: 8FB385D8DF983D0E66482E853B83CF6C
    content: "---\nnames: \n  full: \"Google GCE Disk for critical VMs is not encrypted\
      \ with Customer-Supplied Encryption Key (CSEK)\"\n  contextual: \"Disk for critical\
      \ VMs is not encrypted with Customer-Supplied Encryption Key (CSEK)\"\ndescription:\
      \ \"Customer-Supplied Encryption Keys (CSEK) are a feature in Google Cloud Storage\
      \ and \\\n Google Compute Engine. If you supply your own encryption keys, Google\
      \ uses your key to protect  \\ \n the Google-generated keys used to encrypt\
      \ and decrypt your data. By default, Google Compute Engine \\ \n encrypts all\
      \ data at rest. Compute Engine handles and manages this encryption for you without\
      \ any \\ \n additional actions on your part. However, if you wanted to control\
      \ and manage this encryption \\ \n yourself, you can provide your own encryption\
      \ keys.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  - \"SECURITY\"\nframeworkMappings:\n\
      \  - /frameworks/cis-gcp-v3.0.0/04/07\n  - /frameworks/cloudaware/resource-security/data-encryption\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/ComputeEngine/enable-encryption-with-csek.html\"\
      \n      name: \"Enable VM Disk Encryption with Customer-Supplied Encryption\
      \ Keys\"\n"
  - path: /ce/ca/google/compute-engine/disk-csek-encryption/prod.logic.yaml
    md5Hash: 10F74330B9CFF27836FE31DDF632CBC5
    content: |
      ---
      inputType: "CA10__CaGoogleGceAttachedDisk__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaGoogleGceDisk__c/object.extracts.yaml
      conditions:
        - status: "UNDETERMINED"
          currentStateMessage: "Ensure that VM disk is referencing an active GCE disk."
          check:
            IS_EMPTY_LOOKUP: "CA10__gceDisk__r"
        - status: "COMPLIANT"
          currentStateMessage: "Encryption type is set to Customer supplied key."
          check:
            NOT_EMPTY:
              arg:
                EXTRACT: "CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "Encryption type isn't set to Customer supplied key."
        remediationMessage: "Currently there is no way to update the encryption of an existing disk. \
          Therefore you should create a new disk with Encryption set to Customer supplied key."
  - path: /ce/ca/google/compute-engine/disk-csek-encryption/test-data.json
    md5Hash: E2B3FEB35107B7099A0B90C6F58FC598
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-20T10:57:59Z" },
          "Id": "111",
          "CA10__disappearanceTime__c": "2025-01-18T08:34:24Z",
          "CA10__gceDisk__c": "111d",
          "CA10__gceDiskUniqueId__c": "projects/1116be4b3c14faf",
          "CA10__gceDisk__r": {
            "Id": "111d",
            "CA10__disappearanceTime__c": null,
            "CA10__diskEncryptionKeySha256__c": ""
          }
        },
        {
          "expectedResult": {
            "status": "UNDETERMINED",
            "conditionIndex": "199",
            "conditionText": "isEmptyLookup('CA10__gceDisk__r')",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-20T10:57:59Z" },
          "Id": "112",
          "CA10__disappearanceTime__c": null,
          "CA10__gceDisk__c": "112d",
          "CA10__gceDiskUniqueId__c": "projects/112765a87899d74",
          "CA10__gceDisk__r": {
            "Id": "112d",
            "CA10__disappearanceTime__c": "2025-01-20T09:42:46Z",
            "CA10__diskEncryptionKeySha256__c": ""
          }
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-20T10:57:59Z" },
          "Id": "113",
          "CA10__disappearanceTime__c": null,
          "CA10__gceDisk__c": "113d",
          "CA10__gceDiskUniqueId__c": "projects/113instance-1",
          "CA10__gceDisk__r": {
            "Id": "113d",
            "CA10__disappearanceTime__c": null,
            "CA10__diskEncryptionKeySha256__c": ""
          }
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c').isNotEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-20T10:57:59Z" },
          "Id": "114",
          "CA10__disappearanceTime__c": null,
          "CA10__gceDisk__c": "114d",
          "CA10__gceDiskUniqueId__c": "projects/113instance-12",
          "CA10__gceDisk__r": {
            "Id": "114d",
            "CA10__disappearanceTime__c": null,
            "CA10__diskEncryptionKeySha256__c": "45454567678899090"
          }
        }
      ]
  - path: /types/CA10__CaGoogleGceDisk__c/object.extracts.yaml
    md5Hash: 607B98DD4D0C43210596783883E0C2C3
    content: "---\nextracts:\n  - name: \"CA10__diskEncryptionKeySha256__c\"\n   \
      \ value: \n      FIELD:\n        path: \"CA10__diskEncryptionKeySha256__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" : "111",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "112",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "199",
      "conditionText" : "isEmptyLookup('CA10__gceDisk__r')",
      "runtimeError" : null
    }
  }, {
    "Id" : "113",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "114",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c').isNotEmpty()",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleGceAttachedDisk__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__gceDisk__c STRING,
      CA10__gceDiskUniqueId__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "111",
    "CA10__disappearanceTime__c" : new Date("2025-01-18T08:34:24Z"),
    "CA10__gceDisk__c" : "111d",
    "CA10__gceDiskUniqueId__c" : "projects/1116be4b3c14faf"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "112",
    "CA10__gceDisk__c" : "112d",
    "CA10__gceDiskUniqueId__c" : "projects/112765a87899d74"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "113",
    "CA10__gceDisk__c" : "113d",
    "CA10__gceDiskUniqueId__c" : "projects/113instance-1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "114",
    "CA10__gceDisk__c" : "114d",
    "CA10__gceDiskUniqueId__c" : "projects/113instance-12"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleGceDisk__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__diskEncryptionKeySha256__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "111d",
    "CA10__diskEncryptionKeySha256__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "112d",
    "CA10__disappearanceTime__c" : new Date("2025-01-20T09:42:46Z"),
    "CA10__diskEncryptionKeySha256__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "113d",
    "CA10__diskEncryptionKeySha256__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-20T10:57:59Z")
    },
    "Id" : "114d",
    "CA10__diskEncryptionKeySha256__c" : "45454567678899090"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleGceAttachedDisk__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__gceDisk__c STRING,
          CA10__gceDiskUniqueId__c STRING,
          CA10__gceDisk__r STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__diskEncryptionKeySha256__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]
      if (TextLib.isEmpty(obj.CA10__gceDiskUniqueId__c) || TextLib.isEmpty(obj.CA10__gceDisk__c) || obj.CA10__gceDisk__r.CA10__disappearanceTime__c != null) {
          return {status: 'UNDETERMINED', conditionIndex: 199, conditionText: "isEmptyLookup('CA10__gceDisk__r')", currentStateMessage: "Ensure that VM disk is referencing an active GCE disk.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extractChecked2() {
          if (TextLib.isEmpty(obj.CA10__gceDiskUniqueId__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "extractCheck(obj.CA10__gceDiskUniqueId__c)", currentStateMessage: "Related object via CA10__gceDiskUniqueId__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__gceDisk__c)) {
              throw new Error("UNDETERMINED condition:202", {cause: {status: 'UNDETERMINED', conditionIndex: 202, conditionText: "extractCheck(obj.CA10__gceDisk__c)", currentStateMessage: "Related object via CA10__gceDisk__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10__gceDisk__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:203", {cause: {status: 'UNDETERMINED', conditionIndex: 203, conditionText: "extractCheck(obj.CA10__gceDisk__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10__gceDisk__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__gceDisk__r.CA10__diskEncryptionKeySha256__c; } return this.out; };
      references1.push('Disk Encryption Key: SHA-256 [obj.CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c]: ' + obj.CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c);
      try {
          if (TextLib.isNotEmpty(extractChecked2())) {
              return {status: 'COMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__gceDisk__r.CA10__diskEncryptionKeySha256__c').isNotEmpty()", currentStateMessage: "Encryption type is set to Customer supplied key.", 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: 300, conditionText: "otherwise", currentStateMessage: "Encryption type isn't set to Customer supplied key.", currentStateReferences: references1.join('\n'), remediation: "Currently there is no way to update the encryption of an existing disk. Therefore you should create a new disk with Encryption set to Customer supplied key.", 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__gceDisk__c AS CA10__gceDisk__c,
          sObject.CA10__gceDiskUniqueId__c AS CA10__gceDiskUniqueId__c,
          STRUCT (
              `CA10__gceDisk__r`.Id AS Id,
              `CA10__gceDisk__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
              `CA10__gceDisk__r`.CA10__diskEncryptionKeySha256__c AS CA10__diskEncryptionKeySha256__c
          ) AS CA10__gceDisk__r,
          process_CA10__CaGoogleGceAttachedDisk__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__gceDisk__c AS CA10__gceDisk__c,
                  sObject.CA10__gceDiskUniqueId__c AS CA10__gceDiskUniqueId__c,
                  STRUCT (
                      `CA10__gceDisk__r`.Id AS Id,
                      `CA10__gceDisk__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      `CA10__gceDisk__r`.CA10__diskEncryptionKeySha256__c AS CA10__diskEncryptionKeySha256__c
                  ) AS CA10__gceDisk__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleGceAttachedDisk__c()) AS sObject
      LEFT JOIN UNNEST(mock_CA10__CaGoogleGceDisk__c()) AS `CA10__gceDisk__r`
          ON sObject.CA10__gceDisk__c = `CA10__gceDisk__r`.Id
  ) sObject ON sObject.Id = expectedResult.Id;
