Skip to content

Commit 4b0faec

Browse files
committed
xds: fix the validation code to accept new-style CertificateProviderPluginInstance wherever used (grpc#8892)
1 parent 2564020 commit 4b0faec

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public final class CommonTlsContextUtil {
2626
private CommonTlsContextUtil() {}
2727

2828
static boolean hasCertProviderInstance(CommonTlsContext commonTlsContext) {
29-
return commonTlsContext != null
30-
&& (commonTlsContext.hasTlsCertificateCertificateProviderInstance()
31-
|| hasCertProviderValidationContext(commonTlsContext));
29+
if (commonTlsContext == null) {
30+
return false;
31+
}
32+
return hasIdentityCertificateProviderInstance(commonTlsContext)
33+
|| hasCertProviderValidationContext(commonTlsContext);
3234
}
3335

3436
private static boolean hasCertProviderValidationContext(CommonTlsContext commonTlsContext) {
@@ -37,6 +39,19 @@ private static boolean hasCertProviderValidationContext(CommonTlsContext commonT
3739
commonTlsContext.getCombinedValidationContext();
3840
return combinedCertificateValidationContext.hasValidationContextCertificateProviderInstance();
3941
}
42+
return hasValidationProviderInstance(commonTlsContext);
43+
}
44+
45+
private static boolean hasIdentityCertificateProviderInstance(CommonTlsContext commonTlsContext) {
46+
return commonTlsContext.hasTlsCertificateProviderInstance()
47+
|| commonTlsContext.hasTlsCertificateCertificateProviderInstance();
48+
}
49+
50+
private static boolean hasValidationProviderInstance(CommonTlsContext commonTlsContext) {
51+
if (commonTlsContext.hasValidationContext() && commonTlsContext.getValidationContext()
52+
.hasCaCertificateProviderInstance()) {
53+
return true;
54+
}
4055
return commonTlsContext.hasValidationContextCertificateProviderInstance();
4156
}
4257

xds/src/test/java/io/grpc/xds/internal/sds/ClientSslContextProviderFactoryTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,72 @@ public void createCertProviderClientSslContextProvider_2providers()
208208
verifyWatcher(sslContextProvider, watcherCaptor[1]);
209209
}
210210

211+
@Test
212+
public void createNewCertProviderClientSslContextProvider_withSans() {
213+
final CertificateProvider.DistributorWatcher[] watcherCaptor =
214+
new CertificateProvider.DistributorWatcher[2];
215+
createAndRegisterProviderProvider(certificateProviderRegistry, watcherCaptor, "testca", 0);
216+
createAndRegisterProviderProvider(
217+
certificateProviderRegistry, watcherCaptor, "file_watcher", 1);
218+
219+
CertificateValidationContext staticCertValidationContext =
220+
CertificateValidationContext.newBuilder()
221+
.addAllMatchSubjectAltNames(
222+
ImmutableSet.of(
223+
StringMatcher.newBuilder().setExact("foo").build(),
224+
StringMatcher.newBuilder().setExact("bar").build()))
225+
.build();
226+
UpstreamTlsContext upstreamTlsContext =
227+
CommonTlsContextTestsUtil.buildNewUpstreamTlsContextForCertProviderInstance(
228+
"gcp_id",
229+
"cert-default",
230+
"file_provider",
231+
"root-default",
232+
/* alpnProtocols= */ null,
233+
staticCertValidationContext);
234+
235+
Bootstrapper.BootstrapInfo bootstrapInfo = CommonBootstrapperTestUtils.getTestBootstrapInfo();
236+
clientSslContextProviderFactory =
237+
new ClientSslContextProviderFactory(
238+
bootstrapInfo, certProviderClientSslContextProviderFactory);
239+
SslContextProvider sslContextProvider =
240+
clientSslContextProviderFactory.create(upstreamTlsContext);
241+
assertThat(sslContextProvider).isInstanceOf(CertProviderClientSslContextProvider.class);
242+
verifyWatcher(sslContextProvider, watcherCaptor[0]);
243+
verifyWatcher(sslContextProvider, watcherCaptor[1]);
244+
}
245+
246+
@Test
247+
public void createNewCertProviderClientSslContextProvider_onlyRootCert() {
248+
final CertificateProvider.DistributorWatcher[] watcherCaptor =
249+
new CertificateProvider.DistributorWatcher[1];
250+
createAndRegisterProviderProvider(certificateProviderRegistry, watcherCaptor, "testca", 0);
251+
CertificateValidationContext staticCertValidationContext =
252+
CertificateValidationContext.newBuilder()
253+
.addAllMatchSubjectAltNames(
254+
ImmutableSet.of(
255+
StringMatcher.newBuilder().setExact("foo").build(),
256+
StringMatcher.newBuilder().setExact("bar").build()))
257+
.build();
258+
UpstreamTlsContext upstreamTlsContext =
259+
CommonTlsContextTestsUtil.buildNewUpstreamTlsContextForCertProviderInstance(
260+
/* certInstanceName= */ null,
261+
/* certName= */ null,
262+
"gcp_id",
263+
"root-default",
264+
/* alpnProtocols= */ null,
265+
staticCertValidationContext);
266+
267+
Bootstrapper.BootstrapInfo bootstrapInfo = CommonBootstrapperTestUtils.getTestBootstrapInfo();
268+
clientSslContextProviderFactory =
269+
new ClientSslContextProviderFactory(
270+
bootstrapInfo, certProviderClientSslContextProviderFactory);
271+
SslContextProvider sslContextProvider =
272+
clientSslContextProviderFactory.create(upstreamTlsContext);
273+
assertThat(sslContextProvider).isInstanceOf(CertProviderClientSslContextProvider.class);
274+
verifyWatcher(sslContextProvider, watcherCaptor[0]);
275+
}
276+
211277
@Test
212278
public void createNullCommonTlsContext_exception() throws IOException {
213279
clientSslContextProviderFactory =

xds/src/test/java/io/grpc/xds/internal/sds/ServerSslContextProviderFactoryTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,41 @@ public void createCertProviderServerSslContextProvider_2providers()
206206
verifyWatcher(sslContextProvider, watcherCaptor[0]);
207207
verifyWatcher(sslContextProvider, watcherCaptor[1]);
208208
}
209+
210+
@Test
211+
public void createNewCertProviderServerSslContextProvider_withSans()
212+
throws XdsInitializationException {
213+
final CertificateProvider.DistributorWatcher[] watcherCaptor =
214+
new CertificateProvider.DistributorWatcher[2];
215+
createAndRegisterProviderProvider(certificateProviderRegistry, watcherCaptor, "testca", 0);
216+
createAndRegisterProviderProvider(
217+
certificateProviderRegistry, watcherCaptor, "file_watcher", 1);
218+
CertificateValidationContext staticCertValidationContext =
219+
CertificateValidationContext.newBuilder()
220+
.addAllMatchSubjectAltNames(
221+
ImmutableSet.of(
222+
StringMatcher.newBuilder().setExact("foo").build(),
223+
StringMatcher.newBuilder().setExact("bar").build()))
224+
.build();
225+
226+
DownstreamTlsContext downstreamTlsContext =
227+
CommonTlsContextTestsUtil.buildNewDownstreamTlsContextForCertProviderInstance(
228+
"gcp_id",
229+
"cert-default",
230+
"file_provider",
231+
"root-default",
232+
/* alpnProtocols= */ null,
233+
staticCertValidationContext,
234+
/* requireClientCert= */ true);
235+
236+
Bootstrapper.BootstrapInfo bootstrapInfo = CommonBootstrapperTestUtils.getTestBootstrapInfo();
237+
serverSslContextProviderFactory =
238+
new ServerSslContextProviderFactory(
239+
bootstrapInfo, certProviderServerSslContextProviderFactory);
240+
SslContextProvider sslContextProvider =
241+
serverSslContextProviderFactory.create(downstreamTlsContext);
242+
assertThat(sslContextProvider).isInstanceOf(CertProviderServerSslContextProvider.class);
243+
verifyWatcher(sslContextProvider, watcherCaptor[0]);
244+
verifyWatcher(sslContextProvider, watcherCaptor[1]);
245+
}
209246
}

0 commit comments

Comments
 (0)