Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RabbitMQ user permissions #1126

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 26 additions & 32 deletions sarracenia/rabbitmq_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,24 @@ def exec_rabbitmqadmin(url, options, simulate=False):
command += ' --ssl --port=15671 '
command += ' ' + options

logger.debug("command = %s" % command)
logger.debug(f"command = {command}")
if sys.version_info.major < 3 or (sys.version_info.major == 3
and sys.version_info.minor < 5):
if logger: logger.debug("using subprocess.getstatusoutput")

if simulate:
print("dry_run: %s" % ' '.join(command))
print(f"dry_run: {' '.join(command)}")
return 0, None

return subprocess.getstatusoutput(command)
else:
cmdlin = command.replace("'", '')
cmdlst = cmdlin.split()
if logger:
logger.debug("using subprocess.run cmdlst=%s" %
' '.join(cmdlst))
logger.debug(f"using subprocess.run cmdlst={' '.join(cmdlst)}")

if simulate:
print("dry_run: %s" % cmdlin)
print(f"dry_run: {cmdlin}")
return 0, None

rclass = subprocess.run(cmdlst, stdout=subprocess.PIPE)
Expand All @@ -69,10 +68,10 @@ def exec_rabbitmqadmin(url, options, simulate=False):
except:
if sys.version_info.major < 3 or (sys.version_info.major == 3
and sys.version_info.minor < 5):
if logger: logger.error("trying run command %s %s" % command)
if logger: logger.error(f"trying run command {command[0]} {command[1]}")
else:
if logger:
logger.error("trying run command %s %s" % ' '.join(cmdlst))
logger.error(f"trying run command {' '.join(cmdlst)}")
if logger: logger.debug('Exception details:', exc_info=True)

return 0, None
Expand All @@ -83,9 +82,9 @@ def add_user(url, role, user, passwd, simulate):
add the given user with the given credentials.
"""

declare = "declare user name='%s' password=" % user
declare = f"declare user name='{user}' password="

if passwd != None: declare += "\'%s\'" % urllib.parse.unquote(passwd)
if passwd != None: declare += f"\'{urllib.parse.unquote(passwd)}\'"
if role == 'admin': declare += " tags=administrator "
else: declare += ' tags="" '

Expand All @@ -97,42 +96,39 @@ def add_user(url, role, user, passwd, simulate):
c = "configure=.*"
w = "write=.*"
r = "read=.*"
logger.info("permission user \'%s\' role %s %s %s %s " %
(user + '@' + url.hostname, 'feeder', c, w, r))
declare = "declare permission vhost=/ user=%s %s %s %s" % (user, c, w, r)
logger.info(f"permission user \'{user + '@' + url.hostname}\' role feeder {c} {w} {r} ")
declare = f"declare permission vhost=/ user={user} {c} {w} {r}"
dummy = run_rabbitmqadmin(url, declare, simulate)
return

# source

if role in ['source']:
c = "configure=^q_%s.*|^xs_%s.*" % (user, user)
w = "write=^q_%s.*|^xs_%s.*" % (user, user)
r = "read=^q_%s.*|^x[lrs]_%s.*|^x.*public$" % (user, user)
logger.info("permission user '%s' role %s %s %s %s " %
(user + '@' + url.hostname, 'source', c, w, r))
declare = "declare permission vhost=/ user=%s %s %s %s" % (user, c, w, r)
c = f"configure=^q_{user}.*|^x[rs]_{user}.*"
w = f"write=^q_{user}.*|^x[rs]_{user}.*"
r = f"read=^q_{user}.*|^x[rs]_{user}.*|^x.*public$"
logger.info(f"permission user '{user}@{url.hostname}' role source {c} {w} {r} ")
declare = f"declare permission vhost=/ user={user} {c} {w} {r}"
dummy = run_rabbitmqadmin(url, declare, simulate)
return

# subscribe

if role in ['subscribe', 'subscriber']:
c = "configure=^q_%s.*" % user
w = "write=^q_%s.*|^xs_%s$" % (user, user)
r = "read=^q_%s.*|^x[lrs]_%s.*|^x.*public$" % (user, user)
logger.info("permission user '%s' role %s %s %s %s " %
(user + '@' + url.hostname, 'source', c, w, r))
declare = "declare permission vhost=/ user=%s %s %s %s" % (user, c, w, r)
c = f"configure=^q_{user}.*|^x[r]_{user}$"
w = f"write=^q_{user}.*|^x[r]_{user}$"
r = f"read=^q_{user}.*|^x[rs]_{user}.*|^x.*public$"
logger.info(f"permission user '{user}@{url.hostname}' role source {c} {w} {r} ")
declare = f"declare permission vhost=/ user={user} {c} {w} {r}"
dummy = run_rabbitmqadmin(url, declare, simulate)


def del_user(url, user, simulate):
"""
delete user from the given broker.
"""
logger.info("deleting user %s" % user)
delete = "delete user name='%s'" % user
logger.info(f"deleting user {user}")
delete = f"delete user name='{user}'"
dummy = run_rabbitmqadmin(url, delete, simulate)


Expand Down Expand Up @@ -303,12 +299,11 @@ def user_access(url, user):
lex = list(
map(lambda x: x['name'],
json.loads(exec_rabbitmqadmin(url, "list exchanges name")[1])))
print("exchanges: %s\n\n" % lex)
print(f"exchanges: {lex}\n\n")

u = 'tsource'
up = rabbitmq_user_access(url, u)
print("permissions for %s: \nqueues: %s\nexchanges: %s\nbindings %s" %
(u, up['queues'], up['exchanges'], up['bindings']))
print(f"permissions for {u}: \nqueues: {up['queues']}\nexchanges: {up['exchanges']}\nbindings {up['bindings']}")
#print( "\n\nbindings: %s" % json.loads(exec_rabbitmqadmin(url,"list bindings")[1]) )


Expand All @@ -318,7 +313,7 @@ def run_rabbitmqadmin(url, options, simulate=False):
capture result.
"""

logger.debug("sr_rabbit run_rabbitmqadmin %s" % options)
logger.debug(f"sr_rabbit run_rabbitmqadmin {options}")
try:
(status, answer) = exec_rabbitmqadmin(url, options, simulate)

Expand All @@ -340,7 +335,6 @@ def run_rabbitmqadmin(url, options, simulate=False):
return lst

except:
logger.error("sr_rabbit/run_rabbitmqadmin failed with option '%s'" %
options)
logger.error(f"sr_rabbit/run_rabbitmqadmin failed with option '{options}'")
logger.debug('Exception details: ', exc_info=True)
return []
Loading