-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(auth): Add support for Regional Access Boundaries #13499
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
Open
vverman
wants to merge
4
commits into
googleapis:main
Choose a base branch
from
vverman:regional-access-boundaries-main-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c1505b4
feat(auth): Introduce Regional Access Boundaries for GoogleCredential…
vverman 461843c
chore(auth): Address remaining Regional Access Boundary feedback (#12…
vverman 02e8bdf
fix(auth): Skip RAB lookup in case of non-email response from MDS. (#…
vverman a674758
rebase-fixes
vverman 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,11 +31,14 @@ | |
|
|
||
| package com.google.auth.oauth2; | ||
|
|
||
| import static com.google.auth.oauth2.OAuth2Utils.WORKFORCE_AUDIENCE_PATTERN; | ||
| import static com.google.auth.oauth2.OAuth2Utils.WORKLOAD_AUDIENCE_PATTERN; | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.client.http.HttpHeaders; | ||
| import com.google.api.client.json.GenericJson; | ||
| import com.google.api.client.util.Data; | ||
| import com.google.api.core.InternalApi; | ||
| import com.google.auth.RequestMetadataCallback; | ||
| import com.google.auth.http.HttpTransportFactory; | ||
| import com.google.common.base.MoreObjects; | ||
|
|
@@ -54,6 +57,7 @@ | |
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import javax.annotation.Nullable; | ||
|
|
||
|
|
@@ -63,7 +67,8 @@ | |
| * <p>Handles initializing external credentials, calls to the Security Token Service, and service | ||
| * account impersonation. | ||
| */ | ||
| public abstract class ExternalAccountCredentials extends GoogleCredentials { | ||
| public abstract class ExternalAccountCredentials extends GoogleCredentials | ||
| implements RegionalAccessBoundaryProvider { | ||
|
|
||
| private static final long serialVersionUID = 8049126194174465023L; | ||
|
|
||
|
|
@@ -577,6 +582,11 @@ protected AccessToken exchangeExternalCredentialForAccessToken( | |
| */ | ||
| public abstract String retrieveSubjectToken() throws IOException; | ||
|
|
||
| @Override | ||
| HttpTransportFactory getTransportFactory() { | ||
| return transportFactory; | ||
| } | ||
|
|
||
| public String getAudience() { | ||
| return audience; | ||
| } | ||
|
|
@@ -620,6 +630,37 @@ public String getServiceAccountEmail() { | |
| return ImpersonatedCredentials.extractTargetPrincipal(serviceAccountImpersonationUrl); | ||
| } | ||
|
|
||
| @InternalApi | ||
| @Override | ||
| public String getRegionalAccessBoundaryUrl() throws IOException { | ||
| if (getServiceAccountEmail() != null) { | ||
| return String.format( | ||
| OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT, | ||
| getServiceAccountEmail()); | ||
| } | ||
|
|
||
| Matcher workforceMatcher = WORKFORCE_AUDIENCE_PATTERN.matcher(getAudience()); | ||
| if (workforceMatcher.matches()) { | ||
| String poolId = workforceMatcher.group("pool"); | ||
| return String.format( | ||
| OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL, poolId); | ||
| } | ||
|
|
||
| Matcher workloadMatcher = WORKLOAD_AUDIENCE_PATTERN.matcher(getAudience()); | ||
|
Comment on lines
+642
to
+649
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The String audience = getAudience();
if (audience == null) {
throw new IllegalStateException(
"The audience is null, which is not in a valid format for either a workload identity pool or a workforce pool.");
}
Matcher workforceMatcher = WORKFORCE_AUDIENCE_PATTERN.matcher(audience);
if (workforceMatcher.matches()) {
String poolId = workforceMatcher.group("pool");
return String.format(
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL, poolId);
}
Matcher workloadMatcher = WORKLOAD_AUDIENCE_PATTERN.matcher(audience); |
||
| if (workloadMatcher.matches()) { | ||
| String projectNumber = workloadMatcher.group("project"); | ||
| String poolId = workloadMatcher.group("pool"); | ||
| return String.format( | ||
| OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKLOAD_POOL, | ||
| projectNumber, | ||
| poolId); | ||
| } | ||
|
|
||
| throw new IllegalStateException( | ||
| "The provided audience is not in a valid format for either a workload identity pool or a workforce pool." | ||
| + " Refer: https://docs.cloud.google.com/iam/docs/principal-identifiers"); | ||
| } | ||
|
|
||
| @Nullable | ||
| public String getClientId() { | ||
| return clientId; | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getAudience()method is annotated with@Nullableand can returnnull. CallingWORKFORCE_AUDIENCE_PATTERN.matcher(getAudience())directly without a null check will throw aNullPointerExceptionif the audience is null. We should add a null check and throw anIllegalStateExceptionwith a clear message.