繁体   English   中英

如何在python中将用户名和密码传递给cassandra

[英]How to pass along username and password to cassandra in python

我正在学习,只是设置了我的 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 字段中提供用户名和密码,这是一个函数,返回一个字典,其中包含具有适当字符串值的“用户名”和“密码”键。

就像是

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

按照操作建议使用:

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

有人可以解释使用这两种方法之一对远程集群进行身份验证的最佳实践吗?

给你.. 下面是从 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()

如果您使用的是协议版本 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)

现在您可以执行以下操作:

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

令人惊讶的是,没有人提出至少比硬编码身份验证凭据更安全的方法。 考虑到您使用 git 或任何其他 VCS,这种初始化方式可能会导致非常严重的安全问题。 我建议将您的用户名、密码、IP、端口和其他私人信息存储在某个配置文件中,该文件未存储在 VCS 中,因此如果有人访问代码,数据仍然是安全的。

配置文件:

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

代码.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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM