Skip to content

Add Windows Managed Identity v2 mTLS PoP and bearer integration - #987

Open
Gladwin Johnson VR (gladjohn) wants to merge 12 commits into
devfrom
feature/managed-identity-v2
Open

Gladwin Johnson VR (gladjohn) wants to merge 12 commits into
devfrom
feature/managed-identity-v2

Conversation

@gladjohn

Copy link
Copy Markdown
Contributor

Repository-owned head: AzureAD/microsoft-authentication-library-for-python:feature/managed-identity-v2.
Replaces the fork-based #985 with the identical implementation commit
e339f16aca840a4c048ca8445374482c1124df7a. The bridge dependency on #982 remains.

Summary

Implement a Windows Managed Identity v2 development candidate using the Rust foundation in #982:

  • Explicit mtls_proof_of_possession, request_over_mtls, attestation opt-in, minimum strength and force-refresh options on ManagedIdentityClient.
  • IMDS platform metadata and credential issuance, protected-key PKCS#10 signing, independent KeyGuard attestation, and canonical OAuth client-credentials exchange over native mTLS.
  • Separate ordinary bearer, bearer-over-mTLS and full-leaf-bound PoP token caches; exact-generation synchronous and bounded worker-backed asynchronous resource transports.
  • V2-first capability discovery, deadline-isolated concurrent callers, certificate renewal/rejection recovery, and best-effort Windows certificate-store reattachment.
  • Independent optional Windows wheels, public examples, documentation, collected live tests and an opt-in installed-candidate CI stage.

Ordinary managed identity remains independent of the optional native packages. Explicit protected acquisition does not fall back to a software key.

Stacking and immutable references

Depends on #982. This PR targets dev and includes the #982 bridge commits
followed by the Python integration. It is ready for review so the normal
dev OneBranch Python gate can trigger. Until #982 merges, the diff includes
both the native foundation and the integration; once #982 lands in dev, the
diff narrows to the integration. This is not a request to merge before the
dependency or outstanding acceptance requirements are satisfied.

Reference Revision
Python #982 foundation 10b146ad937ad823b2b5cd2da4e66df3b942f5c2
Python dev, already included c53d5ea47f7713d8fbc8e3d9832436fd1028b5c1
MSAL.NET protocol reference 40039615ab4edad6f12e65206f0f7c5e09fa3442
Additional .NET certificate-cache ordering review, #6275 438f351a33a18494bb0938e4209e3413f4716584
Java #1059 / published preview sources ea0148534593acbc58d700bffe8419b076a9dd37
Java recovery/routing reference bc8f196579ed16b79a679849282e43e79d5a001b

Published sources compared: msal4j:1.26.1-preview1 (215 Java files) and msal4j-key-attestation:1.0.0-preview1 (17 Java files); no differences from the pinned Java reference after line-ending normalization.

Review fixes included

The local deep correctness review found six actionable issues, all addressed with focused regressions:

  1. Failed certificate-store deletion could let another client restore a rejected binding. Invalidation is now shared across clients and remains until a replacement is successfully persisted, including after memory-cache eviction.
  2. Token-cache DEBUG logging exposed the new claims pseudo-parameter. It is now redacted without changing partitioning.
  3. Ordinary forced refresh skipped lookup of a challenged token. The refresh request now retains its SHA-256 reference.
  4. A short discovery owner's deadline prematurely failed longer-budget waiters. Eligible waiters now restart within their own remaining budget.
  5. Accepted HTTP retry configuration did not apply to native ESTS requests. Transient status retries now use the adapter's method/status/delay policy, independently bounded to three retries and a total budget, without certificate reminting.
  6. IMDS errors discarded service codes. Trusted, bounded service codes are preserved without including response bodies or descriptions.

Regression mapping

All Python paths below are in this PR. Reference PR numbers in this table refer to MSAL.NET unless identified otherwise.

