Skip to content

Commit

Permalink
Fix basic dynsec tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Nov 5, 2020
1 parent c2651cf commit 255ae24
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 186 deletions.
28 changes: 13 additions & 15 deletions test/broker/14-dynsec-client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from mosq_test_helper import *
import json
import shutil

def write_config(filename, port):
with open(filename, 'w') as f:
Expand All @@ -28,29 +29,30 @@ def command_check(sock, command_payload, expected_response):
add_client_command = { "commands": [{
"command": "createClient", "username": "user_one",
"password": "password", "clientid": "cid",
"textName": "Name", "textDescription": "Description",
"roleName": "", "correlationData": "2" }]
"textname": "Name", "textdescription": "Description",
"rolename": "", "correlationData": "2" }]
}
add_client_response = {'responses': [{'command': 'createClient', 'correlationData': '2'}]}
add_client_repeat_response = {'responses':[{"command":"createClient","error":"Client already exists", "correlationData":"2"}]}

list_clients_command = { "commands": [{
"command": "listClients", "verbose": False, "correlationData": "10"}]
}
list_clients_response = {'responses': [{"command": "listClients", "data":{"totalCount":1, "clients":["user_one"]},"correlationData":"10"}]}
list_clients_response = {'responses': [{"command": "listClients", "data":{"totalCount":2, "clients":["admin", "user_one"]},"correlationData":"10"}]}

list_clients_verbose_command = { "commands": [{
"command": "listClients", "verbose": True, "correlationData": "20"}]
}
list_clients_verbose_response = {'responses':[{"command": "listClients", "data":{"totalCount":1, "clients":[
{"username":"user_one", "clientid":"cid", "textName":"Name", "textDescription":"Description",
"groups":[], "roles":[]}]}, "correlationData":"20"}]}
list_clients_verbose_response = {'responses':[{"command": "listClients", "data":{"totalCount":2, "clients":[
{'username': 'admin', 'textname': 'Dynsec admin user', 'roles': [{'rolename': 'admin'}], 'groups': []},
{"username":"user_one", "clientid":"cid", "textname":"Name", "textdescription":"Description",
"roles":[], "groups":[]}]}, "correlationData":"20"}]}


get_client_command = { "commands": [{
"command": "getClient", "username": "user_one"}]}
get_client_response = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one', 'clientid': 'cid',
'textName': 'Name', 'textDescription': 'Description', 'groups': [], 'roles': []}}}]}
'textname': 'Name', 'textdescription': 'Description', 'groups': [], 'roles': []}}}]}

set_client_password_command = {"commands": [{
"command": "setClientPassword", "username": "user_one", "password": "password"}]}
Expand All @@ -63,22 +65,18 @@ def command_check(sock, command_payload, expected_response):

rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive)
connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
connack_packet = mosq_test.gen_connack(rc=0)

mid = 2
subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/#", 1)
subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1)
suback_packet = mosq_test.gen_suback(mid, 1)

try:
os.mkdir(str(port))
with open("%d/dynamic-security.json" % port, 'w') as f:
f.write('{"defaultACLAction": {"publishClientSend":"allow", "publishClientReceive":"allow", "subscribe":"allow", "unsubscribe":"allow"}}')
shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
except FileExistsError:
try:
os.remove(f"{port}/dynamic-security.json")
except FileNotFoundError:
pass
pass

broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)

Expand Down
122 changes: 122 additions & 0 deletions test/broker/14-dynsec-disable-client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python3

from mosq_test_helper import *
import json
import shutil

def write_config(filename, port):
with open(filename, 'w') as f:
f.write("listener %d\n" % (port))
f.write("allow_anonymous true\n")
f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n")
f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port))

def command_check(sock, command_payload, expected_response):
command_packet = mosq_test.gen_publish(topic="$CONTROL/dynamic-security/v1", qos=0, payload=json.dumps(command_payload))
sock.send(command_packet)
response = json.loads(mosq_test.read_publish(sock))
if response != expected_response:
print(expected_response)
print(response)
raise ValueError(response)



