14-dynsec-role.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/env python3
  2. from mosq_test_helper import *
  3. import json
  4. import shutil
  5. def write_config(filename, port):
  6. with open(filename, 'w') as f:
  7. f.write("listener %d\n" % (port))
  8. f.write("allow_anonymous true\n")
  9. f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n")
  10. f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port))
  11. def command_check(sock, command_payload, expected_response, msg=""):
  12. command_packet = mosq_test.gen_publish(topic="$CONTROL/dynamic-security/v1", qos=0, payload=json.dumps(command_payload))
  13. sock.send(command_packet)
  14. response = json.loads(mosq_test.read_publish(sock))
  15. if response != expected_response:
  16. print(msg)
  17. print(expected_response)
  18. print(response)
  19. raise ValueError(response)
  20. port = mosq_test.get_port()
  21. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  22. write_config(conf_file, port)
  23. create_client_command = { "commands": [{
  24. "command": "createClient", "username": "user_one",
  25. "password": "password", "clientid": "cid",
  26. "textname": "Name", "textdescription": "Description",
  27. "rolename": "", "correlationData": "2" }]
  28. }
  29. create_client_response = {'responses': [{'command': 'createClient', 'correlationData': '2'}]}
  30. create_client2_command = { "commands": [{
  31. "command": "createClient", "username": "user_two",
  32. "password": "password",
  33. "textname": "Name", "textdescription": "Description",
  34. "rolename": "", "correlationData": "3" }]
  35. }
  36. create_client2_response = {'responses': [{'command': 'createClient', 'correlationData': '3'}]}
  37. create_group_command = { "commands": [{
  38. "command": "createGroup", "groupname": "group_one",
  39. "textname": "Name", "textdescription": "Description",
  40. "correlationData":"3"}]}
  41. create_group_response = {'responses':[{"command":"createGroup","correlationData":"3"}]}
  42. create_role_command = { "commands": [{'command': 'createRole', 'correlationData': '3',
  43. "rolename": "basic", "acls":[
  44. {"acltype":"publishClientSend", "topic": "out/#", "priority":3, "allow": True}], "textname":"name", "textdescription":"desc"
  45. }]}
  46. create_role_response = {'responses': [{'command': 'createRole', 'correlationData': '3'}]}
  47. create_role2_command = { "commands": [{'command': 'createRole', 'correlationData': '3',
  48. "rolename": "basic2", "acls":[
  49. {"acltype":"publishClientSend", "topic": "out/#", "priority":3, "allow": True}], "textname":"name", "textdescription":"desc"
  50. }]}
  51. create_role2_response = {'responses': [{'command': 'createRole', 'correlationData': '3'}]}
  52. add_role_to_client_command = {"commands": [{'command': 'addClientRole', "username": "user_one",
  53. "rolename": "basic"}]}
  54. add_role_to_client_response = {'responses': [{'command': 'addClientRole'}]}
  55. add_role_to_client2_command = {"commands": [{'command': 'addClientRole', "username": "user_one",
  56. "rolename": "basic2"}]}
  57. add_role_to_client2_response = {'responses': [{'command': 'addClientRole'}]}
  58. add_role_to_group_command = {"commands": [{'command': 'addGroupRole', "groupname": "group_one",
  59. "rolename": "basic"}]}
  60. add_role_to_group_response = {'responses': [{'command': 'addGroupRole'}]}
  61. list_roles_verbose_command1 = { "commands": [{
  62. "command": "listRoles", "verbose": True, "correlationData": "21"}]
  63. }
  64. list_roles_verbose_response1 = {'responses': [{'command': 'listRoles', 'data':
  65. {'totalCount':3, 'roles': [
  66. {"rolename":"admin","acls":[
  67. {"acltype": "publishClientSend", "topic": "$CONTROL/dynamic-security/#", "priority":0, "allow": True },
  68. {"acltype": "publishClientReceive", "topic": "$CONTROL/dynamic-security/#", "priority":0, "allow": True },
  69. {"acltype": "publishClientReceive", "topic": "$SYS/#", "priority":0, "allow": True },
  70. {"acltype": "publishClientReceive", "topic": "#", "priority":0, "allow": True },
  71. {"acltype": "subscribePattern", "topic": "$CONTROL/dynamic-security/#", "priority":0, "allow": True },
  72. {"acltype": "subscribePattern", "topic": "$SYS/#", "priority":0, "allow": True },
  73. {"acltype": "subscribePattern", "topic": "#", "priority":0, "allow": True},
  74. {"acltype": "unsubscribePattern", "topic": "#", "priority":0, "allow": True}]},
  75. {'rolename': 'basic', "textname": "name", "textdescription": "desc",
  76. 'acls': [{'acltype':'publishClientSend', 'topic': 'out/#', 'priority': 3, 'allow': True}]},
  77. {'rolename': 'basic2', "textname": "name", "textdescription": "desc",
  78. 'acls': [{'acltype':'publishClientSend', 'topic': 'out/#', 'priority': 3, 'allow': True}]
  79. }]}, 'correlationData': '21'}]}
  80. add_acl_command = {"commands": [{'command': "addRoleACL", "rolename":"basic", "acltype":"subscribeLiteral",
  81. "topic":"basic/out", "priority":1, "allow":True}]}
  82. add_acl_response = {'responses': [{'command': 'addRoleACL'}]}
  83. add_acl2_command = {"commands": [{'command': "addRoleACL", "rolename":"basic", "acltype":"subscribeLiteral",
  84. "topic":"basic/out", "priority":1, "allow":True}]}
  85. add_acl2_response = {'responses': [{'command': 'addRoleACL', 'error':'ACL with this topic already exists'}]}
  86. list_roles_verbose_command2 = { "commands": [{
  87. "command": "listRoles", "verbose": True, "correlationData": "22"}]
  88. }
  89. list_roles_verbose_response2 = {'responses': [{'command': 'listRoles', 'data': {'totalCount':3, 'roles':
  90. [{"rolename":"admin","acls":[
  91. {"acltype": "publishClientSend", "topic": "$CONTROL/dynamic-security/#", "priority":0, "allow": True },
  92. {"acltype": "publishClientReceive", "topic": "$CONTROL/dynamic-security/#", "priority":0, "allow": True },
  93. {"acltype": "publishClientReceive", "topic": "$SYS/#", "priority":0, "allow": True },
  94. {"acltype": "publishClientReceive", "topic": "#", "priority":0, "allow": True },
  95. {"acltype": "subscribePattern", "topic": "$CONTROL/dynamic-security/#", "priority":0, "allow": True },
  96. {"acltype": "subscribePattern", "topic": "$SYS/#", "priority":0, "allow": True },
  97. {"acltype": "subscribePattern", "topic": "#", "priority":0, "allow": True},
  98. {"acltype": "unsubscribePattern", "topic": "#", "priority":0, "allow": True}]},
  99. {'rolename': 'basic', 'textname': 'name', 'textdescription': 'desc', 'acls':
  100. [{'acltype':'publishClientSend', 'topic': 'out/#', 'priority': 3, 'allow': True},
  101. {'acltype':'subscribeLiteral', 'topic': 'basic/out', 'priority': 1, 'allow': True}]},
  102. {'rolename': 'basic2', "textname": "name", "textdescription": "desc",
  103. 'acls': [{'acltype':'publishClientSend', 'topic': 'out/#', 'priority': 3, 'allow': True}]
  104. }]}, 'correlationData': '22'}]}
  105. get_role_command = {"commands": [{'command': "getRole", "rolename":"basic"}]}
  106. get_role_response = {'responses': [{'command': 'getRole', 'data': {'role':
  107. {'rolename': 'basic', 'textname': 'name', 'textdescription': 'desc', 'acls':
  108. [{'acltype':'publishClientSend', 'topic': 'out/#', 'priority': 3, 'allow': True},
  109. {'acltype':'subscribeLiteral', 'topic': 'basic/out', 'priority': 1, 'allow': True}],
  110. }}}]}
  111. remove_acl_command = {"commands": [{'command': "removeRoleACL", "rolename":"basic", "acltype":"subscribeLiteral",
  112. "topic":"basic/out"}]}
  113. remove_acl_response = {'responses': [{'command': 'removeRoleACL'}]}
  114. remove_acl2_command = {"commands": [{'command': "removeRoleACL", "rolename":"basic", "acltype":"subscribeLiteral",
  115. "topic":"basic/out"}]}
  116. remove_acl2_response = {'responses': [{'command': 'removeRoleACL', 'error':'ACL not found'}]}
  117. delete_role_command = {"commands": [{'command': "deleteRole", "rolename":"basic"}]}
  118. delete_role_response = {"responses": [{"command": "deleteRole"}]}
  119. delete_role2_command = {"commands": [{'command': "deleteRole", "rolename":"basic"}]}
  120. delete_role2_response = {"responses": [{"command": "deleteRole"}]}
  121. list_clients_verbose_command = { "commands": [{
  122. "command": "listClients", "verbose": True, "correlationData": "20"}]
  123. }
  124. list_clients_verbose_response = {'responses':[{"command": "listClients", "data":{'totalCount':3, "clients":[
  125. {'username': 'admin', 'textname': 'Dynsec admin user', 'roles': [{'rolename': 'admin'}], 'groups': []},
  126. {"username":"user_one", "clientid":"cid", "textname":"Name", "textdescription":"Description",
  127. "groups":[], "roles":[{'rolename':'basic'}, {'rolename':'basic2'}]},
  128. {"username":"user_two", "textname":"Name", "textdescription":"Description",
  129. "groups":[], "roles":[]}]}, "correlationData":"20"}]}
  130. list_groups_verbose_command = { "commands": [{
  131. "command": "listGroups", "verbose": True, "correlationData": "20"}]
  132. }
  133. list_groups_verbose_response = {'responses':[{"command": "listGroups", "data":{'totalCount':1, "groups":[
  134. {"groupname":"group_one", "textname":"Name", "textdescription":"Description",
  135. "clients":[], "roles":[{'rolename':'basic'}]}]}, "correlationData":"20"}]}
  136. remove_role_from_client_command = {"commands": [{'command': 'removeClientRole', "username": "user_one",
  137. "rolename": "basic"}]}
  138. remove_role_from_client_response = {'responses': [{'command': 'removeClientRole'}]}
  139. remove_role_from_group_command = {"commands": [{'command': 'removeGroupRole', "groupname": "group_one",
  140. "rolename": "basic"}]}
  141. remove_role_from_group_response = {'responses': [{'command': 'removeGroupRole'}]}
  142. rc = 1
  143. keepalive = 10
  144. connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
  145. connack_packet = mosq_test.gen_connack(rc=0)
  146. mid = 2
  147. subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/#", 1)
  148. suback_packet = mosq_test.gen_suback(mid, 1)
  149. try:
  150. os.mkdir(str(port))
  151. shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
  152. except FileExistsError:
  153. pass
  154. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  155. try:
  156. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  157. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  158. # Create client
  159. command_check(sock, create_client2_command, create_client2_response)
  160. command_check(sock, create_client_command, create_client_response)
  161. # Create group
  162. command_check(sock, create_group_command, create_group_response)
  163. # Create role
  164. command_check(sock, create_role_command, create_role_response)
  165. command_check(sock, create_role2_command, create_role2_response)
  166. # Add role to client
  167. command_check(sock, add_role_to_client2_command, add_role_to_client2_response)
  168. command_check(sock, add_role_to_client_command, add_role_to_client_response)
  169. # Add role to group
  170. command_check(sock, add_role_to_group_command, add_role_to_group_response)
  171. # List clients verbose
  172. command_check(sock, list_clients_verbose_command, list_clients_verbose_response)
  173. # List groups verbose
  174. command_check(sock, list_groups_verbose_command, list_groups_verbose_response)
  175. # List roles verbose 1
  176. command_check(sock, list_roles_verbose_command1, list_roles_verbose_response1, "list roles verbose 1a")
  177. # Add ACL
  178. command_check(sock, add_acl_command, add_acl_response)
  179. command_check(sock, add_acl2_command, add_acl2_response)
  180. # List roles verbose 2
  181. command_check(sock, list_roles_verbose_command2, list_roles_verbose_response2, "list roles verbose 2a")
  182. # Kill broker and restart, checking whether our changes were saved.
  183. broker.terminate()
  184. broker.wait()
  185. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  186. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  187. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  188. # List roles verbose 2
  189. command_check(sock, list_roles_verbose_command2, list_roles_verbose_response2, "list roles verbose 2b")
  190. # Get role
  191. command_check(sock, get_role_command, get_role_response)
  192. # Remove ACL
  193. command_check(sock, remove_acl_command, remove_acl_response)
  194. command_check(sock, remove_acl2_command, remove_acl2_response)
  195. # List roles verbose 1
  196. command_check(sock, list_roles_verbose_command1, list_roles_verbose_response1, "list roles verbose 1b")
  197. # Remove role from client
  198. command_check(sock, remove_role_from_client_command, remove_role_from_client_response)
  199. # Remove role from group
  200. command_check(sock, remove_role_from_group_command, remove_role_from_group_response)
  201. # Delete role
  202. command_check(sock, delete_role_command, delete_role_response)
  203. rc = 0
  204. sock.close()
  205. except mosq_test.TestError:
  206. pass
  207. finally:
  208. os.remove(conf_file)
  209. try:
  210. os.remove(f"{port}/dynamic-security.json")
  211. except FileNotFoundError:
  212. pass
  213. os.rmdir(f"{port}")
  214. broker.terminate()
  215. broker.wait()
  216. (stdo, stde) = broker.communicate()
  217. if rc:
  218. print(stde.decode('utf-8'))
  219. exit(rc)