14-dynsec-modify-client.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "correlationData": "2" }]
  28. }
  29. create_client_response = {'responses': [{'command': 'createClient', 'correlationData': '2'}]}
  30. create_groups_command = { "commands": [
  31. {
  32. "command": "createGroup", "groupname": "group_one",
  33. "textname": "Name", "textdescription": "Description",
  34. "correlationData": "12"
  35. },
  36. {
  37. "command": "createGroup", "groupname": "group_two",
  38. "textname": "Name", "textdescription": "Description",
  39. "correlationData": "13"
  40. }
  41. ]
  42. }
  43. create_groups_response = {'responses': [
  44. {'command': 'createGroup', 'correlationData': '12'},
  45. {'command': 'createGroup', 'correlationData': '13'}
  46. ]}
  47. create_roles_command = { "commands": [
  48. {
  49. "command": "createRole", "rolename": "role_one",
  50. "textname": "Name", "textdescription": "Description",
  51. "acls":[], "correlationData": "21"
  52. },
  53. {
  54. "command": "createRole", "rolename": "role_two",
  55. "textname": "Name", "textdescription": "Description",
  56. "acls":[], "correlationData": "22"
  57. },
  58. {
  59. "command": "createRole", "rolename": "role_three",
  60. "textname": "Name", "textdescription": "Description",
  61. "acls":[], "correlationData": "23"
  62. }
  63. ]
  64. }
  65. create_roles_response = {'responses': [
  66. {'command': 'createRole', 'correlationData': '21'},
  67. {'command': 'createRole', 'correlationData': '22'},
  68. {'command': 'createRole', 'correlationData': '23'}
  69. ]}
  70. modify_client_command1 = { "commands": [{
  71. "command": "modifyClient", "username": "user_one",
  72. "textname": "Modified name", "textdescription": "Modified description",
  73. "clientid": "",
  74. "roles":[
  75. {'rolename':'role_one', 'priority':2},
  76. {'rolename':'role_two'},
  77. {'rolename':'role_three', 'priority':10}
  78. ],
  79. "groups":[
  80. {'groupname':'group_one', 'priority':3},
  81. {'groupname':'group_two', 'priority':8}
  82. ],
  83. "correlationData": "3" }]
  84. }
  85. modify_client_response1 = {'responses': [{'command': 'modifyClient', 'correlationData': '3'}]}
  86. modify_client_command2 = { "commands": [{
  87. "command": "modifyClient", "username": "user_one",
  88. "textname": "Modified name", "textdescription": "Modified description",
  89. "groups":[],
  90. "correlationData": "4" }]
  91. }
  92. modify_client_response2 = {'responses': [{'command': 'modifyClient', 'correlationData': '4'}]}
  93. get_client_command1 = { "commands": [{
  94. "command": "getClient", "username": "user_one"}]}
  95. get_client_response1 = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one', 'clientid': 'cid',
  96. 'textname': 'Name', 'textdescription': 'Description',
  97. 'roles': [],
  98. 'groups': [],
  99. }}}]}
  100. get_client_command2 = { "commands": [{
  101. "command": "getClient", "username": "user_one"}]}
  102. get_client_response2 = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one',
  103. 'textname': 'Modified name', 'textdescription': 'Modified description',
  104. 'roles': [
  105. {'rolename':'role_three', 'priority':10},
  106. {'rolename':'role_one', 'priority':2},
  107. {'rolename':'role_two'}
  108. ],
  109. 'groups': [
  110. {'groupname':'group_two', 'priority':8},
  111. {'groupname':'group_one', 'priority':3}
  112. ]}}}]}
  113. get_client_command3 = { "commands": [{
  114. "command": "getClient", "username": "user_one"}]}
  115. get_client_response3 = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one',
  116. 'textname': 'Modified name', 'textdescription': 'Modified description',
  117. 'groups': [],
  118. 'roles': [
  119. {'rolename':'role_three', 'priority':10},
  120. {'rolename':'role_one', 'priority':2},
  121. {'rolename':'role_two'}
  122. ]}}}]}
  123. rc = 1
  124. keepalive = 10
  125. connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
  126. connack_packet = mosq_test.gen_connack(rc=0)
  127. mid = 2
  128. subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/#", 1)
  129. suback_packet = mosq_test.gen_suback(mid, 1)
  130. try:
  131. os.mkdir(str(port))
  132. shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
  133. except FileExistsError:
  134. pass
  135. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  136. try:
  137. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  138. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  139. # Create client
  140. command_check(sock, create_client_command, create_client_response)
  141. # Create groups
  142. command_check(sock, create_groups_command, create_groups_response)
  143. # Create role
  144. command_check(sock, create_roles_command, create_roles_response)
  145. # Get client
  146. command_check(sock, get_client_command1, get_client_response1, "get client 1")
  147. # Modify client - with groups
  148. command_check(sock, modify_client_command1, modify_client_response1)
  149. # Get client
  150. command_check(sock, get_client_command2, get_client_response2, "get client 2a")
  151. # Kill broker and restart, checking whether our changes were saved.
  152. broker.terminate()
  153. broker.wait()
  154. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  155. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  156. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  157. # Get client
  158. command_check(sock, get_client_command2, get_client_response2, "get client 2b")
  159. # Modify client - without groups
  160. command_check(sock, modify_client_command2, modify_client_response2)
  161. # Get client
  162. command_check(sock, get_client_command3, get_client_response3, "get client 3")
  163. rc = 0
  164. sock.close()
  165. except mosq_test.TestError:
  166. pass
  167. finally:
  168. os.remove(conf_file)
  169. try:
  170. os.remove(f"{port}/dynamic-security.json")
  171. pass
  172. except FileNotFoundError:
  173. pass
  174. os.rmdir(f"{port}")
  175. broker.terminate()
  176. broker.wait()
  177. (stdo, stde) = broker.communicate()
  178. if rc:
  179. print(stde.decode('utf-8'))
  180. exit(rc)