port = mosq_test.get_port()
conf_file = os.path.basename(__file__).replace('.py', '.conf')
write_config(conf_file, port)

add_client_command = { "commands": [{
"command": "createClient", "username": "user_one",
"password": "password", "clientid": "cid",
"textname": "Name", "textdescription": "Description",
"rolename": "", "correlationData": "2" }]
}
add_client_response = {'responses': [{'command': 'createClient', 'correlationData': '2'}]}
add_client_repeat_response = {'responses':[{"command":"createClient","error":"Client already exists", "correlationData":"2"}]}

get_client_command = { "commands": [{
"command": "getClient", "username": "user_one"}]}
get_client_response1 = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one', 'clientid': 'cid',
'textname': 'Name', 'textdescription': 'Description', 'groups': [], 'roles': []}}}]}
get_client_response2 = {'responses':[{'command': 'getClient', 'data': {'client': {'username': 'user_one', 'clientid': 'cid',
'textname': 'Name', 'textdescription': 'Description', 'disabled':True, 'groups': [], 'roles': []}}}]}

disable_client_command = { "commands": [{
"command": "disableClient", "username": "user_one"}]}
disable_client_response = {'responses':[{'command': 'disableClient'}]}

enable_client_command = { "commands": [{
"command": "enableClient", "username": "user_one"}]}
enable_client_response = {'responses':[{'command': 'enableClient'}]}

rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
connack_packet = mosq_test.gen_connack(rc=0)

client_connect_packet = mosq_test.gen_connect("cid", keepalive=keepalive, username="user_one", password="password")
client_connack_packet1 = mosq_test.gen_connack(rc=5)
client_connack_packet2 = mosq_test.gen_connack(rc=0)

mid = 2
subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1)
suback_packet = mosq_test.gen_suback(mid, 1)

try:
os.mkdir(str(port))
shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
except FileExistsError:
pass

broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)

try:
sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port)
mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")

# Add client
command_check(sock, add_client_command, add_client_response)

# Get client
command_check(sock, get_client_command, get_client_response1)

# Disable client
command_check(sock, disable_client_command, disable_client_response)

# Get client - should be disabled
command_check(sock, get_client_command, get_client_response2)

# Try to log in - should fail
client_sock = mosq_test.do_client_connect(client_connect_packet, client_connack_packet1, timeout=5, port=port)

# Enable client
command_check(sock, enable_client_command, enable_client_response)

# Get client - should be enabled
command_check(sock, get_client_command, get_client_response1)

# Try to log in - should succeed
client_sock = mosq_test.do_client_connect(client_connect_packet, client_connack_packet2, timeout=5, port=port)
client_sock.close()

rc = 0

sock.close()
except mosq_test.TestError:
pass
finally:
os.remove(conf_file)
try:
os.remove(f"{port}/dynamic-security.json")
except FileNotFoundError:
pass
os.rmdir(f"{port}")
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde.decode('utf-8'))


exit(rc)
40 changes: 19 additions & 21 deletions test/broker/14-dynsec-group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from mosq_test_helper import *
import json
import shutil

def write_config(filename, port):
with open(filename, 'w') as f:
Expand All @@ -28,13 +29,13 @@ def command_check(sock, command_payload, expected_response, msg=""):
create_client_command = { "commands": [{
"command": "createClient", "username": "user_one",
"password": "password", "clientid": "cid",
"textName": "Name", "textDescription": "Description",
"textname": "Name", "textdescription": "Description",
"roleName": "", "correlationData": "2" }]}
create_client_response = {'responses':[{"command":"createClient","correlationData":"2"}]}

