---
policy: /ce/ca/aws/kms/cmk-scheduled-deletion
logic: /ce/ca/aws/kms/cmk-scheduled-deletion/prod.logic.yaml
executionTime: 2025-11-17T14:33:40.796040383Z
generationMs: 56
executionMs: 763
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: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__deletionDate__c').isNotEmpty()
      actual: extract('CA10__deletionDate__c').isNotEmpty()
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__manager__c') != 'CUSTOMER'
      actual: extract('CA10__manager__c') != 'CUSTOMER'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/kms/cmk-scheduled-deletion/policy.yaml
    md5Hash: 80939E10D4AD421C7153CA6E25F6BB5B
    content: "---\nnames:\n  full: \"AWS KMS CMK is scheduled for deletion\"\n  contextual:\
      \ \"CMK is scheduled for deletion\"\ndescription: >\n  Ensure that AWS KMS Customer-Managed\
      \ Keys (CMKs) are not scheduled for deletion \n  and disabled instead, if they're\
      \ not in use. Deleting a CMK is an irreversible \n  action that can lead to\
      \ permanent data loss.\ntype: \"COMPLIANCE_POLICY\"\ncategories:\n  - \"SECURITY\"\
      \n  - \"RELIABILITY\"\nframeworkMappings:\n  - \"/frameworks/aws-fsbp-v1.0.0/kms/03\"\
      \n  - \"/frameworks/cloudaware/secret-and-certificate-governance/expiration-management\"\
      \n  - \"/frameworks/aws-well-architected/sec/08/01\"\n  - \"/frameworks/aws-well-architected/sec/09/01\"\
      \nsimilarPolicies:\n  awsSecurityHub:\n    - name: \"[KMS.3] AWS KMS keys should\
      \ not be deleted unintentionally\"\n      url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/kms-controls.html#kms-3\""
  - path: /ce/ca/aws/kms/cmk-scheduled-deletion/prod.logic.yaml
    md5Hash: 8D6C4A6F8621C4D44E2CC9DFAFB2D8E4
    content: "---\ninputType: \"CA10__CaAwsKmsKey__c\"\ntestData:\n  - file: \"test-data.json\"\
      \nimportExtracts:\n  - file: \"/types/CA10__CaAwsKmsKey__c/object.extracts.yaml\"\
      \nconditions:\n  - status: \"INAPPLICABLE\"\n    currentStateMessage: \"This\
      \ Key is not Customer-managed.\"\n    check:\n      NOT_EQUAL:\n        left:\n\
      \          EXTRACT: \"CA10__manager__c\"\n        right:\n          TEXT: \"\
      CUSTOMER\"\n  - status: \"INCOMPLIANT\"\n    currentStateMessage: \"This KMS\
      \ CMK is scheduled for deletion.\"\n    remediationMessage: \"Consider disabling\
      \ the key instead of deleting it if the key\\ \n      \\ is not in use. Deleting\
      \ a key is an irreversible action.\"\n    check:\n      NOT_EMPTY:\n       \
      \ arg:\n          EXTRACT: \"CA10__deletionDate__c\"\notherwise:\n  status:\
      \ \"COMPLIANT\"\n  currentStateMessage: \"This KMS CMK is not scheduled for\
      \ deletion.\""
  - path: /ce/ca/aws/kms/cmk-scheduled-deletion/test-data.json
    md5Hash: 2E645816C03CF4E21081581B4425F98A
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-29T06:46:08Z"
              },
              "CA10__disappearanceTime__c": "2025-10-23T07:05:14Z",
              "CA10__manager__c": "AWS",
              "CA10__deletionDate__c": "2025-10-22T21:24:16Z",
              "Id": "test1"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__deletionDate__c').isNotEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-29T06:46:08Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10__manager__c": "CUSTOMER",
              "CA10__deletionDate__c": "2025-11-02T17:24:54Z",
              "Id": "test2"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-29T06:46:08Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10__manager__c": "CUSTOMER",
              "CA10__deletionDate__c": null,
              "Id": "test3"
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__manager__c') != 'CUSTOMER'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-10-29T06:46:08Z"
              },
              "CA10__disappearanceTime__c": null,
              "CA10__manager__c": "AWS",
              "CA10__deletionDate__c": null,
              "Id": "test4"
          }
      ]
  - path: /types/CA10__CaAwsKmsKey__c/object.extracts.yaml
    md5Hash: EC6D0EFD0BFEAB3447A84529724D4AFE
    content: "---\nextracts:\n# Values: Creating, Enabled, Disabled, PendingDeletion,\
      \ PendingImport, PendingReplicaDeletion, Unavailable, Updating. Not nullable.\n\
      \  - name: \"CA10__state__c\"\n    value: \n      FIELD:\n        path: \"CA10__state__c\"\
      \n        undeterminedIf:\n          noAccessDelegate: \n            path: \"\
      CA10__state__c\"\n            currentStateMessage: \"Unable to determine Key\
      \ state. Possible permission issue with kms:DescribeKey.\"\n          isEmpty:\
      \ \"KMS Key state is not populated yet.\"\n# Values: RSA_2048, RSA_3072, RSA_4096,\
      \ ECC_NIST_P256, ECC_NIST_P384, ECC_NIST_P521, ECC_SECG_P256K1, SYMMETRIC_DEFAULT,\
      \ HMAC_224, HMAC_256, HMAC_384, HMAC_512, SM2\n# Not nullable\n  - name: \"\
      CA10__keySpec__c\"\n    value: \n      FIELD:\n        path: \"CA10__keySpec__c\"\
      \n        undeterminedIf:\n          noAccessDelegate: \n            path: \"\
      CA10__keySpec__c\"\n            currentStateMessage: \"Unable to determine Key\
      \ state. Possible permission issue with kms:DescribeKey.\"\n          isEmpty:\
      \ \"KMS Key state is not populated yet.\"\n# Checkbox.\n  - name: \"CA10__rotationEnabled__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__rotationEnabled__c\"\n  \
      \      undeterminedIf:\n          noAccessDelegate:\n            path: \"CA10__rotationEnabled__c\"\
      \n            currentStateMessage: \"Unable to determine Key Rotation status.\
      \ Possible permission issue with kms:GetKeyRotationStatus.\"\n# Nullable.\n\
      \  - name: \"CA10__deletionDate__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__deletionDate__c\"\n# Values: AWS | Customer. Not Nullable.\n  - name:\
      \ \"CA10__manager__c\"\n    value: \n      FIELD:\n        path: \"CA10__manager__c\"\
      \n        undeterminedIf:\n          noAccessDelegate: \n            path: \"\
      CA10__manager__c\"\n            currentStateMessage: \"Unable to determine Key\
      \ Manager. Possible permission issue with kms:DescribeKey.\"\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" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__deletionDate__c').isNotEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__manager__c') != 'CUSTOMER'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsKmsKey__c()
  RETURNS ARRAY<STRUCT<
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__manager__c STRING,
      CA10__deletionDate__c TIMESTAMP,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-10-29T06:46:08Z")
    },
    "CA10__disappearanceTime__c" : new Date("2025-10-23T07:05:14Z"),
    "CA10__manager__c" : "AWS",
    "CA10__deletionDate__c" : new Date("2025-10-22T21:24:16Z"),
    "Id" : "test1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-29T06:46:08Z")
    },
    "CA10__manager__c" : "CUSTOMER",
    "CA10__deletionDate__c" : new Date("2025-11-02T17:24:54Z"),
    "Id" : "test2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-29T06:46:08Z")
    },
    "CA10__manager__c" : "CUSTOMER",
    "Id" : "test3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-10-29T06:46:08Z")
    },
    "CA10__manager__c" : "AWS",
    "Id" : "test4"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsKmsKey__c(
      obj STRUCT<
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__manager__c STRING,
          CA10__deletionDate__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 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 fieldChecked4() {
          if (TextLib.isEmpty(obj.CA10__manager__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__manager__c.delegatedTo(CA10__manager__c).isEmpty()", currentStateMessage: "Unable to determine Key Manager. Possible permission issue with kms:DescribeKey.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__manager__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Manager [obj.CA10__manager__c]: ' + obj.CA10__manager__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'CUSTOMER')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__manager__c') != 'CUSTOMER'", currentStateMessage: "This Key is not Customer-managed.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__deletionDate__c; } return this.out; };
      references1.push('Deletion Date [obj.CA10__deletionDate__c]: ' + obj.CA10__deletionDate__c);
      if (IsEmptyLib.simpleIsNotEmpty(extract6.call(extract6))) {
          return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__deletionDate__c').isNotEmpty()", currentStateMessage: "This KMS CMK is scheduled for deletion.", currentStateReferences: references1.join('\n'), remediation: "Consider disabling the key instead of deleting it if the key   is not in use. Deleting a key is an irreversible action.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "This KMS CMK is not scheduled for deletion.", 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__manager__c AS CA10__manager__c,
          sObject.CA10__deletionDate__c AS CA10__deletionDate__c,
          sObject.Id AS Id,
          process_CA10__CaAwsKmsKey__c(
              STRUCT(
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__manager__c AS CA10__manager__c,
                  sObject.CA10__deletionDate__c AS CA10__deletionDate__c,
                  sObject.Id AS Id
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsKmsKey__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
