14-dynsec-modify-group.py 7.0 KB

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