---
policy: /ce/ca/azure/sql-database/server-transparent-data-encryption-protector-with-cmk
logic: /ce/ca/azure/sql-database/server-transparent-data-encryption-protector-with-cmk/prod.logic.yaml
executionTime: 2025-04-24T23:45:50.264366687Z
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: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__encryptionProtectorKind__c') == 'azurekeyvault' &&
        extract('CA10__encryptionProtectorServerKeyType__c') == 'AzureKeyVault' &&
        extract('CA10__encryptionProtectorUri__c').isNotEmpty()
      actual: extract('CA10__encryptionProtectorKind__c') == 'azurekeyvault' && extract('CA10__encryptionProtectorServerKeyType__c')
        == 'AzureKeyVault' && extract('CA10__encryptionProtectorUri__c').isNotEmpty()
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 101
      actual: 101
    conditionText:
      expected: CA10__encryptionProtectorKind__c.delegatedTo(CA10__encryptionProtectorKind__c).isEmpty()
      actual: CA10__encryptionProtectorKind__c.delegatedTo(CA10__encryptionProtectorKind__c).isEmpty()
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/sql-database/server-transparent-data-encryption-protector-with-cmk/policy.yaml
    md5Hash: F433AAD7ABE4C01F67817206295CBD43
    content: |
      ---
      names:
        full: Azure SQL Server Transparent Data Encryption Protector is not encrypted with
          Customer-managed key
        contextual: Server Transparent Data Encryption Protector is not encrypted with Customer-managed
          key
      description: "Transparent Data Encryption (TDE) with Customer-managed key support\
        \ provides increased transparency and control over the TDE Protector, increased\
        \ security with an HSM-backed external service, and promotion of separation of duties."
      type: BEST_PRACTICE
      categories:
        - SECURITY
      frameworkMappings:
        - "/frameworks/cis-azure-v2.1.0/04/01/03"
        - "/frameworks/cis-azure-v3.0.0/05/01/03"
        - "/frameworks/cloudaware/resource-security/data-encryption"
      similarPolicies:
        internal:
          - dec-x-230b5e35
        cloudConformity:
          - url: https://www.trendmicro.com/cloudoneconformity/knowledge-base/azure/Sql/use-byok-for-transparent-data-encryption.html
            name: Use BYOK for Transparent Data Encryption
  - path: /ce/ca/azure/sql-database/server-transparent-data-encryption-protector-with-cmk/prod.logic.yaml
    md5Hash: BC79E4BF9316F4142F17D97D2ECFFADA
    content: |-
      ---
      inputType: "CA10__CaAzureSqlServer__c"
      testData:
        - file: "test-data.json"
      importExtracts:
        - file: "/types/CA10__CaAzureSqlServer__c/object.extracts.yaml"
      conditions:
        - status: "COMPLIANT"
          currentStateMessage: "SQL Server's Transparent Data Encryption (TDE) protector is encrypted with Customer-managed key."
          check:
            AND:
              args:
                - IS_EQUAL:
                    left:
                      EXTRACT: "CA10__encryptionProtectorKind__c"
                    right:
                      TEXT: "azurekeyvault"
                - IS_EQUAL:
                    left:
                      EXTRACT: "CA10__encryptionProtectorServerKeyType__c"
                    right:
                      TEXT: "AzureKeyVault"
                - NOT_EMPTY:
                    arg:
                      EXTRACT: "CA10__encryptionProtectorUri__c"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "SQL Server's Transparent Data Encryption (TDE) protector is not encrypted with Customer-managed key."
        remediationMessage: "Consider encrypting SQL Server's Transparent Data Encryption (TDE) protector with Customer-managed key."
  - path: /types/CA10__CaAzureSqlServer__c/object.extracts.yaml
    md5Hash: E77808BBEDF31D54E009EEA59AC5EF4B
    content: "---\nextracts:\n# Values: Enabled, Disabled. Not Nullable.\n  - name:\
      \ \"CA10__auditing__c\"\n    value: \n      FIELD:\n        path: \"CA10__auditing__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__auditing__c\"\n            currentStateMessage: \"Unable to determine\
      \ Server auditing settings. Possible permission issue with Microsoft.Sql/servers/auditingSettings/read\"\
      \n          isEmpty: \"SQL Server auditing settings are not populated yet\"\n\
      # Number field\n  - name: \"CA10__auditingRetentionDays__c\"\n    value: \n\
      \      FIELD:\n        path: \"CA10__auditingRetentionDays__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__auditingRetentionDays__c\"\
      \n            currentStateMessage: \"Unable to determine Server auditing settings.\
      \ Possible permission issue with Microsoft.Sql/servers/auditingSettings/read\"\
      \n# Values: servicemanaged, azurekeyvault. Not Nullable. \n  - name: \"CA10__encryptionProtectorKind__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__encryptionProtectorKind__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__encryptionProtectorKind__c\"\n            currentStateMessage: \"Unable\
      \ to determine Server Encryption Protectors. Possible permission issue with\
      \ Microsoft.Sql/servers/encryptionProtector/read\"\n          isEmpty: \"SQL\
      \ Server Encryption Protectors are not populated yet\"\n# Values: ServiceManaged,\
      \ AzureKeyVault. Not Nullable. \n  - name: \"CA10__encryptionProtectorServerKeyType__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__encryptionProtectorServerKeyType__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__encryptionProtectorServerKeyType__c\"\n            currentStateMessage:\
      \ \"Unable to determine Server Encryption Protectors. Possible permission issue\
      \ with Microsoft.Sql/servers/encryptionProtector/read\"\n          isEmpty:\
      \ \"SQL Server Encryption Protectors are not populated yet\"\n# Nullable. \n\
      \  - name: \"CA10__encryptionProtectorUri__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__encryptionProtectorUri__c\"\n        undeterminedIf:\n\
      \          noAccessDelegate:\n            path: \"CA10__encryptionProtectorUri__c\"\
      \n            currentStateMessage: \"Unable to determine Server Encryption Protectors.\
      \ Possible permission issue with Microsoft.Sql/servers/encryptionProtector/read\"\
      \n# Values: ActiveDirectory, null. Nullable. \n  - name: \"CA10__activeDirectoryAdminType__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__activeDirectoryAdminType__c\"\
      \n        undeterminedIf:\n          noAccessDelegate:\n            path: \"\
      CA10__activeDirectoryAdminType__c\"\n            currentStateMessage: \"Unable\
      \ to determine Server AD Administrator type. Possible permission issue with\
      \ Microsoft.Sql/servers/administrators\"\n  - name: \"CA10__firewallRulesJson__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__firewallRulesJson__c\"\n\
      \        returnType: BYTES\n        undeterminedIf:\n          noAccessDelegate:\n\
      \            path: \"CA10__firewallRulesJson__c\"\n            currentStateMessage:\
      \ \"Unable to determine Firewall Rules. Possible permission issue with Microsoft.Sql/servers/firewallRules/read\"\
      \n  - name: \"caJsonFrom__firewallRulesJson__c\"\n    value: \n      JSON_FROM:\n\
      \        arg: \n          EXTRACT: \"CA10__firewallRulesJson__c\"\n        undeterminedIf:\n\
      \          isInvalid: \"Firewall Rules JSON is invalid.\"\n# Nullable. Values:\
      \ Enabled, Disabled, Can't have no access, retrieved via Microsoft.Sql/servers\n\
      \  - name: \"CA10__publicNetworkAccess__c\"\n    value: \n      FIELD:\n   \
      \     path: \"CA10__publicNetworkAccess__c\"\n"
  - path: /ce/ca/azure/sql-database/server-transparent-data-encryption-protector-with-cmk/test-data.json
    md5Hash: 100C25F318B197C267BDFD3583A72291
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2024-06-07T13:58:24Z",
              "CA10__encryptionProtectorKind__c": "azurekeyvault",
              "CA10__encryptionProtectorServerKeyType__c": "AzureKeyVault",
              "CA10__encryptionProtectorUri__c": "serverKeyUri"
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "200",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__encryptionProtectorKind__c": "servicemanaged",
              "CA10__encryptionProtectorServerKeyType__c": "ServiceManaged",
              "CA10__encryptionProtectorUri__c": ""
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__encryptionProtectorKind__c') == 'azurekeyvault' && extract('CA10__encryptionProtectorServerKeyType__c') == 'AzureKeyVault' && extract('CA10__encryptionProtectorUri__c').isNotEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__encryptionProtectorKind__c": "azurekeyvault",
              "CA10__encryptionProtectorServerKeyType__c": "AzureKeyVault",
              "CA10__encryptionProtectorUri__c": "serverKeyUri"
          },
          {
              "expectedResult": {
                  "status": "UNDETERMINED",
                  "conditionIndex": "101",
                  "conditionText": "CA10__encryptionProtectorKind__c.delegatedTo(CA10__encryptionProtectorKind__c).isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2024-06-13T04:17:29Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__encryptionProtectorKind__c": "",
              "CA10__encryptionProtectorServerKeyType__c": "",
              "CA10__encryptionProtectorUri__c": ""
          }
      ]
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" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__encryptionProtectorKind__c') == 'azurekeyvault' && extract('CA10__encryptionProtectorServerKeyType__c') == 'AzureKeyVault' && extract('CA10__encryptionProtectorUri__c').isNotEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "101",
      "conditionText" : "CA10__encryptionProtectorKind__c.delegatedTo(CA10__encryptionProtectorKind__c).isEmpty()",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureSqlServer__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__encryptionProtectorKind__c STRING,
      CA10__encryptionProtectorServerKeyType__c STRING,
      CA10__encryptionProtectorUri__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2024-06-07T13:58:24Z"),
    "CA10__encryptionProtectorKind__c" : "azurekeyvault",
    "CA10__encryptionProtectorServerKeyType__c" : "AzureKeyVault",
    "CA10__encryptionProtectorUri__c" : "serverKeyUri"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test2",
    "CA10__encryptionProtectorKind__c" : "servicemanaged",
    "CA10__encryptionProtectorServerKeyType__c" : "ServiceManaged",
    "CA10__encryptionProtectorUri__c" : ""
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test3",
    "CA10__encryptionProtectorKind__c" : "azurekeyvault",
    "CA10__encryptionProtectorServerKeyType__c" : "AzureKeyVault",
    "CA10__encryptionProtectorUri__c" : "serverKeyUri"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-06-13T04:17:29Z")
    },
    "Id" : "test4",
    "CA10__encryptionProtectorKind__c" : "",
    "CA10__encryptionProtectorServerKeyType__c" : "",
    "CA10__encryptionProtectorUri__c" : ""
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureSqlServer__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__encryptionProtectorKind__c STRING,
          CA10__encryptionProtectorServerKeyType__c STRING,
          CA10__encryptionProtectorUri__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 Azure [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__encryptionProtectorKind__c)) {
              throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__encryptionProtectorKind__c.delegatedTo(CA10__encryptionProtectorKind__c).isEmpty()", currentStateMessage: "Unable to determine Server Encryption Protectors. Possible permission issue with Microsoft.Sql/servers/encryptionProtector/read", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__encryptionProtectorKind__c)) {
              throw new Error("UNDETERMINED condition:102", {cause: {status: 'UNDETERMINED', conditionIndex: 102, conditionText: "CA10__encryptionProtectorKind__c.isEmpty()", currentStateMessage: "SQL Server Encryption Protectors are not populated yet", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__encryptionProtectorKind__c;
      }
      function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; };
      function fieldChecked7() {
          if (TextLib.isEmpty(obj.CA10__encryptionProtectorServerKeyType__c)) {
              throw new Error("UNDETERMINED condition:103", {cause: {status: 'UNDETERMINED', conditionIndex: 103, conditionText: "CA10__encryptionProtectorServerKeyType__c.delegatedTo(CA10__encryptionProtectorServerKeyType__c).isEmpty()", currentStateMessage: "Unable to determine Server Encryption Protectors. Possible permission issue with Microsoft.Sql/servers/encryptionProtector/read", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          if (TextLib.isEmpty(obj.CA10__encryptionProtectorServerKeyType__c)) {
              throw new Error("UNDETERMINED condition:104", {cause: {status: 'UNDETERMINED', conditionIndex: 104, conditionText: "CA10__encryptionProtectorServerKeyType__c.isEmpty()", currentStateMessage: "SQL Server Encryption Protectors are not populated yet", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__encryptionProtectorServerKeyType__c;
      }
      function extract6() { if (!this.out) { this.out = fieldChecked7(); } return this.out; };
      function fieldChecked10() {
          if (TextLib.isEmpty(obj.CA10__encryptionProtectorUri__c)) {
              throw new Error("UNDETERMINED condition:105", {cause: {status: 'UNDETERMINED', conditionIndex: 105, conditionText: "CA10__encryptionProtectorUri__c.delegatedTo(CA10__encryptionProtectorUri__c).isEmpty()", currentStateMessage: "Unable to determine Server Encryption Protectors. Possible permission issue with Microsoft.Sql/servers/encryptionProtector/read", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}});
          }
          return obj.CA10__encryptionProtectorUri__c;
      }
      function extract9() { if (!this.out) { this.out = fieldChecked10(); } return this.out; };
      references1.push('Encryption Protector: Kind [obj.CA10__encryptionProtectorKind__c]: ' + obj.CA10__encryptionProtectorKind__c);
      references1.push('Encryption Protector: Server Key Type [obj.CA10__encryptionProtectorServerKeyType__c]: ' + obj.CA10__encryptionProtectorServerKeyType__c);
      references1.push('Encryption Protector: URI [obj.CA10__encryptionProtectorUri__c]: ' + obj.CA10__encryptionProtectorUri__c);
      try {
          if (TextLib.equal(extract3.call(extract3), 'azurekeyvault') && TextLib.equal(extract6.call(extract6), 'AzureKeyVault') && TextLib.isNotEmpty(extract9.call(extract9))) {
              return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__encryptionProtectorKind__c') == 'azurekeyvault' && extract('CA10__encryptionProtectorServerKeyType__c') == 'AzureKeyVault' && extract('CA10__encryptionProtectorUri__c').isNotEmpty()", currentStateMessage: "SQL Server's Transparent Data Encryption (TDE) protector is encrypted with Customer-managed key.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "SQL Server's Transparent Data Encryption (TDE) protector is not encrypted with Customer-managed key.", currentStateReferences: references1.join('\n'), remediation: "Consider encrypting SQL Server's Transparent Data Encryption (TDE) protector with Customer-managed key.", 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__encryptionProtectorKind__c AS CA10__encryptionProtectorKind__c,
          sObject.CA10__encryptionProtectorServerKeyType__c AS CA10__encryptionProtectorServerKeyType__c,
          sObject.CA10__encryptionProtectorUri__c AS CA10__encryptionProtectorUri__c,
          process_CA10__CaAzureSqlServer__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__encryptionProtectorKind__c AS CA10__encryptionProtectorKind__c,
                  sObject.CA10__encryptionProtectorServerKeyType__c AS CA10__encryptionProtectorServerKeyType__c,
                  sObject.CA10__encryptionProtectorUri__c AS CA10__encryptionProtectorUri__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureSqlServer__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
