Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass along username and password to cassandra in python

I'm learning and just setup my cassandra cluster and trying to use python as the client to interact with it. In the yaml, I set the authenticator to be PasswordAuthenticator.

So now I plan to provide my username and password over to the connect function but find no where to put them.

cluster = Cluster(hosts)
session = cluster.connect(keyspace)

Basically, you provide only the host and the keyspace. The documentation kind of suggest a connection with anonymous? http://datastax.github.io/python-driver/getting_started.html#connecting-to-cassandra

If I just use the example, I will get the following error

raise NoHostAvailable("Unable to connect to any servers", errors)
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'hou722067': AuthenticationFailed('Remote end requires authentication.',)})

Is the authentication information supplied through some config files? It seems like a very basic functionality and I can't imagine it's not covered in the python client driver. I must have miss something.

In short, my question is: How do I login to cassandra using python?

Thanks in advance for any hint possible!

================================================= Got it figured.

I should provide the username and password in the auth_provider field which is a function returning a dictionary containing ‘username’ and ‘password’ keys with appropriate string values.

Something like

def getCredential(self, host):
    credential = {'username':'myUser', 'password':'myPassword'}
                    return credential
    
cluster = Cluster(nodes, auth_provider=getCredential)
like image 731
drakihsu Avatar asked Feb 14 '14 19:02

drakihsu


People also ask

How do you insert data into Cassandra using Python?

We first need to declare Cluster object. All transactions such as insert/update, etc., are performed by starting a session with a keyspace. To create a table, use session object to execute CQL query for creating a table. The keyspace so created can be further used to insert rows.

How do I find my Cassandra username and password?

You can enable authentication after installing Edge, or as part of the installation process. If you decide to enable authentication on Cassandra, it uses the following default credentials: username = 'cassandra' password = 'cassandra'

How do I enable Cassandra authentication?

To enable authentication: Update all Edge components that connect to Cassandra with the Cassandra username and password. On all Cassandra nodes, enable authentication. Set the Cassandra username and password on any one node.

What is Cassandra Python?

Apache Cassandra is a column-family NoSQL data store designed for write-heavy persistent storage in Python web applications and data projects. Apache Cassandra is an implementation of the NoSQL database concept. Learn more in the data chapter or view the table of contents for all topics.


3 Answers

Following the ops recommendation use:

from cassandra.cluster import Cluster

def getCredential(self):
    return {'username': 'foo', 'password': 'bar'} 

node_ips = ['0.0.0.0', '0.0.0.1']

cluster = Cluster(node_ips, protocol_version=1, auth_provider=getCredential)
session = cluster.connect()

If you're using protocol version 2 then you must authenticate with an AuthProvider object. EX:

from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster

ap = PlainTextAuthProvider(username=foo, password=bar)
c = Cluster(protocol_version=2, auth_provider=ap)
s = c.connect()

Can someone can explain best practices using either of these methods to authenticate with a remote cluster?

like image 163
bfb Avatar answered Sep 18 '22 18:09

bfb


Here you go.. Below is the code to connect with cassandra from python.

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
auth_provider = PlainTextAuthProvider(username='username', password='password')
cluster = Cluster(["hostname"],auth_provider = auth_provider)
session = cluster.connect()
session.set_keyspace('keyspace')
cluster.connect()
like image 34
Sunil Khaire Avatar answered Sep 19 '22 18:09

Sunil Khaire


If you're using protocol version 4 then you must authenticate with an AuthProvider object, defining a previous auth_provider

from cassandra.auth import PlainTextAuthProvider
from cassandra.cqlengine import connection

    auth_provider = PlainTextAuthProvider(username='user_name', password='user_pwd')

    connection.setup(cassandra_host, default_keyspace, retry_connect=True, protocol_version=4, auth_provider=auth_provider)
like image 37
caroreyp Avatar answered Sep 20 '22 18:09

caroreyp