---
inputType: "CA10__CaAzureVirtualMachine__c"
testData:
  - file: test-data.json
conditions:
  - status: "INCOMPLIANT"
    currentStateMessage: "The Virtual Machine has a Network Security Group with public access to all ports."
    remediationMessage: "Restrict inbound access to all ports from the Internet."
    check:
      RELATED_LIST_HAS:
        status: "INCOMPLIANT"
        relationshipName: "CA10__Azure_Network_Interfaces__r"
otherwise:
  status: "COMPLIANT"
  currentStateMessage: "The Virtual Machine does not allow public access to all ports." 
relatedLists:
  - relationshipName: "CA10__Azure_Network_Interfaces__r"
    importExtracts:
      - file: "/types/CA10__CaAzureNetworkSecurityGroupRule__c/object.extracts.yaml"
    conditions:
      - status: "INCOMPLIANT"
        currentStateMessage: "Network Interface with publicly accessible Network Security Group"
        check:
          RELATED_LIST_HAS:
            status: "INCOMPLIANT"
            relationshipName:  "CA10__networkSecurityGroup__r.CA10__Azure_Network_Security_Group_Rules__r"
    otherwise:
      status: "COMPLIANT"
      currentStateMessage: "Network Interface is not publicly accessible"
    relatedLists:
      - relationshipName: "CA10__networkSecurityGroup__r.CA10__Azure_Network_Security_Group_Rules__r"
        importExtracts:
          - file: "/types/CA10__CaAzureNetworkSecurityGroupRule__c/object.extracts.yaml"
        conditions:
# sort public group rules
          - status: "INAPPLICABLE"
            currentStateMessage: "NSG Outbound Rule."
            check:
              NOT_EQUAL:
                left:
                  EXTRACT: "CA10__direction__c"
                right:
                  TEXT: "Inbound"
          - status: "INAPPLICABLE"
            currentStateMessage: "NSG Deny Rule."
            check:
              NOT_EQUAL:
                left:
                  EXTRACT: "CA10__access__c"
                right:
                  TEXT: "Allow"
          - status: "INAPPLICABLE"
            currentStateMessage: "NSG Rule with the port other than TCP, UDP or Any."
            check:
              NOT:
                arg:
                  OR:
                    args:
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__protocol__c"
                          right:
                            TEXT: "Tcp"
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__protocol__c"
                          right:
                            TEXT: "Udp"
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__protocol__c"
                          right:
                            TEXT: "*"
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__protocol__c"
                          right:
                            TEXT: "null"
          - status: "INAPPLICABLE"
            currentStateMessage: "NSG Rule source is not any address."
            check:
              NOT:
                arg:
                  OR:
                    args:
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__sourceAddressPrefix__c"
                          right:
                            TEXT: "Internet"
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__sourceAddressPrefix__c"
                          right:
                            TEXT: "*"
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__sourceAddressPrefix__c"
                          right:
                            TEXT: "0.0.0.0"
                      - IS_EQUAL:
                          left:
                            EXTRACT: "CA10__sourceAddressPrefix__c"
                          right:
                            TEXT: "Any"
                      - CONTAINS:
                          arg:
                            EXTRACT: "CA10__sourceAddressPrefix__c"
                          substring:
                            TEXT: "/0"
# sort group rules by ports. PORTS : *, -1, 0-65535
          - status: "INCOMPLIANT"
            currentStateMessage: "NSG Rule with ports with public access from the Internet."
            remediationMessage: "Restrict access to the public ports"
            check:
              OR:
                args:
                  - IS_EQUAL:
                      left:
                        EXTRACT: "CA10__destinationPortRange__c"
                      right:
                        TEXT: "-1"
                  - IS_EQUAL:
                      left:
                        EXTRACT: "CA10__destinationPortRange__c"
                      right:
                        TEXT: "*"
                  - AND:
                      args:
                        - IS_EQUAL:
                            left:
                              EXTRACT: "CA10__destinationPortFrom__c"
                            right:
                              NUMBER: 0.0
                        - IS_EQUAL:
                            left:
                              EXTRACT: "CA10__destinationPortTo__c"
                            right:
                              NUMBER: 65535.0
        otherwise:
          status: "COMPLIANT"
          currentStateMessage: "NSG rule without public access on any port"
