def __init__(self, broker, path): self.policy = path self.original = self.read() conn = Connection.establish(broker, protocol='amqp0-10', username='******', password='******', sasl_mechanism='PLAIN') self.agent = Mgmt(conn) self.agent.create('queue', 'acl_test_queue') self.lines = []
def do_simple_topic_test(self, pattern, name, properties={}, autodeleted=True): mgmt = self.create_connection("amqp0-10", True) agent = Mgmt(mgmt) agent.create("TopicPolicy", pattern, properties) try: snd = self.ssn.sender(name) rcv1 = self.ssn.receiver(name) rcv2 = self.ssn.receiver(name) msgs = [Message(content=s, subject=s) for s in ["a", "b", "c", "d"]] for m in msgs: snd.send(m) for rcv in [rcv1, rcv2]: for expected in msgs: msg = rcv.fetch(0) assert msg.content == expected.content, (msg.content, expected.content) self.ssn.acknowledge() rcv1.close() rcv2.close() snd.close() matched = [e for e in agent.list("Exchange") if e["name"] == name] if autodeleted: # ensure that exchange is no longer there (as it is now unused) assert len(matched) == 0, matched else: # ensure that exchange has not been autodeleted in spite of being unused assert len(matched) == 1, matched finally: agent.delete("TopicPolicy", pattern) mgmt.close()
def do_simple_queue_test(self, pattern, name, properties={}, autodeleted=True): mgmt = self.create_connection("amqp0-10", True) agent = Mgmt(mgmt) agent.create("QueuePolicy", pattern, properties) try: snd = self.ssn.sender(name) msgs = [Message(content=s, subject=s) for s in ["a", "b", "c", "d"]] for m in msgs: snd.send(m) snd.close() for expected in msgs: rcv = self.ssn.receiver(name) msg = rcv.fetch(0) assert msg.content == expected.content, (msg.content, expected.content) self.ssn.acknowledge() rcv.close() # close after each message to ensure queue isn't deleted with messages in it self.ssn.close() self.conn.close() matched = [q for q in agent.list("Queue") if q["name"] == name] if autodeleted: # ensure that queue is no longer there (as empty and unused) assert len(matched) == 0, matched else: # ensure that queue is still there though empty and unused assert len(matched) == 1, matched finally: agent.delete("QueuePolicy", pattern) mgmt.close()
def do_simple_topic_test(self, pattern, name, properties={}, autodeleted=True): mgmt = self.create_connection("amqp0-10", True) agent = Mgmt(mgmt) agent.create('TopicPolicy', pattern, properties) try: snd = self.ssn.sender(name) rcv1 = self.ssn.receiver(name) rcv2 = self.ssn.receiver(name) msgs = [ Message(content=s, subject=s) for s in ['a', 'b', 'c', 'd'] ] for m in msgs: snd.send(m) for rcv in [rcv1, rcv2]: for expected in msgs: msg = rcv.fetch(0) assert msg.content == expected.content, (msg.content, expected.content) self.ssn.acknowledge() rcv1.close() rcv2.close() snd.close() matched = [e for e in agent.list("Exchange") if e['name'] == name] if autodeleted: # ensure that exchange is no longer there (as it is now unused) assert len(matched) == 0, (matched) else: # ensure that exchange has not been autodeleted in spite of being unused assert len(matched) == 1, (matched) finally: agent.delete('TopicPolicy', pattern) mgmt.close()
def do_simple_queue_test(self, pattern, name, properties={}, autodeleted=True): mgmt = self.create_connection("amqp0-10", True) agent = Mgmt(mgmt) agent.create('QueuePolicy', pattern, properties) try: snd = self.ssn.sender(name) msgs = [ Message(content=s, subject=s) for s in ['a', 'b', 'c', 'd'] ] for m in msgs: snd.send(m) snd.close() for expected in msgs: rcv = self.ssn.receiver(name) msg = rcv.fetch(0) assert msg.content == expected.content, (msg.content, expected.content) self.ssn.acknowledge() rcv.close( ) #close after each message to ensure queue isn't deleted with messages in it self.ssn.close() self.conn.close() matched = [q for q in agent.list("Queue") if q['name'] == name] if autodeleted: # ensure that queue is no longer there (as empty and unused) assert len(matched) == 0, (matched) else: # ensure that queue is still there though empty and unused assert len(matched) == 1, (matched) finally: agent.delete('QueuePolicy', pattern) mgmt.close()
def test_mgmt(self): mgmt = self.create_connection("amqp0-10", True) agent = Mgmt(mgmt) agent.create('QueuePolicy', 'queue-*') agent.create('QueuePolicy', 'alt.queue.*') agent.create('TopicPolicy', 'topic-*') try: queues = [q['name'] for q in agent.list("QueuePolicy")] topics = [t['name'] for t in agent.list("TopicPolicy")] assert 'queue-*' in queues, (queues) assert 'alt.queue.*' in queues, (queues) try: agent.delete('TopicPolicy', 'queue-*') assert False, ( 'Deletion of policy using wrong type should fail') except: None finally: agent.delete('QueuePolicy', 'queue-*') agent.delete('QueuePolicy', 'alt.queue.*') agent.delete('TopicPolicy', 'topic-*') mgmt.close()
class AclCtrl(object): def __init__(self, broker, path): self.policy = path self.original = self.read() conn = Connection.establish(broker, protocol='amqp0-10', username='******', password='******', sasl_mechanism='PLAIN') self.agent = Mgmt(conn) self.agent.create('queue', 'acl_test_queue') self.lines = [] def deny(self, user=None, *args): self._add_rule('deny', user, *args) return self def allow(self, user=None, *args): self._add_rule('allow', user, *args) return self def apply(self, allow_admin=True): if allow_admin: # admin users needs permission to send a qmf message to # reload policy self.lines.insert(0, 'acl allow admin@QPID all all') self.specify("\n".join(self.lines)) return self def dump(self): print "\n".join(self.lines) return self def clear(self): self.lines = [] return self def _add_rule(self, deny_or_allow, user=None, *args): elements = ['acl', deny_or_allow] if user: elements.append("%s@QPID" % user) else: elements.append('all') if len(args) > 0: for a in args: elements.append(a) else: elements.append('all') self.lines.append(' '.join(elements)) def read(self): f = open(self.policy,'r') content = f.read() f.close() return content def specify(self, acl): f = open(self.policy,'w') f.write(acl) f.close() self.agent.reload_acl_file() def restore(self): self.agent.delete('queue', 'acl_test_queue') self.specify(self.original) self.agent.close()
def test_mgmt(self): mgmt = self.create_connection("amqp0-10", True) agent = Mgmt(mgmt) agent.create("QueuePolicy", "queue-*") agent.create("QueuePolicy", "alt.queue.*") agent.create("TopicPolicy", "topic-*") try: queues = [q["name"] for q in agent.list("QueuePolicy")] topics = [t["name"] for t in agent.list("TopicPolicy")] assert "queue-*" in queues, queues assert "alt.queue.*" in queues, queues try: agent.delete("TopicPolicy", "queue-*") assert False, "Deletion of policy using wrong type should fail" except: None finally: agent.delete("QueuePolicy", "queue-*") agent.delete("QueuePolicy", "alt.queue.*") agent.delete("TopicPolicy", "topic-*") mgmt.close()
class AclCtrl(object): def __init__(self, broker, path): self.policy = path self.original = self.read() conn = Connection.establish(broker, protocol='amqp0-10', username='******', password='******', sasl_mechanism='PLAIN') self.agent = Mgmt(conn) self.agent.create('queue', 'acl_test_queue') self.lines = [] def deny(self, user=None, *args): self._add_rule('deny', user, *args) return self def allow(self, user=None, *args): self._add_rule('allow', user, *args) return self def apply(self, allow_admin=True): if allow_admin: # admin users needs permission to send a qmf message to # reload policy self.lines.insert(0, 'acl allow admin@QPID all all') self.specify("\n".join(self.lines)) return self def dump(self): print "\n".join(self.lines) return self def clear(self): self.lines = [] return self def _add_rule(self, deny_or_allow, user=None, *args): elements = ['acl', deny_or_allow] if user: elements.append("%s@QPID" % user) else: elements.append('all') if len(args) > 0: for a in args: elements.append(a) else: elements.append('all') self.lines.append(' '.join(elements)) def read(self): f = open(self.policy, 'r') content = f.read() f.close() return content def specify(self, acl): f = open(self.policy, 'w') f.write(acl) f.close() self.agent.reload_acl_file() def restore(self): self.agent.delete('queue', 'acl_test_queue') self.specify(self.original) self.agent.close()