| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from mosq_test_helper import * |
| 5 |
from dynsec_helper import * |
| 6 |
import json |
| 7 |
import shutil |
| 8 |
|
| 9 |
def write_config(filename, port): |
| 10 |
with open(filename, 'w') as f: |
| 11 |
f.write("listener %d\n" % (port)) |
| 12 |
f.write("allow_anonymous true\n") |
| •13 |
f.write("plugin mosquitto_dynamic_security.so\n") |
| 14 |
f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) |
| 15 |
|
| 16 |
|
| 17 |
port = mosq_test.get_port() |
| 18 |
conf_file = os.path.basename(__file__).replace('.py', '.conf') |
| 19 |
write_config(conf_file, port) |
| 20 |
|
| 21 |
get_anon_group_none_command = { "commands": [{ |
| 22 |
"command": "getAnonymousGroup", |
| 23 |
"correlationData": "2" }] |
| 24 |
} |
| 25 |
get_anon_group_none_response = {'responses': [{'command': 'getAnonymousGroup', |
| 26 |
'data': {'group': {'groupname': ''}}, |
| 27 |
'correlationData': '2'}]} |
| 28 |
|
| 29 |
create_group_set_anon_command = { "commands": [ |
| 30 |
{ "command": "createGroup", "groupname": "anon-clients", "correlationData": "3" }, |
| 31 |
{ "command": "setAnonymousGroup", "groupname": "anon-clients", "correlationData": "4" } |
| 32 |
] |
| 33 |
} |
| 34 |
create_group_set_anon_response = {'responses': [ |
| 35 |
{'command': 'createGroup', 'correlationData': '3'}, |
| 36 |
{'command': 'setAnonymousGroup', 'correlationData': '4'}, |
| 37 |
]} |
| 38 |
|
| 39 |
|
| 40 |
get_anon_group_command = { "commands": [{ |
| 41 |
"command": "getAnonymousGroup", |
| 42 |
"correlationData": "3" }] |
| 43 |
} |
| 44 |
get_anon_group_response = {'responses': [{'command': 'getAnonymousGroup', |
| 45 |
'data': {'group': {'groupname': 'anon-clients'}}, |
| 46 |
'correlationData': '3'}]} |
| 47 |
|
| 48 |
|
| 49 |
create_role_apply_command = { "commands": [ |
| 50 |
{ "command": "createRole", "rolename": "anon", "correlationData": "4" }, |
| 51 |
{ "command": "addRoleACL", "rolename": "anon", |
| 52 |
"acltype": "subscribeLiteral", "topic": "anon/topic", "allow": True, |
| 53 |
"correlationData": "5" }, |
| 54 |
{ "command": "addGroupRole", "groupname": "anon-clients", |
| 55 |
"rolename": "anon", "correlationData": "6"} |
| 56 |
] |
| 57 |
} |
| 58 |
create_role_apply_response = {'responses': [ |
| 59 |
{'command': 'createRole', 'correlationData': '4'}, |
| 60 |
{'command': 'addRoleACL', 'correlationData': '5'}, |
| 61 |
{'command': 'addGroupRole', 'correlationData': '6'} |
| 62 |
]} |
| 63 |
|
| 64 |
delete_anon_group_command = { "commands": [ |
| 65 |
{ "command": "deleteGroup", "groupname": "anon-clients", "correlationData": "40" } |
| 66 |
] |
| 67 |
} |
| 68 |
delete_anon_group_response = {'responses': [ |
| 69 |
{'command': 'deleteGroup', "error":'Deleting the anonymous group is forbidden', 'correlationData': '40'} |
| 70 |
]} |
| 71 |
|
| 72 |
delete_anon_group_command = { "commands": [ |
| 73 |
{ "command": "deleteGroup", "groupname": "anon-clients", "correlationData": "40" } |
| 74 |
] |
| 75 |
} |
| 76 |
delete_anon_group_response = {'responses': [ |
| 77 |
{'command': 'deleteGroup', "error":'Deleting the anonymous group is forbidden', 'correlationData': '40'} |
| 78 |
]} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
rc = 1 |
| 83 |
|
| 84 |
|
| 85 |
connect_packet_admin = mosq_test.gen_connect("ctrl-test", username="admin", password="admin") |
| 86 |
connack_packet_admin = mosq_test.gen_connack(rc=0) |
| 87 |
|
| 88 |
mid = 1 |
| 89 |
subscribe_packet_admin = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1) |
| 90 |
suback_packet_admin = mosq_test.gen_suback(mid, 1) |
| 91 |
|
| 92 |
|
| 93 |
connect_packet = mosq_test.gen_connect("cid", proto_ver=5) |
| 94 |
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5) |
| 95 |
|
| 96 |
mid = 1 |
| 97 |
subscribe_packet = mosq_test.gen_subscribe(mid, "anon/topic", qos=1, proto_ver=5) |
| 98 |
suback_packet_fail = mosq_test.gen_suback(mid, mqtt5_rc.NOT_AUTHORIZED, proto_ver=5) |
| 99 |
suback_packet_success = mosq_test.gen_suback(mid, 1, proto_ver=5) |
| 100 |
|
| 101 |
disconnect_packet_kick = mosq_test.gen_disconnect(reason_code=mqtt5_rc.ADMINISTRATIVE_ACTION, proto_ver=5) |
| 102 |
|
| 103 |
try: |
| 104 |
os.mkdir(str(port)) |
| 105 |
shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) |
| 106 |
except FileExistsError: |
| 107 |
pass |
| 108 |
|
| 109 |
broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) |
| 110 |
|
| 111 |
try: |
| 112 |
sock = mosq_test.do_client_connect(connect_packet_admin, connack_packet_admin, timeout=5, port=port) |
| 113 |
mosq_test.do_send_receive(sock, subscribe_packet_admin, suback_packet_admin, "suback admin") |
| 114 |
|
| 115 |
|
| 116 |
command_check(sock, get_anon_group_none_command, get_anon_group_none_response) |
| 117 |
|
| 118 |
|
| 119 |
csock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port) |
| 120 |
mosq_test.do_send_receive(csock, subscribe_packet, suback_packet_fail, "suback 1") |
| 121 |
|
| 122 |
|
| 123 |
command_check(sock, create_group_set_anon_command, create_group_set_anon_response) |
| 124 |
command_check(sock, get_anon_group_command, get_anon_group_response) |
| 125 |
|
| 126 |
|
| 127 |
mosq_test.expect_packet(csock, "disconnect 1", disconnect_packet_kick) |
| 128 |
csock.close() |
| 129 |
|
| 130 |
|
| 131 |
csock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port) |
| 132 |
mosq_test.do_send_receive(csock, subscribe_packet, suback_packet_fail, "suback 2") |
| 133 |
|
| 134 |
|
| 135 |
command_check(sock, create_role_apply_command, create_role_apply_response) |
| 136 |
|
| 137 |
|
| 138 |
mosq_test.expect_packet(csock, "disconnect 2", disconnect_packet_kick) |
| 139 |
csock.close() |
| 140 |
|
| 141 |
|
| 142 |
csock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port) |
| 143 |
mosq_test.do_send_receive(csock, subscribe_packet, suback_packet_success, "suback 3") |
| 144 |
|
| 145 |
|
| 146 |
command_check(sock, delete_anon_group_command, delete_anon_group_response) |
| 147 |
|
| 148 |
check_details(sock, 1, 1, 2, 5) |
| 149 |
|
| 150 |
rc = 0 |
| 151 |
|
| 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 |
except FileNotFoundError: |
| 160 |
pass |
| 161 |
os.rmdir(f"{port}") |
| 162 |
broker.terminate() |
| 163 |
if mosq_test.wait_for_subprocess(broker): |
| 164 |
print("broker not terminated") |
| 165 |
if rc == 0: rc=1 |
| 166 |
(stdo, stde) = broker.communicate() |
| 167 |
if rc: |
| 168 |
print(stde.decode('utf-8')) |
| 169 |
|
| 170 |
|
| 171 |
exit(rc) |