Skip to content

Commit

Permalink
Fixed in-flight export of subscription resource type #3231 (#3247)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Feb 7, 2025
1 parent ca8ce6c commit b04a89e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ What's changed since v1.41.0:
- Bug fixes:
- Fixed incorrect generation of resource ID for tenant scoped deployments by @BernieWhite.
[#3237](https://github.com/Azure/PSRule.Rules.Azure/issues/3237)
- Fixed in-flight export of subscription resource type `Microsoft.Subscription` by @BernieWhite.
[#3231](https://github.com/Azure/PSRule.Rules.Azure/issues/3231)

## v1.41.0

Expand Down
13 changes: 7 additions & 6 deletions src/PSRule.Rules.Azure/Pipeline/Export/ResourceExportVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal sealed class ResourceExportVisitor
private const string PROPERTY_PROPERTIES = "properties";
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_SUBSCRIPTION_ID = "subscriptionId";
private const string PROPERTY_RESOURCE_GROUP_NAME = "resourceGroupName";
private const string PROPERTY_KIND = "kind";
private const string PROPERTY_SHAREDKEY = "sharedKey";
private const string PROPERTY_NETWORKPROFILE = "networkProfile";
Expand Down Expand Up @@ -235,11 +235,12 @@ private async Task GetProperties(ResourceContext context, JObject resource, stri
private static void SetResourceIdentifiers(JObject resource, string resourceType, string resourceId)
{
if (ResourceHelper.TryResourceGroup(resourceId, out var subscriptionId, out var resourceGroupName) &&
!string.Equals(resourceType, TYPE_RESOURCES_RESOURCEGROUP, StringComparison.OrdinalIgnoreCase))
resource.Add(PROPERTY_RESOURCEGROUPNAME, resourceGroupName);
!string.Equals(resourceType, TYPE_RESOURCES_RESOURCEGROUP, StringComparison.OrdinalIgnoreCase) &&
!resource.ContainsKeyInsensitive(PROPERTY_RESOURCE_GROUP_NAME))
resource.Add(PROPERTY_RESOURCE_GROUP_NAME, resourceGroupName);

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

private bool TryGetLatestAPIVersion(string resourceType, out string apiVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PSRule.Rules.Azure.Pipeline.Export;
/// <summary>
/// A context to export an Azure subscription.
/// </summary>
internal class SubscriptionExportContext : ExportDataContext, ISubscriptionExportContext
internal sealed class SubscriptionExportContext : ExportDataContext, ISubscriptionExportContext
{
private readonly string _ResourceEndpoint;
private readonly string _ResourceGroupEndpoint;
Expand All @@ -27,7 +27,7 @@ public SubscriptionExportContext(PipelineContext context, ExportSubscriptionScop
{
_ResourceEndpoint = $"{RESOURCE_MANAGER_URL}/subscriptions/{scope.SubscriptionId}/resources?api-version=2021-04-01{GetFilter(tag)}";
_ResourceGroupEndpoint = $"{RESOURCE_MANAGER_URL}/subscriptions/{scope.SubscriptionId}/resourcegroups?api-version=2021-04-01{GetFilter(tag)}";
_SubscriptionEndpoint = $"{RESOURCE_MANAGER_URL}/subscriptions/{scope.SubscriptionId}";
_SubscriptionEndpoint = $"{RESOURCE_MANAGER_URL}/subscriptions/{scope.SubscriptionId}?api-version=2022-12-01";
SubscriptionId = scope.SubscriptionId;
TenantId = scope.TenantId;
RefreshToken(scope.TenantId);
Expand Down
4 changes: 3 additions & 1 deletion src/PSRule.Rules.Azure/Pipeline/ResourceDataPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal sealed class ResourceDataPipeline : ExportDataPipeline
private const string PROPERTY_DISPLAYNAME = "displayName";
private const string PROPERTY_TENANTID = "tenantId";

private const string PLACEHOLDER_SUBSCRIPTION_TYPE = "Microsoft.Subscription";

private const string ERRORID_RESOURCEDATAEXPAND = "PSRule.Rules.Azure.ResourceDataExpand";

private readonly ConcurrentQueue<JObject> _Resources;
Expand Down Expand Up @@ -170,7 +172,7 @@ private async Task<int> GetSubscriptionAsync(ISubscriptionExportContext context)
var r = await context.GetSubscriptionAsync();
if (r != null)
{
r.Add(PROPERTY_TYPE, "Microsoft.Subscription");
r.Add(PROPERTY_TYPE, PLACEHOLDER_SUBSCRIPTION_TYPE);
if (r.TryGetProperty(PROPERTY_DISPLAYNAME, out var displayName))
r.Add(PROPERTY_NAME, displayName);

Expand Down

0 comments on commit b04a89e

Please sign in to comment.