Skip to content

Conversation

@notyashhh
Copy link
Member

No description provided.

@Copilot Copilot AI review requested due to automatic review settings October 17, 2025 03:01
@notyashhh
Copy link
Member Author

Related to this change: Azure/azure-powershell#28711

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request implements Phase 1 of the ChangeSafety feature, adding support for acquiring Azure Policy tokens to validate resource operations. The implementation introduces a new -AcquirePolicyToken dynamic parameter and handler that intercepts write operations to automatically obtain policy tokens from the Azure Authorization API.

Key changes:

  • Introduces AcquirePolicyTokenHandler to intercept HTTP requests and acquire policy tokens for write operations
  • Adds -AcquirePolicyToken dynamic parameter to Azure cmdlets (excludes read-only operations like Get-/List-/Show-*)
  • Implements feature flag support via environment variable AZ_ENABLE_POLICY_TOKEN and config key EnablePolicyToken

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Common/AcquirePolicyTokenHandler.cs New handler that acquires policy tokens from Azure Authorization API and attaches them to outgoing requests
src/Common/AzurePSCmdlet.cs Adds dynamic parameter support, feature flag logic, and handler registration for policy token acquisition
src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs Updates GetDynamicParameters to call base implementation to support inherited dynamic parameters
src/Common/Utilities/GeneralUtilities.cs Adds policy token header to authorization header sanitization list
src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs Defines new config key for EnablePolicyToken feature flag

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +482 to +483
|| commandName.EndsWith("List", StringComparison.OrdinalIgnoreCase)
|| commandName.EndsWith("Show", StringComparison.OrdinalIgnoreCase))
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for excluding read-only cmdlets checks if the command name ends with 'List' or 'Show', but cmdlets typically follow the pattern 'Get-', 'Remove-', 'Set-*' where the verb comes first. The check should use StartsWith for 'List' and 'Show' as well, not EndsWith. For example, 'List-AzResource' would be caught correctly, but a cmdlet named 'Get-ResourceList' would incorrectly be excluded.

Suggested change
|| commandName.EndsWith("List", StringComparison.OrdinalIgnoreCase)
|| commandName.EndsWith("Show", StringComparison.OrdinalIgnoreCase))
|| commandName.StartsWith("List", StringComparison.OrdinalIgnoreCase)
|| commandName.StartsWith("Show", StringComparison.OrdinalIgnoreCase))

Copilot uses AI. Check for mistakes.

if (!(_cmdlet?.IsPolicyTokenFeatureEnabled() ?? false))
{
EnqueueDebug("Skip: feature disabled (EnableAcquirePolicyToken config set to false).");
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug message incorrectly references 'EnableAcquirePolicyToken' config, but the actual config key is 'EnablePolicyToken' as defined in ConfigKeysForCommon.cs. This could confuse users troubleshooting feature flag issues.

Suggested change
EnqueueDebug("Skip: feature disabled (EnableAcquirePolicyToken config set to false).");
EnqueueDebug("Skip: feature disabled (EnablePolicyToken config set to false).");

Copilot uses AI. Check for mistakes.
Comment on lines +175 to +178
using (var http = new HttpClient())
{
EnqueueDebug($"POST acquirePolicyToken {tokenUri}");
var response = await http.SendAsync(tokenRequest, cancellationToken).ConfigureAwait(false);
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new HttpClient instance for each token request is inefficient and can lead to socket exhaustion under load. HttpClient should be reused or managed via a static instance or IHttpClientFactory. Consider using a shared static HttpClient instance for this handler.

Copilot uses AI. Check for mistakes.
if (string.IsNullOrEmpty(token))
{
EnqueueDebug("Response OK but token missing.");
throw new InvalidOperationException($"No token returned. Response:{responseContent}");
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after colon in error message. Should be 'Response: {responseContent}' for consistency with other error messages in the file.

Suggested change
throw new InvalidOperationException($"No token returned. Response:{responseContent}");
throw new InvalidOperationException($"No token returned. Response: {responseContent}");

Copilot uses AI. Check for mistakes.
Comment on lines +240 to +244
catch { }
}
}
catch { }

Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty catch blocks silently swallow exceptions without logging, making debugging configuration issues difficult. Consider logging the exception or at minimum adding a comment explaining why the exception is intentionally ignored.

Suggested change
catch { }
}
}
catch { }
catch (Exception ex)
{
Debug.WriteLine($"Exception in configManager.GetConfigValue: {ex}");
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Exception in IsPolicyTokenFeatureEnabled: {ex}");
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant