-
Notifications
You must be signed in to change notification settings - Fork 231
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
feat: Adds support for user defined subject token suppliers in AWSCredentials and IdentityPoolCredentials #1336
Merged
Merged
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
b21127a
feat: adds programmatic auth credentials for identity pool and aws cr…
aeitzman e6457d9
feat: add quality of life improvements for building external account …
aeitzman 448a8ec
fix: formatting
aeitzman 59eb856
fix: add formatting
aeitzman f2ab1a2
Merge remote-tracking branch 'upstream/main' into fix_builders
aeitzman 0495b7f
Adds @CanIgnoreReturnValue on new builder methods
aeitzman 8d12e07
Merge remote-tracking branch 'upstream/main' into programmatic-auth
aeitzman a8b2f92
Change test for impersonated credentials
aeitzman 616fb13
formatting
aeitzman b2552eb
adding id_token type
aeitzman d32e19c
Merge branch 'fix_builders' into programmatic-auth
aeitzman 6726160
formatting
aeitzman 2fc4f99
Update oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java
aeitzman e5a9c59
PR comments
aeitzman bfb83fa
Added header value constants
aeitzman 6e7a975
Merge branch 'main' into programmatic-auth
lsirac 164ac25
updating java doc
aeitzman a257e55
adding integration tests
aeitzman f09adfa
fix tests
aeitzman 97946b3
fix tests, add javadoc, and format
aeitzman eb08391
PR review comments
aeitzman 5ae2645
Update oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java
aeitzman 61f6ae5
PR comments
aeitzman e43d708
changing to aws_region instead of region to clarify usage and keep re…
aeitzman bd4604f
Merge branch 'main' into programmatic-auth
aeitzman 08bde82
Merge branch 'main' into programmatic-auth
lsirac 4a22e08
Adding Aws Security Credential Providers
aeitzman f40743e
Merge remote-tracking branch 'upstream/main' into programmatic-auth
aeitzman f480b84
Adding identity pool providers
aeitzman 188b803
PR comments
aeitzman e69108e
fix test
aeitzman 4f3e253
refactoring to expose rename provider to supplier and expose it publicly
aeitzman c22a4e9
Merge branch 'main' into programmatic-auth
aeitzman dd659c4
formatting
aeitzman 9cd8d96
Merge branch 'main' into programmatic-auth
lsirac 8963d68
updating codeowners
aeitzman f4cadd2
Merge branch 'main' into programmatic-auth
lsirac 20c2f7d
make subject token supplier interface public
aeitzman abd462b
Making AwsSecurityCredentials public and change name to sessionToken
aeitzman 9835df7
lint
aeitzman d06eb36
Merge branch 'main' into programmatic-auth
aeitzman d59fb92
Merge remote-tracking branch 'upstream/main' into programmatic-auth
aeitzman 4371463
fix tests
aeitzman 5b21010
lint
aeitzman 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
384 changes: 199 additions & 185 deletions
384
oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java
Large diffs are not rendered by default.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
oauth2_http/java/com/google/auth/oauth2/AwsSecurityCredentialsSupplier.java
This file contains 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,58 @@ | ||
/* | ||
* Copyright 2024 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.io.IOException; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* Supplier for retrieving AWS Security credentials for {@Link AwsCredentials} to exchange for GCP | ||
* access tokens. | ||
*/ | ||
public interface AwsSecurityCredentialsSupplier extends Serializable { | ||
|
||
/** | ||
* Gets the AWS region to use. | ||
* | ||
* @return the AWS region that should be used for the credential. | ||
* @throws IOException | ||
*/ | ||
String getRegion() throws IOException; | ||
|
||
/** | ||
* Gets AWS security credentials. | ||
* | ||
* @return valid AWS security credentials that can be exchanged for a GCP access token. | ||
* @throws IOException | ||
*/ | ||
AwsSecurityCredentials getCredentials() throws IOException; | ||
} |
This file contains 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 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
101 changes: 101 additions & 0 deletions
101
oauth2_http/java/com/google/auth/oauth2/FileIdentityPoolSubjectTokenSupplier.java
This file contains 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 2024 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 com.google.api.client.json.GenericJson; | ||
import com.google.api.client.json.JsonObjectParser; | ||
import com.google.auth.oauth2.IdentityPoolCredentialSource.CredentialFormatType; | ||
import com.google.common.io.CharStreams; | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.LinkOption; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Internal provider for retrieving subject tokens for {@Link IdentityPoolCredentials} to exchange | ||
* for GCP access tokens via a local file. | ||
*/ | ||
class FileIdentityPoolSubjectTokenSupplier implements IdentityPoolSubjectTokenSupplier { | ||
|
||
private final long serialVersionUID = 2475549052347431992L; | ||
|
||
private final IdentityPoolCredentialSource credentialSource; | ||
|
||
/** | ||
* Constructor for FileIdentitySubjectTokenProvider | ||
* | ||
* @param credentialSource the credential source to use. | ||
*/ | ||
FileIdentityPoolSubjectTokenSupplier(IdentityPoolCredentialSource credentialSource) { | ||
this.credentialSource = credentialSource; | ||
} | ||
|
||
@Override | ||
public String getSubjectToken() throws IOException { | ||
String credentialFilePath = this.credentialSource.credentialLocation; | ||
if (!Files.exists(Paths.get(credentialFilePath), LinkOption.NOFOLLOW_LINKS)) { | ||
throw new IOException( | ||
String.format( | ||
"Invalid credential location. The file at %s does not exist.", credentialFilePath)); | ||
} | ||
try { | ||
return parseToken(new FileInputStream(new File(credentialFilePath)), this.credentialSource); | ||
} catch (IOException e) { | ||
throw new IOException( | ||
"Error when attempting to read the subject token from the credential file.", e); | ||
} | ||
} | ||
|
||
static String parseToken(InputStream inputStream, IdentityPoolCredentialSource credentialSource) | ||
throws IOException { | ||
if (credentialSource.credentialFormatType == CredentialFormatType.TEXT) { | ||
BufferedReader reader = | ||
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); | ||
return CharStreams.toString(reader); | ||
} | ||
|
||
JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); | ||
GenericJson fileContents = | ||
parser.parseAndClose(inputStream, StandardCharsets.UTF_8, GenericJson.class); | ||
|
||
if (!fileContents.containsKey(credentialSource.subjectTokenFieldName)) { | ||
throw new IOException("Invalid subject token field name. No subject token was found."); | ||
} | ||
return (String) fileContents.get(credentialSource.subjectTokenFieldName); | ||
} | ||
} |
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.
I understand it was technically not final before, could you please remind why? Does it change multiple times? if once - it can be final and can make the rest of the logic simpler
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 issue with having impersonated credentials final is that the ImpersonatedCredentials object is built by passing the current credential as the SourceCredential. If we have it final, the Impersonated Credential needs to get created in the parent constructor before the child constructor runs, which means the credential object that gets passed as the source credential is not fully instantiated. As a series of steps, the exact problem is:
Since the source credential passed to the impersonated credential isn't actually correct here we have 1 of two options if we want any child class specific builder setters
1: Set the impersonatedCredentialOverride in the child constructor and just use that, ignoring the "final" impersonatedCredentials. This is what we do for executableCredentials, but the issue is that its kind of a weird pattern that we just have the unused and incorrect impersonatedCredentials object attached to the credential, and that it means we can't do any input validation on the child credential.
2: Make impersonatedCredentials not final, and just set it when we use it the first time when retrieving an access token. This lets us get rid of the override, and makes it so the impersonatedCredential is never built with the incorrect source credential.