---
policy: /ce/ca/google/iam/service-account-keys
logic: /ce/ca/google/iam/service-account-keys/prod.logic.yaml
executionTime: 2025-05-10T12:05:23.45397324Z
rows:
  - id: aAL1
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: not(extract('CA10__serviceAccountEmail__c').endsWith('iam.gserviceaccount.com'))
      actual: not(extract('CA10__serviceAccountEmail__c').endsWith('iam.gserviceaccount.com'))
    runtimeError: {}
  - id: aAL2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: CA10__Google_IAM_Service_Account_Keys__r.has(INCOMPLIANT)
      actual: CA10__Google_IAM_Service_Account_Keys__r.has(INCOMPLIANT)
    runtimeError: {}
  - id: aAL3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/iam/service-account-keys/policy.yaml
    md5Hash: D98FC422BC24227364397D6A3D35CAEB
    content: "---\nnames: \n  full: \"Google IAM Service Account has User-Managed\
      \ Keys\"\n  contextual: \"Service Account has User-Managed Keys\"\ndescription:\
      \ \"User-managed service accounts should not have user-managed keys.\"\ntype:\
      \ COMPLIANCE_POLICY\ncategories:\n  - \"SECURITY\"\nframeworkMappings:\n  -\
      \ /frameworks/cis-gcp-v3.0.0/01/04\n  - /frameworks/cloudaware/identity-and-access-governance/credential-lifecycle-management\n\
      similarPolicies:\n  cloudConformity:\n    - url: https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/CloudIAM/delete-user-managed-service-account-keys.html\n\
      \      name: \"Delete User-Managed Service Account Keys\"\n"
  - path: /ce/ca/google/iam/service-account-keys/prod.logic.yaml
    md5Hash: 9986C9797E80B1D3D113492B07743421
    content: "---\ninputType: \"CA10__CaGoogleIamServiceAccount__c\"\ntestData:\n\
      \  - file: test-data.json\nimportExtracts:\n  - file: /types/CA10__CaGoogleIamServiceAccount__c/object.extracts.yaml\n\
      conditions:\n  - status: \"INAPPLICABLE\"\n    currentStateMessage: \"Only for\
      \ user-managed service accounts.\"\n    check:\n      NOT:\n        arg:\n \
      \         ENDS_WITH:\n           arg:\n            EXTRACT: CA10__serviceAccountEmail__c\n\
      \           suffix: \n            TEXT: \"iam.gserviceaccount.com\"\n  - status:\
      \ \"INCOMPLIANT\"\n    currentStateMessage: \"User-managed service accounts\
      \ should not have user-managed keys.\"\n    remediationMessage: \"You should\
      \ delete a user managed Service Account Key.\"\n    check:\n      RELATED_LIST_HAS:\n\
      \        status: \"INCOMPLIANT\"\n        relationshipName: \"CA10__Google_IAM_Service_Account_Keys__r\"\
      \notherwise:\n  status: \"COMPLIANT\"\n  currentStateMessage: \"User-managed\
      \ service accounts haven't user-managed keys.\"\nrelatedLists:\n  - relationshipName:\
      \ \"CA10__Google_IAM_Service_Account_Keys__r\"\n    conditions: []\n    otherwise:\n\
      \      status: \"INCOMPLIANT\" \n      currentStateMessage: \"Service Account\
      \ Key is enabled.\""
  - path: /ce/ca/google/iam/service-account-keys/test-data.json
    md5Hash: F2D71725B64986F97F9E2DEAD7468B02
    content: |
      [
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "not(extract('CA10__serviceAccountEmail__c').endsWith('iam.gserviceaccount.com'))",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-20T17:39:01Z" },
          "Id": "aAL1",
          "CA10__disappearanceTime__c": null,
          "CA10__serviceAccountEmail__c": "compute@developer.gserviceaccount.com",
          "CA10__Google_IAM_Service_Account_Keys__r": [
            {
              "Id": "aAK1",
              "CA10__disappearanceTime__c": null,
              "CA10__iamServiceAccount__c": "aAL1"
            }
          ]
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "CA10__Google_IAM_Service_Account_Keys__r.has(INCOMPLIANT)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-20T17:39:01Z" },
          "Id": "aAL2",
          "CA10__disappearanceTime__c": null,
          "CA10__serviceAccountEmail__c": "upload@kap.iam.gserviceaccount.com",
          "CA10__Google_IAM_Service_Account_Keys__r": [
            {
              "Id": "aAK2",
              "CA10__disappearanceTime__c": null,
              "CA10__iamServiceAccount__c": "aAL2"
            }
          ]
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-20T17:39:01Z" },
          "Id": "aAL3",
          "CA10__disappearanceTime__c": null,
          "CA10__serviceAccountEmail__c": "export@cad.iam.gserviceaccount.com",
          "CA10__Google_IAM_Service_Account_Keys__r": []
        }
      ]
  - path: /types/CA10__CaGoogleIamServiceAccount__c/object.extracts.yaml
    md5Hash: EB5FB013DE89FCA54D6B42804FC725D6
    content: "---\nextracts:\n  - name: \"CA10__serviceAccountEmail__c\"\n    value:\
      \ \n       FIELD:\n         path: \"CA10__serviceAccountEmail__c\"\n       \
      \  undeterminedIf:\n          isEmpty: \"Corrupted data. Service Account Email\
      \ cannot be empty.\"\n  - name: \"Name\"\n    value: \n      FIELD:\n      \
      \  path: \"Name\"\n        undeterminedIf:\n         isEmpty: \"Corrupted data.\
      \ Service Account Name cannot be empty.\"\n  - name: \"serviceAccountProjectId\"\
      \n    value: \n      FIELD:\n        path: \"CA10__googleProject__r.CA10__projectId__c\"\
      \n        undeterminedIf:\n          isEmpty: \"Master detail Id cannot be empty.\"\
      \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" : "aAL1",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "not(extract('CA10__serviceAccountEmail__c').endsWith('iam.gserviceaccount.com'))",
      "runtimeError" : null
    }
  }, {
    "Id" : "aAL2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "CA10__Google_IAM_Service_Account_Keys__r.has(INCOMPLIANT)",
      "runtimeError" : null
    }
  }, {
    "Id" : "aAL3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleIamServiceAccount__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__serviceAccountEmail__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-08-20T17:39:01Z")
    },
    "Id" : "aAL1",
    "CA10__serviceAccountEmail__c" : "compute@developer.gserviceaccount.com"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-08-20T17:39:01Z")
    },
    "Id" : "aAL2",
    "CA10__serviceAccountEmail__c" : "upload@kap.iam.gserviceaccount.com"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-08-20T17:39:01Z")
    },
    "Id" : "aAL3",
    "CA10__serviceAccountEmail__c" : "export@cad.iam.gserviceaccount.com"
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleIamServiceAccountKey__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__iamServiceAccount__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-08-20T17:39:01Z")
    },
    "Id" : "aAK1",
    "CA10__iamServiceAccount__c" : "aAL1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-08-20T17:39:01Z")
    },
    "Id" : "aAK2",
    "CA10__iamServiceAccount__c" : "aAL2"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleIamServiceAccount__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__serviceAccountEmail__c STRING,
          CA10__Google_IAM_Service_Account_Keys__r ARRAY<STRUCT<
              Id STRING,
              CA10__disappearanceTime__c TIMESTAMP,
              CA10__iamServiceAccount__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 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]
      function fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__serviceAccountEmail__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__serviceAccountEmail__c.isEmpty()", currentStateMessage: "Corrupted data. Service Account Email cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__serviceAccountEmail__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Email [obj.CA10__serviceAccountEmail__c]: ' + obj.CA10__serviceAccountEmail__c);
      try {
          if (!TextLib.endsWith(extract3.call(extract3), 'iam.gserviceaccount.com')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "not(extract('CA10__serviceAccountEmail__c').endsWith('iam.gserviceaccount.com'))", currentStateMessage: "Only for user-managed service accounts.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      var count_CA10__Google_IAM_Service_Account_Keys__r_INCOMPLIANT5 = 0;
      if (obj.CA10__Google_IAM_Service_Account_Keys__r != null) {
          for (var i6 = 0; i6 < obj.CA10__Google_IAM_Service_Account_Keys__r.length; i6++) {
              if (obj.CA10__Google_IAM_Service_Account_Keys__r[i6].result.status == 'INCOMPLIANT') {
                  count_CA10__Google_IAM_Service_Account_Keys__r_INCOMPLIANT5 += 1;
              }
          }
      }
      // condition[2], conditionIndex:[200..299]
      references1.push('Related list [CA10__Google_IAM_Service_Account_Keys__r] ' + (count_CA10__Google_IAM_Service_Account_Keys__r_INCOMPLIANT5 > 0 ? 'has' : 'does not have') + ' objects in INCOMPLIANT status');
      if (count_CA10__Google_IAM_Service_Account_Keys__r_INCOMPLIANT5 > 0) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "CA10__Google_IAM_Service_Account_Keys__r.has(INCOMPLIANT)", currentStateMessage: "User-managed service accounts should not have user-managed keys.", currentStateReferences: references1.join('\n'), remediation: "You should delete a user managed Service Account Key.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "User-managed service accounts haven't user-managed keys.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;

  CREATE TEMP FUNCTION process_CA10__Google_IAM_Service_Account_Keys__r(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__iamServiceAccount__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 [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};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 100, conditionText: "otherwise", currentStateMessage: "Service Account Key is enabled.", 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.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__serviceAccountEmail__c AS CA10__serviceAccountEmail__c,
          `CA10__Google_IAM_Service_Account_Keys__r`.arr AS CA10__Google_IAM_Service_Account_Keys__r,
          process_CA10__CaGoogleIamServiceAccount__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__serviceAccountEmail__c AS CA10__serviceAccountEmail__c,
                  `CA10__Google_IAM_Service_Account_Keys__r`.arr AS CA10__Google_IAM_Service_Account_Keys__r
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleIamServiceAccount__c()) AS sObject
      LEFT JOIN (
          SELECT
              sObject.CA10__iamServiceAccount__c,
              ARRAY_AGG(
                  STRUCT(
                      sObject.Id AS Id,
                      sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                      sObject.CA10__iamServiceAccount__c AS CA10__iamServiceAccount__c,
                      process_CA10__Google_IAM_Service_Account_Keys__r(
                          STRUCT(
                              sObject.Id AS Id,
                              sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                              sObject.CA10__iamServiceAccount__c AS CA10__iamServiceAccount__c
                          ),
                          sObject.context.snapshotTime
                      ) as result
                  )
              ) AS arr
          FROM UNNEST(mock_CA10__CaGoogleIamServiceAccountKey__c()) AS sObject
          GROUP BY sObject.CA10__iamServiceAccount__c
      ) AS `CA10__Google_IAM_Service_Account_Keys__r`
          ON sObject.Id = `CA10__Google_IAM_Service_Account_Keys__r`.CA10__iamServiceAccount__c
  ) sObject ON sObject.Id = expectedResult.Id;
