Skip to content

Commit

Permalink
Merge pull request #7534 from cangencer/fixes/custom-credentials
Browse files Browse the repository at this point in the history
Stricter type check for ClientAuthenticationCustomCodec in ClusterAuthenticator
  • Loading branch information
Ali committed Feb 17, 2016
2 parents 3b90d54 + 6cf7f3e commit c73bbb2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void authenticate(ClientConnection connection) throws AuthenticationExcep
String ownerUuid = principal.getOwnerUuid();

ClientMessage clientMessage;
if (credentials instanceof UsernamePasswordCredentials) {
if (credentials.getClass().equals(UsernamePasswordCredentials.class)) {
UsernamePasswordCredentials cr = (UsernamePasswordCredentials) credentials;
clientMessage = ClientAuthenticationCodec.encodeRequest(cr.getUsername(), cr.getPassword(), uuid, ownerUuid, false,
ClientTypes.JAVA, client.getSerializationService().getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.nio.serialization.Portable;
import com.hazelcast.nio.serialization.PortableFactory;
import com.hazelcast.security.UsernamePasswordCredentials;
Expand All @@ -21,6 +23,9 @@ public class ClientAuthenticationTest extends HazelcastTestSupport {

private final TestHazelcastFactory hazelcastFactory = new TestHazelcastFactory();

private final String USERNAME = "user";
private final String PASSWORD = "pass";

@After
public void cleanup() {
hazelcastFactory.terminateAll();
Expand All @@ -45,33 +50,16 @@ public void testNoClusterFound() throws Exception {
}

@Test
public void testAuthenticationWithCustomCredentials() {
final String username = "dev";
final String password = "pass";

PortableFactory credentialsFactory = new PortableFactory() {
@Override
public Portable create(int classId) {
return new CustomCredentials() {
@Override
public String getUsername() {
return username;
}
@Override
public String getPassword() {
return password;
}
};
}
};
public void testAuthenticationWithCustomCredentials_when_singleNode() {
PortableFactory factory = new CustomCredentialsPortableFactory();

// with this config, the server will authenticate any credential of type CustomCredentials
Config config = new Config();
config.getGroupConfig()
.setName(username)
.setPassword(password);
.setName(USERNAME)
.setPassword(PASSWORD);
config.getSerializationConfig()
.addPortableFactory(1, credentialsFactory);
.addPortableFactory(1, factory);
hazelcastFactory.newHazelcastInstance(config);

ClientConfig clientConfig = new ClientConfig();
Expand All @@ -81,6 +69,51 @@ public String getPassword() {
hazelcastFactory.newHazelcastClient(clientConfig);
}

@Test
public void testAuthenticationWithCustomCredentials_when_multipleNodes() {
PortableFactory factory = new CustomCredentialsPortableFactory();

// with this config, the server will authenticate any credential of type CustomCredentials
Config config = new Config();
config.getGroupConfig()
.setName(USERNAME)
.setPassword(PASSWORD);
config.getSerializationConfig()
.addPortableFactory(1, factory);

hazelcastFactory.newHazelcastInstance(config);
hazelcastFactory.newHazelcastInstance(config);

ClientConfig clientConfig = new ClientConfig();

// make sure there are no credentials sent over the wire
clientConfig.getSecurityConfig().setCredentials(new CustomCredentials());
HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastClient(clientConfig);

// ensure client opens a connection to all nodes
IMap<Integer, Integer> map = hazelcastInstance.getMap(randomName());
for (int i = 0; i < 100; i++) {
map.put(i,i);
}
}

private class CustomCredentialsPortableFactory implements PortableFactory {
@Override
public Portable create(int classId) {
return new CustomCredentials() {
@Override
public String getUsername() {
return USERNAME;
}

@Override
public String getPassword() {
return PASSWORD;
}
};
}
}

private class CustomCredentials extends UsernamePasswordCredentials {

@Override
Expand Down

0 comments on commit c73bbb2

Please sign in to comment.