-
Notifications
You must be signed in to change notification settings - Fork 800
Add Network Security Group (NSG) support for Azure Virtual Networks #14383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14383Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14383" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds NSG modeling to the Azure Network hosting integration so AppHost authors can define Network Security Groups with rules and associate them to subnets, resulting in correct NSG + rule + subnet references in generated Bicep.
Changes:
- Introduces
AzureNetworkSecurityGroupResourceandAzureSecurityRule, plus builder APIs to add NSGs, add rules, and associate NSGs with subnets. - Updates VNet provisioning to emit NSGs (and their rules) before subnets and to reference NSGs by resource id from subnets.
- Adds unit tests + verified Bicep snapshots and updates the playground end-to-end sample to demonstrate NSG usage.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs | Adds builder APIs and updates VNet provisioning to emit NSGs/rules and pass NSG mapping into subnet generation. |
| src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkResource.cs | Tracks NSGs on the VNet model. |
| src/Aspire.Hosting.Azure.Network/AzureSubnetResource.cs | Adds subnet-to-NSG association support during provisioning. |
| src/Aspire.Hosting.Azure.Network/AzureNetworkSecurityGroupResource.cs | New resource type representing an NSG and converting it (and rules) to provisioning entities. |
| src/Aspire.Hosting.Azure.Network/AzureSecurityRule.cs | New public configuration type for NSG rule definition. |
| src/Aspire.Hosting.Azure.Network/README.md | Documents NSG usage with rules and subnet association. |
| tests/Aspire.Hosting.Azure.Tests/AzureVirtualNetworkExtensionsTests.cs | Adds unit tests for NSG creation, rule handling, subnet association, and Bicep generation. |
| tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureVirtualNetworkExtensionsTests.AddNetworkSecurityGroup_GeneratesCorrectBicep.verified.bicep | Snapshot for NSG-only generation. |
| tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureVirtualNetworkExtensionsTests.AddNetworkSecurityGroup_WithSecurityRules_GeneratesCorrectBicep.verified.bicep | Snapshot for NSG + multiple rules generation. |
| tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureVirtualNetworkExtensionsTests.AddSubnet_WithNetworkSecurityGroup_GeneratesCorrectBicep.verified.bicep | Snapshot for subnet referencing NSG by id. |
| tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureVirtualNetworkExtensionsTests.AddNetworkSecurityGroup_SharedAcrossSubnets_GeneratesCorrectBicep.verified.bicep | Snapshot for a shared NSG across multiple subnets. |
| playground/AzureVirtualNetworkEndToEnd/AzureVirtualNetworkEndToEnd.AppHost/Program.cs | Updates the playground AppHost to create NSGs, add rules, and attach them to subnets. |
| playground/AzureVirtualNetworkEndToEnd/AzureVirtualNetworkEndToEnd.AppHost/vnet.module.bicep | Updates the sample module to include NSGs, rules, and subnet NSG references. |
src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs
Outdated
Show resolved
Hide resolved
tests/Aspire.Hosting.Azure.Tests/AzureVirtualNetworkExtensionsTests.cs
Outdated
Show resolved
Hide resolved
|
This looks...complex. |
|
The Higher-level subnet helpersThe rule API is quite verbose for common patterns. Have we thought about higher-level helpers on subnets that generate the right rules underneath? Something like: // Block all outbound internet from this subnet
subnet.WithInternetAccess(false);
// Allow inbound HTTPS from another subnet
subnet.AllowInboundFrom(otherSubnet, port: 443);
// Allow inbound from a service tag
subnet.AllowInboundFrom("AzureLoadBalancer", port: 443);
// Full isolation — deny all except Azure infrastructure traffic
subnet.WithNetworkIsolation();These would auto-create an NSG if the subnet doesn't have one, generate the rules, and handle priority numbering. The verbose API stays for custom/compliance rules — this would just be sugar on top. Why
|
Adds the ability to create NSGs with security rules and associate them with subnets, enabling fine-grained network traffic control for Azure resources deployed into VNets. New APIs: - AddNetworkSecurityGroup() on IResourceBuilder<AzureVirtualNetworkResource> - WithSecurityRule() on IResourceBuilder<AzureNetworkSecurityGroupResource> - WithNetworkSecurityGroup() on IResourceBuilder<AzureSubnetResource> New types: - AzureNetworkSecurityGroupResource (child of VNet) - AzureSecurityRule (public data class for rule configuration) Key behaviors: - NSGs are created before subnets in bicep for correct dependency ordering - Security rule bicep identifiers are prefixed with NSG name to avoid duplicate symbolic names across multiple NSGs - A single NSG can be shared across multiple subnets - Duplicate rule names within an NSG are rejected with ArgumentException - Subnets reference NSGs via id (not inline properties) in generated bicep
Make helper methods for easily adding rules to subnets. Make NSG a top-level resource.
Security rules added via WithSecurityRule are now applied after CreateExistingOrNewProvisionableResource returns, so they apply to both existing and new NSGs. Previously, rules were only added inside the new-resource callback. Added test verifying security rules work alongside ConfigureInfrastructure customizations.
Description
Adds Network Security Group (NSG) support for Azure Virtual Networks, enabling fine-grained network traffic control for subnets. Includes both a shorthand API for the common case and an explicit API for full control.
Shorthand API (recommended for most users)
Fluent methods on subnet builders that auto-create an NSG, auto-increment priority, and auto-generate rule names:
Explicit API (for full control)
Create standalone NSG resources with explicit
AzureSecurityRuleobjects:New public APIs
Types:
AzureNetworkSecurityGroupResource— standaloneAzureProvisioningResourcewith its own bicep module,IdandNameOutputoutputs, andAddAsExistingResourcesupportAzureSecurityRule— data class for rule configuration.SourcePortRange,SourceAddressPrefix, andDestinationAddressPrefixdefault to"*"to reduce verbosityExtension methods on
IDistributedApplicationBuilder:AddNetworkSecurityGroup(name)— creates a top-level NSG resourceExtension methods on
IResourceBuilder<AzureNetworkSecurityGroupResource>:WithSecurityRule(rule)— adds a security rule (rejects duplicate names)Extension methods on
IResourceBuilder<AzureSubnetResource>:WithNetworkSecurityGroup(nsg)— associates an explicit NSG with a subnetAllowInbound(port, from, to, protocol, priority, name)— shorthand allow inbound ruleDenyInbound(...)— shorthand deny inbound ruleAllowOutbound(...)— shorthand allow outbound ruleDenyOutbound(...)— shorthand deny outbound ruleKey design decisions
AzureProvisioningResource— generates its own bicep module (not inline in the VNet module). Subnets reference the NSG via cross-module parameter (param nsg_outputs_id string){subnet}-nsgwhen no NSG is assigned. CallingWithNetworkSecurityGroupafter shorthand methods throwsInvalidOperationExceptionto prevent silent rule lossallow-inbound-443-AzureLoadBalancer)AzureSecurityRule—SourcePortRange,SourceAddressPrefix,DestinationAddressPrefixall default to"*", reducing the common 10-line rule to ~5 required propertiesArgumentExceptionContributes to #13750
Checklist
<remarks>and<code/>elements on your triple slash comments?aspire.devissue: