---
inputType: "CA10__CaAzureSqlServer__c"
testData:
  - file: "test-data.json"
importExtracts:
  - file: "/types/CA10__CaAzureSqlServer__c/object.extracts.yaml"
conditions:
  - status: "INCOMPLIANT"
    currentStateMessage: "SQL Database Server Firewall Rules contain AllowAllWindowsAzureIps rule allowing ingress from 0.0.0.0/0 (ANY IP)."
    remediationMessage: "Consider removing or disabling the overly permissive rule."
    check:
# searches for the rule with the name AllowAllWindowsAzureIps
      IS_EQUAL:
        left:
          JSON_QUERY_TEXT:
              arg: 
                EXTRACT: "caJsonFrom__firewallRulesJson__c"
              expression: "[? name == 'AllowAllWindowsAzureIps'].name | [0]"
              undeterminedIf: 
                evaluationError: "The JSON query has failed."
                resultTypeMismatch: "The JSON query did not return text type."
        right:
          TEXT: "AllowAllWindowsAzureIps"
  - status: "INCOMPLIANT"
    currentStateMessage: "SQL Database Server Firewall Rules contain a rule allowing ingress from 0.0.0.0/0 (ANY IP)."
    remediationMessage: "Consider removing or disabling the overly permissive rule."
    check:
# number of rules with start and end IPs as 0.0.0.0
      GREATER_THAN:
        left:
          JSON_QUERY_NUMBER:
              arg: 
                EXTRACT: "caJsonFrom__firewallRulesJson__c"
              expression: "length([? properties.startIpAddress == '0.0.0.0' && (properties.endIpAddress == '0.0.0.0' || properties.endIpAddress == '255.255.255.255')])"
              undeterminedIf: 
                evaluationError: "The JSON query has failed."
                resultTypeMismatch: "The JSON query did not return number type."
        right:
          NUMBER: 0.0
  - status: "COMPLIANT"
    currentStateMessage: "SQL Database Server doesn't have any Firewall Rules."
    check:
      IS_EMPTY:
        arg:
          EXTRACT: "CA10__firewallRulesJson__c"
otherwise:
  status: "COMPLIANT"
  currentStateMessage: "SQL Database Server Firewall Rules do not have a rule allowing ingress from 0.0.0.0/0 (ANY IP)."
