Skip to content

Commit

Permalink
xds: cache bootstrapInfo in the SslContextProviderFactory to prevent …
Browse files Browse the repository at this point in the history
…rereading (#8051)
  • Loading branch information
sanjaypujare committed Apr 5, 2021
1 parent 5e9a7b6 commit 1a3b02b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Expand Up @@ -31,6 +31,7 @@ final class ClientSslContextProviderFactory
implements ValueFactory<UpstreamTlsContext, SslContextProvider> {

private final Bootstrapper bootstrapper;
private Bootstrapper.BootstrapInfo bootstrapInfo;
private final CertProviderClientSslContextProvider.Factory
certProviderClientSslContextProviderFactory;

Expand All @@ -54,7 +55,9 @@ public SslContextProvider create(UpstreamTlsContext upstreamTlsContext) {
if (CommonTlsContextUtil.hasCertProviderInstance(
upstreamTlsContext.getCommonTlsContext())) {
try {
Bootstrapper.BootstrapInfo bootstrapInfo = bootstrapper.bootstrap();
if (bootstrapInfo == null) {
bootstrapInfo = bootstrapper.bootstrap();
}
return certProviderClientSslContextProviderFactory.getProvider(
upstreamTlsContext,
bootstrapInfo.getNode().toEnvoyProtoNode(),
Expand All @@ -68,9 +71,12 @@ public SslContextProvider create(UpstreamTlsContext upstreamTlsContext) {
} else if (CommonTlsContextUtil.hasAllSecretsUsingSds(
upstreamTlsContext.getCommonTlsContext())) {
try {
if (bootstrapInfo == null) {
bootstrapInfo = bootstrapper.bootstrap();
}
return SdsClientSslContextProvider.getProvider(
upstreamTlsContext,
bootstrapper.bootstrap().getNode().toEnvoyProtoNodeV2(),
bootstrapInfo.getNode().toEnvoyProtoNodeV2(),
Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
.setNameFormat("client-sds-sslcontext-provider-%d")
.setDaemon(true)
Expand Down
Expand Up @@ -31,6 +31,7 @@ final class ServerSslContextProviderFactory
implements ValueFactory<DownstreamTlsContext, SslContextProvider> {

private final Bootstrapper bootstrapper;
private Bootstrapper.BootstrapInfo bootstrapInfo;
private final CertProviderServerSslContextProvider.Factory
certProviderServerSslContextProviderFactory;

Expand All @@ -55,7 +56,9 @@ public SslContextProvider create(
if (CommonTlsContextUtil.hasCertProviderInstance(
downstreamTlsContext.getCommonTlsContext())) {
try {
Bootstrapper.BootstrapInfo bootstrapInfo = bootstrapper.bootstrap();
if (bootstrapInfo == null) {
bootstrapInfo = bootstrapper.bootstrap();
}
return certProviderServerSslContextProviderFactory.getProvider(
downstreamTlsContext,
bootstrapInfo.getNode().toEnvoyProtoNode(),
Expand All @@ -69,9 +72,12 @@ public SslContextProvider create(
} else if (CommonTlsContextUtil.hasAllSecretsUsingSds(
downstreamTlsContext.getCommonTlsContext())) {
try {
if (bootstrapInfo == null) {
bootstrapInfo = bootstrapper.bootstrap();
}
return SdsServerSslContextProvider.getProvider(
downstreamTlsContext,
bootstrapper.bootstrap().getNode().toEnvoyProtoNodeV2(),
bootstrapInfo.getNode().toEnvoyProtoNodeV2(),
Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
.setNameFormat("server-sds-sslcontext-provider-%d")
.setDaemon(true)
Expand Down
Expand Up @@ -23,6 +23,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -139,6 +141,11 @@ public void createCertProviderClientSslContextProvider() throws XdsInitializatio
clientSslContextProviderFactory.create(upstreamTlsContext);
assertThat(sslContextProvider).isInstanceOf(CertProviderClientSslContextProvider.class);
verifyWatcher(sslContextProvider, watcherCaptor[0]);
// verify that bootstrapInfo is cached...
sslContextProvider =
clientSslContextProviderFactory.create(upstreamTlsContext);
assertThat(sslContextProvider).isInstanceOf(CertProviderClientSslContextProvider.class);
verify(bootstrapper, times(1)).bootstrap();
}

@Test
Expand Down
Expand Up @@ -23,6 +23,8 @@
import static io.grpc.xds.internal.sds.CommonTlsContextTestsUtil.SERVER_1_KEY_FILE;
import static io.grpc.xds.internal.sds.CommonTlsContextTestsUtil.SERVER_1_PEM_FILE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -135,6 +137,11 @@ public void createCertProviderServerSslContextProvider() throws XdsInitializatio
serverSslContextProviderFactory.create(downstreamTlsContext);
assertThat(sslContextProvider).isInstanceOf(CertProviderServerSslContextProvider.class);
verifyWatcher(sslContextProvider, watcherCaptor[0]);
// verify that bootstrapInfo is cached...
sslContextProvider =
serverSslContextProviderFactory.create(downstreamTlsContext);
assertThat(sslContextProvider).isInstanceOf(CertProviderServerSslContextProvider.class);
verify(bootstrapper, times(1)).bootstrap();
}

@Test
Expand Down

0 comments on commit 1a3b02b

Please sign in to comment.