---
policy: /ce/ca/google/logging/storage-bucket-retention-policy
logic: /ce/ca/google/logging/storage-bucket-retention-policy/prod.logic.yaml
executionTime: 2025-04-24T23:47:14.173552124Z
rows:
  - id: a0oVU111
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10G1__disappearanceTime__c)
      actual: isDisappeared(CA10G1__disappearanceTime__c)
    runtimeError: {}
  - id: a0oV222
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: isEmptyLookup('CA10G1__bucket__r')
      actual: isEmptyLookup('CA10G1__bucket__r')
    runtimeError: {}
  - id: a0o33
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: a0oV44
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10G1__bucket__r.CA10__retentionPolicyLock__c') == 'Locked'
      actual: extract('CA10G1__bucket__r.CA10__retentionPolicyLock__c') == 'Locked'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/logging/storage-bucket-retention-policy/policy.yaml
    md5Hash: 81CA6BDD85D9E444F5B457B9D4C7AE64
    content: "---\nnames: \n  full: \"Google Logging Log Sink exports logs to a Storage\
      \ Bucket without Bucket Lock\"\n  contextual: \"og Sink exports logs to a Storage\
      \ Bucket without Bucket Lock\"\ndescription: \"Enabling retention policies on\
      \ log buckets will protect logs stored in cloud storage buckets from being overwritten\
      \ \\\n or accidentally deleted. It is recommended to set up retention policies\
      \ and configure Bucket Lock on all storage buckets that are \\\n used as log\
      \ sinks.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  - \"SECURITY\"\nframeworkMappings:\n\
      \  - /frameworks/cis-gcp-v3.0.0/02/03\n  - /frameworks/cloudaware/logging-and-monitoring/logging-and-monitoring-configuration\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/CloudStorage/retention-policies-with-bucket-lock.html\"\
      \n      name: \"Configure Retention Policies with Bucket Lock\"\n"
  - path: /ce/ca/google/logging/storage-bucket-retention-policy/prod.logic.yaml
    md5Hash: 7826ABD586C51235627C4C5D598F2C96
    content: |
      ---

      inputType: "CA10G1__CaGoogleLoggingLogSink__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10G1__CaGoogleLoggingLogSink__c/object.extracts.yaml
        - file: /types/CA10__CaGoogleStorageBucket__c/object.extracts.yaml
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The Sink does not export logs into a bucket."
          check:
            IS_EMPTY_LOOKUP: "CA10G1__bucket__r"
        - status: "COMPLIANT"
          currentStateMessage: "Sink with a locked bucket."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10G1__bucket__r.CA10__retentionPolicyLock__c"
              right:
                TEXT: "Locked"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "The sink is configured to export logs in a storage bucket without a lock."
        remediationMessage: "Enable a retention policy and Bucket Lock."
  - path: /types/CA10__CaGoogleStorageBucket__c/object.extracts.yaml
    md5Hash: 3651E2495E157A71A7AF35D341E7F6F2
    content: "---\nextracts:\n  - name: \"CA10__levelAccessState__c\"\n    value:\
      \ \n      FIELD:\n        path: \"CA10__levelAccessState__c\"\n  - name: \"\
      CA10__retentionPolicyLock__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10__retentionPolicyLock__c\""
  - path: /ce/ca/google/logging/storage-bucket-retention-policy/test-data.json
    md5Hash: 3E16BC85ED910FF9D79EA85C663293AF
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10G1__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-12-01T20:32:29Z" },
          "Id": "a0oVU111",
          "RecordTypeId": "01211",
          "CA10G1__disappearanceTime__c": "2024-11-25T07:52:01Z",
          "CA10G1__bucket__c": "",
          "CA10G1__bucketName__c": "",
          "RecordType": {
            "DeveloperName": "caGoogleLoggingLogSinkProject",
            "Id": "01211"
          },
          "CA10G1__bucket__r": {
            "Id": null,
            "CA10__disappearanceTime__c": null,
            "CA10__retentionPolicyLock__c": null
          }
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "isEmptyLookup('CA10G1__bucket__r')",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-12-01T20:32:29Z" },
          "Id": "a0oV222",
          "RecordTypeId": "012VU22",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__bucket__c": "",
          "CA10G1__bucketName__c": "",
          "RecordType": {
            "DeveloperName": "caGoogleLoggingLogSinkProject",
            "Id": "012VU22"
          },
          "CA10G1__bucket__r": {
            "Id": null,
            "CA10__disappearanceTime__c": null,
            "CA10__retentionPolicyLock__c": null
          }
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-12-01T20:32:29Z" },
          "Id": "a0o33",
          "RecordTypeId": "01233",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__bucket__c": "a8O0e33",
          "CA10G1__bucketName__c": "cloudaware-logs-for-cloudaware-vm",
          "RecordType": {
            "DeveloperName": "caGoogleLoggingLogSinkProject",
            "Id": "01233"
          },
          "CA10G1__bucket__r": {
            "Id": "a8O0e000000k9dOEAQ",
            "CA10__disappearanceTime__c": null,
            "CA10__retentionPolicyLock__c": ""
          }
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10G1__bucket__r.CA10__retentionPolicyLock__c') == 'Locked'",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-12-01T20:32:29Z" },
          "Id": "a0oV44",
          "RecordTypeId": "012VU000000SkERYA0",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__bucket__c": "a8O044",
          "CA10G1__bucketName__c": "cloudaware-logs-for-cloudaware-vm",
          "RecordType": {
            "DeveloperName": "caGoogleLoggingLogSinkProject",
            "Id": "01244"
          },
          "CA10G1__bucket__r": {
            "Id": "a8O044",
            "CA10__disappearanceTime__c": null,
            "CA10__retentionPolicyLock__c": "Locked"
          }
        }
      ]
  - path: /types/CA10G1__CaGoogleLoggingLogSink__c/object.extracts.yaml
    md5Hash: 0A52360258DB3850B42C9067DA520816
    content: "---\nextracts:\n  - name: \"CA10G1__filter__c\"\n    value: \n     \
      \ FIELD:\n        path: \"CA10G1__filter__c\"\n  - name: \"CA10G1__destination__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10G1__destination__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" : "a0oVU111",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10G1__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "a0oV222",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "isEmptyLookup('CA10G1__bucket__r')",
      "runtimeError" : null
    }
  }, {
    "Id" : "a0o33",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "a0oV44",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10G1__bucket__r.CA10__retentionPolicyLock__c') == 'Locked'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10G1__CaGoogleLoggingLogSink__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      RecordTypeId STRING,
      CA10G1__disappearanceTime__c TIMESTAMP,
      CA10G1__bucket__c STRING,
      CA10G1__bucketName__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : "a0oVU111",
    "RecordTypeId" : "01211",
    "CA10G1__disappearanceTime__c" : new Date("2024-11-25T07:52:01Z"),
    "CA10G1__bucket__c" : "",
    "CA10G1__bucketName__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : "a0oV222",
    "RecordTypeId" : "012VU22",
    "CA10G1__bucket__c" : "",
    "CA10G1__bucketName__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : "a0o33",
    "RecordTypeId" : "01233",
    "CA10G1__bucket__c" : "a8O0e33",
    "CA10G1__bucketName__c" : "cloudaware-logs-for-cloudaware-vm"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : "a0oV44",
    "RecordTypeId" : "012VU000000SkERYA0",
    "CA10G1__bucket__c" : "a8O044",
    "CA10G1__bucketName__c" : "cloudaware-logs-for-cloudaware-vm"
  } ];
  """;

  CREATE TEMP FUNCTION mock_RecordType()
  RETURNS ARRAY<STRUCT<
      DeveloperName STRING,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "DeveloperName" : "caGoogleLoggingLogSinkProject",
    "Id" : "01211"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "DeveloperName" : "caGoogleLoggingLogSinkProject",
    "Id" : "012VU22"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "DeveloperName" : "caGoogleLoggingLogSinkProject",
    "Id" : "01233"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "DeveloperName" : "caGoogleLoggingLogSinkProject",
    "Id" : "01244"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleStorageBucket__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__retentionPolicyLock__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : null,
    "CA10__retentionPolicyLock__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : "a8O0e000000k9dOEAQ",
    "CA10__retentionPolicyLock__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-01T20:32:29Z")
    },
    "Id" : "a8O044",
    "CA10__retentionPolicyLock__c" : "Locked"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10G1__CaGoogleLoggingLogSink__c(
      obj STRUCT<
          Id STRING,
          RecordTypeId STRING,
          CA10G1__disappearanceTime__c TIMESTAMP,
          CA10G1__bucket__c STRING,
          CA10G1__bucketName__c STRING,
          RecordType STRUCT<
              DeveloperName STRING,
              Id STRING
          >,
          CA10G1__bucket__r STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__retentionPolicyLock__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 [CA10G1__disappearanceTime__c]: ' + obj.CA10G1__disappearanceTime__c);
      if (obj.CA10G1__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10G1__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.CA10G1__bucketName__c) || TextLib.isEmpty(obj.CA10G1__bucket__c) || obj.CA10G1__bucket__r.CA10__disappearanceTime__c != null) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "isEmptyLookup('CA10G1__bucket__r')", currentStateMessage: "The Sink does not export logs into a bucket.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extractChecked2() {
          if (TextLib.isEmpty(obj.CA10G1__bucketName__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "extractCheck(obj.CA10G1__bucketName__c)", currentStateMessage: "Related object via CA10G1__bucketName__c has empty ID", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10G1__bucket__c)) {
              throw new Error("UNDETERMINED condition:202", {cause: {status: 'UNDETERMINED', conditionIndex: 202, conditionText: "extractCheck(obj.CA10G1__bucket__c)", currentStateMessage: "Related object via CA10G1__bucket__c is not present", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (obj.CA10G1__bucket__r.CA10__disappearanceTime__c != null) {
              throw new Error("UNDETERMINED condition:203", {cause: {status: 'UNDETERMINED', conditionIndex: 203, conditionText: "extractCheck(obj.CA10G1__bucket__r.CA10__disappearanceTime__c)", currentStateMessage: "Related object via CA10G1__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.CA10G1__bucket__r.CA10__retentionPolicyLock__c; } return this.out; };
      references1.push('Retention Policy: Lock [obj.CA10G1__bucket__r.CA10__retentionPolicyLock__c]: ' + obj.CA10G1__bucket__r.CA10__retentionPolicyLock__c);
      try {
          if (TextLib.equal(extractChecked2(), 'Locked')) {
              return {status: 'COMPLIANT', conditionIndex: 299, conditionText: "extract('CA10G1__bucket__r.CA10__retentionPolicyLock__c') == 'Locked'", currentStateMessage: "Sink with a locked bucket.", 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: "The sink is configured to export logs in a storage bucket without a lock.", currentStateReferences: references1.join('\n'), remediation: "Enable a retention policy and Bucket Lock.", 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.RecordTypeId AS RecordTypeId,
          sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
          sObject.CA10G1__bucket__c AS CA10G1__bucket__c,
          sObject.CA10G1__bucketName__c AS CA10G1__bucketName__c,
          STRUCT (
              `RecordType`.DeveloperName AS DeveloperName,
              `RecordType`.Id AS Id
          ) AS RecordType,
          STRUCT (
              `CA10G1__bucket__r`.Id AS Id,
              `CA10G1__bucket__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
              `CA10G1__bucket__r`.CA10__retentionPolicyLock__c AS CA10__retentionPolicyLock__c
          ) AS CA10G1__bucket__r,
          process_CA10G1__CaGoogleLoggingLogSink__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.RecordTypeId AS RecordTypeId,
                  sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
                  sObject.CA10G1__bucket__c AS CA10G1__bucket__c,
                  sObject.CA10G1__bucketName__c AS CA10G1__bucketName__c,
                  STRUCT (
                      `RecordType`.DeveloperName AS DeveloperName,
                      `RecordType`.Id AS Id
                  ) AS RecordType,
                  STRUCT (
                      `CA10G1__bucket__r`.Id AS Id,
                      `CA10G1__bucket__r`.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      `CA10G1__bucket__r`.CA10__retentionPolicyLock__c AS CA10__retentionPolicyLock__c
                  ) AS CA10G1__bucket__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10G1__CaGoogleLoggingLogSink__c()) AS sObject
      LEFT JOIN UNNEST(mock_RecordType()) AS `RecordType`
          ON sObject.RecordTypeId = `RecordType`.Id
      LEFT JOIN UNNEST(mock_CA10__CaGoogleStorageBucket__c()) AS `CA10G1__bucket__r`
          ON sObject.CA10G1__bucket__c = `CA10G1__bucket__r`.Id
  ) sObject ON sObject.Id = expectedResult.Id;
