OnCallReadyStart learning

Azure Lesson 33.27 · Azure V: Compute, App Hosting & the AZ-104 exam · 23 min read

Secure access: NSGs with ASGs, effective rules, Bastion, service and private endpoints

In plain words

A network security group is a guard with a list: who may come in, to which door. Application security groups let the list say "web servers may talk to database servers" instead of listing every address - you just badge each machine as web or database.

Bastion is a guarded entrance where admins sign in through the browser, so no server needs a door to the internet. Service endpoints and private endpoints are two ways to reach Azure's own services, like storage, without going out onto the public street.

Secure access: NSGs with ASGs, effective rules, Bastion, service and private endpoints

Azure II taught NSG rules by IP range: evaluate by priority, first match wins, subnet NSG and NIC NSG both apply. Rules written with IP ranges rot - VMs are added and removed, IPs change, and six months later nobody knows what 10.95.1.0/27 was for. This lesson writes rules by role instead, with application security groups, reads the rules Azure actually applies to a NIC, and covers the two ways to stop exposing things to the internet at all: Azure Bastion for admin access, and service endpoints or private endpoints for PaaS services.

Need to know: an ASG is a label you attach to a NIC's IP configuration; an NSG rule can use ASGs as its source and/or destination instead of IP ranges (all in the same VNet). Effective security rules = the subnet NSG and the NIC NSG together, with service tags expanded (az network nic list-effective-nsg, VM must be running); IP flow verify (az network watcher test-ip-flow) gives the verdict and the deciding rule for one flow. Bastion needs a subnet named exactly AzureBastionSubnet, /26 or larger, and a Standard public IP; VMs then need no public IP; native client (az network bastion ssh) needs the Standard or Premium SKU with tunneling. A service endpoint extends the subnet's identity to a PaaS service over the backbone (the service's firewall allows the subnet; the service keeps its public IP); a private endpoint gives one resource a private IP in your subnet (and needs private DNS).

Rules by role: application security groups

$ az network vnet subnet show -g rg-az5-sec --vnet-name vnet-sec -n snet-app --query "{prefix:addressPrefix, nsg:networkSecurityGroup.id}" -o json
{
  "nsg": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-az5-sec/providers/Microsoft.Network/networkSecurityGroups/nsg-app",
  "prefix": "10.95.1.0/24"
}
$ az network asg list -g rg-az5-sec --query "[].name" -o tsv
asg-web
asg-db
$ az network nic show -g rg-az5-sec -n vm-web1VMNic --query "ipConfigurations[0].{ip:privateIPAddress, asgs:applicationSecurityGroups[].id}" -o json
{
  "asgs": [
    "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-az5-sec/providers/Microsoft.Network/applicationSecurityGroups/asg-web"
  ],
  "ip": "10.95.1.4"
}

vm-web1 carries asg-web, vm-db1 carries asg-db - both in one subnet, one NSG. The rule "web servers may talk to the database on 5432, nothing else in the VNet may" becomes two rules that never mention an address:

$ az network nsg rule create -g rg-az5-sec --nsg-name nsg-app -n allow-web-to-db --priority 200 --source-asgs asg-web --destination-asgs asg-db --destination-port-ranges 5432 --protocol Tcp --access Allow -o none
$ az network nsg rule create -g rg-az5-sec --nsg-name nsg-app -n deny-vnet-to-db --priority 300 --source-address-prefixes VirtualNetwork --destination-asgs asg-db --destination-port-ranges '*' --protocol '*' --access Deny -o none
$ az network nsg rule list -g rg-az5-sec --nsg-name nsg-app -o table
Name             ResourceGroup  Priority  SourcePortRanges  SourceAddressPrefixes  SourceASG  Access  Protocol  Direction  DestinationPortRanges  DestinationAddressPrefixes  DestinationASG
---------------  -------------  --------  ----------------  ---------------------  ---------  ------  --------  ---------  ---------------------  --------------------------  --------------
allow-web-to-db  rg-az5-sec     200       *                                        asg-web    Allow   Tcp       Inbound    5432                                               asg-db
deny-vnet-to-db  rg-az5-sec     300       *                 VirtualNetwork         None       Deny    *         Inbound    *                                                  asg-db

A tenth web server joins by getting asg-web on its NIC (az vm create --asgs, or az network nic ip-config update --application-security-groups), and the rules never change. The constraints:

  • all NICs in an ASG must be in the same VNet; a rule's source and destination ASGs too;
  • a rule takes ASGs or address prefixes on each side, not both;
  • an ASG says nothing on its own: without an NSG rule that names it, it allows and denies nothing.

The deny-vnet-to-db rule at 300 matters as much as the allow: without it, the default AllowVnetInBound (65000) would let every VM in the VNet - and every peered VNet - reach the database on any port.

What does Azure actually apply?

$ az network nic list-effective-nsg -g rg-az5-sec -n vm-db1VMNic --query "value[0].effectiveSecurityRules[?direction=='Inbound'].{rule:name, priority:priority, access:access, source:sourceAddressPrefix, ports:join(',', destinationPortRanges)}" -o table
Rule                                                Priority  Access  Source                    Ports
--------------------------------------------------  --------  ------  ------------------------  -----
securityRules/allow-web-to-db                       200       Allow   ApplicationSecurityGroup
securityRules/deny-vnet-to-db                       300       Deny    VirtualNetwork
defaultSecurityRules/AllowVnetInBound               65000     Allow   VirtualNetwork
defaultSecurityRules/AllowAzureLoadBalancerInBound  65001     Allow   AzureLoadBalancer
defaultSecurityRules/DenyAllInBound                 65500     Deny    *

Effective security rules merge every NSG that applies to the NIC (its subnet's and its own), in priority order, with ASGs and service tags in place (expandedSourceAddressPrefix in the JSON lists what VirtualNetwork or Internet means for this NIC). For one concrete flow, ask for the verdict:

$ az network watcher test-ip-flow -g rg-az5-sec --vm vm-db1 --direction Inbound --protocol TCP --local 10.95.1.5:5432 --remote 10.95.1.4:50000 -o table
Access  RuleName
------  -----------------------------
Allow   securityRules/allow-web-to-db
$ az network watcher test-ip-flow -g rg-az5-sec --vm vm-db1 --direction Inbound --protocol TCP --local 10.95.1.5:22 --remote 10.95.1.4:50000 -o table
Access  RuleName
------  -----------------------------
Deny    securityRules/deny-vnet-to-db

.4 is vm-web1 (in asg-web): 5432 is allowed by allow-web-to-db, SSH is denied by deny-vnet-to-db. When a subnet NSG and a NIC NSG exist, inbound traffic must pass the subnet NSG first, then the NIC NSG (outbound: NIC first, then subnet) - both must allow; IP flow verify reports whichever denied it.

Azure Bastion: admin access without public IPs

A VM with SSH or RDP open to the internet is scanned within minutes. Azure Bastion is a managed jump host in your VNet: you connect over TLS (the portal, or the native client with the CLI), Bastion connects to the VM's private IP. Requirements, all enforced:

$ az network vnet subnet create -g rg-az5-sec --vnet-name vnet-sec -n AzureBastionSubnet --address-prefixes 10.95.250.0/27 -o none
WARNING: (simulator) a /27 AzureBastionSubnet is accepted here, but Azure Bastion needs /26 or larger: "The subnet AzureBastionSubnet is too small. It must be /26 or larger."
$ az network public-ip create -g rg-az5-sec -n pip-bastion --sku Standard -o none
$ az network bastion create -g rg-az5-sec -n bas-sec --vnet-name vnet-sec --public-ip-address pip-bastion --sku Basic -l westeurope -o none
ERROR: (BadRequest) The subnet AzureBastionSubnet is too small. It must be /26 or larger.
Code: BadRequest
Message: The subnet AzureBastionSubnet is too small. It must be /26 or larger.
$ az network vnet subnet update -g rg-az5-sec --vnet-name vnet-sec -n AzureBastionSubnet --address-prefixes 10.95.250.0/26 -o none
$ az network bastion create -g rg-az5-sec -n bas-sec --vnet-name vnet-sec --public-ip-address pip-bastion --sku Basic -l westeurope -o none
$ az network bastion ssh -g rg-az5-sec -n bas-sec --target-resource-id $(az vm show -g rg-az5-sec -n vm-db1 --query id -o tsv) --auth-type ssh-key --username azureuser --ssh-key ~/.ssh/id_rsa
ERROR: Bastion Host SKU must be Standard or Premium and Native Client must be enabled.

The subnet must be named AzureBastionSubnet and be /26 or larger, hold nothing else, and the public IP must be Standard. Then the SKU decides what you get:

SKUfornotes
Developerdev/test, freeshared infrastructure, one VM at a time, no peered VNets
Basicportal SSH/RDPdedicated, fixed 2 instances
Standardproductionnative client (az network bastion ssh/rdp/tunnel), IP-based connection, shareable links, scale units, file transfer
Premiumregulated environmentsStandard + session recording, private-only deployment
$ az network bastion update -g rg-az5-sec -n bas-sec --sku Standard --enable-tunneling true -o none
$ az network bastion show -g rg-az5-sec -n bas-sec --query "{sku:sku.name, nativeClient:enableTunneling, scaleUnits:scaleUnits}" -o json
{
  "nativeClient": true,
  "scaleUnits": 2,
  "sku": "Standard"
}

With Standard and tunneling, az network bastion ssh ... opens a normal SSH session to vm-db1's private IP from your terminal. Bastion also reaches VMs in peered VNets, so one Bastion in the hub serves every spoke. The VM's NSG must allow 22/3389 from the Bastion subnet (VirtualNetwork covers it); the Bastion subnet itself may carry an NSG, but only with the rules Microsoft documents (443 in from the internet, the GatewayManager tag, outbound to the VNet on 22/3389) or Bastion breaks.

PaaS without the internet: service endpoints vs private endpoints

A storage account is a public endpoint (stsecaz5.blob.core.windows.net). Two ways to let only your VNet use it:

Service endpoints - a switch on the subnet per service (Microsoft.Storage, Microsoft.Sql, Microsoft.KeyVault ...). Traffic from that subnet to the service goes over the Azure backbone with the subnet's identity, and the service's firewall can allow that subnet:

$ az network vnet subnet update -g rg-az5-sec --vnet-name vnet-sec -n snet-app --service-endpoints Microsoft.Storage -o none
$ az network nic show-effective-route-table -g rg-az5-sec -n vm-web1VMNic --query "value[?nextHopType=='VirtualNetworkServiceEndpoint'].addressPrefix[0]" -o tsv
20.60.0.0/16
20.150.0.0/15
52.239.128.0/17
$ az storage account update -n stsecaz5 -g rg-az5-sec --default-action Deny -o none
$ az storage account network-rule add -n stsecaz5 -g rg-az5-sec --vnet-name vnet-sec --subnet snet-app -o none
$ az storage account network-rule list -n stsecaz5 -g rg-az5-sec --query "virtualNetworkRules[].virtualNetworkResourceId" -o tsv
/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-az5-sec/providers/Microsoft.Network/virtualNetworks/vnet-sec/subnets/snet-app
$ az storage container list --account-name stsecaz5 --auth-mode key -o tsv 2>&1 | tail -1
ErrorCode:AuthorizationFailure

The subnet's effective routes gained the storage prefixes with next hop VirtualNetworkServiceEndpoint; the account now admits that subnet and denies everyone else - this box included (AuthorizationFailure). What a service endpoint is not: the account keeps its public IP and public DNS name, it works only from that subnet (not from on-premises or from a peered VNet's other subnets), and it allows the whole service for that subnet - any storage account, yours or an attacker's - unless a service endpoint policy restricts it. It is free.

Private endpoints (Azure II: az.pe) - a NIC with a private IP in your subnet for one resource. The name resolves to the private IP through a privatelink.* private DNS zone; it works from peered VNets and from on-premises over VPN/ExpressRoute; public access on the resource can be turned off entirely. It costs per hour and per GB.

service endpointprivate endpoint
configured onthe subnet (per service)the resource (one NIC per resource)
service's addressstays publica private IP in your VNet
reachable fromthat subnet onlythe VNet, peered VNets, on-premises
scopeevery resource of the service (unless a policy)exactly one resource
DNS changesnoneprivatelink private DNS zone
costfreeper hour + per GB

On the exam (AZ-104 4.2.1-4.2.5): "group VMs by role in NSG rules" = ASGs (same VNet); "which rule applies" = effective security rules / IP flow verify; "RDP/SSH without public IPs" = Bastion in AzureBastionSubnet /26+ with a Standard public IP; "connect from the local CLI" = Bastion Standard + native client; "restrict a storage account to a subnet, free" = service endpoint + storage network rule; "private IP for the service, reachable from on-premises" = private endpoint.

In an interview: "Service endpoint or private endpoint?" - "Service endpoint when it's enough to say 'this subnet may use this service' and the traffic only comes from that subnet - it's free and needs no DNS changes. Private endpoint when the service must have a private IP, be reachable from peered VNets or on-premises, or have public access switched off entirely."

Why it helps

Least-privilege networking is a whole AZ-104 skill group and a constant source of production tickets. ASG rules that never matched, a deny that came after an allow, an NSG on the subnet and another on the NIC, a VM with a public IP for SSH "just for now".

The lesson teaches you to ask Azure what it applies (effective rules, IP flow verify) rather than reading rules by eye, and to choose between a service endpoint and a private endpoint for a reason.

Commands in this lesson

az

FAQ

How are two NSGs combined?

Inbound traffic is checked by the subnet's NSG first, then the NIC's NSG; outbound in the reverse order. Both must allow. Inside each NSG the rule with the lowest priority number that matches wins, and the default rules (65000 and up) decide when nothing else matches.

What are the limits of ASGs?

All NICs in an ASG must be in the same VNet, and a rule's source and destination ASGs must be in the same VNet as each other. A rule side is either ASGs or address prefixes and service tags, not both. A NIC can be in several ASGs.

What does Bastion need?

A subnet named AzureBastionSubnet, /26 or larger, and (for Basic, Standard and Premium) a Standard public IP. VMs then need no public IP; their NSG allows 22 or 3389 from the Bastion subnet. Standard adds native client support and file transfer, Premium session recording.

What does a service endpoint change?

The subnet's traffic to that service goes over the Azure backbone with the subnet's identity (route VirtualNetworkServiceEndpoint), so the service's firewall can allow that subnet. The service keeps its public IP and public DNS name. It is free and needs no DNS changes.

What does a private endpoint change?

It puts a network interface with a private IP from your subnet in front of one resource. With a private DNS zone (privatelink...), the service's name resolves to that IP. Public access can then be turned off, and the IP is reachable from peered VNets and on-premises.

In an interview Mid

Service endpoint or private endpoint - how do you choose?

A service endpoint when it is enough to say "this subnet may use this service" and the traffic only comes from that subnet - it is free and needs no DNS changes. A private endpoint when the service must have a private IP, be reachable from peered VNets or on-premises, or have public access switched off entirely - with a private DNS zone so the name resolves to it.

Also asked: How would you find which NSG rule blocks traffic to a VM? · What are application security groups used for? · How do you give admins SSH access to VMs that have no public IP?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.