Skip to content

Commit 26934ee

Browse files
committed
Address PR feedback
Make helper methods for easily adding rules to subnets. Make NSG a top-level resource.
1 parent 23e222e commit 26934ee

File tree

23 files changed

+1110
-654
lines changed

23 files changed

+1110
-654
lines changed

playground/AzureVirtualNetworkEndToEnd/AzureVirtualNetworkEndToEnd.AppHost/Program.cs

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#pragma warning disable AZPROVISION001 // Azure.Provisioning.Network is experimental
55

6-
using Aspire.Hosting.Azure;
76
using Azure.Provisioning.Network;
87

98
var builder = DistributedApplication.CreateBuilder(args);
@@ -13,80 +12,18 @@
1312
// - One for private endpoints
1413
var vnet = builder.AddAzureVirtualNetwork("vnet");
1514

16-
var containerAppsSubnet = vnet.AddSubnet("container-apps", "10.0.0.0/23");
17-
var privateEndpointsSubnet = vnet.AddSubnet("private-endpoints", "10.0.2.0/27");
15+
var containerAppsSubnet = vnet.AddSubnet("container-apps", "10.0.0.0/23")
16+
.AllowInbound(port: "443", from: "AzureLoadBalancer", protocol: SecurityRuleProtocol.Tcp)
17+
.DenyInbound(from: "VirtualNetwork")
18+
.DenyInbound(from: "Internet");
1819

1920
// Create a NAT Gateway for deterministic outbound IP on the ACA subnet
2021
var natGateway = builder.AddNatGateway("nat");
2122
containerAppsSubnet.WithNatGateway(natGateway);
2223

23-
// Create Network Security Groups for each subnet
24-
var acaNsg = vnet.AddNetworkSecurityGroup("aca-nsg")
25-
.WithSecurityRule(new AzureSecurityRule
26-
{
27-
Name = "allow-https-from-azure-lb",
28-
Priority = 100,
29-
Direction = SecurityRuleDirection.Inbound,
30-
Access = SecurityRuleAccess.Allow,
31-
Protocol = SecurityRuleProtocol.Tcp,
32-
SourceAddressPrefix = "AzureLoadBalancer",
33-
SourcePortRange = "*",
34-
DestinationAddressPrefix = "*",
35-
DestinationPortRange = "443"
36-
})
37-
.WithSecurityRule(new AzureSecurityRule
38-
{
39-
Name = "deny-vnet-inbound",
40-
Priority = 110,
41-
Direction = SecurityRuleDirection.Inbound,
42-
Access = SecurityRuleAccess.Deny,
43-
Protocol = SecurityRuleProtocol.Asterisk,
44-
SourceAddressPrefix = "VirtualNetwork",
45-
SourcePortRange = "*",
46-
DestinationAddressPrefix = "*",
47-
DestinationPortRange = "*"
48-
})
49-
.WithSecurityRule(new AzureSecurityRule
50-
{
51-
Name = "deny-internet-inbound",
52-
Priority = 4096,
53-
Direction = SecurityRuleDirection.Inbound,
54-
Access = SecurityRuleAccess.Deny,
55-
Protocol = SecurityRuleProtocol.Asterisk,
56-
SourceAddressPrefix = "Internet",
57-
SourcePortRange = "*",
58-
DestinationAddressPrefix = "*",
59-
DestinationPortRange = "*"
60-
});
61-
62-
var peNsg = vnet.AddNetworkSecurityGroup("pe-nsg")
63-
.WithSecurityRule(new AzureSecurityRule
64-
{
65-
Name = "allow-https-from-vnet",
66-
Priority = 100,
67-
Direction = SecurityRuleDirection.Inbound,
68-
Access = SecurityRuleAccess.Allow,
69-
Protocol = SecurityRuleProtocol.Tcp,
70-
SourceAddressPrefix = "VirtualNetwork",
71-
SourcePortRange = "*",
72-
DestinationAddressPrefix = "*",
73-
DestinationPortRange = "443"
74-
})
75-
.WithSecurityRule(new AzureSecurityRule
76-
{
77-
Name = "deny-all-internet-inbound",
78-
Priority = 4096,
79-
Direction = SecurityRuleDirection.Inbound,
80-
Access = SecurityRuleAccess.Deny,
81-
Protocol = SecurityRuleProtocol.Asterisk,
82-
SourceAddressPrefix = "Internet",
83-
SourcePortRange = "*",
84-
DestinationAddressPrefix = "*",
85-
DestinationPortRange = "*"
86-
});
87-
88-
containerAppsSubnet.WithNetworkSecurityGroup(acaNsg);
89-
privateEndpointsSubnet.WithNetworkSecurityGroup(peNsg);
24+
var privateEndpointsSubnet = vnet.AddSubnet("private-endpoints", "10.0.2.0/27")
25+
.AllowInbound(port: "443", from: "VirtualNetwork", protocol: SecurityRuleProtocol.Tcp)
26+
.DenyInbound(from: "Internet");
9027

