|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -e |
3 | | -trap 'echo "[ERROR] Failed during NSG creation." >&2' ERR |
| 3 | +trap 'echo "[ERROR] Failed during NSG creation or rule setup." >&2' ERR |
4 | 4 |
|
5 | 5 | SUBSCRIPTION_ID=$1 |
6 | 6 | RG=$2 |
7 | | -LOCATION=${3:-centraluseuap} |
| 7 | +LOCATION=$3 |
8 | 8 |
|
9 | 9 | VNET_A1="cx_vnet_a1" |
| 10 | +SUBNET1_PREFIX="10.10.1.0/24" |
| 11 | +SUBNET2_PREFIX="10.10.2.0/24" |
10 | 12 | NSG_NAME="${VNET_A1}-nsg" |
11 | 13 |
|
12 | 14 | echo "==> Creating Network Security Group: $NSG_NAME" |
13 | 15 | az network nsg create -g "$RG" -n "$NSG_NAME" -l "$LOCATION" --output none \ |
14 | | - && echo "NSG $NSG_NAME created." |
| 16 | + && echo "[OK] NSG '$NSG_NAME' created." |
15 | 17 |
|
16 | | -echo "==> Adding NSG rules" |
17 | | - |
18 | | -# Allow SSH from any |
| 18 | +echo "==> Creating NSG rule to DENY traffic from Subnet1 ($SUBNET1_PREFIX) to Subnet2 ($SUBNET2_PREFIX)" |
19 | 19 | az network nsg rule create \ |
20 | 20 | -g "$RG" \ |
21 | 21 | --nsg-name "$NSG_NAME" \ |
22 | | - -n allow-ssh \ |
| 22 | + -n deny-subnet1-to-subnet2 \ |
23 | 23 | --priority 100 \ |
24 | | - --source-address-prefixes "*" \ |
25 | | - --destination-port-ranges 22 \ |
| 24 | + --source-address-prefixes "$SUBNET1_PREFIX" \ |
| 25 | + --destination-address-prefixes "$SUBNET2_PREFIX" \ |
26 | 26 | --direction Inbound \ |
27 | | - --access Allow \ |
28 | | - --protocol Tcp \ |
29 | | - --description "Allow SSH access" \ |
| 27 | + --access Deny \ |
| 28 | + --protocol "*" \ |
| 29 | + --description "Deny all traffic from Subnet1 to Subnet2" \ |
30 | 30 | --output none \ |
31 | | - && echo "Rule allow-ssh created." |
| 31 | + && echo "[OK] Deny rule from Subnet1 → Subnet2 created." |
32 | 32 |
|
33 | | -# Allow internal VNet traffic |
| 33 | +echo "==> Creating NSG rule to DENY traffic from Subnet2 ($SUBNET2_PREFIX) to Subnet1 ($SUBNET1_PREFIX)" |
34 | 34 | az network nsg rule create \ |
35 | 35 | -g "$RG" \ |
36 | 36 | --nsg-name "$NSG_NAME" \ |
37 | | - -n allow-vnet \ |
| 37 | + -n deny-subnet2-to-subnet1 \ |
38 | 38 | --priority 200 \ |
39 | | - --source-address-prefixes VirtualNetwork \ |
40 | | - --destination-address-prefixes VirtualNetwork \ |
| 39 | + --source-address-prefixes "$SUBNET2_PREFIX" \ |
| 40 | + --destination-address-prefixes "$SUBNET1_PREFIX" \ |
41 | 41 | --direction Inbound \ |
42 | | - --access Allow \ |
| 42 | + --access Deny \ |
43 | 43 | --protocol "*" \ |
44 | | - --description "Allow VNet internal traffic" \ |
45 | | - --output none \ |
46 | | - && echo "Rule allow-vnet created." |
47 | | - |
48 | | -# Allow AKS API traffic |
49 | | -az network nsg rule create \ |
50 | | - -g "$RG" \ |
51 | | - --nsg-name "$NSG_NAME" \ |
52 | | - -n allow-aks-controlplane \ |
53 | | - --priority 300 \ |
54 | | - --source-address-prefixes AzureCloud \ |
55 | | - --destination-port-ranges 443 \ |
56 | | - --direction Inbound \ |
57 | | - --access Allow \ |
58 | | - --protocol Tcp \ |
59 | | - --description "Allow AKS control plane traffic" \ |
| 44 | + --description "Deny all traffic from Subnet2 to Subnet1" \ |
60 | 45 | --output none \ |
61 | | - && echo "Rule allow-aks-controlplane created." |
| 46 | + && echo "[OK] Deny rule from Subnet2 → Subnet1 created." |
62 | 47 |
|
63 | | -echo "NSG '$NSG_NAME' created successfully with rules." |
| 48 | +echo "NSG '$NSG_NAME' created successfully with bidirectional isolation between Subnet1 and Subnet2." |
0 commit comments