Skip to content
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

[BUG] Azure.VNET.UseNGSs is not handling cases correctly when subnets are declared outside the parent #3007

Closed
BenjaminEngeset opened this issue Aug 4, 2024 · 0 comments · Fixed by #3008
Assignees
Labels
bug Something isn't working rule: network Rules for virtual networking

Comments

@BenjaminEngeset
Copy link
Contributor

Existing rule

Azure.VNET.UseNGSs

Description of the issue

There is a small subset of special purpose subnets that do not support NSGs. Currently the rule takes this into consideration, however the handling is not working correctly for child resources declared outside the parent resource because the name is then declared as {parent-resource-name}/{child-resource-name}.

I assume PSRule does not handle this when the parent resource is not to be found within the same deployment.

It's a low effort to handle this in the rule body.

# Synopsis: Virtual network (VNET) subnets should have Network Security Groups (NSGs) assigned.
Rule 'Azure.VNET.UseNSGs' -Ref 'AZR-000263' -Type 'Microsoft.Network/virtualNetworks', 'Microsoft.Network/virtualNetworks/subnets' -Tag @{ release = 'GA'; ruleSet = '2020_06'; 'Azure.WAF/pillar' = 'Security'; 'Azure.MCSB.v1/control' = 'NS-1' } {
$excludedSubnets = @('GatewaySubnet', 'AzureFirewallSubnet', 'AzureFirewallManagementSubnet', 'RouteServerSubnet');
$customExcludedSubnets = $Configuration.GetStringValues('AZURE_VNET_SUBNET_EXCLUDED_FROM_NSG');
$subnet = @($TargetObject);
if ($PSRule.TargetType -eq 'Microsoft.Network/virtualNetworks') {
# Get subnets
$subnet = @($TargetObject.properties.subnets | Where-Object {
$_.Name -notin $excludedSubnets -and $_.Name -notin $customExcludedSubnets -and @($_.properties.delegations | Where-Object { $_.properties.serviceName -eq 'Microsoft.HardwareSecurityModules/dedicatedHSMs' }).Length -eq 0
});
if ($subnet.Length -eq 0 -or !$Assert.HasFieldValue($TargetObject, 'properties.subnets').Result) {
return $Assert.Pass();
}
}
elseif ($PSRule.TargetType -eq 'Microsoft.Network/virtualNetworks/subnets' -and
($PSRule.TargetName -in $excludedSubnets -or $PSRule.TargetName -in $customExcludedSubnets -or @($TargetObject.properties.delegations | Where-Object { $_.properties.serviceName -eq 'Microsoft.HardwareSecurityModules/dedicatedHSMs' }).Length -gt 0)) {
return $Assert.Pass();
}
foreach ($sn in $subnet) {
$Assert.
HasFieldValue($sn, 'properties.networkSecurityGroup.id').
WithReason(($LocalizedData.SubnetNSGNotConfigured -f $sn.Name), $True);
}
}

Error messages

No response

Reproduction

{
    "ResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Network/vnet-J/subnets/AzureFirewallSubnet",
    "Id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Network/vnet-J/subnets/AzureFirewallSubnet",
    "Identity": null,
    "Kind": null,
    "Location": "region",
    "ResourceName": "vnet-J/AzureFirewallSubnet",
    "Name": "vnet-J/AzureFirewallSubnet",
    "Plan": null,
    "Properties": {
      "addressPrefix": "10.7.0.0/26",
      "routeTable": {
        "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Network/routeTables/route-A"
      },
      "serviceEndpoints": [],
      "delegations": [],
      "natGateway": {
        "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Network/natGateways/gateway-A"
      }
    },
    "ResourceGroupName": "test-rg",
    "Type": "Microsoft.Network/virtualNetworks/subnets",
    "ResourceType": "Microsoft.Network/virtualNetworks/subnets",
    "ExtensionResourceType": null,
    "Sku": null,
    "Tags": null,
    "SubscriptionId": "00000000-0000-0000-0000-000000000000"
  }

Version of PSRule

2.9.0

Version of PSRule for Azure

No response

Additional context

No response

@BenjaminEngeset BenjaminEngeset added bug Something isn't working Needs: Triage 🔍 labels Aug 4, 2024
@BenjaminEngeset BenjaminEngeset changed the title [BUG] Azure.VNET.UseNGSs not handling cases correctly when subnets declared outside the parent [BUG] Azure.VNET.UseNGSs not handling cases correctly when subnets are declared outside the parent Aug 4, 2024
@BenjaminEngeset BenjaminEngeset changed the title [BUG] Azure.VNET.UseNGSs not handling cases correctly when subnets are declared outside the parent [BUG] Azure.VNET.UseNGSs is not handling cases correctly when subnets are declared outside the parent Aug 4, 2024
@BernieWhite BernieWhite added rule: network Rules for virtual networking and removed Needs: Triage 🔍 labels Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule: network Rules for virtual networking
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants