Skip to content
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

xds: add null reference checks in SslContextProviderSupplier (backport to v1.38.x) (#8169) #8171

Merged
merged 1 commit into from May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,8 +40,8 @@ public final class SslContextProviderSupplier implements Closeable {

public SslContextProviderSupplier(
BaseTlsContext tlsContext, TlsContextManager tlsContextManager) {
this.tlsContext = tlsContext;
this.tlsContextManager = tlsContextManager;
this.tlsContext = checkNotNull(tlsContext, "tlsContext");
this.tlsContextManager = checkNotNull(tlsContextManager, "tlsContextManager");
}

public BaseTlsContext getTlsContext() {
Expand Down Expand Up @@ -91,10 +91,12 @@ private SslContextProvider getSslContextProvider() {
/** Called by consumer when tlsContext changes. */
@Override
public synchronized void close() {
if (tlsContext instanceof UpstreamTlsContext) {
tlsContextManager.releaseClientSslContextProvider(sslContextProvider);
} else {
tlsContextManager.releaseServerSslContextProvider(sslContextProvider);
if (sslContextProvider != null) {
if (tlsContext instanceof UpstreamTlsContext) {
tlsContextManager.releaseClientSslContextProvider(sslContextProvider);
} else {
tlsContextManager.releaseServerSslContextProvider(sslContextProvider);
}
}
shutdown = true;
}
Expand Down
Expand Up @@ -23,7 +23,9 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -64,16 +66,20 @@ private void prepareSupplier() {
doReturn(mockSslContextProvider)
.when(mockTlsContextManager)
.findOrCreateClientSslContextProvider(eq(upstreamTlsContext));
supplier = new SslContextProviderSupplier(upstreamTlsContext, mockTlsContextManager);
}

private void callUpdateSslContext() {
mockCallback = mock(SslContextProvider.Callback.class);
Executor mockExecutor = mock(Executor.class);
doReturn(mockExecutor).when(mockCallback).getExecutor();
supplier = new SslContextProviderSupplier(upstreamTlsContext, mockTlsContextManager);
supplier.updateSslContext(mockCallback);
}

@Test
public void get_updateSecret() {
prepareSupplier();
callUpdateSslContext();
verify(mockTlsContextManager, times(2))
.findOrCreateClientSslContextProvider(eq(upstreamTlsContext));
verify(mockTlsContextManager, times(0))
Expand All @@ -96,6 +102,7 @@ public void get_updateSecret() {
@Test
public void get_onException() {
prepareSupplier();
callUpdateSslContext();
ArgumentCaptor<SslContextProvider.Callback> callbackCaptor = ArgumentCaptor.forClass(null);
verify(mockSslContextProvider, times(1)).addCallback(callbackCaptor.capture());
SslContextProvider.Callback capturedCallback = callbackCaptor.getValue();
Expand All @@ -108,6 +115,7 @@ public void get_onException() {
@Test
public void testClose() {
prepareSupplier();
callUpdateSslContext();
supplier.close();
verify(mockTlsContextManager, times(1))
.releaseClientSslContextProvider(eq(mockSslContextProvider));
Expand All @@ -119,4 +127,21 @@ public void testClose() {
assertThat(expected).hasMessageThat().isEqualTo("Supplier is shutdown!");
}
}

@Test
public void testClose_nullSslContextProvider() {
prepareSupplier();
doThrow(new NullPointerException()).when(mockTlsContextManager)
.releaseClientSslContextProvider(null);
supplier.close();
verify(mockTlsContextManager, never())
.releaseClientSslContextProvider(eq(mockSslContextProvider));
SslContextProvider.Callback mockCallback = mock(SslContextProvider.Callback.class);
try {
supplier.updateSslContext(mockCallback);
Assert.fail("no exception thrown");
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Supplier is shutdown!");
}
}
}