Skip to content

Commit

Permalink
Fixes Azure.Resource.AllowedRegions #2461 (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 5, 2023
1 parent d62dfec commit b88b914
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since v1.30.0:

- Bug fixes:
- Fixed `Azure.Resource.AllowedRegions` which was failing when no allowed regions were configured by @BernieWhite.
[#2461](https://github.com/Azure/PSRule.Rules.Azure/issues/2461)

## v1.30.0

What's changed since v1.29.0:
Expand Down
2 changes: 0 additions & 2 deletions src/PSRule.Rules.Azure/Data/Template/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Newtonsoft.Json.Linq;
using PSRule.Rules.Azure.Resources;
Expand Down
1 change: 0 additions & 1 deletion src/PSRule.Rules.Azure/Data/Template/ExpressionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Text;
using System.Threading;
using Newtonsoft.Json.Linq;
using PSRule.Rules.Azure.Resources;

namespace PSRule.Rules.Azure.Data.Template
{
Expand Down
8 changes: 7 additions & 1 deletion src/PSRule.Rules.Azure/Runtime/RuntimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,20 @@ public void WithAllowedLocations(string[] locations)
if (locations == null || locations.Length == 0)
return;

_AllowedLocations = new HashSet<string>(locations, LocationHelper.Comparer);
_AllowedLocations = new HashSet<string>(LocationHelper.Comparer);
for (var i = 0; i < locations.Length; i++)
{
if (!string.IsNullOrEmpty(locations[i]))
_AllowedLocations.Add(locations[i]);
}
}

/// <inheritdoc/>
public bool IsAllowedLocation(string location)
{
return location == null ||
_AllowedLocations == null ||
_AllowedLocations.Count == 0 ||
_AllowedLocations.Contains(location);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PSRule.Rules.Azure/rules/Conventions.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Export-PSRuleConvention 'Azure.Context' -Initialize {
$minimum = $Configuration.GetValueOrDefault('AZURE_BICEP_MINIMUM_VERSION', '0.4.451');
$timeout = $Configuration.GetIntegerOrDefault('AZURE_BICEP_FILE_EXPANSION_TIMEOUT', 5);
$check = $Configuration.GetBoolOrDefault('AZURE_BICEP_CHECK_TOOL', $False);
$allowedRegions = @($Configuration.GetValueOrDefault('Azure_AllowedRegions', $Configuration.AZURE_RESOURCE_ALLOWED_LOCATIONS));
$allowedRegions = @($Configuration.GetValueOrDefault('Azure_AllowedRegions', $Configuration.GetStringValues('AZURE_RESOURCE_ALLOWED_LOCATIONS')));
$service = [PSRule.Rules.Azure.Runtime.Helper]::CreateService($minimum, $timeout);

if ($allowedRegions.Length -gt 0) {
Expand Down
28 changes: 27 additions & 1 deletion tests/PSRule.Rules.Azure.Tests/RuntimeHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Management.Automation;
Expand Down Expand Up @@ -51,6 +51,30 @@ public void GetNetworkSecurityGroup()
Assert.True(eval.Outbound("virtualNetwork", 80) == Data.Network.Access.Allow);
}

[Fact]
public void CreateService()
{
var service = Helper.CreateService("1.0.0", 90);
Assert.NotNull(service);
Assert.Equal("1.0.0", service.Minimum);
Assert.Equal(90, service.Timeout);

service.WithAllowedLocations(null);
Assert.True(service.IsAllowedLocation("eastus"));

service.WithAllowedLocations(System.Array.Empty<string>());
Assert.True(service.IsAllowedLocation("eastus"));

service.WithAllowedLocations(new string[] { "westus" });
Assert.True(service.IsAllowedLocation("westus"));
Assert.False(service.IsAllowedLocation("eastus"));

service.WithAllowedLocations(new string[] { "" });
Assert.True(service.IsAllowedLocation("eastus"));
}

#region Helper methods

private static PSObject NewSecurityRule(int priority, string access, string direction, string destinationAddressPrefix = null, string destinationPortRange = null)
{
var result = new PSObject();
Expand All @@ -68,5 +92,7 @@ private static PSObject NewSecurityRule(int priority, string access, string dire
result.Properties.Add(new PSNoteProperty("properties", properties));
return result;
}

#endregion Helper methods
}
}

0 comments on commit b88b914

Please sign in to comment.