簡體   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