14-dynsec-client.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. add_client_command = { "commands": [{
  23. "command": "createClient", "username": "user_one",
  24. "password": "password", "clientid": "cid",
  25. "textname": "Name", "textdescription": "Description",
  26. "rolename": "", "correlationData": "2" }]
  27. }
  28. add_client_response = {'responses': [{'command': 'createClient', 'correlationData': '2'}]}
  29. add_client_repeat_response = {'responses':[{"command":"createClient","error":"Client already exists", "correlationData":"2"}]}
  30. list_clients_command = { "commands": [{
  31. "command": "listClients", "verbose": False, "correlationData": "10"}]
  32. }
  33. list_clients_response = {'responses': [{"command": "listClients", "data":{"totalCount":2, "clients":["admin", "user_one"]},"correlationData":"10"}]}
  34. list_clients_verbose_command = { "commands": [{
  35. "command": "listClients", "verbose": True, "correlationData": "20"}]
  36. }
  37. list_clients_verbose_response = {'responses':[{"command": "listClients", "data":{"totalCount":2, "clients":[
  38. {'username': 'admin', 'textname': 'Dynsec admin user', 'roles': [{'rolename': 'admin'}], 'groups': []},
  39. {"username":"user_one", "clientid":"cid", "textname":"Name", "textdescription":"Description",
  40. "roles":[], "groups":[]}]}, "correlationData":"20"}]}
  41. get_client_command = { "commands": [{
  42. "command": "getClient", "username": "user_one", "correlationData": "42"}]}
  43. get_client_response = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one', 'clientid': 'cid',
  44. 'textname': 'Name', 'textdescription': 'Description', 'groups': [], 'roles': []}}, "correlationData":"42"}]}
  45. set_client_password_command = {"commands": [{
  46. "command": "setClientPassword", "username": "user_one", "password": "password"}]}
  47. set_client_password_response = {"responses": [{"command":"setClientPassword"}]}
  48. delete_client_command = { "commands": [{
  49. "command": "deleteClient", "username": "user_one"}]}
  50. delete_client_response = {'responses':[{'command': 'deleteClient'}]}
  51. rc = 1
  52. keepalive = 10
  53. connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
  54. connack_packet = mosq_test.gen_connack(rc=0)
  55. mid = 2
  56. subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1)
  57. suback_packet = mosq_test.gen_suback(mid, 1)
  58. try:
  59. os.mkdir(str(port))
  60. shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
  61. except FileExistsError:
  62. pass
  63. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  64. try:
  65. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  66. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  67. # Add client
  68. command_check(sock, add_client_command, add_client_response)
  69. # List clients non-verbose
  70. command_check(sock, list_clients_command, list_clients_response)
  71. # List clients verbose
  72. command_check(sock, list_clients_verbose_command, list_clients_verbose_response)
  73. # Kill broker and restart, checking whether our changes were saved.
  74. broker.terminate()
  75. broker.wait()
  76. broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
  77. sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
  78. mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
  79. # Get client
  80. command_check(sock, get_client_command, get_client_response)
  81. # List clients non-verbose
  82. command_check(sock, list_clients_command, list_clients_response)
  83. # List clients verbose
  84. command_check(sock, list_clients_verbose_command, list_clients_verbose_response)
  85. # Add duplicate client
  86. command_check(sock, add_client_command, add_client_repeat_response)
  87. # Set client password
  88. command_check(sock, set_client_password_command, set_client_password_response)
  89. # Delete client
  90. command_check(sock, delete_client_command, delete_client_response)
  91. rc = 0
  92. sock.close()
  93. except mosq_test.TestError:
  94. pass
  95. finally:
  96. os.remove(conf_file)
  97. try:
  98. os.remove(f"{port}/dynamic-security.json")
  99. except FileNotFoundError:
  100. pass
  101. os.rmdir(f"{port}")
  102. broker.terminate()
  103. broker.wait()
  104. (stdo, stde) = broker.communicate()
  105. if rc:
  106. print(stde.decode('utf-8'))
  107. exit(rc)