如何在python中将用户名和密码传递给cassandra
我正在学习和设置我的cassandra集群,并尝试使用python作为与之交互的客户端。在yaml中,我将验证器设置为PasswordAuthenticator 因此,现在我计划将用户名和密码提供给connect函数,但找不到放置它们的位置如何在python中将用户名和密码传递给cassandra,python,authentication,cassandra,Python,Authentication,Cassandra,我正在学习和设置我的cassandra集群,并尝试使用python作为与之交互的客户端。在yaml中,我将验证器设置为PasswordAuthenticator 因此,现在我计划将用户名和密码提供给connect函数,但找不到放置它们的位置 cluster=群集(主机) 会话=cluster.connect(键空间) 基本上,您只提供主机和键空间。文档中显示了与anonymous的联系? 如果我只是使用这个例子,我将得到以下错误 raise NoHostAvailable("Una
cluster=群集(主机)
会话=cluster.connect(键空间)
基本上,您只提供主机和键空间。文档中显示了与anonymous的联系?
如果我只是使用这个例子,我将得到以下错误
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(自身,主机):
凭证={'username':'myUser','password':'myPassword'}
返回凭证
群集=群集(节点,身份验证提供程序=getCredential)
遵循ops建议使用:
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\u提供程序
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)
令人惊讶的是,没有人提出至少比硬编码身份验证凭据更安全的建议。考虑到您使用git或任何其他VCS,这种初始化方式可能会导致相当严重的安全问题。我建议将您的用户名、密码、IP、端口和其他私人信息存储在一些配置文件中,这些配置文件不存储在VCS中,因此如果有人访问代码,数据仍然是安全的 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()
现在,您可以执行以下操作:
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()
如果你已经找到了问题的解决方案,那么请将其发布并接受为答案。这将允许其他用户知道此问题已解决。我是新成员,系统不会让我在10天左右的时间内回答自己的问题。这就是为什么我只是把更新放在帖子里。一旦系统允许我这样做,我会给出答案。嘿,谢谢@bfb这对我来说很有效,但请注意,非关键字参数应该在关键字参数之前