-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Author: Rajini Sivaram <rajinisivaram@googlemail.com> Reviewers: Ismael Juma <ismael@juma.me.uk> Closes #3766 from rajinisivaram/MINOR-sasl (cherry picked from commit 47c2753) Signed-off-by: Rajini Sivaram <rajinisivaram@googlemail.com>
- Loading branch information
1 parent
0cffb62
commit 9f34686
Showing
4 changed files
with
172 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
clients/src/test/java/org/apache/kafka/common/security/plain/PlainSaslServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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.kafka.common.security.plain; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.security.sasl.SaslException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.apache.kafka.common.security.JaasContext; | ||
import org.apache.kafka.common.security.authenticator.TestJaasConfig; | ||
|
||
public class PlainSaslServerTest { | ||
|
||
private static final String USER_A = "userA"; | ||
private static final String PASSWORD_A = "passwordA"; | ||
private static final String USER_B = "userB"; | ||
private static final String PASSWORD_B = "passwordB"; | ||
|
||
private PlainSaslServer saslServer; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
TestJaasConfig jaasConfig = new TestJaasConfig(); | ||
Map<String, Object> options = new HashMap<>(); | ||
options.put("user_" + USER_A, PASSWORD_A); | ||
options.put("user_" + USER_B, PASSWORD_B); | ||
jaasConfig.addEntry("jaasContext", PlainLoginModule.class.getName(), options); | ||
JaasContext jaasContext = new JaasContext("jaasContext", JaasContext.Type.SERVER, jaasConfig); | ||
saslServer = new PlainSaslServer(jaasContext); | ||
} | ||
|
||
@Test | ||
public void noAuthorizationIdSpecified() throws Exception { | ||
byte[] nextChallenge = saslServer.evaluateResponse(saslMessage("", USER_A, PASSWORD_A)); | ||
assertEquals(0, nextChallenge.length); | ||
} | ||
|
||
@Test | ||
public void authorizatonIdEqualsAuthenticationId() throws Exception { | ||
byte[] nextChallenge = saslServer.evaluateResponse(saslMessage(USER_A, USER_A, PASSWORD_A)); | ||
assertEquals(0, nextChallenge.length); | ||
} | ||
|
||
@Test(expected = SaslException.class) | ||
public void authorizatonIdNotEqualsAuthenticationId() throws Exception { | ||
saslServer.evaluateResponse(saslMessage(USER_B, USER_A, PASSWORD_A)); | ||
} | ||
|
||
private byte[] saslMessage(String authorizationId, String userName, String password) { | ||
String nul = "\u0000"; | ||
String message = String.format("%s%s%s%s%s", authorizationId, nul, userName, nul, password); | ||
return message.getBytes(StandardCharsets.UTF_8); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
clients/src/test/java/org/apache/kafka/common/security/scram/ScramSaslServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.kafka.common.security.scram; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
|
||
import javax.security.sasl.SaslException; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.apache.kafka.common.security.authenticator.CredentialCache; | ||
|
||
public class ScramSaslServerTest { | ||
|
||
private static final String USER_A = "userA"; | ||
private static final String USER_B = "userB"; | ||
|
||
private ScramMechanism mechanism; | ||
private ScramFormatter formatter; | ||
private ScramSaslServer saslServer; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
mechanism = ScramMechanism.SCRAM_SHA_256; | ||
formatter = new ScramFormatter(mechanism); | ||
CredentialCache.Cache<ScramCredential> credentialCache = new CredentialCache().createCache(mechanism.mechanismName(), ScramCredential.class); | ||
credentialCache.put(USER_A, formatter.generateCredential("passwordA", 4096)); | ||
credentialCache.put(USER_B, formatter.generateCredential("passwordB", 4096)); | ||
ScramServerCallbackHandler callbackHandler = new ScramServerCallbackHandler(credentialCache); | ||
saslServer = new ScramSaslServer(mechanism, new HashMap<String, Object>(), callbackHandler); | ||
} | ||
|
||
@Test | ||
public void noAuthorizationIdSpecified() throws Exception { | ||
byte[] nextChallenge = saslServer.evaluateResponse(clientFirstMessage(USER_A, null)); | ||
assertTrue("Next challenge is empty", nextChallenge.length > 0); | ||
} | ||
|
||
@Test | ||
public void authorizatonIdEqualsAuthenticationId() throws Exception { | ||
byte[] nextChallenge = saslServer.evaluateResponse(clientFirstMessage(USER_A, USER_A)); | ||
assertTrue("Next challenge is empty", nextChallenge.length > 0); | ||
} | ||
|
||
@Test(expected = SaslException.class) | ||
public void authorizatonIdNotEqualsAuthenticationId() throws Exception { | ||
saslServer.evaluateResponse(clientFirstMessage(USER_A, USER_B)); | ||
} | ||
|
||
private byte[] clientFirstMessage(String userName, String authorizationId) { | ||
String nonce = formatter.secureRandomString(); | ||
String authorizationField = authorizationId != null ? "a=" + authorizationId : ""; | ||
String firstMessage = String.format("n,%s,n=%s,r=%s", authorizationField, userName, nonce); | ||
return firstMessage.getBytes(StandardCharsets.UTF_8); | ||
} | ||
} |