Fix PIM role management policy update bug#4398
Conversation
Fixes #4377 ## Problem The `TestAccPIMRoleManagementPolicies` test was failing in CI because PIM policy updates were not working correctly. When a user modified a rule property (e.g., changing maximumDuration from P365D to P90D), the update appeared to succeed but the value in Azure remained unchanged. ## Root Cause The bug was in the `restoreDefaultsForDeletedRules` function in `custom_pim.go`. PIM policies in Azure typically have 15-20 rules, but users only specify a few rules they want to customize in their Pulumi programs. The provider is supposed to: 1. Read the full policy from Azure (with all rules) during CREATE 2. Save that as "originalState" 3. During UPDATE, send ALL rules to Azure (user-specified + unspecified) The bug: `restoreDefaultsForDeletedRules` was iterating over `oldRules` (rules from the previous Pulumi state) instead of `origRules` (rules from Azure's original state). This meant rules that were never specified by the user were NOT included in update requests, resulting in an incomplete policy update that Azure would silently ignore. ## Solution Changed the iteration in `restoreDefaultsForDeletedRules` from: ```go for id := range oldRules { // ❌ Only previously managed rules ``` to: ```go for id, origRule := range origRules { // ✅ All rules from Azure ``` This ensures ALL rules from Azure's original state are included in every update, not just the ones previously specified by the user. ## Testing - Added new test case "restores rules never specified by user" - All existing PIM unit tests pass - All role management policy tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Does the PR have any schema changes?Looking good! No breaking changes found. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4398 +/- ##
=======================================
Coverage 59.37% 59.37%
=======================================
Files 91 91
Lines 11448 11444 -4
=======================================
- Hits 6797 6795 -2
+ Misses 4015 4014 -1
+ Partials 636 635 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| ) | ||
| }) | ||
|
|
||
| t.Run("restores rules never specified by user", func(t *testing.T) { |
There was a problem hiding this comment.
I assume this was a failing test that reproduced the problem?
There was a problem hiding this comment.
Yes, a test failed and this was the result of the analysis. Unfortunately, the problem is intermittent; a re-run was successful.
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the restoreDefaultsForDeletedRules function to change its behavior from restoring rules that were in olds (user-specified) to restoring rules from the original Azure state, regardless of whether they were ever specified by the user. This ensures that unspecified rules from Azure are preserved during updates.
- Simplified logic by removing the need to check
oldRulesand instead directly iterating overorigRules - Changed comment to reflect the new behavior
- Added comprehensive test case covering the scenario where user never specified certain rules
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| provider/pkg/resources/customresources/custom_pim.go | Refactored restoreDefaultsForDeletedRules to iterate over original state rules instead of old user-specified rules, removing unnecessary conditionals |
| provider/pkg/resources/customresources/custom_pim_test.go | Added test case verifying that rules never specified by the user are correctly restored from original Azure state |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This PR has been shipped in release v3.11.0. |
Fixes: https://github.com/pulumi/pulumi-azure-native/actions/runs/19138381455/job/54700360112
Problem
The
TestAccPIMRoleManagementPoliciestest was failing in CI because PIM policy updates were not working correctly. When a user modified a rule property (e.g., changingmaximumDurationfromP365DtoP90D), the update appeared to succeed but the value in Azure remained unchanged.Root Cause
The bug was in the
restoreDefaultsForDeletedRulesfunction incustom_pim.go. PIM policies in Azure typically have 15-20 rules, but users only specify a few rules they want to customize in their Pulumi programs. The provider is supposed to:The bug:
restoreDefaultsForDeletedRuleswas iterating overoldRules(rules from the previous Pulumi state) instead oforigRules(rules from Azure's original state). This meant rules that were never specified by the user were NOT included in update requests, resulting in an incomplete policy update that Azure would silently ignore.Solution
Changed the iteration in
restoreDefaultsForDeletedRulesfrom:to:
This ensures ALL rules from Azure's original state are included in every update, not just the ones previously specified by the user.
Testing
Example
Before this fix, the following scenario would fail:
Then updating to:
The update would complete without error, but Azure would still have
P365Dbecause the other ~15 rules weren't included in the request.After this fix, all rules are properly preserved and the update succeeds.
🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com