---
policy: /ce/ca/aws/iam/role-unused
logic: /ce/ca/aws/iam/role-unused/prod.logic.yaml
executionTime: 2025-04-24T23:44:56.574547285Z
rows:
  - id: 111
    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: 112
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__lastUsedDate__c').isEmpty()
      actual: extract('CA10__lastUsedDate__c').isEmpty()
    runtimeError: {}
  - id: 113
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__lastUsedDate__c').beyondLastDays(90)
      actual: extract('CA10__lastUsedDate__c').beyondLastDays(90)
    runtimeError: {}
  - id: 114
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/iam/role-unused/policy.yaml
    md5Hash: 645A8526A392E1AD10F10BB04BCBEE0B
    content: |
      ---
      names:
        full: AWS IAM Role unused
        contextual: IAM Role Unused
      description: "IAM provides last used information to help you identify unused roles so that you can remove them.\
        \ An IAM role is considered unused if there has been no usage/activity for this role in the past 90 days.\
        \ This helps you to better adhere to the best practice of least privilege."
      type: "BEST_PRACTICE"
      categories:
        - "SECURITY"
      frameworkMappings:
        - "/frameworks/cloudaware/identity-and-access-governance/rbac-management"
  - path: /ce/ca/aws/iam/role-unused/prod.logic.yaml
    md5Hash: 128FD18CA80CEA117C0110A736B4D386
    content: |-
      ---
      inputType: CA10__CaAwsRole__c
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10__CaAwsRole__c/object.extracts.yaml"
      conditions:
        - status: INCOMPLIANT
          currentStateMessage: "Role has not been used within the tracking period and meets the unused criteria"
          remediationMessage: "Delete unused role"
          check:
            IS_EMPTY:
              arg:
                EXTRACT: "CA10__lastUsedDate__c"
        - status: INCOMPLIANT
          currentStateMessage: "Role has not been used in the past 90 days"
          remediationMessage: "Delete unused role"
          check:
            IS_BEYOND_LAST_DAYS:
              offsetDays: 90
              arg:
                EXTRACT: "CA10__lastUsedDate__c"
      otherwise:
        status: COMPLIANT
        currentStateMessage: "The role was used."
  - path: /ce/ca/aws/iam/role-unused/test-data.json
    md5Hash: C701129617ACE393E12B504C4653D251
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-27T10:28:24Z" },
          "Id": "111",
          "CA10__disappearanceTime__c": "2020-08-28T09:44:02Z",
          "CA10__lastUsedDate__c": null
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__lastUsedDate__c').isEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-27T10:28:24Z" },
          "Id": "112",
          "CA10__disappearanceTime__c": null,
          "CA10__lastUsedDate__c": null
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "299",
            "conditionText": "extract('CA10__lastUsedDate__c').beyondLastDays(90)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-27T10:28:24Z" },
          "Id": "113",
          "CA10__disappearanceTime__c": null,
          "CA10__lastUsedDate__c": "2023-08-09T12:48:19Z"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "300",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-01-27T10:28:24Z" },
          "Id": "114",
          "CA10__disappearanceTime__c": null,
          "CA10__lastUsedDate__c": "2025-01-26T07:32:19Z"
        }
      ]
  - path: /types/CA10__CaAwsRole__c/object.extracts.yaml
    md5Hash: 25AA041C020A86410A029D7DB711150E
    content: "---\nextracts:\n  - name: \"CA10__lastUsedDate__c\"\n    value: \n \
      \     FIELD:\n        path: \"CA10__lastUsedDate__c\"\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" : "111",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "112",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__lastUsedDate__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "113",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__lastUsedDate__c').beyondLastDays(90)",
      "runtimeError" : null
    }
  }, {
    "Id" : "114",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsRole__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__lastUsedDate__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-01-27T10:28:24Z")
    },
    "Id" : "111",
    "CA10__disappearanceTime__c" : new Date("2020-08-28T09:44:02Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-27T10:28:24Z")
    },
    "Id" : "112"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-27T10:28:24Z")
    },
    "Id" : "113",
    "CA10__lastUsedDate__c" : new Date("2023-08-09T12:48:19Z")
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-01-27T10:28:24Z")
    },
    "Id" : "114",
    "CA10__lastUsedDate__c" : new Date("2025-01-26T07:32:19Z")
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsRole__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__lastUsedDate__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 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]
      function extract3() { if (!this.out) { this.out = obj.CA10__lastUsedDate__c; } return this.out; };
      references1.push('Last Used Date [obj.CA10__lastUsedDate__c]: ' + obj.CA10__lastUsedDate__c);
      if (IsEmptyLib.simpleIsEmpty(extract3.call(extract3))) {
          return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__lastUsedDate__c').isEmpty()", currentStateMessage: "Role has not been used within the tracking period and meets the unused criteria", currentStateReferences: references1.join('\n'), remediation: "Delete unused role", runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__lastUsedDate__c; } return this.out; };
      if (extract6.call(extract6) != null && extract6.call(extract6).getTime() < todayMinus90Day) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__lastUsedDate__c').beyondLastDays(90)", currentStateMessage: "Role has not been used in the past 90 days", currentStateReferences: references1.join('\n'), remediation: "Delete unused role", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The role was used.", 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__lastUsedDate__c AS CA10__lastUsedDate__c,
          process_CA10__CaAwsRole__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__lastUsedDate__c AS CA10__lastUsedDate__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsRole__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