Reference / regression Python implementation Python regression coverage
#6178/#6181: GetManagedIdentityCapabilities_TimedOutWaiter_DoesNotCancelLockOwnerAsync; kill-switch/minimum-strength behavior _managed_identity_v2._CapabilityDiscovery; ManagedIdentityClient tests/test_managed_identity_v2.py::CapabilityDiscoveryTests; PublicProtectedFlowTests::test_disabled_or_invalid_modes_do_no_http_or_native_work; test_keyguard_failure_preserves_required_strength_and_native_category
#6200/#6238/#5741/#5887: regional/cloud routing, tenant metadata and fresh endpoint/key-generation attestation cache _ImdsClient, _MaaTokenCache, _NativeAttestation ImdsV2ProtocolTests; MaaCacheTests; NativeAttestationOwnershipTests; PublicProtectedFlowTests::test_attestation_denial_prevents_credential_issuance in tests/test_managed_identity_v2.py
#6215/#6173/#5761: mTLSPop_ForceRefresh_RemintsCachedCert_PostsCanonicalClientId_AndReusesCachedAttestation; rejected-binding recovery _V2Client, _is_binding_rejection PublicProtectedFlowTests::test_force_refresh_remints_leaf_but_reuses_fresh_attestation_generation; test_wrapped_binding_reset_retries_once_but_unrelated_oauth_failure_does_not; test_native_certificate_alerts_recover_once_and_nested_trust_failure_never_retries
#5566/#6123: Read_Boundary_Exactly24h_IsRejected; persistent-cache binding restoration _CertificateStore, _V2Client WindowsCertificateStoreTests; PublicProtectedFlowTests::test_restart_reattaches_persisted_leaf_before_returning_serialized_pop_token; test_failed_invalidation_is_shared_with_another_client
#6275: dynamic-certificate rotation/retry cache-ordering invariant; not itself an MI implementation change _V2Client.acquire: resolve binding before lookup; update partition after recovery PublicProtectedFlowTests::test_invalid_client_remints_once_but_wrong_token_types_never_enter_cache; force-refresh and restart tests
.NET ImdsV2Tests.TestCsrGeneration_VmIdAndVmssId / OnlyVmId; Java Pkcs10BuilderTest subject, CUID and DER tests _create_csr tests/test_native_mtls.py::NativeKeyOwnershipTests::test_csr_preserves_platform_identity_cuid_der_and_pss_parameters; test_csr_omits_absent_cuid_fields
Python #982: request/response allocation contract and external C mTLS fixture _NativeRuntime, additive native protected-key exports tests/test_native_mtls.py::NativeOwnershipTests; WindowsRustOAuthTests::test_real_oauth_form_posts_present_the_opaque_cng_key_in_both_modes; native Debug/Release matrix

Platform differences: Python uses OpenSSL alert categories / Windows reset 10054, not Schannel exceptions; PoP partitioning uses SHA-256 of the entire leaf DER, not a certificate serial number. The async adapter is bounded worker-backed, not an aiohttp SSL-context integration. The audience is not a wildcard for arbitrary resource origins.

CI and PR targeting

The original draft targeted feature/native-primitives and initially ran only
the package build: native Actions filtered for dev, and OneBranch targets
dev and excludes drafts. Credential-free checks were added for that stacked
base and have now passed remotely at
e339f16aca840a4c048ca8445374482c1124df7a:

  • The existing Build workflow runs unit tests on Python 3.9 through 3.14 for
    PRs targeting feature/native-primitives, including drafts.
  • Native Windows CI accepts that stacked base and relevant Python integration
    paths. Its existing complete Debug/Release native matrix and dependency checks
    are retained, followed by Python 3.12 native integration tests.
  • Both workflows upload JUnit artifacts. Existing OneBranch branch/draft rules,
    SDL checks and live-test restrictions are unchanged.

All six Python matrix jobs, Windows native/Python integration and dependency
policy checks passed. The PR now targets dev and is ready for review, retaining
the bridge in its head history so the normal OneBranch gate can run as well.
Those completed runs were on the earlier fork PR #985. This replacement uses a
branch in the AzureAD repository to avoid fork restrictions on trusted CI;
new run status is shown in this PR's checks.

The workflow changes passed pinned actionlint validation. The exact Windows
Python test command passed locally: 117 tests and 121 subtests. Remote CI results
are reported in the PR checks; the local results below are not remote-CI claims.

Local test evidence

Tests were run on Windows x64 with Python 3.12.10:

Run Result
Automated repository baseline before review fixes 513 passed, 26 skipped, 2 deselected, 780 subtests passed
Initial six review fixes: protected flows, native Release interop, legacy MI and token cache 225 passed, 770 subtests passed
Final persistence hardening: protected flows, Windows store, native Release interop and token cache 117 passed, 121 subtests passed
Final wheel installed in a separate environment, executed outside the checkout: native tests, IMDS protocol, public protected flows and discovery 54 unittest tests passed, including all seven new review regressions
Native Debug and Release matrices, formatting, Clippy, dependency audit and policy checks Passed
Sphinx HTML, pipeline YAML parse and diff whitespace Passed

Focused source commands:

$env:MSAL_TEST_NATIVE_MTLS_DIR = (Resolve-Path .\native\artifacts\Release).Path
python -m pytest -q tests\test_managed_identity_v2.py tests\test_native_mtls.py tests\test_mi.py tests\test_token_cache.py
python -m pytest -q tests\test_managed_identity_v2.py tests\test_native_mtls.py tests\test_token_cache.py

Clean-installed command, using copied test modules outside the checkout:

python -m unittest -q test_native_mtls test_managed_identity_v2.ImdsV2ProtocolTests test_managed_identity_v2.PublicProtectedFlowTests test_managed_identity_v2.CapabilityDiscoveryTests

The automated baseline excluded existing live integration files and the interactive Windows broker sign-in test (which timed out waiting for user interaction). Two existing latest-cryptography policy tests were deselected. The 513-test result is a baseline, not a post-fix full-suite claim. The installed unittest run reports implicit temporary-directory cleanup ResourceWarnings from fixtures.

All seven relevant installed Python modules, including token_cache, matched checkout hashes. The live-profile SDK fingerprint is 668df0f7f2e8d6eafeb04f7d8b7098e5ad40bec78cbfbbca7543d50daf8b3aac.

Outstanding acceptance evidence

This is not a release-ready or positively attested implementation claim.

  • Ten live cases are collected but were not executed. Actual VBS/KeyGuard creation/liveness/recreation, MAA, IMDSv2, ESTS and strict Key Vault binding/negative controls remain outstanding.
  • Actual supported-host representation of the CNG Virtual Iso property remains to be verified; software-CNG fixtures are not VBS evidence.
  • Cross-process/live rotation and remaining historical parity mapping require follow-through.
  • Maintainer/security release review and production signing are still required. Development integrity manifests do not authenticate a publisher.
  • Optional wheels remain development versions. No compiled native libraries, downloaded packages, credentials or private keys are included in this PR.

See docs/managed-identity-v2.rst for build/install instructions, supported transport constraints, samples and the full installed live-validation profile.

Host the native workspace separately from Python runtime and packaging.
Clarify compatible dependency ranges without changing the lockfile.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Duplicate selected public material when no private material exists.
Add an external OpenSSL regression and correct the example DLL path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Reject retained explicit MGF1 mismatches before signature state updates.
Account for IPv6 brackets before reserving the plaintext request buffer.
Add sequential-update and exact wire-limit regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Move CMake/vcpkg setup ahead of the first quick-start build and reuse
the quick-start configuration steps from the build/test section.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Guard sensitive copies before validation can fail. Accept HTTP token field names and surrounding Content-Length whitespace while preserving strict framing. Add allocation-observer and protocol regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Permit secure delayed TLS client authentication under the existing I/O deadline. Preserve key and signing configuration during null-key context reuse. Bound RSA encodings before conversion and zeroize transient URI/name copies. Add Windows/CNG regressions, with 67 Rust tests per Debug/Release configuration and the complete native matrix passing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Connect the Rust native foundation to explicit PoP and bearer-over-mTLS managed identity acquisition, attestation, generation-bound transports, persistence, packaging and regression coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Keep the feature/native-primitives stack and existing OneBranch policies. Add credential-free Python matrix checks for stacked drafts and enable Windows native/Python integration coverage on the stacked base.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The installed-candidate fingerprint omits the changed msal.token_cache module, allowing a wheel with stale token-cache code to pass source identity checks.

No open issues 🎉

What changed in this PR

Adds a Windows Managed Identity v2 development candidate on the Rust foundation from #982. It connects MSAL Python to optional protected-key mTLS and attestation packages while keeping ordinary managed identity independent of them. Live KeyGuard and service acceptance evidence remains outstanding.