9128
// Configure the Container App Environment to use the VNet
9229
builder.AddAzureContainerAppEnvironment("env")

playground/AzureVirtualNetworkEndToEnd/AzureVirtualNetworkEndToEnd.AppHost/aspire-manifest.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
"type": "azure.bicep.v0",
66
"path": "vnet.module.bicep",
77
"params": {
8-
"nat_outputs_id": "{nat.outputs.id}"
8+
"nat_outputs_id": "{nat.outputs.id}",
9+
"container_apps_nsg_outputs_id": "{container-apps-nsg.outputs.id}",
10+
"private_endpoints_nsg_outputs_id": "{private-endpoints-nsg.outputs.id}"
911
}
1012
},
13+
"container-apps-nsg": {
14+
"type": "azure.bicep.v0",
15+
"path": "container-apps-nsg.module.bicep"
16+
},
1117
"nat": {
1218
"type": "azure.bicep.v0",
1319
"path": "nat.module.bicep"
1420
},
21+
"private-endpoints-nsg": {
22+
"type": "azure.bicep.v0",
23+
"path": "private-endpoints-nsg.module.bicep"
24+
},
1525
"env-acr": {
1626
"type": "azure.bicep.v0",
1727
"path": "env-acr.module.bicep"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
@description('The location for the resource(s) to be deployed.')
2+
param location string = resourceGroup().location
3+
4+
resource container_apps_nsg_allow_inbound_443_AzureLoadBalancer 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
5+
name: 'allow-inbound-443-AzureLoadBalancer'
6+
properties: {
7+
access: 'Allow'
8+
destinationAddressPrefix: '*'
9+
destinationPortRange: '443'
10+
direction: 'Inbound'
11+
priority: 100
12+
protocol: 'Tcp'
13+
sourceAddressPrefix: 'AzureLoadBalancer'
14+
sourcePortRange: '*'
15+
}
16+
parent: container_apps_nsg
17+
}
18+
19+
resource container_apps_nsg_deny_inbound_VirtualNetwork 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
20+
name: 'deny-inbound-VirtualNetwork'
21+
properties: {
22+
access: 'Deny'
23+
destinationAddressPrefix: '*'
24+
destinationPortRange: '*'
25+
direction: 'Inbound'
26+
priority: 200
27+
protocol: '*'
28+
sourceAddressPrefix: 'VirtualNetwork'
29+
sourcePortRange: '*'
30+
}
31+
parent: container_apps_nsg
32+
}
33+
34+
resource container_apps_nsg_deny_inbound_Internet 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
35+
name: 'deny-inbound-Internet'
36+
properties: {
37+
access: 'Deny'
38+
destinationAddressPrefix: '*'
39+
destinationPortRange: '*'
40+
direction: 'Inbound'
41+
priority: 300
42+
protocol: '*'
43+
sourceAddressPrefix: 'Internet'
44+
sourcePortRange: '*'
45+
}
46+
parent: container_apps_nsg
47+
}
48+
49+
resource container_apps_nsg 'Microsoft.Network/networkSecurityGroups@2025-05-01' = {
50+
name: take('container_apps_nsg-${uniqueString(resourceGroup().id)}', 80)
51+
location: location
52+
tags: {
53+
'aspire-resource-name': 'container-apps-nsg'
54+
}
55+
}
56+
57+
output id string = container_apps_nsg.id
58+
59+
output name string = container_apps_nsg.name
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@description('The location for the resource(s) to be deployed.')
2+
param location string = resourceGroup().location
3+
4+
resource private_endpoints_nsg_allow_inbound_443_VirtualNetwork 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
5+
name: 'allow-inbound-443-VirtualNetwork'
6+
properties: {
7+
access: 'Allow'
8+
destinationAddressPrefix: '*'
9+
destinationPortRange: '443'
10+
direction: 'Inbound'
11+
priority: 100
12+
protocol: 'Tcp'
13+
sourceAddressPrefix: 'VirtualNetwork'
14+
sourcePortRange: '*'
15+
}
16+
parent: private_endpoints_nsg
17+
}
18+
19+
resource private_endpoints_nsg_deny_inbound_Internet 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
20+
name: 'deny-inbound-Internet'
21+
properties: {
22+
access: 'Deny'
23+
destinationAddressPrefix: '*'
24+
destinationPortRange: '*'
25+
direction: 'Inbound'
26+
priority: 200
27+
protocol: '*'
28+
sourceAddressPrefix: 'Internet'
29+
sourcePortRange: '*'
30+
}
31+
parent: private_endpoints_nsg
32+
}
33+
34+
resource private_endpoints_nsg 'Microsoft.Network/networkSecurityGroups@2025-05-01' = {
35+
name: take('private_endpoints_nsg-${uniqueString(resourceGroup().id)}', 80)
36+
location: location
37+
tags: {
38+
'aspire-resource-name': 'private-endpoints-nsg'
39+
}
40+
}
41+
42+
output id string = private_endpoints_nsg.id
43+
44+
output name string = private_endpoints_nsg.name

