Skip to content

Commit e116552

Browse files
committed
Merge branch 'systemrootcerts-ignore-trusted-root-updates' into system-root-changes-needed
# Conflicts: # xds/src/main/java/io/grpc/xds/internal/security/SslContextProviderSupplier.java # xds/src/main/java/io/grpc/xds/internal/security/certprovider/CertProviderClientSslContextProvider.java # xds/src/test/java/io/grpc/xds/internal/security/SslContextProviderSupplierTest.java
2 parents 4417fcc + d2b722a commit e116552

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.grpc.xds.internal.security;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import com.google.common.annotations.VisibleForTesting;
2022
import com.google.common.base.MoreObjects;
2123
import io.grpc.netty.GrpcSslContexts;
@@ -30,8 +32,6 @@
3032
import javax.net.ssl.SSLException;
3133
import java.util.Set;
3234

33-
import static com.google.common.base.Preconditions.checkNotNull;
34-
3535
/**
3636
* Enables Client or server side to initialize this object with the received {@link BaseTlsContext}
3737
* and communicate it to the consumer i.e. {@link SecurityProtocolNegotiators}
@@ -66,8 +66,10 @@ public synchronized void updateSslContext(final SslContextProvider.Callback call
6666
sslContextProvider = getSslContextProvider(sni);
6767
}
6868
}
69-
7069
// we want to increment the ref-count so call findOrCreate again...
70+
final SslContextProvider toRelease = getSslContextProvider();
71+
toRelease.addCallback(
72+
new SslContextProvider.Callback(callback.getExecutor()) {
7173
final SslContextProvider toRelease = getSslContextProvider(sni);
7274
// When using system root certs on client side, SslContext updates via CertificateProvider is
7375
// only required if Mtls is also enabled, i.e. tlsContext has a cert provider instance.
@@ -86,19 +88,18 @@ public synchronized void updateSslContext(final SslContextProvider.Callback call
8688
toRelease.addCallback(
8789
new SslContextProvider.Callback(callback.getExecutor()) {
8890

89-
@Override
90-
public void updateSslContext(SslContext sslContext) {
91-
callback.updateSslContext(sslContext);
92-
releaseSslContextProvider(toRelease, sni);
93-
}
94-
95-
@Override
96-
public void onException(Throwable throwable) {
97-
callback.onException(throwable);
98-
releaseSslContextProvider(toRelease, sni);
99-
}
100-
});
101-
}
91+
@Override
92+
public void updateSslContext(SslContext sslContext) {
93+
callback.updateSslContext(sslContext);
94+
releaseSslContextProvider(toRelease, sni);
95+
}
96+
97+
@Override
98+
public void onException(Throwable throwable) {
99+
callback.onException(throwable);
100+
releaseSslContextProvider(toRelease, sni);
101+
}
102+
});
102103
} catch (final Throwable throwable) {
103104
callback.getExecutor().execute(new Runnable() {
104105
@Override

xds/src/main/java/io/grpc/xds/internal/security/certprovider/CertProviderClientSslContextProvider.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
import java.security.cert.X509Certificate;
2929
import java.util.Map;
3030
import javax.annotation.Nullable;
31+
import javax.net.ssl.SSLException;
3132

3233
/** A client SslContext provider using CertificateProviderInstance to fetch secrets. */
33-
public final class CertProviderClientSslContextProvider extends CertProviderSslContextProvider {
34-
35-
private final String sniForSanMatching;
34+
final class CertProviderClientSslContextProvider extends CertProviderSslContextProvider {
3635

3736
CertProviderClientSslContextProvider(
3837
Node node,
@@ -50,6 +49,17 @@ public final class CertProviderClientSslContextProvider extends CertProviderSslC
5049
staticCertValidationContext,
5150
upstreamTlsContext,
5251
certificateProviderStore);
52+
// Null rootCertInstance implies hasSystemRootCerts because of the check in
53+
// CertProviderClientSslContextProviderFactory.
54+
if (rootCertInstance == null && !isMtls()) {
55+
try {
56+
// Instantiate sslContext so that addCallback will immediately update the callback with
57+
// the SslContext.
58+
sslContext = getSslContextBuilder(staticCertificateValidationContext).build();
59+
} catch (SSLException | CertStoreException e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
5363
this.sniForSanMatching = upstreamTlsContext.getAutoSniSanValidation()? sniForSanMatching : null;
5464
}
5565

@@ -58,8 +68,6 @@ protected final SslContextBuilder getSslContextBuilder(
5868
CertificateValidationContext certificateValidationContext)
5969
throws CertStoreException {
6070
SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
61-
// Null rootCertInstance implies hasSystemRootCerts because of the check in
62-
// CertProviderClientSslContextProviderFactory.
6371
if (rootCertInstance != null) {
6472
if (savedSpiffeTrustMap != null) {
6573
sslContextBuilder = sslContextBuilder.trustManager(

xds/src/main/java/io/grpc/xds/internal/security/certprovider/CertProviderSslContextProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ private void updateSslContextWhenReady() {
162162
updateSslContext();
163163
clearKeysAndCerts();
164164
}
165-
} else if (isNormalTlsAndClientSide()) {
165+
} else if (isRegularTlsAndClientSide()) {
166166
if (savedTrustedRoots != null || savedSpiffeTrustMap != null) {
167167
updateSslContext();
168168
clearKeysAndCerts();
169169
}
170-
} else if (isNormalTlsAndServerSide()) {
170+
} else if (isRegularTlsAndServerSide()) {
171171
if (savedKey != null) {
172172
updateSslContext();
173173
clearKeysAndCerts();
@@ -186,14 +186,14 @@ protected final boolean isMtls() {
186186
return certInstance != null && (rootCertInstance != null || isUsingSystemRootCerts);
187187
}
188188

189-
protected final boolean isNormalTlsAndClientSide() {
189+
protected final boolean isRegularTlsAndClientSide() {
190190
// We don't do (rootCertInstance != null || isUsingSystemRootCerts) here because of how this
191191
// method is used. With the rootCertInstance being null when using system root certs, there
192192
// is nothing to update in the SslContext
193193
return rootCertInstance != null && certInstance == null;
194194
}
195195

196-
protected final boolean isNormalTlsAndServerSide() {
196+
protected final boolean isRegularTlsAndServerSide() {
197197
return certInstance != null && rootCertInstance == null;
198198
}
199199

xds/src/test/java/io/grpc/xds/internal/security/CommonTlsContextTestsUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ public static CommonTlsContext.Builder addCertificateValidationContext(
234234
String rootInstanceName,
235235
String rootCertName,
236236
CertificateValidationContext staticCertValidationContext) {
237+
if (staticCertValidationContext == null && rootInstanceName == null) {
238+
return builder;
239+
}
237240
CertificateValidationContext.Builder contextBuilder;
238241
if (staticCertValidationContext == null) {
239242
contextBuilder = CertificateValidationContext.newBuilder();

xds/src/test/java/io/grpc/xds/internal/security/certprovider/CertProviderClientSslContextProviderTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,7 @@ public void testProviderForClient_mtls() throws Exception {
189189
}
190190

191191
@Test
192-
/**
193-
* Note this route will not really be invoked since {@link SslContextProviderSupplier} will
194-
* shortcircuit creating the certificate provider and directly invoke the callback with the
195-
* SslContext in this case.
196-
*/
197-
public void testProviderForClient_systemRootCerts_regularTls() throws Exception {
192+
public void testProviderForClient_systemRootCerts_regularTls() {
198193
final CertificateProvider.DistributorWatcher[] watcherCaptor =
199194
new CertificateProvider.DistributorWatcher[1];
200195
TestCertificateProvider.createAndRegisterProviderProvider(
@@ -214,7 +209,10 @@ public void testProviderForClient_systemRootCerts_regularTls() throws Exception
214209
assertThat(provider.savedKey).isNull();
215210
assertThat(provider.savedCertChain).isNull();
216211
assertThat(provider.savedTrustedRoots).isNull();
217-
assertThat(provider.getSslContext()).isNull();
212+
assertThat(provider.getSslContext()).isNotNull();
213+
TestCallback testCallback =
214+
CommonTlsContextTestsUtil.getValueThruCallback(provider);
215+
assertThat(testCallback.updatedSslContext).isEqualTo(provider.getSslContext());
218216

219217
assertThat(watcherCaptor[0]).isNull();
220218
}

0 commit comments

Comments
 (0)