Skip to content

Commit

Permalink
Fixed Azure field binding Azure#2460 (Azure#2471)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 7, 2023
1 parent 4b2297f commit 971a625
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 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.1:

- Bug fixes:
- Fixed binding of results resourceId and resourceGroupName by @BenrieWhite.
[#2460](https://github.com/Azure/PSRule.Rules.Azure/issues/2460)

## v1.30.1

What's changed since v1.30.0:
Expand Down
16 changes: 12 additions & 4 deletions src/PSRule.Rules.Azure/Pipeline/Export/ResourceExportVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal sealed class ResourceExportVisitor
private const string PROPERTY_ZONES = "zones";
private const string PROPERTY_RESOURCES = "resources";
private const string PROPERTY_SUBSCRIPTIONID = "subscriptionId";
private const string PROPERTY_RESOURCEGROUPNAME = "resourceGroupName";
private const string PROPERTY_KIND = "kind";
private const string PROPERTY_SHAREDKEY = "sharedKey";
private const string PROPERTY_NETWORKPROFILE = "networkProfile";
Expand Down Expand Up @@ -128,8 +129,8 @@ private async Task<bool> ExpandResource(IResourceExportContext context, JObject

var resourceContext = new ResourceContext(context, tenantId);

// Set the subscription Id.
SetSubscriptionId(resource, resourceId);
// Set subscriptionId and resourceGroupName.
SetResourceIdentifiers(resource, resourceType, resourceId);

// Ignore expand of these.
if (string.Equals(resourceType, TYPE_VISUALSTUDIO_ACCOUNT, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -181,9 +182,16 @@ private async Task GetProperties(ResourceContext context, JObject resource, stri
resource.Add(PROPERTY_PROPERTIES, properties);
}

private static void SetSubscriptionId(JObject resource, string resourceId)
/// <summary>
/// Set <c>subscriptionId</c> and <c>resourceGroupName</c> on the resource based on the provided <c>resourceId</c>.
/// </summary>
private static void SetResourceIdentifiers(JObject resource, string resourceType, string resourceId)
{
if (ResourceHelper.TrySubscriptionId(resourceId, out var subscriptionId))
if (ResourceHelper.TryResourceGroup(resourceId, out var subscriptionId, out var resourceGroupName) &&
!string.Equals(resourceType, TYPE_RESOURCES_RESOURCEGROUP, StringComparison.OrdinalIgnoreCase))
resource.Add(PROPERTY_RESOURCEGROUPNAME, resourceGroupName);

if (!string.IsNullOrEmpty(subscriptionId))
resource.Add(PROPERTY_SUBSCRIPTIONID, subscriptionId);
}

Expand Down
9 changes: 5 additions & 4 deletions src/PSRule.Rules.Azure/rules/Config.Rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ spec:
- ResourceType
- type
field:
resourceId: [ 'ResourceId' ]
subscriptionId: [ 'SubscriptionId' ]
resourceGroupName: [ 'ResourceGroupName' ]
resourceId: [ 'ResourceId', 'id' ]
subscriptionId: [ 'subscriptionId' ]
resourceGroupName: [ 'resourceGroupName' ]
configuration:
AZURE_PARAMETER_FILE_EXPANSION: false
AZURE_PARAMETER_FILE_METADATA_LINK: false
AZURE_BICEP_FILE_EXPANSION: false
AZURE_BICEP_PARAMS_FILE_EXPANSION: false
AZURE_BICEP_MINIMUM_VERSION: '0.4.451'
AZURE_BICEP_CHECK_TOOL: false
AZURE_BICEP_CHECK_TOOL: false

# Configure minimum AKS cluster version
AZURE_AKS_CLUSTER_MINIMUM_VERSION: '1.26.6'

AZURE_DEPLOYMENT_SENSITIVE_PROPERTY_NAMES:
- adminUsername
- administratorLogin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public async Task VisitAsync()
var resource = GetResourceObject("Microsoft.ContainerService/managedClusters");
await visitor.VisitAsync(context, resource);

Assert.Equal("rg-test", resource["resourceGroupName"].Value<string>());
Assert.Equal("ffffffff-ffff-ffff-ffff-ffffffffffff", resource["subscriptionId"].Value<string>());
Assert.Equal("ffffffff-ffff-ffff-ffff-ffffffffffff", resource["tenantId"].Value<string>());
}
Expand Down

0 comments on commit 971a625

Please sign in to comment.