如何在Python中传递用户名和密码给Cassandra

16

我正在学习并刚刚设置了我的cassandra集群,并尝试使用Python作为客户端与其交互。在yaml中,我将认证器设置为PasswordAuthenticator。

现在我计划将我的用户名和密码提供给连接函数,但找不到放置它们的地方。

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

基本上,您只提供主机和键空间。文档有点暗示了一个匿名连接?http://datastax.github.io/python-driver/getting_started.html#connecting-to-cassandra

如果我只使用示例,将会得到以下错误。

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

认证信息是通过某些配置文件提供的吗?这似乎是一个非常基本的功能,我无法想象它不在Python客户端驱动程序中涵盖。我一定漏掉了什么。

简而言之,我的问题是:如何使用Python登录到Cassandra?

提前感谢任何可能的提示!

================================================= 明白了。

我应该在auth_provider字段中提供用户名和密码,这是一个返回包含‘username’和‘password’键和适当字符串值的字典的函数。

像这样:

def getCredential(self, host):
    credential = {'username':'myUser', 'password':'myPassword'}
                    return credential
    
cluster = Cluster(nodes, auth_provider=getCredential)

1
如果您已经找到了解决问题的方法,请发布并接受它作为答案。这将使其他用户知道该问题已得到解决。 - ρss
我是一个新成员,系统不允许我在10天左右回答自己的问题。这就是为什么我只是在帖子中更新了信息。一旦系统允许我这样做,我会提供答案的。 - drakihsu
5个回答

18

根据运营建议,使用:

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()
如果您正在使用协议版本2,则必须使用AuthProvider对象进行身份验证。例如:
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()

有人能解释一下使用这些方法之一与远程群集进行身份验证的最佳实践吗?


嘿,谢谢@bfb,这对我有用,但请注意,非关键字参数应该在关键字参数之前。 - marcin_j

9

以下是连接Python与Cassandra的代码。

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()

5
如果您正在使用协议版本4,则必须通过一个AuthProvider对象进行身份验证,定义一个之前的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)

0

现在你可以做以下事情:

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

auth_provider = PlainTextAuthProvider(
        username='cassandra', password='cassandra')
cluster = Cluster(['non-local-address'], port=9042, auth_provider = auth_provider)

session = cluster.connect('yourkeyspace')
session.execute('USE yourkeyspace')
session.execute("SELECT * FROM yourkeyspace.yourtable").one()

-1

令人惊讶的是,没有人建议至少使用比硬编码认证凭据更安全的方法。考虑到您使用git或任何其他版本控制系统,这种初始化方式可能会导致相当严重的安全问题。我建议将您的用户名、密码、IP地址、端口和其他私人信息存储在某个配置文件中,该文件不会存储在版本控制系统中,因此如果有人访问代码,数据仍将是安全的。

config.yaml:

cassandra:
    username: username
    password: password
    hosts: [10.42.42.42, 10.10.42.42]

code.py:

import yaml
import cassandra.cluster
import cassandra.auth

from cassandra.policies import DCAwareRoundRobinPolicy


class Cassandra:
    def __init__(self, config):
        config = config['cassandra']
        auth_provider = cassandra.auth.PlainTextAuthProvider(
            username=config['username'],
            password=config['password'])

        self.__cluster = cassandra.cluster.Cluster(
            contact_points=config['hosts'],
            load_balancing_policy=DCAwareRoundRobinPolicy(),
            auth_provider=auth_provider)

        self.__session = self.__cluster.connect()

def query():
    result = self.__session.execute("SELECT * FROM table")
    return result._current_rows

with open('config.yaml', 'r') as f:
    config = yaml.load(f)

cassandra = Cassandra(config)
data = cassandra.query()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接