create_group_command = { "commands": [{
"command": "createGroup", "groupName": "group_one",
"textName": "Name", "textDescription": "Description",
"command": "createGroup", "groupname": "group_one",
"textname": "Name", "textdescription": "Description",
"correlationData":"3"}]}
create_group_response = {'responses':[{"command":"createGroup","correlationData":"3"}]}
create_group_repeat_response = {'responses':[{"command":"createGroup","error":"Group already exists","correlationData":"3"}]}
Expand All @@ -46,50 +47,47 @@ def command_check(sock, command_payload, expected_response, msg=""):
list_groups_verbose_command = { "commands": [{
"command": "listGroups", "verbose": True, "correlationData": "15"}]}
list_groups_verbose_response = {'responses':[{'command': 'listGroups', 'data': {"totalCount":1, 'groups':
[{'groupName': 'group_one', 'textName': 'Name', 'textDescription': 'Description', 'clients': [
[{'groupname': 'group_one', 'textname': 'Name', 'textdescription': 'Description', 'clients': [
{"username":"user_one"}], "roles":[]}]},
'correlationData': '15'}]}

list_clients_verbose_command = { "commands": [{
"command": "listClients", "verbose": True, "correlationData": "20"}]}
list_clients_verbose_response = {'responses':[{"command": "listClients", "data":{"totalCount":1, "clients":[
{"username":"user_one", "clientid":"cid", "textName":"Name", "textDescription":"Description",
"groups":[{"groupName":"group_one"}], "roles":[]}]}, "correlationData":"20"}]}
list_clients_verbose_response = {'responses':[{"command": "listClients", "data":{"totalCount":2, "clients":[
{'username': 'admin', 'textname': 'Dynsec admin user', 'roles': [{'rolename': 'admin'}], 'groups': []},
{"username":"user_one", "clientid":"cid", "textname":"Name", "textdescription":"Description",
"groups":[{"groupname":"group_one"}], "roles":[]}]}, "correlationData":"20"}]}

get_group_command = { "commands": [{"command": "getGroup", "groupName":"group_one"}]}
get_group_response = {'responses':[{'command': 'getGroup', 'data': {'group': {'groupName': 'group_one',
'textName':'Name', 'textDescription':'Description', 'clients': [{"username":"user_one"}], 'roles': []}}}]}
get_group_command = { "commands": [{"command": "getGroup", "groupname":"group_one"}]}
get_group_response = {'responses':[{'command': 'getGroup', 'data': {'group': {'groupname': 'group_one',
'textname':'Name', 'textdescription':'Description', 'clients': [{"username":"user_one"}], 'roles': []}}}]}

add_client_to_group_command = {"commands": [{"command":"addGroupClient", "username":"user_one",
"groupName": "group_one", "correlationData":"1234"}]}
"groupname": "group_one", "correlationData":"1234"}]}
add_client_to_group_response = {'responses':[{'command': 'addGroupClient', 'correlationData': '1234'}]}

remove_client_from_group_command = {"commands": [{"command":"removeGroupClient", "username":"user_one",
"groupName": "group_one", "correlationData":"4321"}]}
"groupname": "group_one", "correlationData":"4321"}]}
remove_client_from_group_response = {'responses':[{'command': 'removeGroupClient', 'correlationData': '4321'}]}

delete_group_command = {"commands": [{"command":"deleteGroup", "groupName":"group_one", "correlationData":"5678"}]}
delete_group_command = {"commands": [{"command":"deleteGroup", "groupname":"group_one", "correlationData":"5678"}]}
delete_group_response = {'responses':[{"command":"deleteGroup", "correlationData":"5678"}]}


rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive)
connect_packet = mosq_test.gen_connect("ctrl-test", keepalive=keepalive, username="admin", password="admin")
connack_packet = mosq_test.gen_connack(rc=0)

mid = 2
subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/#", 1)
subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1)
suback_packet = mosq_test.gen_suback(mid, 1)

try:
os.mkdir(str(port))
with open("%d/dynamic-security.json" % port, 'w') as f:
f.write('{"defaultACLAction": {"publishClientSend":"allow", "publishClientReceive":"allow", "subscribe":"allow", "unsubscribe":"allow"}}')
shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port))
except FileExistsError:
try:
os.remove(f"{port}/dynamic-security.json")
except FileNotFoundError:
pass
pass

broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)

Expand Down
Loading

0 comments on commit 255ae24

Please sign in to comment.