Repository navigation
Provision Entra API identifier URIs, scopes, and caller permissions - #21278
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21278Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21278" |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved permission-validation and provisioning defects can grant unintended access, retain removed configuration, or miswire API dependencies.
6 open issues
Reject whitespace in delegated permission names · New Clear pre-authorized applications when the final client is removed · New Let default URI selection override custom URI · New Clear required resource access when final declaration is removed · New Clear OAuth scopes when the final scope is retired · New Use collision-safe identities for generated Bicep identifiers · New
What changed in this PR
Extends Aspire.Hosting.Azure.EntraId to provision API identities and caller permissions through generated Microsoft Graph Bicep, addressing #21070 while keeping existing registrations configuration-only.
Changes:
- Adds application ID URIs, explicit delegated scopes with stable IDs, and client pre-authorization.
- Separates permission declarations from explicit admin consent for modeled and external APIs.
- Adds validation, dependency-cycle checks, documentation, and generated-artifact tests.
| File | Description |
|---|---|
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithScope_UsesDefaultApplicationIdUriSetBySecondDeclaration.verified.bicep |
Captures default URI and scope provisioning. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithPreAuthorizedApplication_WritesClientIdsAndScopeIds.verified.bicep |
Captures client pre-authorization. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithApplicationIdUri_Default_ExposesApiWithoutScopes.verified.bicep |
Captures default URI without scopes. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithApplicationIdUri_Custom_IsWrittenToTheApplicationWithoutASecondDeclaration.verified.bicep |
Captures custom URI and scope options. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithApiPermissions_ExistingApi_UsesConfiguredScopeIdsAndClientId.verified.bicep |
Captures permissions targeting existing APIs. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithApiPermissions_Declares_ButDoesNotGrantOrConsent.verified.bicep |
Captures declarations without consent. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithApiPermissions_AcceptsPermissionsOutsideTheCatalogById.verified.bicep |
Captures explicit permission IDs. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.WithAdminConsent_GrantsDelegatedScopesToAllUsersAndAppRolesToTheServicePrincipal.verified.bicep |
Captures delegated grants and role assignments. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.Publish_WiresApiOutputsIntoTheCallerModuleAndPreAuthorizedClientsIntoTheApiModule#02.verified.bicep |
Captures the caller module. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.Publish_WiresApiOutputsIntoTheCallerModuleAndPreAuthorizedClientsIntoTheApiModule#01.verified.bicep |
Captures the API module. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.Publish_WiresApiOutputsIntoTheCallerModuleAndPreAuthorizedClientsIntoTheApiModule#00.verified.bicep |
Captures cross-module dependencies. |
tests/Aspire.Hosting.Azure.EntraId.Tests/Snapshots/EntraIdApiProvisioningTests.ExistingRegistrationsDoNotCreateDependencyCycles.verified.bicep |
Captures existing-client references without cycles. |
tests/Aspire.Hosting.Azure.EntraId.Tests/EntraIdApiProvisioningTests.cs |
Tests validation, stable IDs, consent separation, and publishing. |
src/Aspire.Hosting.Azure.EntraId/README.md |
Documents API exposure, consent, and scope retirement. |
src/Aspire.Hosting.Azure.EntraId/Provisioning/MicrosoftGraphServicePrincipal.cs |
Adds existing service-principal lookup by application ID. |
src/Aspire.Hosting.Azure.EntraId/Provisioning/MicrosoftGraphPermissionGrants.cs |
Models delegated grants and app-role assignments. |
src/Aspire.Hosting.Azure.EntraId/Provisioning/MicrosoftGraphApplication.cs |
Adds Graph application permission properties. |
src/Aspire.Hosting.Azure.EntraId/Provisioning/MicrosoftGraphApiConstructs.cs |
Models scopes, pre-authorization, and required access. |
src/Aspire.Hosting.Azure.EntraId/Provisioning/EntraIdBicepConstants.cs |
Adds output and grant resource constants. |
src/Aspire.Hosting.Azure.EntraId/Provisioning/EntraIdApiProvisioning.cs |
Generates API configuration and consent resources. |
src/Aspire.Hosting.Azure.EntraId/EntraIdValidation.cs |
Validates application ID URIs and scope names. |
src/Aspire.Hosting.Azure.EntraId/EntraIdResourceExtensions.cs |
Adds fluent configuration APIs and dependency validation. |
src/Aspire.Hosting.Azure.EntraId/EntraIdApplicationResource.cs |
Stores API configuration and derives scope IDs and URIs. |
src/Aspire.Hosting.Azure.EntraId/EntraIdApiAccess.cs |
Tracks declarations, consent, and pre-authorized clients. |
src/Aspire.Hosting.Azure.EntraId/EntraIdApi.cs |
Defines API catalogs, permission types, and scope options. |
Review effort: Balanced
Eric Erhardt (eerhardt)
left a comment
There was a problem hiding this comment.
Code review: 5 findings, none duplicating the existing threads. Categories: 2 correctness, 1 validation, 1 documentation, 1 test coverage.
Add application ID URI, exposed scopes, pre-authorization, required resource access, and opt-in admin consent (delegated grants and app role assignments) to Entra ID application registrations, including first-party APIs such as Microsoft Graph. Fixes #21070 Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
0c686b9 to
fc7b0d8
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
Scope validation accepts names that Microsoft Graph rejects during provisioning, and consent prerequisites need correction.
8 open issues
Clear pre-authorized applications when the final client is removed Reject whitespace in delegated permission names Reject scope names beginning with a period · New Use collision-safe identities for generated Bicep identifiers Clear OAuth scopes when the final scope is retired Clear required resource access when final declaration is removed Let default URI selection override custom URI Document required roles for Microsoft Graph application permissions · New
Review effort: Balanced
…ation
Referencing a client registration now injects one Microsoft.Identity.Web
DownstreamApis entry per API it declared permissions to. Entries for APIs
modeled in the AppHost are named after the resources that reference them, like
service discovery; first-party APIs use their display name. Delegated scopes
are full scope URIs, and application permissions are a separate {name}AppOnly
entry using .default. WithReference accepts downstreamApisConfigKey to change
the root key. Referencing a registration that exposes scopes also sets
AzureAd__Scopes.
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
Eric Erhardt (eerhardt)
left a comment
There was a problem hiding this comment.
Code review of the latest commit and the full PR: 4 new findings (2 correctness, 1 design/validation, 1 test coverage). Existing threads were not duplicated.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical issues can grant undeclared scopes, retain removed client pre-authorizations, and miswire or mix API permissions through naming and configuration collisions.
10 open issues
Detect downstream API configuration key collisions · New Clear pre-authorized applications when the final client is removed Reject whitespace in delegated permission names Restrict OpenID scope filtering to Microsoft Graph · New Reject scope names beginning with a period Use collision-safe identities for generated Bicep identifiers Clear OAuth scopes when the final scope is retired Clear required resource access when final declaration is removed Let default URI selection override custom URI Document required roles for Microsoft Graph application permissions
Review effort: Balanced
…e URIs
- EntraIdApi.FromApplicationId takes the API's application ID URI instead of
assuming api://{appId}.
- An existing API registration without an application ID URI is rejected, not
guessed.
- Two API registrations referenced by the same resource that a client declares
permissions to are rejected before startup by a validate-{name}-api-access
pipeline step, with an actionable message.
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
- Validate permission and scope names against Graph's value rules (no spaces, no leading period, allowed characters), so a name can't smuggle a second permission into a consent grant. - Reject an explicit permission ID that belongs to a different catalog name. - WithApplicationIdUri() overrides an earlier custom URI; reject file URIs consistently across platforms. - Limit the OpenID scope exclusion in DownstreamApis to Microsoft Graph. - Send an API's scope and pre-authorization lists in full, even when empty, once it is declared, so the last scope or pre-authorized client can be removed. - Correct the admin-consent role guidance for Microsoft Graph application permissions and the docs for the exceptions that are thrown. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
Resource URI handling can produce incorrect token audiences, and the new deployment behavior lacks automated end-to-end regression coverage.
7 open issues
Detect downstream API configuration key collisions Clear pre-authorized applications when the final client is removed Preserve trailing slashes in resource URIs · New Add deployment regression coverage for API and caller updates · New Use collision-safe identities for generated Bicep identifiers Clear OAuth scopes when the final scope is retired Clear required resource access when final declaration is removed
5 resolved since last review
Review effort: Balanced
Parameters and resources written for API permissions were named from display text, so orders-api and ordersApi, or two custom APIs with one display name, produced the same Bicep symbol. A repeated parameter binding silently kept only the last API. Names are now allocated per template from the API's identity (resource name or application ID), made unique against everything else in the template, and always start with a letter. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
This comment has been minimized.
This comment has been minimized.
Graph can reject api://{clientId} on an application that it created seconds earlier, the same replication lag as the service principal and federated credential errors that the deployment helper already retries.
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
|
🚀 Deployment tests starting on PR #21278... This will deploy to real Azure infrastructure. Results will be posted here when complete. |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
Fresh registrations can fail their first deployment because the provisioning path lacks bounded, cancellation-aware recovery for transient application ID replication delays.
1 open issue
1 resolved since last review
Review effort: Balanced
## Description Adds `EntraIdAppRole` and `WithAppRole(EntraIdAppRole)` to declare application roles (for example `User` and `Admin`) on Entra registrations created by Aspire. Roles are written to the application's `appRoles` in the generated Graph Bicep. - Role IDs are derived from the resource name and role value (or supplied with `id`), so they stay stable across deployments. The tenant resource suffix is not part of the ID. - `AllowedMemberTypes` are open-ended strings passed through to Graph (default `["User"]`). - Existing registrations are never modified: `WithAppRole` throws for them. - `AppRoles` on the resource is a read-only list; roles are added only through `WithAppRole`. **Removal behavior** (verified against a real tenant using Bicep generated from this code): - Dropping a role that is still enabled fails with `CannotDeleteOrUpdateEnabledEntitlement`, and the directory is unchanged. - Deploying the role with `isEnabled: false` and then dropping it succeeds. - `appRoles` is sent only when the application declares at least one role, matching the restraint of the scopes work in #21278. To retire the last role, leave it declared with `isEnabled: false`. This is documented in the README. **Not included** (follow-ups): role assignments (`Microsoft.Graph/appRoleAssignedTo`), consent, and real-token `roles`/audience verification, which depends on assignments. This will conflict with #21278 in `MicrosoftGraphApplication` and the README, so whichever lands second needs a rebase. Part of #21083. ## Checklist - Is this feature complete? - [ ] Yes. Ready to ship. - [x] No. Follow-up changes expected. - Are you including unit tests for the changes and scenario tests if relevant? - [x] Yes - [ ] No - Did you add public API? - [x] Yes - If yes, did you have an API Review for it? - [ ] Yes - [x] No - Did you add `<remarks />` and `<code />` elements on your triple slash comments? - [x] Yes - [ ] No - [ ] No - Does the change make any security assumptions or guarantees? - [ ] Yes - If yes, have you done a threat model and had a security review? - [ ] Yes - [ ] No - [x] No Fixes: #21083 --------- Co-authored-by: Copilot App <[email protected]> Copilot-Session: cfaafd3e-55f3-48d0-a51e-8da68dcbeb54
…entifier-uris-scopes Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟡 Changes recommended
The README can leave scope authorization unenforced, disabled scopes can still be emitted, and Graph configuration uses incorrect endpoints for sovereign clouds.
3 open issues
1 resolved since last review
Review effort: Balanced
Re-validate scope references when the model is complete, so a scope disabled after WithApiPermissions or WithPreAuthorizedApplication fails with a clear error. Add an optional baseUrl to EntraIdApi.FromApplicationId so sovereign-cloud APIs can be defined. Show RequiredScope with its configuration key and Authorize in the README. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 1a24c9b5-39fc-4b24-ac51-9b12595eaac5
Tests selector2 / 100 PR test projects · 1 PR job, from 39 changed files. Selected PR test projects (2 / 100)
Selected PR jobs (1)
How these were chosen — grouped by what changed🔧 🔧 🔧 🔧 🔧 🔧 🔧 🔧 🔧 🔧 🔧 🔧 🔧 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 Job reasons
Selection computed for commit |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
…exporter No conflicts. The incoming change (#21278) only touches the Entra ID integration and its tests. Co-authored-by: Copilot App <[email protected]>



Description
Completes the delegated web-to-API provisioning slice for
Aspire.Hosting.Azure.EntraIdthrough Azure.Provisioning-generated Graph Bicep.API side
WithApplicationIdUri()/WithApplicationIdUri(string): defaultapi://{clientId}or a custom URI.WithScope(name, description, configure?): explicit delegated scopes with stable, derived GUIDs (or an explicitEntraIdScope.Id).WithPreAuthorizedApplication(client, scopes).EntraIdApplicationResource.GetScopeUri(scope)builds the full scope name.Caller side
WithApiPermissions(api, scopes)for Aspire-modeled APIs andWithApiPermissions(EntraIdApi, EntraIdApiPermission[])for first-party APIs (EntraIdApi.MicrosoftGraph,AzureResourceManager,AzureStorage,FromApplicationId). Delegated and application permissions stay distinct, names resolve through a catalog, and unknown names are rejected.WithAdminConsent(...)is the only thing that grants anything: delegated scopes become anAllPrincipalsoauth2PermissionGrant, application permissions becomeappRoleAssignedTo. Declaring a permission never consents. A permission must be declared before it is consented.Configuration flow to consumers
DownstreamApisentry per API it declared permissions to:{root}__{name}__Scopes__{n}, with full scope URIs for delegated permissions. Application permissions (first-party APIs) are a separate{name}AppOnlyentry using.defaultandRequestAppToken=true. Graph and ARM also get aBaseUrl.{name}is the name of each resource that references it (like service discovery); first-party APIs use their display name. Entries with the same name throw.WithReference(entra, connectionName, downstreamApisConfigKey)changes the root key (defaultDownstreamApis).AzureAd__Scopes(the enabled scope names, for[RequiredScope]).Design decisions validated against a real tenant
api://{clientId}): a second declaration of the same application under the sameuniqueNamesetsidentifierUrisfrom the first declaration'sappId. Graph treats this as a PATCH, so nothing from the first declaration is lost.api://{clientId}is what default tenant policy accepts; a custom URI is passed through and fails the deployment if tenant policy rejects it.CannotDeleteOrUpdateEnabledEntitlement). SettingIsEnabled = falseand deploying, then removing, works and is documented.WithScopewith an explicit ID andWithApplicationIdUriare config).Validation
dotnet testforAspire.Hosting.Azure.EntraId.Tests: 350 passed (newEntraIdApiProvisioningTestsuse Verify snapshots of generated Bicep, plus publish tests for cross-module wiring).main.bicepplus modules) to a test tenant, twice for idempotency:identifierUris, scopes, pre-authorization,requiredResourceAccess,AllPrincipalsgrants and the Graph app role assignment were all created and stable across redeployment.aud= the API's client ID,scp= the requested scope, with no consent prompt.roles=User.Read.All, and a Graph call succeeded. App-only token for the API:aud= the API's client ID.AzureEntraIdDeploymentTestsHex1b E2E, since it cannot run locally. The scenarios above were verified manually.Fixes #21070
Checklist
<remarks />and<code />elements on your triple slash comments?