-
Notifications
You must be signed in to change notification settings - Fork 263
Fix: Make derived classes of CredentialSource public #1236
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
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4c6838e
fix: Make derived classes of Credential Source public
mario-vimal 3efb366
fix: Lint
mario-vimal 14e5a78
move nested public class outside
mario-vimal b32392e
make CredentialSource public
mario-vimal 05eec70
Rename CredentialSource to ExternalAccountCredentialSource
mario-vimal a798e39
fix format
mario-vimal 22853b1
Merge remote-tracking branch 'upstream/main' into CredentialSource_pu…
mario-vimal 129693b
Move ExternalAccountCredentialSource back to ExternalAccountCredential
mario-vimal ae2b44e
Fix comment
mario-vimal ed78703
optimize imports
mario-vimal e9f4bc8
Merge branch 'main' into CredentialSource_public
aeitzman 28ed6a7
Merge branch 'main' into CredentialSource_public
lsirac c2902e9
Merge branch 'main' into CredentialSource_public
aeitzman 18038b6
chore(deps): update dependency com.google.auto.service:auto-service-a…
renovate-bot cda439a
Merge branch 'main' into CredentialSource_public
mario-vimal 2368a9e
Merge branch 'main' into CredentialSource_public
lsirac 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
move nested public class outside
- Loading branch information
commit 14e5a78cf27970d835f767f9e377528f8edebc81
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
oauth2_http/java/com/google/auth/oauth2/AwsCredentialSource.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,101 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package com.google.auth.oauth2; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * The AWS credential source. Stores data required to retrieve the AWS credential from the AWS | ||
| * metadata server. | ||
| */ | ||
| public class AwsCredentialSource extends ExternalAccountCredentials.CredentialSource { | ||
aeitzman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| static final String IMDSV2_SESSION_TOKEN_URL_FIELD_NAME = "imdsv2_session_token_url"; | ||
| static final long serialVersionUID = -4180558200808134436L; | ||
|
|
||
| final String regionUrl; | ||
| final String url; | ||
| final String regionalCredentialVerificationUrl; | ||
| final String imdsv2SessionTokenUrl; | ||
|
|
||
| /** | ||
| * The source of the AWS credential. The credential source map must contain the | ||
| * `regional_cred_verification_url` field. | ||
| * | ||
| * <p>The `regional_cred_verification_url` is the regional GetCallerIdentity action URL, used to | ||
| * determine the account ID and its roles. | ||
| * | ||
| * <p>The `environment_id` is the environment identifier, in the format “aws${version}”. This | ||
| * indicates whether breaking changes were introduced to the underlying AWS implementation. | ||
| * | ||
| * <p>The `region_url` identifies the targeted region. Optional. | ||
| * | ||
| * <p>The `url` locates the metadata server used to retrieve the AWS credentials. Optional. | ||
| */ | ||
| public AwsCredentialSource(Map<String, Object> credentialSourceMap) { | ||
| super(credentialSourceMap); | ||
| if (!credentialSourceMap.containsKey("regional_cred_verification_url")) { | ||
| throw new IllegalArgumentException( | ||
| "A regional_cred_verification_url representing the" | ||
| + " GetCallerIdentity action URL must be specified."); | ||
| } | ||
|
|
||
| String environmentId = (String) credentialSourceMap.get("environment_id"); | ||
|
|
||
| // Environment version is prefixed by "aws". e.g. "aws1". | ||
| Matcher matcher = Pattern.compile("(aws)([\\d]+)").matcher(environmentId); | ||
| if (!matcher.matches()) { | ||
| throw new IllegalArgumentException("Invalid AWS environment ID."); | ||
| } | ||
|
|
||
| int environmentVersion = Integer.parseInt(matcher.group(2)); | ||
| if (environmentVersion != 1) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "AWS version %s is not supported in the current build.", environmentVersion)); | ||
| } | ||
|
|
||
| this.regionUrl = (String) credentialSourceMap.get("region_url"); | ||
| this.url = (String) credentialSourceMap.get("url"); | ||
| this.regionalCredentialVerificationUrl = | ||
| (String) credentialSourceMap.get("regional_cred_verification_url"); | ||
|
|
||
| if (credentialSourceMap.containsKey(IMDSV2_SESSION_TOKEN_URL_FIELD_NAME)) { | ||
| this.imdsv2SessionTokenUrl = | ||
| (String) credentialSourceMap.get(IMDSV2_SESSION_TOKEN_URL_FIELD_NAME); | ||
| } else { | ||
| this.imdsv2SessionTokenUrl = null; | ||
| } | ||
| } | ||
| } | ||
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
130 changes: 130 additions & 0 deletions
130
oauth2_http/java/com/google/auth/oauth2/IdentityPoolCredentialSource.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,130 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package com.google.auth.oauth2; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * The IdentityPool credential source. Dictates the retrieval method of the external credential, | ||
| * which can either be through a metadata server or a local file. | ||
| */ | ||
| public class IdentityPoolCredentialSource extends ExternalAccountCredentials.CredentialSource { | ||
|
|
||
| private static final long serialVersionUID = -745855247050085694L; | ||
| IdentityPoolCredentialSourceType credentialSourceType; | ||
| CredentialFormatType credentialFormatType; | ||
| String credentialLocation; | ||
| @Nullable String subjectTokenFieldName; | ||
| @Nullable Map<String, String> headers; | ||
|
|
||
| /** | ||
| * The source of the 3P credential. | ||
| * | ||
| * <p>If this is a file based 3P credential, the credentials file can be retrieved using the | ||
| * `file` key. | ||
| * | ||
| * <p>If this is URL-based 3p credential, the metadata server URL can be retrieved using the `url` | ||
| * key. | ||
| * | ||
| * <p>The third party credential can be provided in different formats, such as text or JSON. The | ||
| * format can be specified using the `format` header, which returns a map with keys `type` and | ||
| * `subject_token_field_name`. If the `type` is json, the `subject_token_field_name` must be | ||
| * provided. If no format is provided, we expect the token to be in the raw text format. | ||
| * | ||
| * <p>Optional headers can be present, and should be keyed by `headers`. | ||
| */ | ||
| public IdentityPoolCredentialSource(Map<String, Object> credentialSourceMap) { | ||
| super(credentialSourceMap); | ||
|
|
||
| if (credentialSourceMap.containsKey("file") && credentialSourceMap.containsKey("url")) { | ||
| throw new IllegalArgumentException( | ||
| "Only one credential source type can be set, either file or url."); | ||
| } | ||
|
|
||
| if (credentialSourceMap.containsKey("file")) { | ||
| credentialLocation = (String) credentialSourceMap.get("file"); | ||
| credentialSourceType = IdentityPoolCredentialSourceType.FILE; | ||
| } else if (credentialSourceMap.containsKey("url")) { | ||
| credentialLocation = (String) credentialSourceMap.get("url"); | ||
| credentialSourceType = IdentityPoolCredentialSourceType.URL; | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| "Missing credential source file location or URL. At least one must be specified."); | ||
| } | ||
|
|
||
| Map<String, String> headersMap = (Map<String, String>) credentialSourceMap.get("headers"); | ||
| if (headersMap != null && !headersMap.isEmpty()) { | ||
| headers = new HashMap<>(); | ||
| headers.putAll(headersMap); | ||
| } | ||
|
|
||
| // If the format is not provided, we expect the token to be in the raw text format. | ||
| credentialFormatType = CredentialFormatType.TEXT; | ||
|
|
||
| Map<String, String> formatMap = (Map<String, String>) credentialSourceMap.get("format"); | ||
| if (formatMap != null && formatMap.containsKey("type")) { | ||
| String type = formatMap.get("type"); | ||
|
|
||
| if (type != null && "json".equals(type.toLowerCase(Locale.US))) { | ||
| // For JSON, the subject_token field name must be provided. | ||
| if (!formatMap.containsKey("subject_token_field_name")) { | ||
| throw new IllegalArgumentException( | ||
| "When specifying a JSON credential type, the subject_token_field_name must be set."); | ||
| } | ||
| credentialFormatType = CredentialFormatType.JSON; | ||
| subjectTokenFieldName = formatMap.get("subject_token_field_name"); | ||
| } else if (type != null && "text".equals(type.toLowerCase(Locale.US))) { | ||
| credentialFormatType = CredentialFormatType.TEXT; | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| String.format("Invalid credential source format type: %s.", type)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| boolean hasHeaders() { | ||
| return headers != null && !headers.isEmpty(); | ||
| } | ||
|
|
||
| enum IdentityPoolCredentialSourceType { | ||
| FILE, | ||
| URL | ||
| } | ||
|
|
||
| enum CredentialFormatType { | ||
| TEXT, | ||
| JSON | ||
| } | ||
| } |
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.