Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_rabbit_connection(config_path, include_password, conn="amqps"):
"""
Given the path to the directory where the broker configurations are stored
setup and return the RabbitMQ connection string.
:param config_path : The path for ssl certificates and passwords
:param include_password : Format the connection for ouput by setting this True
"""
LOG.debug(f"Broker: connection = {conn}")
vhost = CONFIG.broker.vhost
LOG.debug(f"Broker: vhost = {vhost}")
username = CONFIG.broker.username
LOG.debug(f"Broker: username = {username}")
server = CONFIG.broker.server
LOG.debug(f"Broker: server = {server}")
try:
password_filepath = CONFIG.broker.password
LOG.debug(f"Broker: password filepath = {password_filepath}")
password_filepath = os.path.abspath(expanduser(password_filepath))
except KeyError:
raise ValueError("Broker: No password provided for RabbitMQ")
try:
def get_redissock_connection(config_path, include_password):
"""
Given the path to the directory where the broker configurations are stored
setup and return the redis+socket connection string.
:param config_path : The path for ssl certificates and passwords
:param include_password : Format the connection for ouput by setting this True
"""
try:
db_num = CONFIG.broker.db_num
except (AttributeError, KeyError):
db_num = 0
LOG.debug(f"Broker: redis+socket using default db_num = {db_num}")
redis_config = {"db_num": db_num, "path": CONFIG.broker.path}
return REDISSOCK_CONNECTION.format(**redis_config)
def get_redis_connection(config_path, include_password, ssl=False):
"""
Return the redis or rediss specific connection
:param config_path : The path for ssl certificates and passwords
:param include_password : Format the connection for ouput by setting this True
:param ssl : Flag to use rediss output
"""
server = CONFIG.broker.server
LOG.debug(f"Broker: server = {server}")
urlbase = "rediss" if ssl else "redis"
try:
port = CONFIG.broker.port
LOG.debug(f"Broker: redis port = {port}")
except (AttributeError, KeyError):
port = 6379
LOG.debug(f"Broker: redis using default port = {port}")
try:
db_num = CONFIG.broker.db_num
except (AttributeError, KeyError):
db_num = 0
LOG.debug(f"Broker: redis using default db_num = {db_num}")
try:
username = CONFIG.broker.username
except (AttributeError, KeyError):
username = ""
def get_redis_connection(config_path, include_password, ssl=False):
"""
Return the redis or rediss specific connection
:param config_path : The path for ssl certificates and passwords
:param include_password : Format the connection for ouput by setting this True
:param ssl : Flag to use rediss output
"""
server = CONFIG.broker.server
LOG.debug(f"Broker: server = {server}")
urlbase = "rediss" if ssl else "redis"
try:
port = CONFIG.broker.port
LOG.debug(f"Broker: redis port = {port}")
except (AttributeError, KeyError):
port = 6379
LOG.debug(f"Broker: redis using default port = {port}")
try:
db_num = CONFIG.broker.db_num
except (AttributeError, KeyError):
db_num = 0
LOG.debug(f"Broker: redis using default db_num = {db_num}")
"""
Given the path to the directory where the broker configurations are stored
setup and return the RabbitMQ connection string.
:param config_path : The path for ssl certificates and passwords
:param include_password : Format the connection for ouput by setting this True
"""
LOG.debug(f"Broker: connection = {conn}")
vhost = CONFIG.broker.vhost
LOG.debug(f"Broker: vhost = {vhost}")
username = CONFIG.broker.username
LOG.debug(f"Broker: username = {username}")
server = CONFIG.broker.server
LOG.debug(f"Broker: server = {server}")
try:
password_filepath = CONFIG.broker.password
LOG.debug(f"Broker: password filepath = {password_filepath}")
password_filepath = os.path.abspath(expanduser(password_filepath))
except KeyError:
raise ValueError("Broker: No password provided for RabbitMQ")
try:
password = read_file(password_filepath)
except IOError:
raise ValueError(
f"Broker: RabbitMQ password file {password_filepath} does not exist"
)
try:
port = CONFIG.broker.port
LOG.debug(f"Broker: redis port = {port}")
except (AttributeError, KeyError):
port = 6379
LOG.debug(f"Broker: redis using default port = {port}")
try:
db_num = CONFIG.broker.db_num
except (AttributeError, KeyError):
db_num = 0
LOG.debug(f"Broker: redis using default db_num = {db_num}")
try:
username = CONFIG.broker.username
except (AttributeError, KeyError):
username = ""
try:
password_filepath = CONFIG.broker.password
try:
password = read_file(password_filepath)
except IOError:
password = CONFIG.broker.password
if include_password:
spass = "%s:%s@" % (username, password)
else:
spass = "%s:%s@" % (username, "******")
except (AttributeError, KeyError):
spass = ""
LOG.debug(f"Broker: redis using default password = {spass}")
port = 6379
LOG.debug(f"Broker: redis using default port = {port}")
try:
db_num = CONFIG.broker.db_num
except (AttributeError, KeyError):
db_num = 0
LOG.debug(f"Broker: redis using default db_num = {db_num}")
try:
username = CONFIG.broker.username
except (AttributeError, KeyError):
username = ""
try:
password_filepath = CONFIG.broker.password
try:
password = read_file(password_filepath)
except IOError:
password = CONFIG.broker.password
if include_password:
spass = "%s:%s@" % (username, password)
else:
spass = "%s:%s@" % (username, "******")
except (AttributeError, KeyError):
spass = ""
LOG.debug(f"Broker: redis using default password = {spass}")
return f"{urlbase}://{spass}{server}:{port}/{db_num}"
def get_redissock_connection(config_path, include_password):
"""
Given the path to the directory where the broker configurations are stored
setup and return the redis+socket connection string.
:param config_path : The path for ssl certificates and passwords
:param include_password : Format the connection for ouput by setting this True
"""
try:
db_num = CONFIG.broker.db_num
except (AttributeError, KeyError):
db_num = 0
LOG.debug(f"Broker: redis+socket using default db_num = {db_num}")
redis_config = {"db_num": db_num, "path": CONFIG.broker.path}
return REDISSOCK_CONNECTION.format(**redis_config)
"""
Return the connection string based on the configuration specified in the
`app.yaml` config file.
If the url variable is present, return that as the connection string.
:param include_password : The connection can be formatted for output by
setting this to True
"""
try:
return CONFIG.broker.url
except AttributeError:
pass
try:
broker = CONFIG.broker.name.lower()
except AttributeError:
broker = ""
try:
config_path = CONFIG.celery.certs
config_path = os.path.abspath(os.path.expanduser(config_path))
except AttributeError:
config_path = None
if broker not in BROKERS:
raise ValueError(f"Error: {broker} is not a supported broker.")
if broker == "rabbitmq" or broker == "amqps":
return get_rabbit_connection(config_path, include_password, conn="amqps")
elif broker == "amqp":