Changes:

  • Adds explicit PoP and bearer-over-mTLS acquisition, certificate persistence, and generation-bound synchronous and asynchronous resource transports.
  • Includes the standalone Rust CNG/OpenSSL foundation, native ABI, conformance tests, and development guidance.
  • Adds optional Windows wheel tooling, public examples, live validation profiles, and CI integration.
File Description
tools/​prepare_mi_attestation.ps1 Downloads and verifies the pinned attestation DLL.
tools/​build_mi_native_wheels.ps1 Verifies artifacts and builds optional Windows wheels.
tests/​test_mi_v2_e2e.py Adds installed-candidate live acquisition and binding checks.
sample/​managed_identity_v2.py Demonstrates PoP, async PoP, and bearer requests.
README.md Introduces the native subproject.
packages/​msal-native-primitives/​setup.py Defines the Windows native wheel.
packages/​msal-native-primitives/​msal_native_primitives/​__init__.py Adds native package metadata without loading DLLs.
packages/​msal-key-attestation/​setup.py Defines the separate attestation wheel.
packages/​msal-key-attestation/​msal_key_attestation/​__init__.py Adds attestation package metadata without loading DLLs.
native/​vcpkg.json Pins the native OpenSSL dependency baseline.
native/​THIRD-PARTY-NOTICES.md Documents dependency notice and redistribution obligations.
native/​tests/​native/​support/​cng_key.h Declares CNG test-support interfaces.
native/​SUPPORT.md Defines pre-release support expectations.
native/​SECURITY.md Documents reporting and security-sensitive review areas.
native/​scripts/​test.ps1 Runs the native Debug/Release validation matrix.
native/​scripts/​loc.ps1 Reports Rust production line counts.
native/​scripts/​build.ps1 Builds native artifacts and integrity manifests.
native/​rustfmt.toml Sets Rust formatting options.
native/​README.md Documents native architecture, setup, and usage.
native/​LICENSE Supplies the native project's MIT license.
native/​include/​microsoft_identity_native_primitives.h Defines transport and protected-key C ABI contracts.
native/​include/​cng_provider_params.h Defines provider parameters and URI limits.
native/​examples/​c/​mtls_request.c Demonstrates a native certificate-bound request.
native/​docs/​versioning.md Defines product, ABI, and dependency versioning.
native/​docs/​security-review.md Establishes security review evidence requirements.
native/​docs/​security-model.md Documents enforced protections and trust boundaries.
native/​docs/​releasing.md Defines native release validation and packaging steps.
native/​docs/​development.md Documents native setup and engineering conventions.
native/​docs/​building-and-testing.md Explains build, test, and OpenSSL servicing requirements.
native/​docs/​architecture.md Describes crate responsibilities and request flow.
native/​docs/​abi.md Documents ownership, protected keys, and ABI compatibility.
native/​deny.toml Configures dependency license and source policies.
native/​crates/​rust-tests/​tests/​key_creation_race.rs Tests cross-process key-creation convergence.
native/​crates/​rust-tests/​src/​lib.rs Adds a native ABI smoke test.
native/​crates/​rust-tests/​src/​bin/​key-race-helper.rs Supports concurrent key-creation testing.
native/​crates/​rust-tests/​src/​bin/​key-audit.rs Detects leftover project-owned test keys.
native/​crates/​rust-tests/​Cargo.toml Defines cross-crate test dependencies.
native/​crates/​openssl-provider-sys/​Cargo.toml Defines OpenSSL FFI crate dependencies.
native/​crates/​mtls-bridge/​src/​lib.rs Implements request execution and response ownership.
native/​crates/​mtls-bridge/​src/​abi.rs Defines ABI layouts, bounds, and structured errors.
native/​crates/​mtls-bridge/​Cargo.toml Configures the native transport library.
native/​crates/​cng-provider/​src/​uri.rs Parses bounded CNG key URIs with zeroizing storage.
native/​crates/​cng-provider/​src/​store.rs Implements OpenSSL STORE loading and key ownership.
native/​crates/​cng-provider/​src/​params.rs Handles typed OpenSSL parameters.
native/​crates/​cng-provider/​src/​lib.rs Registers provider algorithms and callbacks.
native/​crates/​cng-provider/​src/​error.rs Produces bounded provider error messages.
native/​crates/​cng-provider/​src/​allocation.rs Adds fallible allocation and cleanup test helpers.
native/​crates/​cng-provider/​Cargo.toml Configures the CNG provider DLL.
native/​crates/​cng-core/​Cargo.toml Defines Windows CNG core dependencies.
native/​CONTRIBUTING.md Documents native contribution and validation requirements.
native/​CMakeLists.txt Builds external C tests and the native example.
native/​CHANGELOG.md Records native additions and hardening changes.
native/​Cargo.toml Defines workspace dependencies and lint policies.
native/​Cargo.lock Records exact native dependency versions.
native/​AGENTS.md Adds native-specific coding and review guidance.
native/​.gitignore Excludes native build and local outputs.
native/​.gitattributes Defines native text and binary file handling.
native/​.editorconfig Sets native editing conventions.
msal/​token_cache.py Redacts the managed identity v2 claims parameter.
msal/​managed_identity.py Adds protected acquisition options and capability discovery.
msal/​_native_attestation.py Wraps the optional KeyGuard attestation ABI.
msal/​_managed_identity_store.py Persists and restores public certificate bindings.
msal/​_managed_identity_binding.py Adds generation-bound sync and async transports.
docs/​managed-identity-v2.rst Documents installation, caller contracts, and validation profiles.
docs/​index.rst Links the Managed Identity v2 guide.
.Pipelines/​template-pipeline-stages.yml Adds an opt-in installed-candidate live stage.
.Pipelines/​CI-AND-RELEASE-PIPELINES.md Explains stacked-PR validation coverage.
.gitignore Excludes optional wheel staging outputs.
.github/​workflows/​python-package.yml Adds a stacked-PR Python unit-test matrix.
.github/​workflows/​native-primitives.yml Adds Windows native and Python integration CI.
.github/​dependabot.yml Adds Cargo monitoring for the native workspace.

