Here are the examples of the python api cassandra.auth.PlainTextAuthProvider taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
11 Examples
3
Example 1
Project: python-driver
License: View license
Source File: test_authentication.py
Function: get_authentication_provider
License: View license
Source File: test_authentication.py
Function: get_authentication_provider
def get_authentication_provider(self, username, password):
"""
Return correct authentication provider based on protocol version.
There is a difference in the semantics of authentication provider argument with protocol versions 1 and 2
For protocol version 2 and higher it should be a PlainTextAuthProvider object.
For protocol version 1 it should be a function taking hostname as an argument and returning a dictionary
containing username and password.
:param username: authentication username
:param password: authentication password
:return: authentication object suitable for Cluster.connect()
"""
if PROTOCOL_VERSION < 2:
return lambda hostname: dict(username=username, password=password)
else:
return PlainTextAuthProvider(username=username, password=password)
3
Example 2
def connect(migration_keyspace):
global cluster, session
# Setup the auth provider
auth_provider = PlainTextAuthProvider(username=username, password=password)
# Contact to Cassandra
cluster = Cluster(contact_points, auth_provider=auth_provider)
session = cluster.connect(migration_keyspace)
0
Example 3
def __init__(self, **options):
self.hosts = options.get('HOST').split(',')
self.keyspace = options.get('NAME')
self.user = options.get('USER')
self.password = options.get('PASSWORD')
self.options = options.get('OPTIONS', {})
self.connection_options = self.options.get('connection', {})
self.session_options = self.options.get('session', {})
if self.user and self.password and \
'auth_provider' not in self.connection_options:
self.connection_options['auth_provider'] = \
PlainTextAuthProvider(username=self.user,
password=self.password)
self.lock = Lock()
self.session = None
self.cluster = None
self.setup()
0
Example 4
def get_auth_provider(user, password):
return PlainTextAuthProvider(username=user, password=password)
0
Example 5
def __init__(self, config):
self.config = config
auth_provider = None
if config.clusteruser():
auth_provider = PlainTextAuthProvider(
username=config.clusteruser(),
password=config.clusterpass()
)
self.cluster = Cluster(
config.cluster(),
auth_provider=auth_provider
)
self.session = self.cluster.connect(config.keyspace())
self.deletequery = self.session.prepare(
"""
DELETE FROM metric
WHERE tenant=''
AND period=?
AND rollup=?
AND path=?
""")
0
Example 6
def auth_provider_if_configured(config):
if config.has_option('server', 'cassandra_user') and config.has_option('server', 'cassandra_password'):
from cassandra.auth import PlainTextAuthProvider
return PlainTextAuthProvider(username=config.get('server', 'cassandra_user'), password=config.get('server', 'cassandra_password'))
return None
0
Example 7
def run_query(self, query, user):
from cassandra.cluster import Cluster
connection = None
try:
if self.configuration.get('username', '') and self.configuration.get('password', ''):
from cassandra.auth import PlainTextAuthProvider
auth_provider = PlainTextAuthProvider(username='{}'.format(self.configuration.get('username', '')),
password='{}'.format(self.configuration.get('password', '')))
connection = Cluster([self.configuration.get('host', '')], auth_provider=auth_provider)
else:
connection = Cluster([self.configuration.get('host', '')])
session = connection.connect()
logger.debug("Cassandra running query: %s", query)
result = session.execute(query)
column_names = result.column_names
columns = self.fetch_columns(map(lambda c: (c, 'string'), column_names))
rows = [dict(zip(column_names, row)) for row in result]
data = {'columns': columns, 'rows': rows}
json_data = json.dumps(data, cls=JSONEncoder)
error = None
except cassandra.cluster.Error, e:
error = e.args[1]
except KeyboardInterrupt:
error = "Query cancelled by user."
return json_data, error
0
Example 8
def setup_cluster():
if args.host is None:
nodes = ['localhost']
else:
nodes = [args.host]
if args.port is None:
port = 9042
else:
port = args.port
cluster = None
if args.protocol_version is not None:
auth = None
if args.username is not None and args.password is not None:
if args.protocol_version == 1:
auth = get_credentials
elif args.protocol_version > 1:
auth = PlainTextAuthProvider(username=args.username, password=args.password)
cluster = Cluster(contact_points=nodes, port=port, protocol_version=args.protocol_version, auth_provider=auth, load_balancing_policy=cassandra.policies.WhiteListRoundRobinPolicy(nodes))
else:
cluster = Cluster(contact_points=nodes, port=port, load_balancing_policy=cassandra.policies.WhiteListRoundRobinPolicy(nodes))
session = cluster.connect()
session.default_timeout = TIMEOUT
session.default_fetch_size = FETCH_SIZE
session.row_factory = cassandra.query.ordered_dict_factory
return session
0
Example 9
def _connection(conf, datacenter, keyspace=None):
"""connection.
:param datacenter
:returns session
"""
ssl_options = None
if conf.ssl_enabled:
ssl_options = {
'ca_certs': conf.ssl_ca_certs,
'ssl_version': ssl.PROTOCOL_TLSv1
}
auth_provider = None
if conf.auth_enabled:
auth_provider = auth.PlainTextAuthProvider(
username=conf.username,
password=conf.password
)
load_balancing_policy_class = getattr(policies, conf.load_balance_strategy)
if load_balancing_policy_class is policies.DCAwareRoundRobinPolicy:
load_balancing_policy = load_balancing_policy_class(datacenter)
else:
load_balancing_policy = load_balancing_policy_class()
cluster_connection = cluster.Cluster(
conf.cluster,
auth_provider=auth_provider,
load_balancing_policy=load_balancing_policy,
port=conf.port,
ssl_options=ssl_options,
max_schema_agreement_wait=conf.max_schema_agreement_wait
)
session = cluster_connection.connect()
if not keyspace:
keyspace = conf.keyspace
try:
session.set_keyspace(keyspace)
except cassandra.InvalidRequest:
_create_keyspace(session, keyspace, conf.replication_strategy)
if conf.automatic_schema_migration:
migration_session = copy.copy(session)
migration_session.default_consistency_level = \
getattr(cassandra.ConsistencyLevel,
conf.migrations_consistency_level)
_run_migrations(conf.migrations_path, migration_session)
session.row_factory = query.dict_factory
return session
0
Example 10
def cassandra_connection(env, config):
ssl_options = None
if config.get(env, 'ssl_enabled'):
ssl_options = {}
ssl_options['ca_certs'] = config.get(env, 'ssl_ca_certs')
ssl_options['ssl_version'] = config.get(env, 'ssl_version')
if ssl_options['ssl_version'] == 'TLSv1':
ssl_options['ssl_version'] = ssl.PROTOCOL_TLSv1
elif ssl_options['ssl_version'] == 'TLSv1.1':
ssl_options['ssl_version'] = ssl.PROTOCOL_TLSv1_1
elif ssl_options['ssl_version'] == 'TLSv1.2':
ssl_options['ssl_version'] = ssl.PROTOCOL_TLSv1_2
else:
print('Unknown SSL Version')
sys.exit(4)
auth_provider = None
if config.get(env, 'auth_enabled'):
auth_provider = auth.PlainTextAuthProvider(
username=config.get(env, 'username'),
password=config.get(env, 'password')
)
cluster_connection = cluster.Cluster(
config.get(env, 'cluster').split(","),
auth_provider=auth_provider,
port=config.get(env, 'port'),
ssl_options=ssl_options,
)
return cluster_connection
0
Example 11
def cassandra_connection(env, config):
ssl_options = None
if config.getboolean(env, 'ssl_enabled'):
ssl_options = dict()
ssl_options['ca_certs'] = config.get(env, 'ssl_ca_certs')
ssl_options['ssl_version'] = config.get(env, 'ssl_version')
if ssl_options['ssl_version'] == 'TLSv1':
ssl_options['ssl_version'] = ssl.PROTOCOL_TLSv1
elif ssl_options['ssl_version'] == 'TLSv1.1':
ssl_options['ssl_version'] = ssl.PROTOCOL_TLSv1_1
elif ssl_options['ssl_version'] == 'TLSv1.2':
ssl_options['ssl_version'] = ssl.PROTOCOL_TLSv1_2
else:
print('Unknown SSL Version')
sys.exit(4)
auth_provider = None
if config.getboolean(env, 'auth_enabled'):
auth_provider = auth.PlainTextAuthProvider(
username=config.get(env, 'username'),
password=config.get(env, 'password')
)
cluster_connection = cluster.Cluster(
config.get(env, 'cluster').split(","),
auth_provider=auth_provider,
port=config.getint(env, 'port'),
ssl_options=ssl_options,
)
return cluster_connection