Python cassandra.auth.PlainTextAuthProvider() Examples

The following are 13 code examples of cassandra.auth.PlainTextAuthProvider(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cassandra.auth , or try the search function .
Example #1
Source File: cassandra_utils.py    From cassandra-medusa with Apache License 2.0 6 votes vote down vote up
def __init__(self, ip_addresses, cassandra_config):
        self._ip_addresses = ip_addresses
        self._auth_provider = None
        self._ssl_context = None
        self._cassandra_config = cassandra_config

        if cassandra_config.cql_username is not None and cassandra_config.cql_password is not None:
            auth_provider = PlainTextAuthProvider(username=cassandra_config.cql_username,
                                                  password=cassandra_config.cql_password)
            self._auth_provider = auth_provider

        if cassandra_config.certfile is not None and cassandra_config.usercert is not None and \
           cassandra_config.userkey is not None:
            ssl_context = SSLContext(PROTOCOL_TLSv1)
            ssl_context.load_verify_locations(cassandra_config.certfile)
            ssl_context.verify_mode = CERT_REQUIRED
            ssl_context.load_cert_chain(
                certfile=cassandra_config.usercert,
                keyfile=cassandra_config.userkey)
            self._ssl_context = ssl_context

        load_balancing_policy = WhiteListRoundRobinPolicy(ip_addresses)
        self._execution_profiles = {
            'local': ExecutionProfile(load_balancing_policy=load_balancing_policy)
        } 
Example #2
Source File: ga_chp_bq_batch_inference.py    From MorphL-Community-Edition with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
        self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
        self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
        self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')

        self.prep_stmt = {}

        template_for_prediction = 'INSERT INTO ga_chp_bq_predictions (client_id,prediction) VALUES (?,?)'
        template_for_predictions_by_date = 'INSERT INTO ga_chp_bq_predictions_by_prediction_date (prediction_date, client_id, prediction) VALUES (?,?,?)'
        template_for_predictions_statistics = 'UPDATE ga_chp_bq_predictions_statistics SET loyal=loyal+?, neutral=neutral+?, churning=churning+?, lost=lost+? WHERE prediction_date=?'


        self.CASS_REQ_TIMEOUT = 3600.0

        self.auth_provider = PlainTextAuthProvider(
            username=self.MORPHL_CASSANDRA_USERNAME,
            password=self.MORPHL_CASSANDRA_PASSWORD)
        self.cluster = Cluster(
            [self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)
        self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)

        self.prep_stmt['prediction'] = self.session.prepare(template_for_prediction)
        self.prep_stmt['predictions_by_date'] = self.session.prepare(template_for_predictions_by_date)
        self.prep_stmt['predictions_statistics'] = self.session.prepare(template_for_predictions_statistics) 
Example #3
Source File: model_serving_endpoint.py    From MorphL-Community-Edition with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
        self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
        self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
        self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')

        self.QUERY = 'SELECT * FROM ga_chp_bq_predictions WHERE client_id = ? LIMIT 1'
        self.CASS_REQ_TIMEOUT = 3600.0

        self.auth_provider = PlainTextAuthProvider(
            username=self.MORPHL_CASSANDRA_USERNAME,
            password=self.MORPHL_CASSANDRA_PASSWORD)
        self.cluster = Cluster(
            [self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)

        self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)
        self.session.row_factory = dict_factory
        self.session.default_fetch_size = 100

        self.prepare_statements() 
Example #4
Source File: model_serving_endpoint.py    From MorphL-Community-Edition with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
        self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
        self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
        self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')

        self.CASS_REQ_TIMEOUT = 3600.0

        self.auth_provider = PlainTextAuthProvider(
            username=self.MORPHL_CASSANDRA_USERNAME,
            password=self.MORPHL_CASSANDRA_PASSWORD)
        self.cluster = Cluster(
            [self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)

        self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)
        self.session.row_factory = dict_factory
        self.session.default_fetch_size = 100

        self.prepare_statements() 
Example #5
Source File: migrator.py    From cassandra-migrate with MIT License 5 votes vote down vote up
def __init__(self, config, profile='dev', hosts=['127.0.0.1'], port=9042,
                 user=None, password=None, host_cert_path=None,
                 client_key_path=None, client_cert_path=None):
        self.config = config

        try:
            self.current_profile = self.config.profiles[profile]
        except KeyError:
            raise ValueError("Invalid profile name '{}'".format(profile))

        if user:
            auth_provider = PlainTextAuthProvider(user, password)
        else:
            auth_provider = None

        if host_cert_path:
            ssl_options = self._build_ssl_options(
                host_cert_path,
                client_key_path,
                client_cert_path)
        else:
            ssl_options = None

        self.cluster = Cluster(
            contact_points=hosts,
            port=port,
            auth_provider=auth_provider,
            max_schema_agreement_wait=300,
            control_connection_timeout=10,
            connect_timeout=30,
            ssl_options=ssl_options)

        self._session = None 
Example #6
Source File: dtest.py    From cassandra-dtest with Apache License 2.0 5 votes vote down vote up
def get_auth_provider(user, password):
    return PlainTextAuthProvider(username=user, password=password) 
Example #7
Source File: datastore.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setup_connection(self):
        assert isinstance(self.settings, CassandraSettings), self.settings

        # Optionally construct an "auth provider" object.
        if self.settings.username and self.settings.password:
            auth_provider = PlainTextAuthProvider(
                username=self.settings.username, password=self.settings.password
            )
        else:
            auth_provider = None

        # Resolve the consistency level to a driver object.
        try:
            consistency_level_name = self.settings.consistency.upper()
            consistency_level = ConsistencyLevel.name_to_value[consistency_level_name]
        except KeyError:
            msg = "Cassandra consistency level name '{}' not found." "".format(
                self.settings.consistency
            )
            raise DatasourceSettingsError(msg)

        # Use the other self.settings directly.
        cassandra.cqlengine.connection.setup(
            hosts=self.settings.hosts,
            consistency=consistency_level,
            default_keyspace=self.settings.default_keyspace,
            port=self.settings.port,
            auth_provider=auth_provider,
            protocol_version=self.settings.protocol_version,
            lazy_connect=True,
            retry_connect=True,
        ) 
Example #8
Source File: cassandra.py    From airflow with Apache License 2.0 5 votes vote down vote up
def __init__(self, cassandra_conn_id: str = 'cassandra_default'):
        super().__init__()
        conn = self.get_connection(cassandra_conn_id)

        conn_config = {}
        if conn.host:
            conn_config['contact_points'] = conn.host.split(',')

        if conn.port:
            conn_config['port'] = int(conn.port)

        if conn.login:
            conn_config['auth_provider'] = PlainTextAuthProvider(
                username=conn.login, password=conn.password)

        policy_name = conn.extra_dejson.get('load_balancing_policy', None)
        policy_args = conn.extra_dejson.get('load_balancing_policy_args', {})
        lb_policy = self.get_lb_policy(policy_name, policy_args)
        if lb_policy:
            conn_config['load_balancing_policy'] = lb_policy

        cql_version = conn.extra_dejson.get('cql_version', None)
        if cql_version:
            conn_config['cql_version'] = cql_version

        ssl_options = conn.extra_dejson.get('ssl_options', None)
        if ssl_options:
            conn_config['ssl_options'] = ssl_options

        self.cluster = Cluster(**conn_config)
        self.keyspace = conn.schema
        self.session = None 
Example #9
Source File: util.py    From cstar_perf with Apache License 2.0 5 votes vote down vote up
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 
Example #10
Source File: test_repositories.py    From monasca-api with Apache License 2.0 5 votes vote down vote up
def test_init(self, cassandra_connect_mock):
        repo = cassandra_repo.MetricsRepository()
        self.assertIsNone(
            repo.cluster.auth_provider,
            'cassandra cluster auth provider is expected to None'
        )

        repo.conf.cassandra.user = 'cassandra'
        repo.conf.cassandra.password = 'cassandra'
        repo = cassandra_repo.MetricsRepository()
        self.assertIsInstance(
            repo.cluster.auth_provider,
            PlainTextAuthProvider,
            'cassandra cluster auth provider is expected to be PlainTextAuthProvider'
        ) 
Example #11
Source File: ga_chp_connector.py    From MorphL-Community-Edition with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.DAY_OF_DATA_CAPTURE = getenv('DAY_OF_DATA_CAPTURE')
        self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
        self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
        self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
        self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')
        self.CASS_REQ_TIMEOUT = 3600.0

        self.auth_provider = PlainTextAuthProvider(
            username=self.MORPHL_CASSANDRA_USERNAME, password=self.MORPHL_CASSANDRA_PASSWORD)
        self.cluster = Cluster(
            contact_points=[self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)
        self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)

        self.prepare_statements() 
Example #12
Source File: ga_chp_batch_inference.py    From MorphL-Community-Edition with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.MORPHL_SERVER_IP_ADDRESS = getenv('MORPHL_SERVER_IP_ADDRESS')
        self.MORPHL_CASSANDRA_USERNAME = getenv('MORPHL_CASSANDRA_USERNAME')
        self.MORPHL_CASSANDRA_PASSWORD = getenv('MORPHL_CASSANDRA_PASSWORD')
        self.MORPHL_CASSANDRA_KEYSPACE = getenv('MORPHL_CASSANDRA_KEYSPACE')

        self.prep_stmt = {}

        template_for_prediction = 'INSERT INTO ga_chp_predictions (client_id,prediction) VALUES (?,?)'
        template_for_predictions_by_date = 'INSERT INTO ga_chp_predictions_by_prediction_date (prediction_date, client_id, prediction) VALUES (?,?,?)'
        template_for_predictions_statistics = 'UPDATE ga_chp_predictions_statistics SET loyal=loyal+?, neutral=neutral+?, churning=churning+?, lost=lost+? WHERE prediction_date=?'

        self.CASS_REQ_TIMEOUT = 3600.0

        self.auth_provider = PlainTextAuthProvider(
            username=self.MORPHL_CASSANDRA_USERNAME,
            password=self.MORPHL_CASSANDRA_PASSWORD)
        self.cluster = Cluster(
            [self.MORPHL_SERVER_IP_ADDRESS], auth_provider=self.auth_provider)
        self.session = self.cluster.connect(self.MORPHL_CASSANDRA_KEYSPACE)

        self.prep_stmt['prediction'] = self.session.prepare(
            template_for_prediction)
        self.prep_stmt['predictions_by_date'] = self.session.prepare(
            template_for_predictions_by_date)
        self.prep_stmt['predictions_statistics'] = self.session.prepare(
            template_for_predictions_statistics) 
Example #13
Source File: metrics_repository.py    From monasca-api with Apache License 2.0 4 votes vote down vote up
def __init__(self):

        try:
            self.conf = cfg.CONF

            if self.conf.cassandra.user:
                auth_provider = PlainTextAuthProvider(username=self.conf.cassandra.user,
                                                      password=self.conf.cassandra.password)
            else:
                auth_provider = None

            self.cluster = Cluster(self.conf.cassandra.contact_points,
                                   port=self.conf.cassandra.port,
                                   auth_provider=auth_provider,
                                   connect_timeout=self.conf.cassandra.connection_timeout,
                                   load_balancing_policy=TokenAwarePolicy(
                                       DCAwareRoundRobinPolicy(
                                           local_dc=self.conf.cassandra.local_data_center))
                                   )
            self.session = self.cluster.connect(self.conf.cassandra.keyspace)

            self.dim_val_by_metric_stmt = self.session.prepare(DIMENSION_VALUE_BY_METRIC_CQL)

            self.dim_val_stmt = self.session.prepare(DIMENSION_VALUE_CQL)

            self.dim_name_by_metric_stmt = self.session.prepare(DIMENSION_NAME_BY_METRIC_CQL)

            self.dim_name_stmt = self.session.prepare(DIMENSION_NAME_CQL)

            self.metric_name_by_dimension_stmt = self.session.prepare(METRIC_NAME_BY_DIMENSION_CQL)

            self.metric_name_by_dimension_offset_stmt = self.session.prepare(
                METRIC_NAME_BY_DIMENSION_OFFSET_CQL)

            self.metric_name_stmt = self.session.prepare(METRIC_NAME_CQL)

            self.metric_name_offset_stmt = self.session.prepare(METRIC_NAME_OFFSET_CQL)

            self.metric_by_id_stmt = self.session.prepare(METRIC_BY_ID_CQL)

        except Exception as ex:
            LOG.exception(ex)
            raise exceptions.RepositoryException(ex)

        self.epoch = datetime.utcfromtimestamp(0)