---
policy: /ce/ca/google/project/api-keys-for-active-services
logic: /ce/ca/google/project/api-keys-for-active-services/prod.logic.yaml
executionTime: 2025-04-24T23:47:17.790536261Z
rows:
  - id: a4p1
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10G1__Google_API_Keys__r.has(INCOMPLIANT)
      actual: CA10G1__Google_API_Keys__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: a4p2
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/project/api-keys-for-active-services/policy.yaml
    md5Hash: 48AFC83D0C49EC30BF5054F4F689CCA9
    content: "---\nnames: \n  full: \"Google Project has API Keys\"\n  contextual:\
      \ \"Google Project has API Keys\"\ndescription: \"API Keys should only be used\
      \ for services in cases where other \\\n authentication methods are unavailable.\
      \ Unused keys with their permissions \\\n in tact may still exist within a project.\
      \ Keys are insecure because they can \\ \n be viewed publicly, such as from\
      \ within a browser, or they can be accessed on \\\n a device where the key resides.\
      \ It is recommended to use standard authentication flow instead.\"\ntype: COMPLIANCE_POLICY\n\
      categories:\n  - \"SECURITY\"\nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/01/12\n\
      \  - /frameworks/cloudaware/secret-and-certificate-governance/cryptographic-configuration\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/CloudAPI/api-key-only-active-services.html\"\
      \n      name: \"API Keys Should Only Exist for Active Services\"\n "
  - path: /ce/ca/google/project/api-keys-for-active-services/prod.logic.yaml
    md5Hash: 9D140D0E8A7EFDED2D365884DF31F7CE
    content: |-
      ---
      inputType: "CA10__CaGoogleProject__c"
      testData:
        - file: test-data.json
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "API Keys exist for a project."
          remediationMessage: "To avoid the security risk in using API keys, it is recommended to use standard authentication flow instead."
          check:
            RELATED_LIST_HAS:
              status: "INCOMPLIANT"
              relationshipName: "CA10G1__Google_API_Keys__r"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "API keys are not created for a project."
      relatedLists:
        - relationshipName: "CA10G1__Google_API_Keys__r"
          conditions: []
          otherwise:
            status: "INCOMPLIANT"
            currentStateMessage: "API Key exists."
  - path: /ce/ca/google/project/api-keys-for-active-services/test-data.json
    md5Hash: 4F7540DB00DF7B7F42EBEA44EC4B9929
    content: |
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "CA10G1__Google_API_Keys__r.has(INCOMPLIANT)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-07T17:46:07Z" },
          "Id": "a4p1",
          "CA10G1__Google_API_Keys__r": [
            {
              "Id": "a01",
              "CA10G1__disappearanceTime__c": null,
              "CA10G1__project__c": "a4p1"
            }
          ]
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-07T17:46:07Z" },
          "Id": "a4p2",
          "CA10G1__Google_API_Keys__r": []
        }
      ]
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" : "a4p1",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "CA10G1__Google_API_Keys__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "a4p2",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleProject__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-08-07T17:46:07Z")
    },
    "Id" : "a4p1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-08-07T17:46:07Z")
    },
    "Id" : "a4p2"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10G1__CaGoogleApiKey__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10G1__disappearanceTime__c TIMESTAMP,
      CA10G1__project__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-08-07T17:46:07Z")
    },
    "Id" : "a01",
    "CA10G1__project__c" : "a4p1"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleProject__c(
      obj STRUCT<
          Id STRING,
          CA10G1__Google_API_Keys__r ARRAY<STRUCT<
              Id STRING,
              CA10G1__disappearanceTime__c TIMESTAMP,
              CA10G1__project__c STRING,
              result STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError 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 references1 = [];
      // condition[0], conditionIndex:[0..99]
      if (false) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared()", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      var count_CA10G1__Google_API_Keys__r_INCOMPLIANT2 = 0;
      if (obj.CA10G1__Google_API_Keys__r != null) {
          for (var i3 = 0; i3 < obj.CA10G1__Google_API_Keys__r.length; i3++) {
              if (obj.CA10G1__Google_API_Keys__r[i3].result.status == 'INCOMPLIANT') {
                  count_CA10G1__Google_API_Keys__r_INCOMPLIANT2 += 1;
              }
          }
      }
      // condition[1], conditionIndex:[100..199]
      references1.push('Related list [CA10G1__Google_API_Keys__r] ' + (count_CA10G1__Google_API_Keys__r_INCOMPLIANT2 > 0 ? 'has' : 'does not have') + ' objects in INCOMPLIANT status');
      if (count_CA10G1__Google_API_Keys__r_INCOMPLIANT2 > 0) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "CA10G1__Google_API_Keys__r.has(INCOMPLIANT)", currentStateMessage: "API Keys exist for a project.", currentStateReferences: references1.join('\n'), remediation: "To avoid the security risk in using API keys, it is recommended to use standard authentication flow instead.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "API keys are not created for a project.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10G1__Google_API_Keys__r(
      obj STRUCT<
          Id STRING,
          CA10G1__disappearanceTime__c TIMESTAMP,
          CA10G1__project__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 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};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 100, conditionText: "otherwise", currentStateMessage: "API Key exists.", 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,
          `CA10G1__Google_API_Keys__r`.arr AS CA10G1__Google_API_Keys__r,
          process_CA10__CaGoogleProject__c(
              STRUCT(
                  sObject.Id AS Id,
                  `CA10G1__Google_API_Keys__r`.arr AS CA10G1__Google_API_Keys__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleProject__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10G1__project__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
                      sObject.CA10G1__project__c AS CA10G1__project__c,
                      process_CA10G1__Google_API_Keys__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
                              sObject.CA10G1__project__c AS CA10G1__project__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10G1__CaGoogleApiKey__c()) AS sObject
          GROUP BY sObject.CA10G1__project__c
      ) AS `CA10G1__Google_API_Keys__r`
          ON sObject.Id = `CA10G1__Google_API_Keys__r`.CA10G1__project__c
  ) sObject ON sObject.Id = expectedResult.Id;
