---
policy: /ce/ca/aws/acm/rsa-certificate-key-length-less-than-2048-bits
logic: /ce/ca/aws/acm/rsa-certificate-key-length-less-than-2048-bits/prod.logic.yaml
executionTime: 2025-05-10T12:01:26.886322858Z
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: extract('CA10__status__c') != 'ISSUED'
      actual: extract('CA10__status__c') != 'ISSUED'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__keyAlgorithm__c') == 'RSA-1024'
      actual: extract('CA10__keyAlgorithm__c') == 'RSA-1024'
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 300
      actual: 300
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/aws/acm/rsa-certificate-key-length-less-than-2048-bits/policy.yaml
    md5Hash: D5787C55F480B64458193A6EA141EBE7
    content: "---\nnames:\n  full: \"AWS ACM RSA Certificate key length is less than\
      \ 2048 bits\"\n  contextual: \"RSA Certificate key length is less than 2048\
      \ bits\"\ndescription: >\n  Ensure that RSA certificates managed by AWS Certificate\
      \ Manager (ACM) have a \n  key length of at least 2,048 bits since the strength\
      \ of encryption directly \n  correlates with key size.\ncategories:\n  - \"\
      SECURITY\"\ntype: \"COMPLIANCE_POLICY\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/secret-and-certificate-governance/cryptographic-configuration\"\
      \n  - \"/frameworks/aws-fsbp-v1.0.0/acm/02\"\nsimilarPolicies:\n  internal:\n\
      \    - \"dec-x-4de1c44e\""
  - path: /ce/ca/aws/acm/rsa-certificate-key-length-less-than-2048-bits/prod.logic.yaml
    md5Hash: B9EC0531D9631D020381F5A8B0339CA9
    content: |
      ---
      inputType: "CA10__CaAwsAcmCertificate__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAwsAcmCertificate__c/object.extracts.yaml
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The certificate is not active."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__status__c"
              right:
                TEXT: "ISSUED"
        - status: "INCOMPLIANT"
          currentStateMessage: "The certificate key length is smaller than 2,048 bits."
          remediationMessage: "Consider deleting the certificate with a key length smaller than 2,048 bits."
          check:
            IS_EQUAL:
              left:
                EXTRACT: "CA10__keyAlgorithm__c"
              right:
                TEXT: "RSA-1024"
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: "The certificate uses a key length of at least 2,048 bits."
  - path: /ce/ca/aws/acm/rsa-certificate-key-length-less-than-2048-bits/test-data.json
    md5Hash: BD258A117D8C1A80DF8120F4347273F7
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-10T20:07:16Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2024-12-05T15:45:56Z",
              "CA10__status__c": "ISSUED",
              "CA10__keyAlgorithm__c": "RSA-1024"
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__status__c') != 'ISSUED'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-10T20:07:16Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "EXPIRED",
              "CA10__keyAlgorithm__c": "RSA-1024"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__keyAlgorithm__c') == 'RSA-1024'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-10T20:07:16Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "ISSUED",
              "CA10__keyAlgorithm__c": "RSA-1024"
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "300",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-12-10T20:07:16Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "ISSUED",
              "CA10__keyAlgorithm__c": "RSA-2048"
          }
      ]
  - path: /types/CA10__CaAwsAcmCertificate__c/object.extracts.yaml
    md5Hash: D20EE095842A506CDEF3AC47E90A5A3B
    content: "---\nextracts:\n# Values: PENDING_VALIDATION | ISSUED | INACTIVE | EXPIRED\
      \ | REVOKED | FAILED | VALIDATION_TIMED_OUT\n# Not Nullable can't have no access\n\
      \  - name: CA10__status__c\n    value: \n      FIELD:\n        path: CA10__status__c\n\
      \        undeterminedIf:\n          noAccessDelegate:\n            path: CA10__status__c\n\
      \            currentStateMessage: Unable to determine certificate status. Possible\
      \ permission issue with acm:DescribeCertificate\n          isEmpty: Status is\
      \ not populated yet\n# Nullable\n  - name: CA10__notAfter__c\n    value: \n\
      \      FIELD:\n        path: CA10__notAfter__c\n        undeterminedIf:\n  \
      \        noAccessDelegate:\n            path: CA10__notAfter__c\n          \
      \  currentStateMessage: Unable to determine certificate 'NotAfter'. Possible\
      \ permission issue with acm:DescribeCertificate\n# Not Nullable\n  - name: CA10__domainName__c\n\
      \    value: \n      FIELD:\n        path: CA10__domainName__c\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: CA10__domainName__c\n      \
      \      currentStateMessage: Unable to determine certificate domain name. Possible\
      \ permission issue with acm:DescribeCertificate\n          isEmpty: Domain name\
      \ is not populated yet\n# Values: ELIGIBLE | INELIGIBLE, Not Nullable\n  - name:\
      \ CA10__renewalEligibility__c\n    value: \n      FIELD:\n        path: CA10__renewalEligibility__c\n\
      \        undeterminedIf:\n          noAccessDelegate:\n            path: CA10__renewalEligibility__c\n\
      \            currentStateMessage: Unable to determine certificate renewal eligibility.\
      \ Possible permission issue with acm:DescribeCertificate\n          isEmpty:\
      \ Renewal eligibility is not populated yet\n# Values: RSA-1024 | RSA-2048 |\
      \ RSA-3072 | RSA-4096 | EC-prime256v1 | EC-secp384r1 | EC-secp521r1\n# Not Nullable\n\
      \  - name: CA10__keyAlgorithm__c\n    value: \n      FIELD:\n        path: CA10__keyAlgorithm__c\n\
      \        undeterminedIf:\n          noAccessDelegate:\n            path: CA10__keyAlgorithm__c\n\
      \            currentStateMessage: Unable to determine certificate status. Possible\
      \ permission issue with acm:DescribeCertificate\n          isEmpty: Key Algorithm\
      \ is not populated yet\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" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__status__c') != 'ISSUED'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__keyAlgorithm__c') == 'RSA-1024'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "300",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsAcmCertificate__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__status__c STRING,
      CA10__keyAlgorithm__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-12-10T20:07:16Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2024-12-05T15:45:56Z"),
    "CA10__status__c" : "ISSUED",
    "CA10__keyAlgorithm__c" : "RSA-1024"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-10T20:07:16Z")
    },
    "Id" : "test2",
    "CA10__status__c" : "EXPIRED",
    "CA10__keyAlgorithm__c" : "RSA-1024"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-10T20:07:16Z")
    },
    "Id" : "test3",
    "CA10__status__c" : "ISSUED",
    "CA10__keyAlgorithm__c" : "RSA-1024"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-12-10T20:07:16Z")
    },
    "Id" : "test4",
    "CA10__status__c" : "ISSUED",
    "CA10__keyAlgorithm__c" : "RSA-2048"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsAcmCertificate__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__status__c STRING,
          CA10__keyAlgorithm__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 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 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__status__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__status__c.delegatedTo(CA10__status__c).isEmpty()", currentStateMessage: "Unable to determine certificate status. Possible permission issue with acm:DescribeCertificate", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__status__c)) {
              throw new Error("UNDETERMINED condition:102", {cause: {status: 'UNDETERMINED', conditionIndex: 102, conditionText: "CA10__status__c.isEmpty()", currentStateMessage: "Status is not populated yet", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__status__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      references1.push('Status [obj.CA10__status__c]: ' + obj.CA10__status__c);
      try {
          if (TextLib.notEqual(extract3.call(extract3), 'ISSUED')) {
              return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__status__c') != 'ISSUED'", currentStateMessage: "The certificate is not active.", 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 fieldChecked7() {
          if (TextLib.isEmpty(obj.CA10__keyAlgorithm__c)) {
              throw new Error("UNDETERMINED condition:201", {cause: {status: 'UNDETERMINED', conditionIndex: 201, conditionText: "CA10__keyAlgorithm__c.delegatedTo(CA10__keyAlgorithm__c).isEmpty()", currentStateMessage: "Unable to determine certificate status. Possible permission issue with acm:DescribeCertificate", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__keyAlgorithm__c)) {
              throw new Error("UNDETERMINED condition:202", {cause: {status: 'UNDETERMINED', conditionIndex: 202, conditionText: "CA10__keyAlgorithm__c.isEmpty()", currentStateMessage: "Key Algorithm is not populated yet", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__keyAlgorithm__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      references1.push('Key Algorithm [obj.CA10__keyAlgorithm__c]: ' + obj.CA10__keyAlgorithm__c);
      try {
          if (TextLib.equal(extract6.call(extract6), 'RSA-1024')) {
              return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "extract('CA10__keyAlgorithm__c') == 'RSA-1024'", currentStateMessage: "The certificate key length is smaller than 2,048 bits.", currentStateReferences: references1.join('\n'), remediation: "Consider deleting the certificate with a key length smaller than 2,048 bits.", runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'COMPLIANT', conditionIndex: 300, conditionText: "otherwise", currentStateMessage: "The certificate uses a key length of at least 2,048 bits.", 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__status__c AS CA10__status__c,
          sObject.CA10__keyAlgorithm__c AS CA10__keyAlgorithm__c,
          process_CA10__CaAwsAcmCertificate__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__status__c AS CA10__status__c,
                  sObject.CA10__keyAlgorithm__c AS CA10__keyAlgorithm__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsAcmCertificate__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
