---
policy: /ce/ca/google/api/api-key-rotation
logic: /ce/ca/google/api/api-key-rotation/prod.logic.yaml
executionTime: 2025-04-24T23:46:44.48649836Z
rows:
  - id: a0n11
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10G1__createTime__c').beyondLastDays(90)
      actual: extract('CA10G1__createTime__c').beyondLastDays(90)
    runtimeError: {}
  - id: a0n22
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/api/api-key-rotation/policy.yaml
    md5Hash: 4F20E53AB20E822F59E8C4AE02A00E02
    content: "---\nnames: \n  full: \"Google API Key is not rotated every 90 days\"\
      \n  contextual: \"API Key is not rotated every 90 days\"\ndescription: \"API\
      \ Keys should only be used for services in cases where other authentication\
      \ methods \\ \n  are unavailable. If they are in use it is recommended to rotate\
      \ API keys every 90 days.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  - \"SECURITY\"\
      \nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/01/15\n  - /frameworks/cloudaware/secret-and-certificate-governance/expiration-management\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/CloudAPI/rotate-api-keys.html\"\
      \n      name: \"Rotate Google Cloud API Keys\"\n "
  - path: /ce/ca/google/api/api-key-rotation/prod.logic.yaml
    md5Hash: 700FBA87FC4CE7A18287963C510D38D3
    content: "---\n\ninputType: \"CA10G1__CaGoogleApiKey__c\"\ntestData:\n  - file:\
      \ test-data.json\nimportExtracts:\n  - file: /types/CA10G1__CaGoogleApiKey__c/object.extracts.yaml\n\
      conditions:\n  - status: \"INCOMPLIANT\"\n    currentStateMessage: \"API Key\
      \ is older than 90 days.\"\n    remediationMessage: \"It's recommended to rotate\
      \ API Key every 90 days.\"\n    check:\n      IS_BEYOND_LAST_DAYS:\n       \
      \ offsetDays: 90\n        arg:\n          EXTRACT: CA10G1__createTime__c   \
      \  \notherwise:\n  status: \"COMPLIANT\"\n  currentStateMessage: \"API Key is\
      \ regularly rotated.\"\n\n"
  - path: /ce/ca/google/api/api-key-rotation/test-data.json
    md5Hash: 9E4913BB77BE2875E005B77EDE43C6F8
    content: |
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10G1__createTime__c').beyondLastDays(90)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a0n11",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__createTime__c": "2020-02-13T04:56:18Z"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-05T17:52:17Z" },
          "Id": "a0n22",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__createTime__c": "2024-07-17T12:36:12Z"
        }
      ]
  - path: /types/CA10G1__CaGoogleApiKey__c/object.extracts.yaml
    md5Hash: 406AEFB1FA1BD7FD242EC239A00FF313
    content: "---\nextracts:\n  - name: \"CA10G1__restrictionsAllowedApplications__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10G1__restrictionsAllowedApplications__c\"\
      \n  - name: \"CA10G1__restrictionsAllowedBundleIds__c\"\n    value: \n     FIELD:\n\
      \       path: \"CA10G1__restrictionsAllowedBundleIds__c\"\n  - name: \"CA10G1__restrictionsAllowedIps__c\"\
      \n    value: \n     FIELD:\n       path: \"CA10G1__restrictionsAllowedIps__c\"\
      \n  - name: \"CA10G1__restrictionsAllowedReferrers__c\"\n    value: \n     FIELD:\n\
      \       path: \"CA10G1__restrictionsAllowedReferrers__c\"\n  - name: \"CA10G1__restrictionsApiTargets__c\"\
      \n    value: \n     FIELD:\n       path: \"CA10G1__restrictionsApiTargets__c\"\
      \n  - name: \"CA10G1__createTime__c\"\n    value: \n     FIELD:\n       path:\
      \ \"CA10G1__createTime__c\"\n       undeterminedIf:\n        isEmpty: \"Create\
      \ time should not be empty\"\n        \n\n\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" : "a0n11",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10G1__createTime__c').beyondLastDays(90)",
      "runtimeError" : null
    }
  }, {
    "Id" : "a0n22",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10G1__CaGoogleApiKey__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10G1__disappearanceTime__c TIMESTAMP,
      CA10G1__createTime__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a0n11",
    "CA10G1__createTime__c" : new Date("2020-02-13T04:56:18Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-05T17:52:17Z")
    },
    "Id" : "a0n22",
    "CA10G1__createTime__c" : new Date("2024-07-17T12:36:12Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10G1__CaGoogleApiKey__c(
      obj STRUCT<
          Id STRING,
          CA10G1__disappearanceTime__c TIMESTAMP,
          CA10G1__createTime__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 IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      var todayMinus90Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-90 * 86400000);
      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]
      function fieldChecked4() {
          if (IsEmptyLib.simpleIsEmpty(obj.CA10G1__createTime__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10G1__createTime__c.isEmpty()", currentStateMessage: "Create time should not be empty", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10G1__createTime__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Create Time [obj.CA10G1__createTime__c]: ' + obj.CA10G1__createTime__c);
      try {
          if (extract3.call(extract3) != null && extract3.call(extract3).getTime() < todayMinus90Day) {
              return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10G1__createTime__c').beyondLastDays(90)", currentStateMessage: "API Key is older than 90 days.", currentStateReferences: references1.join('\n'), remediation: "It's recommended to rotate API Key every 90 days.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "API Key is regularly rotated.", 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.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
          sObject.CA10G1__createTime__c AS CA10G1__createTime__c,
          process_CA10G1__CaGoogleApiKey__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
                  sObject.CA10G1__createTime__c AS CA10G1__createTime__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10G1__CaGoogleApiKey__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
