Skip to content

Commit c9e327d

Browse files
authored
xds: extend SslContextProviderSupplier to DowmstreamTlsContext for server side (#8146)
1 parent 27b1641 commit c9e327d

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private void updateMaxConcurrentRequests(@Nullable Long maxConcurrentRequests) {
264264
private void updateSslContextProviderSupplier(@Nullable UpstreamTlsContext tlsContext) {
265265
UpstreamTlsContext currentTlsContext =
266266
sslContextProviderSupplier != null
267-
? sslContextProviderSupplier.getUpstreamTlsContext()
267+
? (UpstreamTlsContext)sslContextProviderSupplier.getTlsContext()
268268
: null;
269269
if (Objects.equals(currentTlsContext, tlsContext)) {
270270
return;

xds/src/main/java/io/grpc/xds/internal/sds/SslContextProviderSupplier.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,82 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
2121

22+
import io.grpc.xds.EnvoyServerProtoData.BaseTlsContext;
23+
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
2224
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
2325
import io.netty.handler.ssl.SslContext;
2426

2527
/**
26-
* Enables the CDS policy to initialize this object with the received {@link UpstreamTlsContext} &
27-
* communicate it to the consumer i.e. {@link SdsProtocolNegotiators.ClientSdsProtocolNegotiator}
28+
* Enables Client or server side to initialize this object with the received {@link BaseTlsContext}
29+
* and communicate it to the consumer i.e. {@link SdsProtocolNegotiators}
2830
* to lazily evaluate the {@link SslContextProvider}. The supplier prevents credentials leakage in
29-
* cases where the user is not using xDS credentials but the CDS policy contains a non-default
30-
* {@link UpstreamTlsContext}.
31+
* cases where the user is not using xDS credentials but the client/server contains a non-default
32+
* {@link BaseTlsContext}.
3133
*/
3234
public final class SslContextProviderSupplier implements Closeable {
3335

34-
private final UpstreamTlsContext upstreamTlsContext;
36+
private final BaseTlsContext tlsContext;
3537
private final TlsContextManager tlsContextManager;
3638
private SslContextProvider sslContextProvider;
3739
private boolean shutdown;
3840

3941
public SslContextProviderSupplier(
40-
UpstreamTlsContext upstreamTlsContext, TlsContextManager tlsContextManager) {
41-
this.upstreamTlsContext = upstreamTlsContext;
42+
BaseTlsContext tlsContext, TlsContextManager tlsContextManager) {
43+
this.tlsContext = tlsContext;
4244
this.tlsContextManager = tlsContextManager;
4345
}
4446

45-
public UpstreamTlsContext getUpstreamTlsContext() {
46-
return upstreamTlsContext;
47+
public BaseTlsContext getTlsContext() {
48+
return tlsContext;
4749
}
4850

4951
/** Updates SslContext via the passed callback. */
5052
public synchronized void updateSslContext(final SslContextProvider.Callback callback) {
5153
checkNotNull(callback, "callback");
5254
checkState(!shutdown, "Supplier is shutdown!");
5355
if (sslContextProvider == null) {
54-
sslContextProvider =
55-
tlsContextManager.findOrCreateClientSslContextProvider(upstreamTlsContext);
56+
sslContextProvider = getSslContextProvider();
5657
}
5758
// we want to increment the ref-count so call findOrCreate again...
58-
final SslContextProvider toRelease =
59-
tlsContextManager.findOrCreateClientSslContextProvider(upstreamTlsContext);
59+
final SslContextProvider toRelease = getSslContextProvider();
6060
sslContextProvider.addCallback(
6161
new SslContextProvider.Callback(callback.getExecutor()) {
6262

6363
@Override
6464
public void updateSecret(SslContext sslContext) {
6565
callback.updateSecret(sslContext);
66-
tlsContextManager.releaseClientSslContextProvider(toRelease);
66+
releaseSslContextProvider(toRelease);
6767
}
6868

6969
@Override
7070
public void onException(Throwable throwable) {
7171
callback.onException(throwable);
72-
tlsContextManager.releaseClientSslContextProvider(toRelease);
72+
releaseSslContextProvider(toRelease);
7373
}
7474
});
7575
}
7676

77-
/** Called by {@link io.grpc.xds.CdsLoadBalancer} when upstreamTlsContext changes. */
77+
private void releaseSslContextProvider(SslContextProvider toRelease) {
78+
if (tlsContext instanceof UpstreamTlsContext) {
79+
tlsContextManager.releaseClientSslContextProvider(toRelease);
80+
} else {
81+
tlsContextManager.releaseServerSslContextProvider(toRelease);
82+
}
83+
}
84+
85+
private SslContextProvider getSslContextProvider() {
86+
return tlsContext instanceof UpstreamTlsContext
87+
? tlsContextManager.findOrCreateClientSslContextProvider((UpstreamTlsContext) tlsContext)
88+
: tlsContextManager.findOrCreateServerSslContextProvider((DownstreamTlsContext) tlsContext);
89+
}
90+
91+
/** Called by consumer when tlsContext changes. */
7892
@Override
7993
public synchronized void close() {
80-
if (sslContextProvider != null) {
94+
if (tlsContext instanceof UpstreamTlsContext) {
8195
tlsContextManager.releaseClientSslContextProvider(sslContextProvider);
96+
} else {
97+
tlsContextManager.releaseServerSslContextProvider(sslContextProvider);
8298
}
8399
shutdown = true;
84100
}

xds/src/test/java/io/grpc/xds/ClusterImplLoadBalancerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private void subtest_endpointAddressesAttachedWithTlsConfig(boolean enableSecuri
520520
SslContextProviderSupplier supplier =
521521
eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
522522
if (enableSecurity) {
523-
assertThat(supplier.getUpstreamTlsContext()).isEqualTo(upstreamTlsContext);
523+
assertThat(supplier.getTlsContext()).isEqualTo(upstreamTlsContext);
524524
} else {
525525
assertThat(supplier).isNull();
526526
}
@@ -554,7 +554,7 @@ private void subtest_endpointAddressesAttachedWithTlsConfig(boolean enableSecuri
554554
SslContextProviderSupplier supplier =
555555
eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
556556
if (enableSecurity) {
557-
assertThat(supplier.getUpstreamTlsContext()).isEqualTo(upstreamTlsContext);
557+
assertThat(supplier.getTlsContext()).isEqualTo(upstreamTlsContext);
558558
} else {
559559
assertThat(supplier).isNull();
560560
}

0 commit comments

Comments
 (0)