playground/AzureVirtualNetworkEndToEnd/AzureVirtualNetworkEndToEnd.AppHost/vnet.module.bicep

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ param location string = resourceGroup().location
33

44
param nat_outputs_id string
55

6+
param container_apps_nsg_outputs_id string
7+
8+
param private_endpoints_nsg_outputs_id string
9+
610
resource vnet 'Microsoft.Network/virtualNetworks@2025-05-01' = {
711
name: take('vnet-${uniqueString(resourceGroup().id)}', 64)
812
properties: {
@@ -18,91 +22,6 @@ resource vnet 'Microsoft.Network/virtualNetworks@2025-05-01' = {
1822
}
1923
}
2024

21-
resource aca_nsg 'Microsoft.Network/networkSecurityGroups@2025-05-01' = {
22-
name: take('aca_nsg-${uniqueString(resourceGroup().id)}', 80)
23-
location: location
24-
}
25-
26-
resource aca_nsg_allow_https_from_azure_lb 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
27-
name: 'allow-https-from-azure-lb'
28-
properties: {
29-
access: 'Allow'
30-
destinationAddressPrefix: '*'
31-
destinationPortRange: '443'
32-
direction: 'Inbound'
33-
priority: 100
34-
protocol: 'Tcp'
35-
sourceAddressPrefix: 'AzureLoadBalancer'
36-
sourcePortRange: '*'
37-
}
38-
parent: aca_nsg
39-
}
40-
41-
resource aca_nsg_deny_vnet_inbound 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
42-
name: 'deny-vnet-inbound'
43-
properties: {
44-
access: 'Deny'
45-
destinationAddressPrefix: '*'
46-
destinationPortRange: '*'
47-
direction: 'Inbound'
48-
priority: 110
49-
protocol: '*'
50-
sourceAddressPrefix: 'VirtualNetwork'
51-
sourcePortRange: '*'
52-
}
53-
parent: aca_nsg
54-
}
55-
56-
resource aca_nsg_deny_internet_inbound 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
57-
name: 'deny-internet-inbound'
58-
properties: {
59-
access: 'Deny'
60-
destinationAddressPrefix: '*'
61-
destinationPortRange: '*'
62-
direction: 'Inbound'
63-
priority: 4096
64-
protocol: '*'
65-
sourceAddressPrefix: 'Internet'
66-
sourcePortRange: '*'
67-
}
68-
parent: aca_nsg
69-
}
70-
71-
resource pe_nsg 'Microsoft.Network/networkSecurityGroups@2025-05-01' = {
72-
name: take('pe_nsg-${uniqueString(resourceGroup().id)}', 80)
73-
location: location
74-
}
75-
76-
resource pe_nsg_allow_https_from_vnet 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
77-
name: 'allow-https-from-vnet'
78-
properties: {
79-
access: 'Allow'
80-
destinationAddressPrefix: '*'
81-
destinationPortRange: '443'
82-
direction: 'Inbound'
83-
priority: 100
84-
protocol: 'Tcp'
85-
sourceAddressPrefix: 'VirtualNetwork'
86-
sourcePortRange: '*'
87-
}
88-
parent: pe_nsg
89-
}
90-
91-
resource pe_nsg_deny_all_internet_inbound 'Microsoft.Network/networkSecurityGroups/securityRules@2025-05-01' = {
92-
name: 'deny-all-internet-inbound'
93-
properties: {
94-
access: 'Deny'
95-
destinationAddressPrefix: '*'
96-
destinationPortRange: '*'
97-
direction: 'Inbound'
98-
priority: 4096
99-
protocol: '*'
100-
sourceAddressPrefix: 'Internet'
101-
sourcePortRange: '*'
102-
}
103-
parent: pe_nsg
104-
}
105-
10625
resource container_apps 'Microsoft.Network/virtualNetworks/subnets@2025-05-01' = {
10726
name: 'container-apps'
10827
properties: {
@@ -119,7 +38,7 @@ resource container_apps 'Microsoft.Network/virtualNetworks/subnets@2025-05-01' =
11938
id: nat_outputs_id
12039
}
12140
networkSecurityGroup: {
122-
id: aca_nsg.id
41+
id: container_apps_nsg_outputs_id
12342
}
12443
}
12544
parent: vnet
@@ -130,7 +49,7 @@ resource private_endpoints 'Microsoft.Network/virtualNetworks/subnets@2025-05-01
13049
properties: {
13150
addressPrefix: '10.0.2.0/27'
13251
networkSecurityGroup: {
133-
id: pe_nsg.id
52+
id: private_endpoints_nsg_outputs_id
13453
}
13554
}
13655
parent: vnet

0 commit comments

Comments
 (0)