Skip to content

Commit

Permalink
[fix][broker] Avoid IllegalStateException while client_version is not…
Browse files Browse the repository at this point in the history
… set (#16788)

(cherry picked from commit 4c6989c)
  • Loading branch information
codelipenghui committed Aug 8, 2022
1 parent 41d7cf1 commit 39e85f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.pulsar.broker.service;

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.pulsar.broker.admin.impl.PersistentTopicsBase.unsafeGetPartitionedTopicMetadataAsync;
import static org.apache.pulsar.broker.lookup.TopicLookupBase.lookupTopicAsync;
Expand Down Expand Up @@ -905,7 +906,8 @@ protected void handleAuthResponse(CommandAuthResponse authResponse) {

try {
AuthData clientData = AuthData.of(authResponse.getResponse().getAuthData());
doAuthentication(clientData, authResponse.getProtocolVersion(), authResponse.getClientVersion());
doAuthentication(clientData, authResponse.getProtocolVersion(),
authResponse.hasClientVersion() ? authResponse.getClientVersion() : EMPTY);
} catch (AuthenticationException e) {
service.getPulsarStats().recordConnectionCreateFail();
log.warn("[{}] Authentication failed: {} ", remoteAddress, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
import static org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.matches;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
Expand Down Expand Up @@ -89,6 +93,7 @@
import org.apache.pulsar.common.api.proto.BaseCommand;
import org.apache.pulsar.common.api.proto.BaseCommand.Type;
import org.apache.pulsar.common.api.proto.CommandAck.AckType;
import org.apache.pulsar.common.api.proto.CommandAuthResponse;
import org.apache.pulsar.common.api.proto.CommandConnected;
import org.apache.pulsar.common.api.proto.CommandError;
import org.apache.pulsar.common.api.proto.CommandLookupTopicResponse;
Expand Down Expand Up @@ -1996,4 +2001,22 @@ public boolean isCompletedExceptionally(){
channel.finish();
}
}

@Test
public void testHandleAuthResponseWithoutClientVersion() {
ServerCnx cnx = mock(ServerCnx.class, CALLS_REAL_METHODS);
CommandAuthResponse authResponse = mock(CommandAuthResponse.class);
org.apache.pulsar.common.api.proto.AuthData authData = mock(org.apache.pulsar.common.api.proto.AuthData.class);
when(authResponse.getResponse()).thenReturn(authData);
when(authResponse.hasResponse()).thenReturn(true);
when(authResponse.getResponse().hasAuthMethodName()).thenReturn(true);
when(authResponse.getResponse().hasAuthData()).thenReturn(true);
when(authResponse.hasClientVersion()).thenReturn(false);
try {
cnx.handleAuthResponse(authResponse);
} catch (Exception ignore) {
}
verify(authResponse, times(1)).hasClientVersion();
verify(authResponse, times(0)).getClientVersion();
}
}

0 comments on commit 39e85f8

Please sign in to comment.