Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_a_path_is_required(self):
with pytest.raises(ImproperlyConfigured):
FilesystemBackend(app=self.app)
def test_security_conf(self):
self.app.conf.task_serializer = 'auth'
with pytest.raises(ImproperlyConfigured):
self.app.setup_security()
self.app.conf.accept_content = ['auth']
with pytest.raises(ImproperlyConfigured):
self.app.setup_security()
_import = builtins.__import__
def import_hook(name, *args, **kwargs):
if name == 'cryptography':
raise ImportError
return _import(name, *args, **kwargs)
builtins.__import__ = import_hook
with pytest.raises(ImproperlyConfigured):
self.app.setup_security()
def __init__(self, *args, **kwargs):
"""Initialize MongoDB backend instance.
:raises celery.exceptions.ImproperlyConfigured: if
module :mod:`pymongo` is not available.
"""
self.options = {}
super(MongoBackend, self).__init__(*args, **kwargs)
self.expires = kwargs.get('expires') or maybe_timedelta(
self.app.conf.CELERY_TASK_RESULT_EXPIRES)
if not pymongo:
raise ImproperlyConfigured(
'You need to install the pymongo library to use the '
'MongoDB backend.')
config = self.app.conf.get('CELERY_MONGODB_BACKEND_SETTINGS')
if config is not None:
if not isinstance(config, dict):
raise ImproperlyConfigured(
'MongoDB backend settings should be grouped in a dict')
config = dict(config) # do not modify original
self.host = config.pop('host', self.host)
self.port = int(config.pop('port', self.port))
self.user = config.pop('user', self.user)
self.password = config.pop('password', self.password)
self.database_name = config.pop('database', self.database_name)
self.taskmeta_collection = config.pop(
info, left = _settings_info, is_in_old
else:
# no settings, just use new format.
info, left = _settings_info, is_in_old
if prefix:
# always use new format if prefix is used.
info, left = _settings_info, set()
# only raise error for keys that the user didn't provide two keys
# for (e.g., both ``result_expires`` and ``CELERY_TASK_RESULT_EXPIRES``).
really_left = {key for key in left if info.convert[key] not in have}
if really_left:
# user is mixing old/new, or new/old settings, give renaming
# suggestions.
raise ImproperlyConfigured(info.mix_error.format(renames='\n'.join(
FMT_REPLACE_SETTING.format(replace=key, with_=info.convert[key])
for key in sorted(really_left)
)))
preconf = {info.convert.get(k, k): v for k, v in items(preconf)}
defaults = dict(deepcopy(info.defaults), **preconf)
return Settings(
preconf, [conf, defaults],
(_old_key_to_new, _new_key_to_old),
prefix=prefix,
)
def by_name(backend=None, loader=None,
extension_namespace='celery.result_backends'):
"""Get backend class by name/alias."""
backend = backend or 'disabled'
loader = loader or current_app.loader
aliases = dict(BACKEND_ALIASES, **loader.override_backends)
aliases.update(
load_extension_class_names(extension_namespace) or {})
try:
cls = symbol_by_name(backend, aliases)
except ValueError as exc:
reraise(ImproperlyConfigured, ImproperlyConfigured(
UNKNOWN_BACKEND.strip().format(backend, exc)), sys.exc_info()[2])
if isinstance(cls, types.ModuleType):
raise ImproperlyConfigured(UNKNOWN_BACKEND.strip().format(
backend, 'is a Python module, not a backend class.'))
return cls
def __init__(self, url=None, *args, **kwargs):
kwargs.setdefault('expires_type', int)
super(CouchbaseBackend, self).__init__(*args, **kwargs)
self.url = url
if Couchbase is None:
raise ImproperlyConfigured(
'You need to install the couchbase library to use the '
'Couchbase backend.',
)
uhost = uport = uname = upass = ubucket = None
if url:
_, uhost, uport, uname, upass, ubucket, _ = _parse_url(url)
ubucket = ubucket.strip('/') if ubucket else None
config = self.app.conf.get('couchbase_backend_settings', None)
if config is not None:
if not isinstance(config, dict):
raise ImproperlyConfigured(
'Couchbase backend settings should be grouped in a dict',
)
else:
def __init__(self, servers=None, keyspace=None, table=None, entry_ttl=None,
port=9042, **kwargs):
"""Initialize Cassandra backend.
Raises :class:`celery.exceptions.ImproperlyConfigured` if
the :setting:`cassandra_servers` setting is not set.
"""
super(CassandraBackend, self).__init__(**kwargs)
if not cassandra:
raise ImproperlyConfigured(E_NO_CASSANDRA)
conf = self.app.conf
self.servers = (servers or
conf.get('cassandra_servers', None))
self.port = (port or
conf.get('cassandra_port', None))
self.keyspace = (keyspace or
conf.get('cassandra_keyspace', None))
self.table = (table or
conf.get('cassandra_table', None))
if not self.servers or not self.keyspace or not self.table:
raise ImproperlyConfigured('Cassandra backend not configured.')
expires = (entry_ttl or conf.get('cassandra_entry_ttl', None))
super(TyrantBackend, self).__init__(**kwargs)
if not pytyrant:
raise ImproperlyConfigured(
"You need to install the pytyrant library to use the "
+ "Tokyo Tyrant backend.")
self.tyrant_host = (tyrant_host or
self.app.conf.get("TT_HOST") or
self.tyrant_host)
self.tyrant_port = (tyrant_port or
self.app.conf.get("TT_PORT") or
self.tyrant_port)
if self.tyrant_port:
self.tyrant_port = int(self.tyrant_port)
if not self.tyrant_host or not self.tyrant_port:
raise ImproperlyConfigured(
"To use the Tokyo Tyrant backend, you have to "
"set the TT_HOST and TT_PORT settings in your settings.py")
self._connection = None
def _get_database(self):
""""Get database from MongoDB connection and perform authentication
if necessary."""
if self._database is None:
conn = self._get_connection()
db = conn[self.mongodb_database]
if self.mongodb_user and self.mongodb_password:
auth = db.authenticate(self.mongodb_user,
self.mongodb_password)
if not auth:
raise ImproperlyConfigured(
"Invalid MongoDB username or password.")
self._database = db
return self._database
write_cons = conf.get('cassandra_write_consistency') or 'LOCAL_QUORUM'
self.read_consistency = getattr(
cassandra.ConsistencyLevel, read_cons,
cassandra.ConsistencyLevel.LOCAL_QUORUM)
self.write_consistency = getattr(
cassandra.ConsistencyLevel, write_cons,
cassandra.ConsistencyLevel.LOCAL_QUORUM)
self.auth_provider = None
auth_provider = conf.get('cassandra_auth_provider', None)
auth_kwargs = conf.get('cassandra_auth_kwargs', None)
if auth_provider and auth_kwargs:
auth_provider_class = getattr(cassandra.auth, auth_provider, None)
if not auth_provider_class:
raise ImproperlyConfigured(E_NO_SUCH_CASSANDRA_AUTH_PROVIDER)
self.auth_provider = auth_provider_class(**auth_kwargs)
self._connection = None
self._session = None
self._write_stmt = None
self._read_stmt = None
self._make_stmt = None