---
policy: /ce/ca/google/sql/sql-instance-ssl
logic: /ce/ca/google/sql/sql-instance-ssl/prod.logic.yaml
executionTime: 2025-04-24T23:47:30.615135784Z
rows:
  - id: a8LVU0
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 200
      actual: 200
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: a8LVU1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__sslMode__c') == 'ENCRYPTED_ONLY'
      actual: extract('CA10__sslMode__c') == 'ENCRYPTED_ONLY'
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/sql/sql-instance-ssl/policy.yaml
    md5Hash: 8E0AF356F09852090782AF8AC1EECF0E
    content: "---\nnames: \n  full: \"Google Cloud SQL Instance SSL Connections are\
      \ not enforced\"\n  contextual: \"SQL Instance SSL Connections are not enforced\"\
      \ndescription: \"It is recommended to enforce all incoming connections to SQL\
      \ database instance to use SSL.\"\ntype: COMPLIANCE_POLICY\ncategories:\n  -\
      \ \"SECURITY\"\nframeworkMappings:\n  - /frameworks/cis-gcp-v3.0.0/06/04\n \
      \ - /frameworks/cloudaware/resource-security/secure-access\nsimilarPolicies:\n\
      \  cloudConformity:\n    - url: \"https://www.trendmicro.com/cloudoneconformity-staging/knowledge-base/gcp/CloudSQL/enable-ssl-for-incoming-connections.html\"\
      \n      name: \"Enable SSL/TLS for Cloud SQL Incoming Connections\"\n"
  - path: /ce/ca/google/sql/sql-instance-ssl/prod.logic.yaml
    md5Hash: 9613394DCB99811F02F46006C18B2BAA
    content: |-
      ---

      inputType: "CA10__CaGoogleSqlInstance__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaGoogleSqlInstance__c/object.extracts.yaml
      conditions:
        - status: "COMPLIANT"
          currentStateMessage: "The SQL Instance requires all incoming connections to use SSL."
          check:
                IS_EQUAL:
                  left:
                    EXTRACT: "CA10__sslMode__c"
                  right:
                    TEXT: "ENCRYPTED_ONLY"
      otherwise:
        status: "INCOMPLIANT"
        currentStateMessage: "The SQL Instance does not require incoming connections use SSL."
        remediationMessage: "Enforce all incoming connections to SQL database instance to use SSL."
  - path: /types/CA10__CaGoogleSqlInstance__c/object.extracts.yaml
    md5Hash: CF5DA82560FA69E6A48E44B5BF123F0B
    content: "---\nextracts:\n  - name: \"CA10__databaseVersion__c\"\n    value: \n\
      \      FIELD:\n        path: \"CA10__databaseVersion__c\"\n        undeterminedIf:\n\
      \          isEmpty:  \"Database version should not be empty\"\n  - name: \"\
      CA10__ipv4Enabled__c\"\n    value: \n      FIELD:\n        path: \"CA10__ipv4Enabled__c\"\
      \n        undeterminedIf:\n          isEmpty:  \"Ipv4Enabled should not be empty\"\
      \n  - name: \"caExtract__databaseFlagsJson__c\"\n    value: \n      JSON_FROM:\n\
      \        arg:\n          FIELD:\n            path: CA10__databaseFlagsJson__c\n\
      \            returnType: BYTES\n        undeterminedIf:\n          isInvalid:\
      \ \"Database Flags JSON is invalid.\"\n          isEmpty: \"Database Flags JSON\
      \ is empty.\"\n\n  - name: \"caExtract_databaseFlags_skip_show_database_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'skip_show_database'].value[] |\
      \ [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_local_infile_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'local_infile'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_log_error_verbosity_flagValue__c\"\n\
      \    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression:  \"to_string([?name == 'log_error_verbosity'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_log_connections_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_connections'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_log_disconnections_flagValue__c\"\n\
      \    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression:  \"to_string([?name == 'log_disconnections'].value[] |\
      \ [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_log_statement_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_statement'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_log_min_messages_flagValue__c\"\n  \
      \  value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_min_messages'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_log_min_error_statement_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_min_error_statement'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_log_min_duration_statement_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'log_min_duration_statement'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n\n  - name: \"caExtract_databaseFlags_cloudsql_enable_pgaudit_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'cloudsql.enable_pgaudit'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_external_scripts_enabled_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'external scripts enabled'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_cross_db_ownership_chaining_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'cross db ownership chaining'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"caExtract_databaseFlags_user_connections_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'user connections'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_user_options_flagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'user options'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_remote_access_flagValue__c\"\n    value:\
      \ \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'remote access'].value[] | [0])\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_3625_flagValue__c\"\n    value: \n \
      \     JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == '3625'].value[] | [0])\"\n     \
      \   undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return text type.\"\
      \n\n  - name: \"caExtract_databaseFlags_contained_database_authentication_flagValue__c\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caExtract__databaseFlagsJson__c\"\
      \n        expression: \"to_string([?name == 'contained database authentication'].value[]\
      \ | [0])\"\n        undeterminedIf:\n          evaluationError: \"The JSON query\
      \ has failed.\"\n          resultTypeMismatch: \"The JSON query did not return\
      \ text type.\"\n\n  - name: \"CA10__authorizedNetworksJson__c\"\n    value:\
      \ \n      FIELD:\n        path: \"CA10__authorizedNetworksJson__c\"\n      \
      \  returnType: BYTES\n  - name: \"caJsonFrom_authorizedNetworksJson__c\"\n \
      \   value: \n      JSON_FROM:\n        arg: \n          EXTRACT: \"CA10__authorizedNetworksJson__c\"\
      \n        undeterminedIf:\n          isInvalid: \"Authorized Networks JSON is\
      \ invalid.\"  \n  - name: \"caJsonFrom_authorizedNetworksJson_value__c\"\n \
      \   value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom_authorizedNetworksJson__c\"\
      \n        expression: \"to_string([].value)\"\n        undeterminedIf:\n   \
      \       evaluationError: \"The JSON query was failed.\"\n          resultTypeMismatch:\
      \ \"The JSON query did not return a text.\"\n\n  - name: \"CA10__instanceType__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__instanceType__c\"\n     \
      \   undeterminedIf:\n          isEmpty: \"Instance type should not be empty\"\
      \n  - name: \"CA10__backendType__c\"\n    value: \n      FIELD:\n        path:\
      \ \"CA10__backendType__c\"\n        undeterminedIf:\n          isEmpty: \"Backend\
      \ type should not be empty\"\n  - name: \"CA10__backupEnabled__c\"\n    value:\
      \ \n      FIELD:\n        path: \"CA10__backupEnabled__c\"\n        undeterminedIf:\n\
      \          isEmpty: \"Backup enabled should not be empty\"\n  - name: \"CA10__backupStartTime__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__backupStartTime__c\"\n  -\
      \ name: \"CA10__sslMode__c\"\n    value: \n      FIELD:\n        path: \"CA10__sslMode__c\"\
      \n"
  - path: /ce/ca/google/sql/sql-instance-ssl/test-data.json
    md5Hash: 04C28D354D4426F541AF319D6D8A2867
    content: |
      [
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "200",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-11-25T19:20:39Z" },
          "Id": "a8LVU0",
          "CA10__disappearanceTime__c": null,
          "CA10__sslMode__c": "ALLOW_UNENCRYPTED_AND_ENCRYPTED"
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__sslMode__c') == 'ENCRYPTED_ONLY'",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2024-11-25T19:20:39Z" },
          "Id": "a8LVU1",
          "CA10__disappearanceTime__c": null,
          "CA10__sslMode__c": "ENCRYPTED_ONLY"
        }
      ]
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" : "a8LVU0",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "200",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "a8LVU1",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__sslMode__c') == 'ENCRYPTED_ONLY'",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleSqlInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__sslMode__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-11-25T19:20:39Z")
    },
    "Id" : "a8LVU0",
    "CA10__sslMode__c" : "ALLOW_UNENCRYPTED_AND_ENCRYPTED"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-11-25T19:20:39Z")
    },
    "Id" : "a8LVU1",
    "CA10__sslMode__c" : "ENCRYPTED_ONLY"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleSqlInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__sslMode__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 Google [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__sslMode__c; } return this.out; };
      references1.push('SSL Mode [obj.CA10__sslMode__c]: ' + obj.CA10__sslMode__c);
      if (TextLib.equal(extract3.call(extract3), 'ENCRYPTED_ONLY')) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "extract('CA10__sslMode__c') == 'ENCRYPTED_ONLY'", currentStateMessage: "The SQL Instance requires all incoming connections to use SSL.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The SQL Instance does not require incoming connections use SSL.", currentStateReferences: references1.join('\n'), remediation: "Enforce all incoming connections to SQL database instance to use SSL.", 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__sslMode__c AS CA10__sslMode__c,
          process_CA10__CaGoogleSqlInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__sslMode__c AS CA10__sslMode__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleSqlInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
