-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[xDS] A65 mTLS credentials in bootstrap (part 2) #12255
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
base: master
Are you sure you want to change the base?
Changes from all commits
52c06c0
82021c2
7e9f6c4
2556c97
4d51850
a7f01e4
f6a2379
3ea5749
afbc96e
434579a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import java.io.Closeable; | ||
|
||
/** | ||
* {@code ChannelCredentials} which holds allocated resources (e.g. file watchers) upon | ||
* instantiation of a given {@code ChannelCredentials} object, which must be closed once | ||
* mentioned {@code ChannelCredentials} are no longer in use. | ||
*/ | ||
public final class ResourceAllocatingChannelCredentials extends ChannelCredentials { | ||
public static ChannelCredentials create( | ||
ChannelCredentials channelCreds, ImmutableList<Closeable> resources) { | ||
return new ResourceAllocatingChannelCredentials(channelCreds, resources); | ||
} | ||
|
||
private final ChannelCredentials channelCreds; | ||
private final ImmutableList<Closeable> resources; | ||
|
||
private ResourceAllocatingChannelCredentials( | ||
ChannelCredentials channelCreds, ImmutableList<Closeable> resources) { | ||
this.channelCreds = Preconditions.checkNotNull(channelCreds, "channelCreds"); | ||
this.resources = Preconditions.checkNotNull(resources, "resources"); | ||
} | ||
|
||
public ChannelCredentials getChannelCredentials() { | ||
return channelCreds; | ||
} | ||
|
||
public ImmutableList<Closeable> getAllocatedResources() { | ||
return resources; | ||
} | ||
|
||
@Override | ||
public ChannelCredentials withoutBearerTokens() { | ||
return channelCreds.withoutBearerTokens(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.io.Closeable; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link ResourceAllocatingChannelCredentials}. */ | ||
@RunWith(JUnit4.class) | ||
public class ResourceAllocatingChannelCredentialsTest { | ||
@Test | ||
public void withoutBearerTokenDelegatesCall() { | ||
ChannelCredentials channelChreds = new ChannelCredentials() { | ||
@Override | ||
public ChannelCredentials withoutBearerTokens() { | ||
return this; | ||
} | ||
}; | ||
ImmutableList<Closeable> resources = ImmutableList.<Closeable>of(); | ||
ChannelCredentials creds = | ||
ResourceAllocatingChannelCredentials.create(channelChreds, resources); | ||
assertThat(creds.withoutBearerTokens()).isEqualTo(channelChreds); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.grpc.ChannelCredentials; | ||
import com.google.common.collect.ImmutableSet; | ||
import io.grpc.internal.JsonUtil; | ||
import io.grpc.xds.client.BootstrapperImpl; | ||
import io.grpc.xds.client.XdsInitializationException; | ||
|
@@ -90,47 +90,43 @@ protected String getJsonContent() throws XdsInitializationException, IOException | |
} | ||
|
||
@Override | ||
protected Object getImplSpecificConfig(Map<String, ?> serverConfig, String serverUri) | ||
protected ImmutableMap<String, ?> getImplSpecificConfig(Map<String, ?> serverConfig, | ||
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. All of this plumbing change should go back to the old way. We need to parse early so that it is actually validated. Instead, we communicate through the ChannelCredentials. (I'll make a comment elsewhere.) |
||
String serverUri) | ||
throws XdsInitializationException { | ||
return getChannelCredentials(serverConfig, serverUri); | ||
return getChannelCredentialsConfig(serverConfig, serverUri); | ||
} | ||
|
||
private static ChannelCredentials getChannelCredentials(Map<String, ?> serverConfig, | ||
String serverUri) | ||
private static ImmutableMap<String, ?> getChannelCredentialsConfig(Map<String, ?> serverConfig, | ||
String serverUri) | ||
throws XdsInitializationException { | ||
List<?> rawChannelCredsList = JsonUtil.getList(serverConfig, "channel_creds"); | ||
if (rawChannelCredsList == null || rawChannelCredsList.isEmpty()) { | ||
throw new XdsInitializationException( | ||
"Invalid bootstrap: server " + serverUri + " 'channel_creds' required"); | ||
} | ||
ChannelCredentials channelCredentials = | ||
ImmutableMap<String, ?> channelCredentialsConfig = | ||
parseChannelCredentials(JsonUtil.checkObjectList(rawChannelCredsList), serverUri); | ||
if (channelCredentials == null) { | ||
if (channelCredentialsConfig == null) { | ||
throw new XdsInitializationException( | ||
"Server " + serverUri + ": no supported channel credentials found"); | ||
} | ||
return channelCredentials; | ||
return channelCredentialsConfig; | ||
} | ||
|
||
@Nullable | ||
private static ChannelCredentials parseChannelCredentials(List<Map<String, ?>> jsonList, | ||
String serverUri) | ||
private static ImmutableMap<String, ?> parseChannelCredentials(List<Map<String, ?>> jsonList, | ||
String serverUri) | ||
throws XdsInitializationException { | ||
for (Map<String, ?> channelCreds : jsonList) { | ||
String type = JsonUtil.getString(channelCreds, "type"); | ||
if (type == null) { | ||
throw new XdsInitializationException( | ||
"Invalid bootstrap: server " + serverUri + " with 'channel_creds' type unspecified"); | ||
} | ||
XdsCredentialsProvider provider = XdsCredentialsRegistry.getDefaultRegistry() | ||
.getProvider(type); | ||
if (provider != null) { | ||
Map<String, ?> config = JsonUtil.getObject(channelCreds, "config"); | ||
if (config == null) { | ||
config = ImmutableMap.of(); | ||
} | ||
|
||
return provider.newChannelCredentials(config); | ||
ImmutableSet<String> supportedNames = XdsCredentialsRegistry.getDefaultRegistry() | ||
.getSupportedCredentialNames(); | ||
if (supportedNames.contains(type)) { | ||
return ImmutableMap.copyOf(channelCreds); | ||
} | ||
} | ||
return null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.grpc.CallCredentials; | ||
import io.grpc.CallOptions; | ||
import io.grpc.ChannelCredentials; | ||
|
@@ -28,9 +30,15 @@ | |
import io.grpc.ManagedChannel; | ||
import io.grpc.Metadata; | ||
import io.grpc.MethodDescriptor; | ||
import io.grpc.ResourceAllocatingChannelCredentials; | ||
import io.grpc.Status; | ||
import io.grpc.internal.GrpcUtil; | ||
import io.grpc.internal.JsonUtil; | ||
import io.grpc.xds.client.Bootstrapper; | ||
import io.grpc.xds.client.XdsInitializationException; | ||
import io.grpc.xds.client.XdsTransportFactory; | ||
import java.io.Closeable; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
final class GrpcXdsTransportFactory implements XdsTransportFactory { | ||
|
@@ -42,7 +50,7 @@ final class GrpcXdsTransportFactory implements XdsTransportFactory { | |
} | ||
|
||
@Override | ||
public XdsTransport create(Bootstrapper.ServerInfo serverInfo) { | ||
public XdsTransport create(Bootstrapper.ServerInfo serverInfo) throws XdsInitializationException { | ||
return new GrpcXdsTransport(serverInfo, callCredentials); | ||
} | ||
|
||
|
@@ -56,8 +64,9 @@ static class GrpcXdsTransport implements XdsTransport { | |
|
||
private final ManagedChannel channel; | ||
private final CallCredentials callCredentials; | ||
private final ImmutableList<Closeable> resources; | ||
|
||
public GrpcXdsTransport(Bootstrapper.ServerInfo serverInfo) { | ||
public GrpcXdsTransport(Bootstrapper.ServerInfo serverInfo) throws XdsInitializationException { | ||
this(serverInfo, null); | ||
} | ||
|
||
|
@@ -66,9 +75,27 @@ public GrpcXdsTransport(ManagedChannel channel) { | |
this(channel, null); | ||
} | ||
|
||
public GrpcXdsTransport(Bootstrapper.ServerInfo serverInfo, CallCredentials callCredentials) { | ||
public GrpcXdsTransport(Bootstrapper.ServerInfo serverInfo, CallCredentials callCredentials) | ||
throws XdsInitializationException { | ||
String target = serverInfo.target(); | ||
ChannelCredentials channelCredentials = (ChannelCredentials) serverInfo.implSpecificConfig(); | ||
Map<String, ?> implSpecificConfig = serverInfo.implSpecificConfig(); | ||
String type = JsonUtil.getString(implSpecificConfig, "type"); | ||
XdsCredentialsProvider provider = XdsCredentialsRegistry.getDefaultRegistry() | ||
.getProvider(type); | ||
Map<String, ?> config = JsonUtil.getObject(implSpecificConfig, "config"); | ||
if (config == null) { | ||
config = ImmutableMap.of(); | ||
} | ||
ChannelCredentials channelCredentials = provider.newChannelCredentials(config); | ||
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. Instead of creating the ChannelCredentials now, which is necessary because your ResourceAllocatingChannelCredentials API can only release the resources once, we want to share the credentials and keep track of how many usages there are. public final class ResourceAllocatingChannelCredentials extends ChannelCredentials {
public static ChannelCredentials create(
ChannelCredentials channelCreds, Supplier<Closeable> resource) {
return new ResourceAllocatingChannelCredentials(channelCreds, resource);
}
private final ChannelCredentials channelCreds;
private final Closeable resourceReleaser;
private final int refCount;
// constructor...
public synchronized ChannelCredentials acquireChannelCredentials() {
if (refCount++ == 0) {
this.resourceReleaser = resource.get();
}
return channelCreds;
}
public synchronized releaseChannelCredentials() {
if (--refCount == 0) {
resourceReleaser.close();
resourceReleaser = null;
}
assert refCount >= 0;
}
@Override
public ChannelCredentials withoutBearerTokens() {
// Could be implemented, but unnecessary. Would need to return a ResourceAllocatingChannelCredentials or similar class
throw new UnsupportedOperationException();
}
} Unfortunately, the error handling in That's not really what we want, as we don't want a transient failure to become permanent, but it seems really hard to "correctly," so I think we should accept something lesser for the while. |
||
if (channelCredentials == null) { | ||
throw new XdsInitializationException( | ||
"Cannot create channel credentials of type " + type + " for target " + target); | ||
} | ||
// if {@code ChannelCredentials} instance has allocated resource of any type, save them to be | ||
// released once the channel is shutdown | ||
this.resources = (channelCredentials instanceof ResourceAllocatingChannelCredentials) | ||
? ((ResourceAllocatingChannelCredentials) channelCredentials).getAllocatedResources() | ||
: ImmutableList.<Closeable>of(); | ||
this.channel = Grpc.newChannelBuilder(target, channelCredentials) | ||
.keepAliveTime(5, TimeUnit.MINUTES) | ||
.build(); | ||
|
@@ -79,6 +106,7 @@ public GrpcXdsTransport(Bootstrapper.ServerInfo serverInfo, CallCredentials call | |
public GrpcXdsTransport(ManagedChannel channel, CallCredentials callCredentials) { | ||
this.channel = checkNotNull(channel, "channel"); | ||
this.callCredentials = callCredentials; | ||
this.resources = ImmutableList.<Closeable>of(); | ||
} | ||
|
||
@Override | ||
|
@@ -99,6 +127,9 @@ public <ReqT, RespT> StreamingCall<ReqT, RespT> createStreamingCall( | |
@Override | ||
public void shutdown() { | ||
channel.shutdown(); | ||
for (Closeable resource : resources) { | ||
GrpcUtil.closeQuietly(resource); | ||
} | ||
} | ||
|
||
private class XdsStreamingCall<ReqT, RespT> implements | ||
|
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.
Put this in XDS. We don't want a new public type to workaround our internals.