---
policy: /ce/ca/aws/iam/access-key-unused
logic: /ce/ca/aws/iam/access-key-unused/prod.logic.yaml
executionTime: 2025-12-27T12:02:01.272434745Z
generationMs: 62
executionMs: 749
rows:
  - id: test1
    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: test2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: CA10__status__c != 'Active'
      actual: CA10__status__c != 'Active'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: CA10__lastUsedDate__c.isNotEmpty() && CA10__lastUsedDate__c.beyondLastDays(90)
      actual: CA10__lastUsedDate__c.isNotEmpty() && CA10__lastUsedDate__c.beyondLastDays(90)
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: CA10__lastUsedDate__c.isEmpty() && CA10__createDate__c.beyondLastDays(90)
      actual: CA10__lastUsedDate__c.isEmpty() && CA10__createDate__c.beyondLastDays(90)
    runtimeError: {}
  - id: test5
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 400
      actual: 400
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/iam/access-key-unused/policy.yaml
    md5Hash: F8AA6A1593E4DF95675C29EB1C02DF41
    content: "---\nnames:\n  full: AWS IAM Access Key is unused\n  contextual: IAM\
      \ Access Key is unused\ndescription: >\n  Identify and remove active IAM access\
      \ keys that have not been used for 90 days \n  or more. Reducing the number\
      \ of active, unused credentials minimizes the risk \n  of unauthorized access\
      \ if a key is compromised.\ntype: \"BEST_PRACTICE\"\ncategories:\n  - \"SECURITY\"\
      \nframeworkMappings:\n  - \"/frameworks/cloudaware/identity-and-access-governance/credential-lifecycle-management\"\
      \n  - \"/frameworks/aws-fsbp-v1.0.0/iam/08\"\nsimilarPolicies:\n  awsSecurityHub:\n\
      \    - name: \"[IAM.8] Unused IAM user credentials should be removed\"\n   \
      \   url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/iam-controls.html#iam-8\"\
      \n  cloudConformity:\n    - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/aws/IAM/unnecessary-access-keys.html\n\
      \      name: Unnecessary Access Keys\n"
  - path: /ce/ca/aws/iam/access-key-unused/prod.logic.yaml
    md5Hash: 6E83BB8DAC44B3D0D9F8996862744493
    content: |-
      ---
      inputType: CA10__CaAwsAccessKey__c
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10__CaAwsAccessKey__c/object.extracts.yaml"
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The access key is currently Inactive."
          check:
            NOT_EQUAL:
              left:
                FIELD:
                  path: "CA10__status__c"
              right:
                TEXT: "Active"
        - status: "INCOMPLIANT"
          currentStateMessage: "The access key is active but has not been used in over 90 days."
          remediationMessage: "Deactivate the unused access key."
          check:
            AND:
              args:
                - NOT_EMPTY:
                    arg:
                      FIELD:
                        path: "CA10__lastUsedDate__c"
                - IS_BEYOND_LAST_DAYS:
                    offsetDays: 90
                    arg:
                      FIELD:
                        path: "CA10__lastUsedDate__c"
        - status: "INCOMPLIANT"
          currentStateMessage: "The access key is active, has never been used, and was created over 90 days ago."
          remediationMessage: "Deactivate the unused access key."
          check:
            AND:
              args:
                - IS_EMPTY:
                    arg:
                      FIELD:
                        path: "CA10__lastUsedDate__c"
                - IS_BEYOND_LAST_DAYS:
                    offsetDays: 90
                    arg:
                      FIELD:
                        path: "CA10__createDate__c"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The access key is active and has been used within the last 90 days (or is recently created)."
  - path: /ce/ca/aws/iam/access-key-unused/test-data.json
    md5Hash: E0E76674D24F2914CAF556C2DDF9515D
    content: |-
      [
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "conditionIndex": 99,
                  "status": "DISAPPEARED"
              },
              "context": {
                  "snapshotTime": "2025-12-19T02:21:26Z"
              },
              "CA10__createDate__c": "2025-11-05T14:18:01Z",
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-12-12T21:58:51Z",
              "CA10__lastUsedDate__c": "2025-11-05T14:20:00Z",
              "CA10__status__c": "Active"
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "CA10__status__c != 'Active'",
                  "conditionIndex": 199,
                  "status": "INAPPLICABLE"
              },
              "context": {
                  "snapshotTime": "2025-12-19T02:21:26Z"
              },
              "CA10__createDate__c": "2023-08-04T18:46:57Z",
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__lastUsedDate__c": null,
              "CA10__status__c": "Inactive"
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "CA10__lastUsedDate__c.isNotEmpty() && CA10__lastUsedDate__c.beyondLastDays(90)",
                  "conditionIndex": 299,
                  "status": "INCOMPLIANT"
              },
              "context": {
                  "snapshotTime": "2025-12-19T02:21:26Z"
              },
              "CA10__createDate__c": "2025-08-06T17:01:57Z",
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__lastUsedDate__c": "2025-08-26T20:22:00Z",
              "CA10__status__c": "Active"
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "CA10__lastUsedDate__c.isEmpty() && CA10__createDate__c.beyondLastDays(90)",
                  "conditionIndex": 399,
                  "status": "INCOMPLIANT"
              },
              "context": {
                  "snapshotTime": "2025-12-19T02:21:26Z"
              },
              "CA10__createDate__c": "2025-05-07T20:33:32Z",
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__lastUsedDate__c": null,
              "CA10__status__c": "Active"
          },
          {
              "expectedResult": {
                  "runtimeError": null,
                  "conditionText": "otherwise",
                  "conditionIndex": 400,
                  "status": "COMPLIANT"
              },
              "context": {
                  "snapshotTime": "2025-12-19T02:21:26Z"
              },
              "CA10__createDate__c": "2025-07-25T20:46:37Z",
              "Id": "test5",
              "CA10__disappearanceTime__c": null,
              "CA10__lastUsedDate__c": "2025-12-18T21:39:00Z",
              "CA10__status__c": "Active"
          }
      ]
  - path: /types/CA10__CaAwsAccessKey__c/object.extracts.yaml
    md5Hash: 40ECF1BF559EC81799BEDCBB6DEAAC0B
    content: "---\nextracts:\n# Date/Time. Nullable.\n  - name: CA10__lastUsedDate__c\n\
      \    value: \n      FIELD:\n        path: CA10__lastUsedDate__c\n# Values: Active\
      \ | Inactive. Not nullable.\n  - name: CA10__status__c\n    value: \n      FIELD:\n\
      \        path: CA10__status__c\n# Date/Time. Not nullable.\n  - name: CA10__createDate__c\n\
      \    value: \n      FIELD:\n        path: CA10__createDate__c\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" : "test1",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "conditionIndex" : 99,
      "status" : "DISAPPEARED"
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "CA10__status__c != 'Active'",
      "conditionIndex" : 199,
      "status" : "INAPPLICABLE"
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "CA10__lastUsedDate__c.isNotEmpty() && CA10__lastUsedDate__c.beyondLastDays(90)",
      "conditionIndex" : 299,
      "status" : "INCOMPLIANT"
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "CA10__lastUsedDate__c.isEmpty() && CA10__createDate__c.beyondLastDays(90)",
      "conditionIndex" : 399,
      "status" : "INCOMPLIANT"
    }
  }, {
    "Id" : "test5",
    "expectedResult" : {
      "runtimeError" : null,
      "conditionText" : "otherwise",
      "conditionIndex" : 400,
      "status" : "COMPLIANT"
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsAccessKey__c()
  RETURNS ARRAY<STRUCT<
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__status__c STRING,
      CA10__lastUsedDate__c TIMESTAMP,
      CA10__createDate__c TIMESTAMP,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-12-19T02:21:26Z")
    },
    "CA10__disappearanceTime__c" : new Date("2025-12-12T21:58:51Z"),
    "CA10__status__c" : "Active",
    "CA10__lastUsedDate__c" : new Date("2025-11-05T14:20:00Z"),
    "CA10__createDate__c" : new Date("2025-11-05T14:18:01Z"),
    "Id" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-19T02:21:26Z")
    },
    "CA10__status__c" : "Inactive",
    "CA10__createDate__c" : new Date("2023-08-04T18:46:57Z"),
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-19T02:21:26Z")
    },
    "CA10__status__c" : "Active",
    "CA10__lastUsedDate__c" : new Date("2025-08-26T20:22:00Z"),
    "CA10__createDate__c" : new Date("2025-08-06T17:01:57Z"),
    "Id" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-19T02:21:26Z")
    },
    "CA10__status__c" : "Active",
    "CA10__createDate__c" : new Date("2025-05-07T20:33:32Z"),
    "Id" : "test4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-12-19T02:21:26Z")
    },
    "CA10__status__c" : "Active",
    "CA10__lastUsedDate__c" : new Date("2025-12-18T21:39:00Z"),
    "CA10__createDate__c" : new Date("2025-07-25T20:46:37Z"),
    "Id" : "test5"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsAccessKey__c(
      obj STRUCT<
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__status__c STRING,
          CA10__lastUsedDate__c TIMESTAMP,
          CA10__createDate__c TIMESTAMP,
          Id 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));
      };
      this.containsAll = function(arg, substrings) {
          if (substrings == null || substrings.length === 0) return false;
          let normalizedArg = this.normalize(arg);
          return substrings.every(sub => normalizedArg.includes(this.normalize(sub)));
      };
      this.containsAny = function(arg, substrings) {
          if (substrings == null || substrings.length === 0) return false;
          let normalizedArg = this.normalize(arg);
          return substrings.some(sub => normalizedArg.includes(this.normalize(sub)));
      };
  }();
  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 AWS [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]
      references1.push('Status [obj.CA10__status__c]: ' + obj.CA10__status__c);
      if (TextLib.notEqual(obj.CA10__status__c, 'Active')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "CA10__status__c != 'Active'", currentStateMessage: "The access key is currently Inactive.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      references1.push('Last Used: Date [obj.CA10__lastUsedDate__c]: ' + obj.CA10__lastUsedDate__c);
      if (IsEmptyLib.simpleIsNotEmpty(obj.CA10__lastUsedDate__c) && (obj.CA10__lastUsedDate__c != null && obj.CA10__lastUsedDate__c.getTime() < todayMinus90Day)) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "CA10__lastUsedDate__c.isNotEmpty() && CA10__lastUsedDate__c.beyondLastDays(90)", currentStateMessage: "The access key is active but has not been used in over 90 days.", currentStateReferences: references1.join('\n'), remediation: "Deactivate the unused access key.", runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      references1.push('Create Date [obj.CA10__createDate__c]: ' + obj.CA10__createDate__c);
      if (IsEmptyLib.simpleIsEmpty(obj.CA10__lastUsedDate__c) && (obj.CA10__createDate__c != null && obj.CA10__createDate__c.getTime() < todayMinus90Day)) {
          return {status: 'INCOMPLIANT', conditionIndex: 399, conditionText: "CA10__lastUsedDate__c.isEmpty() && CA10__createDate__c.beyondLastDays(90)", currentStateMessage: "The access key is active, has never been used, and was created over 90 days ago.", currentStateReferences: references1.join('\n'), remediation: "Deactivate the unused access key.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 400, conditionText: "otherwise", currentStateMessage: "The access key is active and has been used within the last 90 days (or is recently created).", 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.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__status__c AS CA10__status__c,
          sObject.CA10__lastUsedDate__c AS CA10__lastUsedDate__c,
          sObject.CA10__createDate__c AS CA10__createDate__c,
          sObject.Id AS Id,
          process_CA10__CaAwsAccessKey__c(
              STRUCT(
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__status__c AS CA10__status__c,
                  sObject.CA10__lastUsedDate__c AS CA10__lastUsedDate__c,
                  sObject.CA10__createDate__c AS CA10__createDate__c,
                  sObject.Id AS Id
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsAccessKey__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
