Cannot connect java client to cassandra with password authentication enabled

13,363

Found I had a trailing whitespace and that was the root cause.

Cluster cluster = Cluster.builder().addContactPoints(hosts)
                .withCredentials(username.trim(), password.trim())
                //.withSSL()
                .build();
Share:
13,363
Greg L.
Author by

Greg L.

Updated on June 16, 2022

Comments

  • Greg L.
    Greg L. almost 2 years

    I have a default install of Datastax enterprise on my macbook. I was able to create my keyspace and setup all my applications including using solr.

    I am trying to develop a set of steps to turn on password authentication for our dev cluster.

    Thus far I have updated /usr/local/dse/resources/cassandra/conf/cassandra.yaml and changed the following properties:

    authenticator: PasswordAuthenticator
    authorizer: CassandraAuthorizer
    

    I restarted the node and could login and query my keyspace using cqlsh:

    cqlsh -u cassandra -p cassandra
    

    At this point I tried setting the Credentials on the Session builder: Host is: cassandra.host=localhost

    Session session = keyspaceToSessionMap.get(keyspace);
        if( session == null){
            Cluster cluster = Cluster.builder().addContactPoints(hosts)
                    .withCredentials(username, password)
                    //.withSSL()
                    .build();
            session = cluster.connect(keyspace);
            keyspaceToSessionMap.put(keyspace,session);
        }
    

    I could not successfully connect however. So I added a new user and was able to again login via cqlsh but still cannot get the Java driver to connect.

    cqlsh -u username -p password
    Connected to LocalCluster at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 2.1.8.689 | DSE 4.7.3 | CQL spec 3.2.0 | Native protocol v3]
    

    I am using 'com.datastax.cassandra:cassandra-driver-dse:2.1.9' via gradle for the driver.

    I always get the following stack trace and through debugging can see the username and password are set properly:

    Caused by: com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host localhost/127.0.0.1:9042: Username and/or password are incorrect
    at com.datastax.driver.core.Connection$8.apply(Connection.java:376)
    at com.datastax.driver.core.Connection$8.apply(Connection.java:346)
    

    This seems like it should be simple but I am stumped.

    My dependencies graph in relation to cassandra driver contains the following:

    +--- com.datastax.cassandra:cassandra-driver-dse:2.1.9
    |    \--- com.datastax.cassandra:cassandra-driver-core:2.1.9 -> 2.1.8
    |         +--- io.netty:netty-handler:4.0.27.Final
    |         |    +--- io.netty:netty-buffer:4.0.27.Final
    |         |    |    \--- io.netty:netty-common:4.0.27.Final
    |         |    +--- io.netty:netty-transport:4.0.27.Final
    |         |    |    \--- io.netty:netty-buffer:4.0.27.Final (*)
    |         |    \--- io.netty:netty-codec:4.0.27.Final
    |         |         \--- io.netty:netty-transport:4.0.27.Final (*)
    |         +--- com.google.guava:guava:14.0.1 -> 18.0
    |         \--- com.codahale.metrics:metrics-core:3.0.2
    |              \--- org.slf4j:slf4j-api:1.7.5 -> 1.7.12
    

    I created the following test which passes.

    Cluster cluster = Cluster.builder().addContactPoints("localhost")
                        .withCredentials("username", "password")
                        //.withSSL()
                        .build();
                Session session = cluster.connect("keyspace");
                Assert.assertNotNull(session);
    

    The only difference I can tell between the two is that "localhost" is now a constant rather than an array of size 1.