Review effort: Balanced

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6fe4a5cf-79f6-48f9-aa71-bd8af58c504b
Copilot AI balanced review requested due to automatic review settings November 14, 2026 20:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Malformed cache metadata and case-sensitive header removal can break credential acquisition and request dispatch, while the installation fingerprint can miss an outdated token-cache module with the claims-logging defect.

2 open issues

Review effort: Balanced

if not isinstance(body, bytes):
raise _V2Error("Bound request bodies must be bounded bytes, not streams.",
stage="binding", error_code="unsupported_transport_option")
headers = dict(request.headers)
Comment on lines +151 to +152
client = metadata["client_id"]
uuid.UUID(client)
Build candidate wheels automatically and use the shared SAMI Key Vault fixture. Retain the full manual profile and allow an explicit MSAL_SKIP_MI_V2 opt-out.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 30f6db0b-e29c-474d-8fba-b560bd96f1bf
Copilot AI balanced review requested due to automatic review settings November 16, 2026 00:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The line-count script truncates files at the first #[cfg(test)], omitting subsequent production code and undercounting files such as crates/cng-provider/src/allocation.rs.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Exclude test-gated items without dropping later production code

native/​scripts/​loc.ps1:14

Stopping at the first #[cfg(test)] drops production code that follows a test-only item. For example, crates/cng-provider/src/allocation.rs is counted as only three lines, omitting the production allocators starting at line 86. Exclude individual test-gated items rather than discarding the rest of each file.

Review effort: Balanced

Keep the C++ toolchain off the managed-identity runner. Build the candidate on windows-2022 and download its same-run artifact for live tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 30f6db0b-e29c-474d-8fba-b560bd96f1bf
Copilot AI balanced review requested due to automatic review settings November 16, 2026 15:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Converting headers to a plain dict makes Content-Length handling case-sensitive, causing valid POST requests with lowercase content-length headers to be rejected by the native transport’s allowlist.

2 open issues

Review effort: Balanced

Explicitly publish the candidate artifact from the custom hosted build job before the managed-identity job downloads it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 30f6db0b-e29c-474d-8fba-b560bd96f1bf
Copilot AI balanced review requested due to automatic review settings November 16, 2026 15:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Converting prepared headers to a plain dict makes Content-Length removal case-sensitive, leaving lowercase variants to be rejected by the native header allowlist and causing valid GET requests to fail.

2 open issues

Review effort: Balanced

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.

2 participants