14-dynsec-modify-role.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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):
  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(expected_response)
  17. print(response)
  18. raise ValueError(response)
  19. port = mosq_test.get_port()
  20. conf_file = os.path.basename(__file__).replace('.py', '.conf')
  21. write_config(conf_file, port)
  22. create_role_command = { "commands": [{
  23. "command": "createRole", "rolename": "role_one",
  24. "textname": "Name", "textdescription": "Description",
  25. "acls":[
  26. {
  27. "acltype": "publishClientSend",
  28. "allow": True,
  29. "topic": "topic/#",
  30. "priority": 8
  31. },
  32. {
  33. "acltype": "publishClientSend",
  34. "allow": True,
  35. "topic": "topic/2/#",
  36. "priority": 9
  37. }
  38. ], "correlationData": "2" }]
  39. }
  40. create_role_response = {'responses': [{'command': 'createRole', 'correlationData': '2'}]}
  41. modify_role_command = { "commands": [{
  42. "command": "modifyRole", "rolename": "role_one",
  43. "textname": "Modified name", "textdescription": "Modified description",
  44. "acls":[
  45. {
  46. "acltype": "publishClientReceive",
  47. "allow": True,
  48. "topic": "topic/#",
  49. "priority": 2
  50. },
  51. {
  52. "acltype": "publishClientReceive",
  53. "allow": True,
  54. "topic": "topic/2/#",
  55. "priority": 1
  56. }
  57. ],
  58. "correlationData": "3" }]
  59. }
  60. modify_role_response = {'responses': [{'command': 'modifyRole', 'correlationData': '3'}]}
  61. get_role_command1 = { "commands": [{"command": "getRole", "rolename": "role_one"}]}
  62. get_role_response1 = {'responses':[{'command': 'getRole', 'data': {'role': {'rolename': 'role_one',
  63. 'textname': 'Name', 'textdescription': 'Description',
  64. 'acls': [
  65. {
  66. "acltype": "publishClientSend",
  67. "topic": "topic/2/#",
  68. "allow": True,
  69. "priority": 9
  70. },
  71. {
  72. "acltype": "publishClientSend",
  73. "topic": "topic/#",
  74. "allow": True,
  75. "priority": 8
  76. }
  77. ]}}}]}
  78. get_role_command2 = { "commands": [{
  79. "command": "getRole", "rolename": "role_one"}]}
  80. get_role_response2 = {'responses':[{'command': 'getRole', 'data': {'role': {'rolename': 'role_one',
  81. 'textname': 'Modified name', 'textdescription': 'Modified description',
  82. 'acls': [
  83. {
  84. "acltype": "publishClientReceive",
  85. "topic": "topic/#",
  86. "allow": True,
  87. "priority": 2
  88. },
  89. {
  90. "acltype": "publishClientReceive",
  91. "topic": "topic/2/#",
  92. "allow": True,
  93. "priority": 1
  94. }
  95. ]}}}]}
  96. rc = 1
  97. keepalive = 10
  98. connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
  99. connack_packet = mosq_test.gen_connack(rc=0)
  100. mid = 2
  101. subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/#", 1)
  102. suback_packet = mosq_test.gen_suback(mid, 1)
  103. try:
  104. os.mkdir(str(port))
  105. shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
  106. except FileExistsError:
  107. pass
  108. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  109. try:
  110. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  111. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  112. # Add role
  113. command_check(sock, create_role_command, create_role_response)
  114. # Get role
  115. command_check(sock, get_role_command1, get_role_response1)
  116. # Modify role
  117. command_check(sock, modify_role_command, modify_role_response)
  118. # Get role
  119. command_check(sock, get_role_command2, get_role_response2)
  120. # Kill broker and restart, checking whether our changes were saved.
  121. broker.terminate()
  122. broker.wait()
  123. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  124. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  125. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  126. # Get role
  127. command_check(sock, get_role_command2, get_role_response2)
  128. rc = 0
  129. sock.close()
  130. except mosq_test.TestError:
  131. pass
  132. finally:
  133. os.remove(conf_file)
  134. try:
  135. os.remove(f"{port}/dynamic-security.json")
  136. except FileNotFoundError:
  137. pass
  138. os.rmdir(f"{port}")
  139. broker.terminate()
  140. broker.wait()
  141. (stdo, stde) = broker.communicate()
  142. if rc:
  143. print(stde.decode('utf-8'))
  144. exit(rc)