Skip to content

feat: list built-in policy definition versions in bulk - #256

Open
Yingjie Chen (Y1ngJ1eChen) wants to merge 1 commit into
user/yingjie/fix-issue-4310-parallel-builtin-definition-fetchfrom
user/yingjie/fix-issue-4328-ListBuiltIn-request-remain
Open

Yingjie Chen (Y1ngJ1eChen) wants to merge 1 commit into
user/yingjie/fix-issue-4310-parallel-builtin-definition-fetchfrom
user/yingjie/fix-issue-4328-ListBuiltIn-request-remain

Conversation

@Y1ngJ1eChen

Copy link
Copy Markdown
Contributor

Summary

Without a cache, GetDefinitionsFromAzure lists the versions of each built-in policy definition with a request of its own: 680 of the 696 requests for the alz architecture, even with the concurrency of #253. This PR adds AlzLib.AddPolicyClientFromCredential. With it, the versions are listed in bulk: 26 requests instead of 696, and the definitions stored are the same.

  • Opt-in, through a new method. AddPolicyClientFromCredential(cred, opts) creates the policy client factory and an Azure Resource Manager client from the same credential and options. AddPolicyClient is unchanged: its callers, including terraform-provider-alz today, send the same requests as before.
  • Same result. The bulk-listed versions of a definition replace its version listing only when they are exactly the versions that the definition lists. Otherwise, and whenever the bulk listing fails, the versions are listed one definition at a time, 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 the fetchMu, the name grouping and the Options.Parallelism of #253. I'll rebase it onto main once #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

  • addVersionedPolicyDefinition and fetchReferencedPolicyDefinitionVersions list the versions of a definition with DefinitionVersionsClient.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).
  • Azure lists the versions of every built-in policy definition (5,003 versions of 3,813 definitions on 2026-11-19) in 6 pages: POST /providers/Microsoft.Authorization/listPolicyDefinitionVersions?$top=1000, where each nextLink is also requested with POST. But the REST API specification (2023-04-01) declares this listing without x-ms-pageable or $top. armpolicy's DefinitionVersionsClient.ListAllBuiltins therefore sends one request and returns the first 500 versions; this is also why the comment in cache/azure.go says that it only returns the latest version of each definition. A ClientFactory keeps its pipeline unexported, so alzlib, which only receives the factory, cannot request the other pages.
  • The bulk listing also lists versions that cannot be read: on 2026-11-19, 20 definitions had a version, such as 1.0.0-preview, that GET returns 404 for and that their own version listing does not list. Used as they are, they would change what constraints such as 1.*.*-preview resolve to.

Changes

File Change
alzlib.go New AddPolicyClientFromCredential. AddPolicyClient also clears the Azure Resource Manager client. GetDefinitionsFromAzure keeps the bulk listing for the duration of the call; getBuiltInPolicies and ensureReferencedPolicyDefinitions prepare it; addVersionedPolicyDefinition and fetchReferencedPolicyDefinitionVersions get their pager from policyDefinitionVersionsPager.
builtin_bulk.go (new) The threshold, the two listings, the nextLink check 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) See Testing.
internal/testutil/fakearm/fakearm.go Serves both listings, paged as Azure pages them ($top, POST to nextLink), 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, ClientOptions and NewClientFactory let tests call AddPolicyClientFromCredential against the fake.
cmd/alzlibtool/command/generate/architecture.go, check/library.go, cache/create.go Use AddPolicyClientFromCredential; they already create their own credential.
docs/cache.md Mentions AddPolicyClientFromCredential.

go.mod is unchanged.

