Repository navigation
[Identity] Add GitHubActionsCredential #50916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vcolin7
merged 18 commits into
Azure:main
from
vcolin7:vcolin7-github-actions-credential
Nov 5, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b91d08d
Add GitHub Actions credential to Azure Identity
vcolin7 b159374
Add GitHub Actions credential to Identity v2
vcolin7 da6a7c8
Add GitHub Actions credential live tests
vcolin7 76bf195
Merge commit 'e492fbe63363558821fc267818b62e5f42886289' into vcolin7-…
vcolin7 b13ac2c
Merge commit '50c204e32a363f1b122cfd4b0cb74c7127c4853f' into vcolin7-…
vcolin7 480854b
Merge commit '51525d7c1fad6eb26fc3dc30958f034ed6bf7f0f' into vcolin7-…
vcolin7 a8f2b48
Merge commit 'ed5c42f371e12815c6055572b0e11d5a13eb3d61' into vcolin7-…
vcolin7 ed53422
Merge commit 'e827d94d5eb45e92a7400a4cc59271b55aeb1329' into vcolin7-…
vcolin7 b10fac2
Merge commit '806284f9d27a64cc45b5f487e2de0802dd16debe' into vcolin7-…
vcolin7 9ff1aea
Improve GitHub Actions credential setup diagnostics
vcolin7 51e6b0e
Updated CSpell files
vcolin7 b05b5df
Relocated snippet ffiles for AZure Identity V2
vcolin7 7691857
Fix GitHub Actions credential retries and CAE handling
vcolin7 e173b38
Align GitHub Actions credential error messages
vcolin7 00d231a
Use the default authority for GitHub Actions live tests
vcolin7 61b37e9
Removed unnecessary changelog entries
vcolin7 b3f76e7
Merge origin/main into GitHub Actions credential branch
vcolin7 394223e
Updated CHANGELOGs
vcolin7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| name: Identity GitHub Actions credential | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - vcolin7-github-actions-credential | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| authenticate: | ||
| name: ${{ matrix.package }} | ||
| runs-on: ubuntu-24.04 | ||
| timeout-minutes: 30 | ||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| include: | ||
| - package: com.azure:azure-identity | ||
| pom: sdk/identity/azure-identity/pom.xml | ||
| - package: com.azure.v2:azure-identity | ||
| pom: sdk/identity-v2/azure-identity/pom.xml | ||
| env: | ||
| AZURE_TENANT_ID: ${{ vars.AZURE_IDENTITY_GITHUB_ACTIONS_TENANT_ID }} | ||
| AZURE_CLIENT_ID: ${{ vars.AZURE_IDENTITY_GITHUB_ACTIONS_CLIENT_ID }} | ||
| AZURE_TEST_MODE: LIVE | ||
| RUN_GITHUB_ACTIONS_CREDENTIAL_LIVE_TESTS: 'true' | ||
| SDK_COORDINATES: ${{ matrix.package }} | ||
| SDK_POM: ${{ matrix.pom }} | ||
| steps: | ||
| - name: Check test identity configuration | ||
| shell: bash | ||
| run: | | ||
| if [[ -z "$AZURE_TENANT_ID" || -z "$AZURE_CLIENT_ID" ]]; then | ||
| echo "::error::Set AZURE_IDENTITY_GITHUB_ACTIONS_TENANT_ID and AZURE_IDENTITY_GITHUB_ACTIONS_CLIENT_ID repository variables." | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Checkout code | ||
| uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up JDK 25 | ||
| uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0 | ||
| with: | ||
| distribution: temurin | ||
| java-version: '25' | ||
| cache: maven | ||
|
|
||
| - name: Build the selected SDK and its dependencies | ||
| shell: bash | ||
| run: > | ||
| mvn -B -ntp -f pom.xml -pl "$SDK_COORDINATES" -am | ||
| install -DskipTests -Djacoco.skip=true -Dgpg.skip=true | ||
|
|
||
| - name: Authenticate through GitHub OIDC and Microsoft Entra | ||
| shell: bash | ||
| run: > | ||
| mvn -B -ntp -f "$SDK_POM" | ||
| test -Dtest=GitHubActionsCredentialLiveTest |
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
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
154 changes: 154 additions & 0 deletions
154
...entity-v2/azure-identity/src/main/java/com/azure/v2/identity/GitHubActionsCredential.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.v2.identity; | ||
|
|
||
| import com.azure.v2.core.credentials.TokenCredential; | ||
| import com.azure.v2.core.credentials.TokenRequestContext; | ||
| import com.azure.v2.identity.exceptions.CredentialAuthenticationException; | ||
| import com.azure.v2.identity.implementation.models.ConfidentialClientOptions; | ||
| import io.clientcore.core.credentials.oauth.AccessToken; | ||
| import io.clientcore.core.http.models.HttpHeaderName; | ||
| import io.clientcore.core.http.models.HttpHeaders; | ||
| import io.clientcore.core.http.models.HttpMethod; | ||
| import io.clientcore.core.http.models.HttpRequest; | ||
| import io.clientcore.core.http.models.Response; | ||
| import io.clientcore.core.instrumentation.logging.ClientLogger; | ||
| import io.clientcore.core.models.CoreException; | ||
| import io.clientcore.core.models.binarydata.BinaryData; | ||
| import io.clientcore.core.serialization.json.JsonReader; | ||
| import io.clientcore.core.serialization.json.JsonToken; | ||
| import io.clientcore.core.utils.CoreUtils; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Authenticates an application in GitHub Actions using an OIDC federated identity credential. | ||
| * | ||
| * <p>Configure a federated identity credential on the Microsoft Entra application that trusts the GitHub workflow's | ||
| * issuer, subject, and audience. The workflow needs {@code permissions: id-token: write}. GitHub supplies the | ||
| * OIDC request URL and bearer token. The client and tenant IDs default to {@code AZURE_CLIENT_ID} and | ||
| * {@code AZURE_TENANT_ID}; they can also be set on {@link GitHubActionsCredentialBuilder}.</p> | ||
| * | ||
| * <p>The assertion audience is selected from the authority host. This credential supports Azure Public, China, | ||
| * US Government, France Government, Germany Government, and Singapore Government. It is not included in | ||
| * {@link DefaultAzureCredential}.</p> | ||
| * | ||
| * <!-- src_embed com.azure.v2.identity.credential.githubactionscredential.construct --> | ||
| * <pre> | ||
| * GitHubActionsCredential credential = new GitHubActionsCredentialBuilder().build(); | ||
| * </pre> | ||
| * <!-- end com.azure.v2.identity.credential.githubactionscredential.construct --> | ||
| * | ||
| * @see GitHubActionsCredentialBuilder | ||
| */ | ||
| public class GitHubActionsCredential implements TokenCredential { | ||
| private static final ClientLogger LOGGER = new ClientLogger(GitHubActionsCredential.class); | ||
| private final ClientAssertionCredential clientAssertionCredential; | ||
|
|
||
| GitHubActionsCredential(String requestUrl, String requestToken, ConfidentialClientOptions options) { | ||
| options.setClientAssertionFunction(pipeline -> { | ||
| HttpRequest request = new HttpRequest().setMethod(HttpMethod.GET) | ||
| .setUri(requestUrl) | ||
| .setHeaders(new HttpHeaders().set(HttpHeaderName.AUTHORIZATION, "Bearer " + requestToken)); | ||
| try (Response<BinaryData> response = pipeline.send(request)) { | ||
| if (response.getStatusCode() != 200) { | ||
| throw authenticationError("OIDC request returned status code " + response.getStatusCode() + "."); | ||
| } | ||
| return readAssertion(response); | ||
| } | ||
| }); | ||
| clientAssertionCredential = new ClientAssertionCredential(options); | ||
| } | ||
|
|
||
| /** | ||
| * Acquires an access token, using the token cache when possible. | ||
| * | ||
| * <p>Requests with a Continuous Access Evaluation (CAE) claims challenge acquire a new token instead of reusing a | ||
| * cached token.</p> | ||
| * | ||
| * @param request the scopes, tenant, and authentication options for the token request. | ||
| * @return the access token. | ||
| * @throws CredentialAuthenticationException if the GitHub OIDC request fails or returns an invalid assertion response. | ||
| */ | ||
| @Override | ||
| public AccessToken getToken(TokenRequestContext request) { | ||
| try { | ||
| return clientAssertionCredential.getToken(request); | ||
| } catch (CoreException e) { | ||
| // Preserve the OIDC failure category through client-assertion delegation. | ||
| Throwable cause = e.getCause(); | ||
| if (cause instanceof CredentialAuthenticationException) { | ||
| throw (CredentialAuthenticationException) cause; | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private static String readAssertion(Response<BinaryData> response) { | ||
| BinaryData body = response.getValue(); | ||
| String text = body == null ? null : body.toString(); | ||
| if (CoreUtils.isNullOrEmpty(text)) { | ||
| throw authenticationError("Received null token from OIDC request."); | ||
| } | ||
| try (JsonReader reader = JsonReader.fromString(text)) { | ||
| JsonToken token = reader.nextToken(); | ||
| if (token == JsonToken.END_DOCUMENT) { | ||
| throw authenticationError("Failed to parse OIDC response."); | ||
| } | ||
| String assertion = null; | ||
| if (token == JsonToken.START_OBJECT) { | ||
| assertion = reader.readObject(objectReader -> { | ||
| String value = null; | ||
| while (objectReader.nextToken() != JsonToken.END_OBJECT) { | ||
| String fieldName = objectReader.getFieldName(); | ||
| objectReader.nextToken(); | ||
| if ("value".equals(fieldName)) { | ||
| if (objectReader.currentToken() == JsonToken.STRING) { | ||
| value = objectReader.getString(); | ||
| } else { | ||
| value = null; | ||
| objectReader.skipChildren(); | ||
| } | ||
| } else { | ||
| objectReader.skipChildren(); | ||
| } | ||
| } | ||
| return value; | ||
| }); | ||
| } else { | ||
| reader.skipChildren(); | ||
| } | ||
| if (reader.nextToken() != JsonToken.END_DOCUMENT) { | ||
| throw authenticationError("Failed to parse OIDC response."); | ||
| } | ||
| if (CoreUtils.isNullOrEmpty(assertion)) { | ||
| throw authenticationError("\"value\" field not detected in the response."); | ||
| } | ||
| return assertion; | ||
| } catch (IOException | IllegalStateException e) { | ||
| String message = "GitHubActionsCredential: Authentication Failed. Failed to parse OIDC response."; | ||
| throw LOGGER.throwableAtError() | ||
| .log(message, sanitizedCause(e), | ||
| (ignored, cause) -> new CredentialAuthenticationException(message, cause)); | ||
| } | ||
| } | ||
|
|
||
| // Parser and URI exception messages can contain secrets. Retain their type and stack trace, not their input. | ||
| static RuntimeException sanitizedCause(Exception exception) { | ||
| if (exception == null) { | ||
| return null; | ||
| } | ||
| RuntimeException sanitized | ||
| = new IllegalArgumentException(exception.getClass().getSimpleName() + " (input redacted)"); | ||
| sanitized.setStackTrace(exception.getStackTrace()); | ||
| return sanitized; | ||
| } | ||
|
|
||
| private static CredentialAuthenticationException authenticationError(String message) { | ||
| throw LOGGER.throwableAtError() | ||
| .addKeyValue("reason", message) | ||
| .log("GitHubActionsCredential: Authentication Failed.", ignored -> new CredentialAuthenticationException( | ||
| "GitHubActionsCredential: Authentication Failed. " + message)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.