-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[xDS] A97 - JWT token file call creds #12242
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
Zgoda91
wants to merge
15
commits into
grpc:master
Choose a base branch
from
Zgoda91:A97_jwt_token_call_creds
base: master
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
15 commits
Select commit
Hold shift + click to select a range
6b79ff2
JWT token file call creds
Zgoda91 f7fe53a
Moving JwtToken related classes to xds package
Zgoda91 494439c
Fixing bazel build issues
Zgoda91 f3d5108
Fixing typo in documentation
Zgoda91 13881d8
Removing separate CallCredentials from ServerInfo
Zgoda91 b61de62
Default implementation for `newCallCredentials`
Zgoda91 fb3805a
Removed redundant method
Zgoda91 bdcfc0a
Reverted change from transport factory
Zgoda91 dd1de65
Improved JWT Token utils lib
Zgoda91 76e9e8f
Moved JWT call credentials to internal package
Zgoda91 3a36865
Removed hardcoded class list for Android
Zgoda91 743c248
Improved `JwtTokenFileXdsCredentialsProvider`
Zgoda91 20a50c8
Inverted condition statements for readability
Zgoda91 b7c4e55
Added new provider and registry classes
Zgoda91 ae8d9e3
Activated new registry class in bootstrapping code
Zgoda91 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
47 changes: 47 additions & 0 deletions
47
xds/src/main/java/io/grpc/xds/XdsCallCredentialsProvider.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,47 @@ | ||
/* | ||
* Copyright 2025 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.xds; | ||
|
||
import io.grpc.CallCredentials; | ||
import io.grpc.Internal; | ||
import java.util.Map; | ||
|
||
/** | ||
* Provider of credentials data which will be propagated to the server for each RPC. The actual call | ||
* credential to be used for a particular xDS communication will be chosen based on the bootstrap | ||
* configuration. | ||
*/ | ||
@Internal | ||
public abstract class XdsCallCredentialsProvider { | ||
/** | ||
* Creates a {@link CallCredentials} from the given jsonConfig, or | ||
* {@code null} if the given config is invalid. The provider is free to ignore | ||
* the config if it's not needed for producing the call credentials. | ||
* | ||
* @param jsonConfig json config that can be consumed by the provider to create | ||
* the call credentials | ||
* | ||
*/ | ||
protected abstract CallCredentials newCallCredentials(Map<String, ?> jsonConfig); | ||
|
||
/** | ||
* Returns the xDS call credential name associated with this provider which makes it selectable | ||
* via {@link XdsCallCredentialsRegistry#getProvider}. This is called only when the class is | ||
* loaded. It shouldn't change, and there is no point doing so. | ||
*/ | ||
protected abstract String getName(); | ||
} |
76 changes: 76 additions & 0 deletions
76
xds/src/main/java/io/grpc/xds/XdsCallCredentialsRegistry.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,76 @@ | ||
/* | ||
* Copyright 2025 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.xds; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.grpc.xds.internal.JwtTokenFileXdsCallCredentialsProvider; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** | ||
* Registry of {@link XdsCallCredentialsProvider}s. The {@link #getDefaultRegistry default | ||
* instance} loads hardcoded providers at runtime. | ||
*/ | ||
@ThreadSafe | ||
final class XdsCallCredentialsRegistry { | ||
private static XdsCallCredentialsRegistry instance; | ||
|
||
private final Map<String, XdsCallCredentialsProvider> registeredProviders = | ||
new HashMap<>(); | ||
|
||
/** | ||
* Returns the default registry that loads hardcoded providers at runtime. | ||
*/ | ||
public static synchronized XdsCallCredentialsRegistry getDefaultRegistry() { | ||
if (instance == null) { | ||
instance = newRegistry().register(new JwtTokenFileXdsCallCredentialsProvider()); | ||
} | ||
return instance; | ||
} | ||
|
||
@VisibleForTesting | ||
static XdsCallCredentialsRegistry newRegistry() { | ||
return new XdsCallCredentialsRegistry(); | ||
} | ||
|
||
@VisibleForTesting | ||
XdsCallCredentialsRegistry register(XdsCallCredentialsProvider... providers) { | ||
for (XdsCallCredentialsProvider provider : providers) { | ||
registeredProviders.put(provider.getName(), provider); | ||
} | ||
return this; | ||
} | ||
|
||
@VisibleForTesting | ||
synchronized Map<String, XdsCallCredentialsProvider> providers() { | ||
return registeredProviders; | ||
} | ||
|
||
/** | ||
* Returns the registered provider for the given xDS call credential name, or {@code null} if no | ||
* suitable provider can be found. | ||
* Each provider declares its name via {@link XdsCallCredentialsProvider#getName}. | ||
*/ | ||
@Nullable | ||
public synchronized XdsCallCredentialsProvider getProvider(String name) { | ||
return registeredProviders.get(checkNotNull(name, "name")); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
xds/src/main/java/io/grpc/xds/internal/JwtTokenFileCallCredentials.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,66 @@ | ||
/* | ||
* Copyright 2025 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.xds.internal; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.api.client.json.gson.GsonFactory; | ||
import com.google.api.client.json.webtoken.JsonWebSignature; | ||
import com.google.auth.oauth2.AccessToken; | ||
import com.google.auth.oauth2.OAuth2Credentials; | ||
import com.google.common.io.Files; | ||
import io.grpc.CallCredentials; | ||
import io.grpc.auth.MoreCallCredentials; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Date; | ||
|
||
/** | ||
* JWT token file call credentials. | ||
* See gRFC A97 (https://github.com/grpc/proposal/pull/492). | ||
*/ | ||
public final class JwtTokenFileCallCredentials extends OAuth2Credentials { | ||
private static final long serialVersionUID = 0L; | ||
private final String path; | ||
|
||
private JwtTokenFileCallCredentials(String path) { | ||
this.path = checkNotNull(path, "path"); | ||
} | ||
|
||
@Override | ||
public AccessToken refreshAccessToken() throws IOException { | ||
String tokenString = new String(Files.toByteArray(new File(path)), StandardCharsets.UTF_8); | ||
Long expTime = JsonWebSignature.parse(new GsonFactory(), tokenString) | ||
.getPayload() | ||
.getExpirationTimeSeconds(); | ||
if (expTime == null) { | ||
throw new IOException("No expiration time found for JWT token"); | ||
} | ||
|
||
return AccessToken.newBuilder() | ||
.setTokenValue(tokenString) | ||
.setExpirationTime(new Date(expTime * 1000L)) | ||
.build(); | ||
} | ||
|
||
// using {@link MoreCallCredentials} adapter to be compatible with {@link CallCredentials} iface | ||
public static CallCredentials create(String path) { | ||
JwtTokenFileCallCredentials jwtTokenFileCallCredentials = new JwtTokenFileCallCredentials(path); | ||
return MoreCallCredentials.from(jwtTokenFileCallCredentials); | ||
} | ||
} |
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.