简体   繁体   English

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

[英]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.我正在学习,只是设置了我的 cassandra 集群并尝试使用 python 作为客户端与之交互。 In the yaml, I set the authenticator to be PasswordAuthenticator.在 yaml 中,我将身份验证器设置为 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 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.这似乎是一个非常基本的功能,我无法想象它没有包含在 python 客户端驱动程序中。 I must have miss something.我一定错过了什么。

In short, my question is: How do I login to cassandra using python?简而言之,我的问题是:如何使用 python 登录到 cassandra?

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

Something like就像是

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

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.如果您使用的是协议版本 2,那么您必须使用 AuthProvider 对象进行身份验证。 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?有人可以解释使用这两种方法之一对远程集群进行身份验证的最佳实践吗?

Here you go.. Below is the code to connect with cassandra from python.给你.. 下面是从 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()

If you're using protocol version 4 then you must authenticate with an AuthProvider object, defining a previous auth_provider如果您使用的是协议版本 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)

Now you can do the following:现在您可以执行以下操作:

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

Surprisingly enough, no one suggested at least something safer than hard-coding authentication credentials.令人惊讶的是,没有人提出至少比硬编码身份验证凭据更安全的方法。 Considering you use git or any other VCS, this way of initialization may lead to a pretty significant security problems.考虑到您使用 git 或任何其他 VCS,这种初始化方式可能会导致非常严重的安全问题。 I would recommend to store your username, password, IPs, ports and other private information in some configuration file, which isn't stored in VCS so if someone accesses the code, the data will still be safe.我建议将您的用户名、密码、IP、端口和其他私人信息存储在某个配置文件中,该文件未存储在 VCS 中,因此如果有人访问代码,数据仍然是安全的。

config.yaml:配置文件:

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

code.py:代码.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.

相关问题 我应该如何使用 python 脚本将用户名和密码传递给 Cassandra - How should i pass username and password to Cassandra using python script 如何在 python 脚本中使用 sudo su - 用户名并将密码传递给它 - how to use sudo su - username in python script and pass password to it 在 python 中,如何将用户名和密码从变量传递到 python 中的 cx_Oracle.connect - In python how can I pass the username and password from a variable to cx_Oracle.connect in python Python使用配置文件创建Windows exe以传递用户名和密码 - Python create windows exe with a config file to pass username and password paho.mqtt.python 无法传递用户名和密码 - paho.mqtt.python unable to pass username and password python命令行参数并将其作为用户名和密码在脚本中传递 - python command line argument and pass it as username and password in script Python - 如何存储输入的用户名/密码 - Python - How to store a username/password from input PYTHON:如何显示打印用户名和密码? - PYTHON: How to show print username and password? 如何获取Python MYSQL连接用户名和密码? - How to get Python MYSQL connection username and password? 如何使用python为xml格式添加用户名和密码 - How to add username and password for xml format with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM