Repository navigation
feat: list built-in policy definition versions in bulk - #256
Open
Conversation
Qi Zhang (Zhangqi910)
requested a review
from Akshay Rohilla (akshayrohilla)
November 23, 2026 07:29
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Without a cache,
GetDefinitionsFromAzurelists the versions of each built-in policy definition with a request of its own: 680 of the 696 requests for thealzarchitecture, even with the concurrency of #253. This PR addsAlzLib.AddPolicyClientFromCredential. With it, the versions are listed in bulk: 26 requests instead of 696, and the definitions stored are the same.AddPolicyClientFromCredential(cred, opts)creates the policy client factory and an Azure Resource Manager client from the same credential and options.AddPolicyClientis unchanged: its callers, including terraform-provider-alz today, send the same requests as before.Stacked on #253. The base branch is
user/yingjie/fix-issue-4310-parallel-builtin-definition-fetch, so the diff only shows this change, which builds on thefetchMu, the name grouping and theOptions.Parallelismof #253. I'll rebase it ontomainonce #253 is merged. This PR and #255 have no files in common.Refs Azure/Azure-Landing-Zones#4328. Not "Fixes": this PR is the alzlib part of Item 3 (see "Where this fits in Azure/Azure-Landing-Zones#4328" below).
Problem
addVersionedPolicyDefinitionandfetchReferencedPolicyDefinitionVersionslist the versions of a definition withDefinitionVersionsClient.NewListBuiltInPager(name): one request per definition. fix: honor Options.Parallelism when fetching built-in policy definitions #253 sends them 10 at a time, but Azure Resource Manager throttles tenant-level reads per identity and region (a bucket of 250, refilled at 25 per second). One build empties it, and builds that run at the same time in a tenant get HTTP 429 and wait: 251 × 429 for three builds at once (see Testing).POST /providers/Microsoft.Authorization/listPolicyDefinitionVersions?$top=1000, where eachnextLinkis also requested with POST. But the REST API specification (2023-04-01) declares this listing withoutx-ms-pageableor$top. armpolicy'sDefinitionVersionsClient.ListAllBuiltinstherefore sends one request and returns the first 500 versions; this is also why the comment incache/azure.gosays that it only returns the latest version of each definition. AClientFactorykeeps its pipeline unexported, so alzlib, which only receives the factory, cannot request the other pages.1.0.0-preview, thatGETreturns 404 for and that their own version listing does not list. Used as they are, they would change what constraints such as1.*.*-previewresolve to.Changes
alzlib.goAddPolicyClientFromCredential.AddPolicyClientalso clears the Azure Resource Manager client.GetDefinitionsFromAzurekeeps the bulk listing for the duration of the call;getBuiltInPoliciesandensureReferencedPolicyDefinitionsprepare it;addVersionedPolicyDefinitionandfetchReferencedPolicyDefinitionVersionsget their pager frompolicyDefinitionVersionsPager.builtin_bulk.go(new)nextLinkcheck and the page cap, the filter, and a pager that returns the bulk-listed versions of a definition as one page.builtin_bulk_test.go(new)internal/testutil/fakearm/fakearm.go$top, POST tonextLink), with versions that cannot be read, versions left out of or listed twice in the bulk listing, definitions listed twice, a failure of a given page, and call counters.Credential,ClientOptionsandNewClientFactorylet tests callAddPolicyClientFromCredentialagainst the fake.cmd/alzlibtool/command/generate/architecture.go,check/library.go,cache/create.goAddPolicyClientFromCredential; they already create their own credential.docs/cache.mdAddPolicyClientFromCredential.go.modis unchanged.How it works
AddPolicyClientFromCredential(cred azcore.TokenCredential, opts *arm.ClientOptions) errorcreatesarmpolicy.NewClientFactory("", cred, opts)andarm.NewClient("github.com/Azure/alzlib", <module version>, cred, opts). Both use the same options, so the cloud, the caller's policies (such as its User-Agent), retries andDisableRPRegistrationapply to every request.GetDefinitionsFromAzureuses the bulk listing when the versions of at least 10 built-in policy definitions need to be listed: definitions requested with a version that neither theAlzLibnor its cache has. They are counted for the requested definitions, and again for the definitions that policy set definitions reference. The listing is made at most once per call.Options.Parallelismis 1:POST .../listPolicyDefinitionVersions?$top=1000&api-version=2023-04-01, then POST to eachnextLink(6 pages today);DefinitionsClient.NewListBuiltInPagerand$top=1000(4 pages today), for the versions that each definition lists (properties.versionsandproperties.version).addVersionedPolicyDefinitionfor requested definitions,fetchReferencedPolicyDefinitionVersionsfor referenced ones), its bulk-listed versions are returned as one page through the existingpolicyDefinitionVersionsPagerinterface. Version resolution (Resolve policy definition versions to the version the assignment actually runs #248), what is stored, and empty listings (fix: handle empty built-in policy version listings instead of panicking #254) are unchanged, so the same definitions are stored.nextLinkthat is not https on the Azure Resource Manager host (the pipeline sends the bearer token with every request). A failed listing is not tried again in the same call. alzlib has no logger, so the fallback is silent; the tests assert it.POST .../listPolicySetDefinitionVersionsfails withResponseTooLarge.Testing
All tests were run locally on Windows with Go 1.27.4, and golangci-lint with
GOTOOLCHAIN=go1.25.2as in CI.In-repo
go build ./...andgo vet ./...pass.go test ./...passes; the 7 tests that need Azure credentials were not run (the live tests below read from Azure).-race(MinGW-w64; Linux is left to CI). golangci-lint v2.11.4 (--new-from-rev) reports 0 issues.builtin_bulk_test.go, againstfakearm:ResponseTooLarge, throttling, a failed definition listing, too many pages of either listing, a definition or a version missing from the bulk listing, a version listed twice with different content, and a definition listed twice;nextLinkon another host, or over http, is not requested;AddPolicyClient, nothing is listed in bulk;Verification suite outside the repo (this branch against #253 head
7dd6640)7dd6640, the 6 tests that detect the issue fail, and the 29 guards pass on both. For example, for analz-sized fake,7dd6640makes 624 requests, 609 of them to list versions one definition at a time; this branch makes 18.AddPolicyClient, this branch and7dd6640send the same 782 requests (method, path, query, status and order) in 5 scenarios, and store the same definitions.nextLinkto another host followed, no page cap, a threshold of 1, the listing kept between calls,Options.Parallelismignored, and GET instead of POST.39b1495builds, vets and passesgo test ./...with this branch, both unchanged and with the follow-up change (configureAlzLibcallsAddPolicyClientFromCredential).Live, read-only, against Azure on 2026-11-20 (reads of built-in definitions only; each measured run starts after a 20 s pause, so that it does not inherit the throttling of the previous run)
alzarchitecture ofintegrationtest/testdata/alzlib-2025-09-0, no cache7dd6640(#253)AddPolicyClientFromCredentialFromArchitecture, median of 6 interleaved runsAddPolicyClient, this PR sends the same requests as7dd6640.alzlibtool generate architecturewithout a cache: 2,388 files byte-identical to7dd6640, for testdataalz,platform/alz@2026.11.0,platform/amba@2026.10.0andplatform/slz@2026.11.0, each with and without--for-alz-bicep.terraform planof analz_architecturedata source (platform/alz@2026.11.0, no cache), terraform-provider-alz39b1495with7dd6640against39b1495with this PR and the follow-up change, 2 runs each: the outputs are identical; about 880 requests become 26 (plus 4 retries in one run); the bulk listing carries the provider's User-Agent, as its other Azure Policy requests do;ReadDataSourcemedian 46.8 s → 20.3 s (−57 %).Backwards compatibility and upgrade impact
AlzLib.AddPolicyClientFromCredential. No signatures change, andgo.modis unchanged.AddPolicyClient, including terraform-provider-alz today, send the same requests as before.AddPolicyClientcalled afterAddPolicyClientFromCredentialturns the bulk listing off.AddPolicyClientFromCredential, the requests change and the stored definitions do not. A call that lists in bulk reads about 23 MB of listings; foralz, peak heap was 35 MB higher.azsdk-go-alzlib/<version>in the User-Agent, unless the caller's policies set it (terraform-provider-alz's do).nextLinkmust be on the host of the cloud's Azure Resource Manager endpoint; if the listing fails there, the versions are listed one definition at a time.Where this fits in Azure/Azure-Landing-Zones#4328
cache_file_save_enabledsaves an empty cacheConfigureProvidertakes ~33–37 s on every commandListBuiltInrequestsconfigureAlzLibcallsAddPolicyClientFromCredential(token, popts)instead ofAddPolicyClientQuestions for reviewers
AddPolicyClientFromCredential(cred, opts)creates both clients from one credential and one set of options, so they cannot disagree on the cloud or the credential. The alternative,AddARMClient(*arm.Client)next toAddPolicyClient, leaves that to the caller. Is this shape and name fine?alzarchitecture: 25.6 s → 11.0 s) or when Azure throttles. Keep 10, or raise it, for example to 100?Out of scope
configureAlzLib, and bumping alzlib.alzlibtool cache createwithout--library:cache.NewCacheFromAzurestill reads the first page ofListAllBuiltinsand lists the versions of the other definitions one at a time (about 3,800 requests), and can store versions that cannot be read. It can use the bulk listing in a separate PR.listPolicyDefinitionVersionspageable (POSTnextLink,$top), armpolicy would get a pager.AddPolicyClientFromCredentialcould then use it without an API change.Suggested release note