---
policy: /ce/ca/google/api/api-key-for-application
logic: /ce/ca/google/api/api-key-for-application/prod.logic.yaml
executionTime: 2025-04-24T23:46:43.735542393Z
rows:
  - id: a0n11
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10G1__restrictionsApiTargets__c').isEmpty()
      actual: extract('CA10G1__restrictionsApiTargets__c').isEmpty()
    runtimeError: {}
  - id: a0n22
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: "extract('CA10G1__restrictionsApiTargets__c').contains('service: \\\
        'cloudapis.googleapis.com\\'')"
      actual: "extract('CA10G1__restrictionsApiTargets__c').contains('service: \\\
        'cloudapis.googleapis.com\\'')"
    runtimeError: {}
  - id: a0n33
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/api/api-key-for-application/policy.yaml
    md5Hash: 134CEF9E73DD5B1270BC892064968F28
    content: "---\nnames: \n  full: \"Google API Key is not restricted for unused\
      \ APIs\"\n  contextual: \"API Key is not restricted for unused APIs\"\ndescription:\
      \ \"API Keys should only be used for services in cases where other authentication\
      \ methods \\\n  are unavailable. API keys are always at risk because they can\
      \ be viewed publicly, such as from \\\n  within a browser, or they can be accessed\
      \ on a device where the key resides. It is recommended \\ \n  to restrict API\
      \ keys to use (call) only APIs required by an application.\"\ntype: COMPLIANCE_POLICY\n\
      categories:\n  - \"SECURITY\"\nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/01/14\n\
      \  - /frameworks/cloudaware/secret-and-certificate-governance/cryptographic-configuration\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity/knowledge-base/gcp/CloudAPI/check-for-api-key-application-restrictions.html\"\
      \n      name: \"Check for API Key Application Restrictions\"\n\n "
  - path: /ce/ca/google/api/api-key-for-application/prod.logic.yaml
    md5Hash: 3DE7685CC67AA08E568334A5315B254F
    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: \"Key Restrictions\
      \ section is empty.\"\n    remediationMessage: \"Set API restrictions for the\
      \ API Key.\"\n    check:\n      IS_EMPTY:\n        arg:\n          EXTRACT:\
      \ CA10G1__restrictionsApiTargets__c \n  - status: \"INCOMPLIANT\"\n    currentStateMessage:\
      \ \"API Restrictions are set to Google Cloud APIs, this option allows access\
      \ to all services offered by Google Cloud.\"\n    remediationMessage: \"Set\
      \ API Restrictions for the API Key.\"\n    check:\n      CONTAINS:\n       \
      \ arg:\n          EXTRACT: CA10G1__restrictionsApiTargets__c\n        substring:\n\
      \          TEXT: \"service: 'cloudapis.googleapis.com'\"          \notherwise:\n\
      \  status: \"COMPLIANT\"\n  currentStateMessage: \"API Key should only be used\
      \ for services where other authentication methods are unavailable.\""
  - 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"
  - path: /ce/ca/google/api/api-key-for-application/test-data.json
    md5Hash: 278C312A34C5F26A42DB149761BC8F6B
    content: |
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10G1__restrictionsApiTargets__c').isEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-10T18:10:52Z" },
          "Id": "a0n11",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__restrictionsApiTargets__c": ""
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10G1__restrictionsApiTargets__c').contains('service: \\'cloudapis.googleapis.com\\'')",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-10T18:10:52Z" },
          "Id": "a0n22",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__restrictionsApiTargets__c": "service: 'cloudapis.googleapis.com'"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-09-10T18:10:52Z" },
          "Id": "a0n33",
          "CA10G1__disappearanceTime__c": null,
          "CA10G1__restrictionsApiTargets__c": "service: 'cloudbuild.googleapis.com', methods: ''"
        }
      ]
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__restrictionsApiTargets__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "a0n22",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10G1__restrictionsApiTargets__c').contains('service: \\'cloudapis.googleapis.com\\'')",
      "runtimeError" : null
    }
  }, {
    "Id" : "a0n33",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10G1__CaGoogleApiKey__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10G1__disappearanceTime__c TIMESTAMP,
      CA10G1__restrictionsApiTargets__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-09-10T18:10:52Z")
    },
    "Id" : "a0n11",
    "CA10G1__restrictionsApiTargets__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-10T18:10:52Z")
    },
    "Id" : "a0n22",
    "CA10G1__restrictionsApiTargets__c" : "service: 'cloudapis.googleapis.com'"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-09-10T18:10:52Z")
    },
    "Id" : "a0n33",
    "CA10G1__restrictionsApiTargets__c" : "service: 'cloudbuild.googleapis.com', methods: ''"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10G1__CaGoogleApiKey__c(
      obj STRUCT<
          Id STRING,
          CA10G1__disappearanceTime__c TIMESTAMP,
          CA10G1__restrictionsApiTargets__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]
      function extract3() { if (!this.out) { this.out = obj.CA10G1__restrictionsApiTargets__c; } return this.out; };
      references1.push('Restrictions: API Targets [obj.CA10G1__restrictionsApiTargets__c]: ' + obj.CA10G1__restrictionsApiTargets__c);
      if (TextLib.isEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10G1__restrictionsApiTargets__c').isEmpty()", currentStateMessage: "Key Restrictions section is empty.", currentStateReferences: references1.join('\n'), remediation: "Set API restrictions for the API Key.", runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10G1__restrictionsApiTargets__c; } return this.out; };
      if (TextLib.contains(extract6.call(extract6), 'service: \'cloudapis.googleapis.com\'')) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10G1__restrictionsApiTargets__c').contains('service: \\'cloudapis.googleapis.com\\'')", currentStateMessage: "API Restrictions are set to Google Cloud APIs, this option allows access to all services offered by Google Cloud.", currentStateReferences: references1.join('\n'), remediation: "Set API Restrictions for the API Key.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "API Key should only be used for services where other authentication methods are unavailable.", 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__restrictionsApiTargets__c AS CA10G1__restrictionsApiTargets__c,
          process_CA10G1__CaGoogleApiKey__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10G1__disappearanceTime__c AS CA10G1__disappearanceTime__c,
                  sObject.CA10G1__restrictionsApiTargets__c AS CA10G1__restrictionsApiTargets__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10G1__CaGoogleApiKey__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
