-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
ZOOKEEPER-3008: Potential NPE in SaslQuorumAuthLearner#authenticate and SaslQuorumAuthServer#authenticate #496
base: master
Are you sure you want to change the base?
Changes from all commits
7d8d523
700dfb7
1ad4da8
4458bb3
7fad199
765180f
cf611d1
5eec876
925bfd2
c787912
5c9b577
e967e0f
c4db5e2
cf9fb5f
f7da9b9
a12b13f
0b85882
7eb9e1c
fb36cf8
4df1044
841cc4f
b1c4856
ddf1c6c
86910c6
843e3db
974d8b5
4d07262
aa6d016
d0536b3
989a35a
60e592f
fe31819
8bfbd4a
2234630
4f9da0d
97ee54c
a56d309
13f0efa
dc8e7dd
3486086
d010be3
78ff7f0
e08f199
841e582
6f1a618
32baa8b
ad6245d
5f96907
acf8d22
490efd7
76b56e7
7517635
521b441
1723a27
ca7ba07
7a6894a
712151d
890addd
3dfa3f6
1193ecc
c451947
1cfbe2b
0fd753a
b83abd7
eae2702
72d3ded
3fc2f21
464f002
ac95ac3
47df0e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.zookeeper; | ||
|
||
import org.apache.zookeeper.common.ZKConfig; | ||
import javax.security.auth.callback.CallbackHandler; | ||
import javax.security.auth.login.LoginException; | ||
|
||
public interface LoginFactory { | ||
Login createLogin(final String loginContextName, CallbackHandler callbackHandler, final ZKConfig zkConfig) throws LoginException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.zookeeper; | ||
|
||
import org.apache.zookeeper.common.ZKConfig; | ||
|
||
import javax.security.auth.callback.CallbackHandler; | ||
import javax.security.auth.login.LoginException; | ||
|
||
public class LoginFactoryImpl implements LoginFactory { | ||
@Override | ||
public Login createLogin(String loginContextName, CallbackHandler callbackHandler, ZKConfig zkConfig) throws LoginException { | ||
return new Login(loginContextName, callbackHandler, zkConfig); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
import org.apache.jute.BinaryInputArchive; | ||
import org.apache.jute.BinaryOutputArchive; | ||
import org.apache.zookeeper.Login; | ||
import org.apache.zookeeper.LoginFactory; | ||
import org.apache.zookeeper.SaslClientCallbackHandler; | ||
import org.apache.zookeeper.common.ZKConfig; | ||
import org.apache.zookeeper.server.quorum.QuorumAuthPacket; | ||
|
@@ -52,7 +53,7 @@ public class SaslQuorumAuthLearner implements QuorumAuthLearner { | |
private final String quorumServicePrincipal; | ||
|
||
public SaslQuorumAuthLearner(boolean quorumRequireSasl, | ||
String quorumServicePrincipal, String loginContext) | ||
String quorumServicePrincipal, String loginContext, LoginFactory loginFactory) | ||
throws SaslException { | ||
this.quorumRequireSasl = quorumRequireSasl; | ||
this.quorumServicePrincipal = quorumServicePrincipal; | ||
|
@@ -66,8 +67,8 @@ public SaslQuorumAuthLearner(boolean quorumRequireSasl, | |
+ "section '" + loginContext | ||
+ "' could not be found."); | ||
} | ||
this.learnerLogin = new Login(loginContext, | ||
new SaslClientCallbackHandler(null, "QuorumLearner"), new ZKConfig()); | ||
this.learnerLogin = loginFactory.createLogin(loginContext, | ||
new SaslClientCallbackHandler(null, "QuorumLearner"), new ZKConfig()); | ||
this.learnerLogin.startThreadIfNeeded(); | ||
} catch (LoginException e) { | ||
throw new SaslException("Failed to initialize authentication mechanism using SASL", e); | ||
|
@@ -94,7 +95,10 @@ public void authenticate(Socket sock, String hostName) throws IOException { | |
principalConfig, | ||
QuorumAuth.QUORUM_SERVER_PROTOCOL_NAME, | ||
QuorumAuth.QUORUM_SERVER_SASL_DIGEST, LOG, "QuorumLearner"); | ||
|
||
if (sc == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same feedback as #495
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will try unit test written by @brettKK ~~ |
||
LOG.error("SaslClient object is null while trying to create SASL client"); | ||
throw new SaslException("Exception while trying to create SASL client"); | ||
} | ||
if (sc.hasInitialResponse()) { | ||
responseToken = createSaslToken(new byte[0], sc, learnerLogin); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.zookeeper.server.quorum.auth; | ||
|
||
import org.apache.zookeeper.Login; | ||
import org.apache.zookeeper.LoginFactory; | ||
import org.apache.zookeeper.common.ZKConfig; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.security.auth.Subject; | ||
import javax.security.auth.callback.CallbackHandler; | ||
import javax.security.auth.login.AppConfigurationEntry; | ||
import javax.security.auth.login.Configuration; | ||
import javax.security.auth.login.LoginException; | ||
import javax.security.sasl.SaslException; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.security.Principal; | ||
|
||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class SaslQuorumAuthLearnerTest { | ||
|
||
private SaslQuorumAuthLearner learner; | ||
|
||
@Before | ||
public void setUp() throws SaslException, LoginException { | ||
Configuration configMock = mock(Configuration.class); | ||
when(configMock.getAppConfigurationEntry(any(String.class))).thenReturn(new AppConfigurationEntry[1]); | ||
Configuration.setConfiguration(configMock); | ||
//mock object | ||
Login loginMock = mock(Login.class); | ||
Subject subjectMock = new Subject(); | ||
Principal principalMock = mock(Principal.class); | ||
when(principalMock.getName()).thenReturn("hello"); | ||
subjectMock.getPrincipals().add(principalMock); | ||
when(loginMock.getSubject()).thenReturn(subjectMock); | ||
|
||
LoginFactory loginFactoryMock = mock(LoginFactory.class); | ||
when(loginFactoryMock.createLogin(any(String.class), any(CallbackHandler.class), any(ZKConfig.class))).thenReturn(loginMock); | ||
|
||
learner = new SaslQuorumAuthLearner(true, null, "andorContext", loginFactoryMock); | ||
} | ||
|
||
@Test(expected = SaslException.class) | ||
public void testNullCheckSc() throws IOException { | ||
assertThat(learner, is(notNullValue())); | ||
|
||
Socket socketMock = mock(Socket.class); | ||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[0]); | ||
when(socketMock.getOutputStream()).thenReturn(byteArrayOutputStream); | ||
when(socketMock.getInputStream()).thenReturn(byteArrayInputStream); | ||
|
||
learner.authenticate(socketMock, null); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be put on above line. makes it more readable.