How it works

  1. AddPolicyClientFromCredential(cred azcore.TokenCredential, opts *arm.ClientOptions) error creates armpolicy.NewClientFactory("", cred, opts) and arm.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 and DisableRPRegistration apply to every request.
  2. GetDefinitionsFromAzure uses 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 the AlzLib nor 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.
  3. It makes two listings, concurrently unless Options.Parallelism is 1:
    • the versions of every built-in policy definition: POST .../listPolicyDefinitionVersions?$top=1000&api-version=2023-04-01, then POST to each nextLink (6 pages today);
    • every built-in policy definition, with DefinitionsClient.NewListBuiltInPager and $top=1000 (4 pages today), for the versions that each definition lists (properties.versions and properties.version).
  4. Of the bulk-listed versions of a definition, only those that the definition lists are kept. If a version that it lists is missing, if a version is listed twice with different content (as in [Bug]: terraform-provider-alz v0.22.0 panics (nil pointer dereference) in alz_architecture data source – ensureReferencedPolicyDefinitions (alzlib v0.32.0) Azure-Landing-Zones#4327), or if the definition is listed twice, that definition is listed on its own.
  5. Where the versions of a definition were listed (addVersionedPolicyDefinition for requested definitions, fetchReferencedPolicyDefinitionVersions for referenced ones), its bulk-listed versions are returned as one page through the existing policyDefinitionVersionsPager interface. 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.
  6. If either listing fails, the call lists the versions one definition at a time, as before. That is the case for an error response after the SDK's retries, more than 50 pages, or a nextLink that 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.
  7. The listing is dropped at the end of the call, so a later call finds definitions added to Azure since. Versions are kept as raw JSON and decoded only for the definitions used.
  8. Policy set definitions are still listed one at a time: POST .../listPolicySetDefinitionVersions fails with ResponseTooLarge.

Testing

All tests were run locally on Windows with Go 1.27.4, and golangci-lint with GOTOOLCHAIN=go1.25.2 as in CI.

In-repo

  • go build ./... and go vet ./... pass. go test ./... passes; the 7 tests that need Azure credentials were not run (the live tests below read from Azure).
  • The new tests and the existing fetch tests pass 3 runs in a row, and with -race (MinGW-w64; Linux is left to CI). golangci-lint v2.11.4 (--new-from-rev) reports 0 issues.
  • builtin_bulk_test.go, against fakearm:
    • the bulk listing stores the same definitions as listing each definition, including deprecated, preview, shared, cached and referenced definitions;
    • versions that a definition does not list are not used, also for definitions referenced by a policy set definition;
    • the fallback in 10 cases: a failed page, 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;
    • a nextLink on another host, or over http, is not requested;
    • with AddPolicyClient, nothing is listed in bulk;
    • the threshold: 9 definitions are listed one at a time; 10, requested or referenced, are listed in bulk.

Verification suite outside the repo (this branch against #253 head 7dd6640)

  • 36 tests pass 5 runs in a row. Against 7dd6640, the 6 tests that detect the issue fail, and the 29 guards pass on both. For example, for an alz-sized fake, 7dd6640 makes 624 requests, 609 of them to list versions one definition at a time; this branch makes 18.
  • With AddPolicyClient, this branch and 7dd6640 send the same 782 requests (method, path, query, status and order) in 5 scenarios, and store the same definitions.
  • 11 deliberate bugs in a copy of this change are each caught by a test. Among them: versions that cannot be read kept, an incomplete or conflicting listing used, a nextLink to another host followed, no page cap, a threshold of 1, the listing kept between calls, Options.Parallelism ignored, and GET instead of POST.
  • terraform-provider-alz 39b1495 builds, vets and passes go test ./... with this branch, both unchanged and with the follow-up change (configureAlzLib calls AddPolicyClientFromCredential).

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)

alz architecture of integrationtest/testdata/alzlib-2025-09-0, no cache 7dd6640 (#253) This PR, AddPolicyClientFromCredential
Requests 696 26: 6 + 4 listing pages, 1 definition, 15 policy set definition version listings
Definitions stored 681 definitions with 943 versions, 15 policy set definitions the same (identical SHA-256 of the exported definitions)
FromArchitecture, median of 6 interleaved runs 25.6 s; 63 × HTTP 429 in 6 runs 11.0 s (−57 %); no 429
Three runs at once, as builds sharing a tenant 74–102 s each; 251 × 429 15.5–20.8 s each; no 429
Responses read / peak heap 14.7 MB / 80 MB 33.8 MB / 115 MB
  • With AddPolicyClient, this PR sends the same requests as 7dd6640.
  • alzlibtool generate architecture without a cache: 2,388 files byte-identical to 7dd6640, for testdata alz, platform/alz@2026.11.0, platform/amba@2026.10.0 and platform/slz@2026.11.0, each with and without --for-alz-bicep.
  • From a cache missing K definitions (K = 0, 5, 9, 10, 20, 50, 100, 200), the definitions are identical with the factory and with the new method for every K. Up to K = 10, nothing is listed in bulk (the 10 were split between requested and referenced definitions); from K = 20, the bulk listing makes 10 requests.
  • terraform plan of an alz_architecture data source (platform/alz@2026.11.0, no cache), terraform-provider-alz 39b1495 with 7dd6640 against 39b1495 with 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; ReadDataSource median 46.8 s → 20.3 s (−57 %).

Backwards compatibility and upgrade impact

  • One public API addition: AlzLib.AddPolicyClientFromCredential. No signatures change, and go.mod is unchanged.
  • Callers of AddPolicyClient, including terraform-provider-alz today, send the same requests as before. AddPolicyClient called after AddPolicyClientFromCredential turns the bulk listing off.
  • With AddPolicyClientFromCredential, the requests change and the stored definitions do not. A call that lists in bulk reads about 23 MB of listings; for alz, peak heap was 35 MB higher.
  • The Azure Resource Manager client identifies itself as azsdk-go-alzlib/<version> in the User-Agent, unless the caller's policies set it (terraform-provider-alz's do).
  • Not tested in sovereign clouds. A nextLink must 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

Item Where Status
1. cache_file_save_enabled saves an empty cache terraform-provider-alz Azure/terraform-provider-alz#224, open
2. ConfigureProvider takes ~33–37 s on every command alzlib #255, then terraform-provider-alz #255 in review
3. ~700 per-definition ListBuiltIn requests alzlib: this PR. terraform-provider-alz: configureAlzLib calls AddPolicyClientFromCredential(token, popts) instead of AddPolicyClient This PR; the provider change follows the alzlib release
4. Ship #253 and make its parallelism configurable alzlib #253 (Azure/Azure-Landing-Zones#4310), then terraform-provider-alz #253 in review

Questions for reviewers

  1. Name and shape of the new method. 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 to AddPolicyClient, leaves that to the caller. Is this shape and name fine?
  2. Threshold. The bulk listing is used from 10 definitions to list. It always takes about 10 requests, and took 7–18 s without throttling in two runs, whereas listing 20–200 definitions one at a time took 2–6 s. So from 10 definitions it saves requests, but it only saves time for many definitions (the whole alz architecture: 25.6 s → 11.0 s) or when Azure throttles. Keep 10, or raise it, for example to 100?

Out of scope

  • terraform-provider-alz: calling the new method in configureAlzLib, and bumping alzlib.
  • alzlibtool cache create without --library: cache.NewCacheFromAzure still reads the first page of ListAllBuiltins and 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.
  • Upstream: if azure-rest-api-specs declared listPolicyDefinitionVersions pageable (POST nextLink, $top), armpolicy would get a pager. AddPolicyClientFromCredential could then use it without an API change.

Suggested release note

Feature: new AlzLib.AddPolicyClientFromCredential. With it, GetDefinitionsFromAzure lists the versions of built-in policy definitions in bulk: 26 requests instead of about 700 for the alz architecture without a cache. The definitions are the same as when they are listed one definition at a time, and if the bulk listing fails, they are listed one at a time as before. alzlibtool generate architecture, check library and cache create --library use it (Azure/Azure-Landing-Zones#4328).

This branch has not been deployed

No deployments
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