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:
| SKU | for | notes |
|---|---|---|
| Developer | dev/test, free | shared infrastructure, one VM at a time, no peered VNets |
| Basic | portal SSH/RDP | dedicated, fixed 2 instances |
| Standard | production | native client (az network bastion ssh/rdp/tunnel), IP-based connection, shareable links, scale units, file transfer |
| Premium | regulated environments | Standard + 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 endpoint | private endpoint | |
|---|---|---|
| configured on | the subnet (per service) | the resource (one NIC per resource) |
| service's address | stays public | a private IP in your VNet |
| reachable from | that subnet only | the VNet, peered VNets, on-premises |
| scope | every resource of the service (unless a policy) | exactly one resource |
| DNS changes | none | privatelink private DNS zone |
| cost | free | per 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."