---
policy: /ce/ca/google/iam/service-account-key-rotation
logic: /ce/ca/google/iam/service-account-key-rotation/prod.logic.yaml
executionTime: 2025-04-24T23:47:02.897675359Z
rows:
  - id: aA1
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: aA2
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__validAfter__c').beyondLastDays(90)
      actual: extract('CA10__validAfter__c').beyondLastDays(90)
    runtimeError: {}
  - id: aA3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/iam/service-account-key-rotation/policy.yaml
    md5Hash: 96D671B53702B9437225A4BF207FE3FC
    content: "---\nnames: \n  full: \"Google IAM Service Account User-Managed Key\
      \ is not rotated every 90 days\"\n  contextual: \"Service Account User-Managed\
      \ Key is not rotated every 90 days\"\ndescription: \"Service Account keys consist\
      \ of a key ID (Private_key_Id) and Private key, which are \\\n  used to sign\
      \ programmatic requests users make to Google Cloud services accessible to \\\
      \n  that particular service account. It is recommended that all Service Account\
      \ keys are regularly rotated.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  - \"\
      SECURITY\"\n  - \"RELIABILITY\"\nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/01/07\n\
      \  - /frameworks/cloudaware/secret-and-certificate-governance/expiration-management\n\
      similarPolicies:\n  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity/knowledge-base/gcp/CloudIAM/rotate-service-account-user-managed-keys.html\"\
      \n      name: \"Rotate User-Managed Service Account Keys\"\n \n"
  - path: /ce/ca/google/iam/service-account-key-rotation/prod.logic.yaml
    md5Hash: 843E06AB8218228A631A2F2719D80626
    content: |
      ---
      inputType: "CA10__CaGoogleIamServiceAccountKey__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaGoogleIamServiceAccountKey__c/object.extracts.yaml
      conditions:
        - status: "INCOMPLIANT"
          currentStateMessage: "Service Account key is older than 90 days."
          remediationMessage: "It's recommended to rotate Service Account key."
          check:
            IS_BEYOND_LAST_DAYS:
              offsetDays: 90
              arg:
                EXTRACT: CA10__validAfter__c
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "Service Account key is regularly rotated."
  - path: /ce/ca/google/iam/service-account-key-rotation/test-data.json
    md5Hash: D2DD12B93957E57AE7B94231F0BC103F
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-07T17:46:07Z" },
          "Id": "aA1",
          "CA10__disappearanceTime__c": "2024-07-29T08:38:51Z",
          "CA10__validAfter__c": "2024-07-12T16:21:58Z"
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__validAfter__c').beyondLastDays(90)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-08T17:46:07Z" },
          "Id": "aA2",
          "CA10__disappearanceTime__c": null,
          "CA10__validAfter__c": "2023-12-03T18:07:03Z"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-08-08T17:46:07Z" },
          "Id": "aA3",
          "CA10__disappearanceTime__c": null,
          "CA10__validAfter__c": "2024-07-07T06:51:51Z"
        }
      ]
  - path: /types/CA10__CaGoogleIamServiceAccountKey__c/object.extracts.yaml
    md5Hash: 77CA840C297D7AA897F12C624C90A9A4
    content: "---\nextracts:\n  - name: \"CA10__validAfter__c\"\n    value: \n   \
      \    FIELD:\n         path: \"CA10__validAfter__c\"\n         undeterminedIf:\n\
      \          isEmpty: \"Corrupted data. Field `Valid after` cannot be empty.\""
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" : "aA1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "aA2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__validAfter__c').beyondLastDays(90)",
      "runtimeError" : null
    }
  }, {
    "Id" : "aA3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleIamServiceAccountKey__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__validAfter__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-08-07T17:46:07Z")
    },
    "Id" : "aA1",
    "CA10__disappearanceTime__c" : new Date("2024-07-29T08:38:51Z"),
    "CA10__validAfter__c" : new Date("2024-07-12T16:21:58Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-08-08T17:46:07Z")
    },
    "Id" : "aA2",
    "CA10__validAfter__c" : new Date("2023-12-03T18:07:03Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-08-08T17:46:07Z")
    },
    "Id" : "aA3",
    "CA10__validAfter__c" : new Date("2024-07-07T06:51:51Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleIamServiceAccountKey__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__validAfter__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 [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 (IsEmptyLib.simpleIsEmpty(obj.CA10__validAfter__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__validAfter__c.isEmpty()", currentStateMessage: "Corrupted data. Field `Valid after` cannot be empty.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__validAfter__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Valid After [obj.CA10__validAfter__c]: ' + obj.CA10__validAfter__c);
      try {
          if (extract3.call(extract3) != null && extract3.call(extract3).getTime() < todayMinus90Day) {
              return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__validAfter__c').beyondLastDays(90)", currentStateMessage: "Service Account key is older than 90 days.", currentStateReferences: references1.join('\n'), remediation: "It's recommended to rotate Service Account key.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "Service Account 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.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__validAfter__c AS CA10__validAfter__c,
          process_CA10__CaGoogleIamServiceAccountKey__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__validAfter__c AS CA10__validAfter__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleIamServiceAccountKey__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
