2021-04-24 17:22:54 +02:00
|
|
|
import ./make-test-python.nix ({ pkgs, lib, ... }:
|
2019-04-24 10:23:13 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
port = 1888;
|
2021-05-26 18:59:37 +02:00
|
|
|
tlsPort = 1889;
|
2021-10-24 19:34:25 +02:00
|
|
|
anonPort = 1890;
|
2022-10-26 19:21:33 +02:00
|
|
|
bindTestPort = 18910;
|
2019-04-24 10:23:13 +02:00
|
|
|
password = "VERY_secret";
|
2021-05-26 18:59:37 +02:00
|
|
|
hashedPassword = "$7$101$/WJc4Mp+I+uYE9sR$o7z9rD1EYXHPwEP5GqQj6A7k4W1yVbePlb8TqNcuOLV9WNCiDgwHOB0JHC1WCtdkssqTBduBNUnUGd6kmZvDSw==";
|
2019-04-24 10:23:13 +02:00
|
|
|
topic = "test/foo";
|
2021-05-26 18:59:37 +02:00
|
|
|
|
|
|
|
snakeOil = pkgs.runCommand "snakeoil-certs" {
|
|
|
|
buildInputs = [ pkgs.gnutls.bin ];
|
|
|
|
caTemplate = pkgs.writeText "snakeoil-ca.template" ''
|
|
|
|
cn = server
|
|
|
|
expiration_days = -1
|
|
|
|
cert_signing_key
|
|
|
|
ca
|
|
|
|
'';
|
|
|
|
certTemplate = pkgs.writeText "snakeoil-cert.template" ''
|
|
|
|
cn = server
|
|
|
|
expiration_days = -1
|
|
|
|
tls_www_server
|
|
|
|
encryption_key
|
|
|
|
signing_key
|
|
|
|
'';
|
|
|
|
userCertTemplate = pkgs.writeText "snakeoil-user-cert.template" ''
|
|
|
|
organization = snakeoil
|
|
|
|
cn = client1
|
|
|
|
expiration_days = -1
|
|
|
|
tls_www_client
|
|
|
|
encryption_key
|
|
|
|
signing_key
|
|
|
|
'';
|
|
|
|
} ''
|
|
|
|
mkdir "$out"
|
|
|
|
|
|
|
|
certtool -p --bits 2048 --outfile "$out/ca.key"
|
|
|
|
certtool -s --template "$caTemplate" --load-privkey "$out/ca.key" \
|
|
|
|
--outfile "$out/ca.crt"
|
|
|
|
certtool -p --bits 2048 --outfile "$out/server.key"
|
|
|
|
certtool -c --template "$certTemplate" \
|
|
|
|
--load-ca-privkey "$out/ca.key" \
|
|
|
|
--load-ca-certificate "$out/ca.crt" \
|
|
|
|
--load-privkey "$out/server.key" \
|
|
|
|
--outfile "$out/server.crt"
|
|
|
|
|
|
|
|
certtool -p --bits 2048 --outfile "$out/client1.key"
|
|
|
|
certtool -c --template "$userCertTemplate" \
|
|
|
|
--load-privkey "$out/client1.key" \
|
|
|
|
--load-ca-privkey "$out/ca.key" \
|
|
|
|
--load-ca-certificate "$out/ca.crt" \
|
|
|
|
--outfile "$out/client1.crt"
|
|
|
|
'';
|
|
|
|
|
2019-08-13 23:52:01 +02:00
|
|
|
in {
|
2019-04-24 10:23:13 +02:00
|
|
|
name = "mosquitto";
|
2021-01-10 20:08:30 +01:00
|
|
|
meta = with pkgs.lib; {
|
2021-05-26 18:59:37 +02:00
|
|
|
maintainers = with maintainers; [ pennae peterhoeg ];
|
2019-04-24 10:23:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
nodes = let
|
|
|
|
client = { pkgs, ... }: {
|
|
|
|
environment.systemPackages = with pkgs; [ mosquitto ];
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
server = { pkgs, ... }: {
|
2021-10-24 19:34:25 +02:00
|
|
|
networking.firewall.allowedTCPPorts = [ port tlsPort anonPort ];
|
2023-05-09 11:41:17 +02:00
|
|
|
networking.useNetworkd = true;
|
2019-04-24 10:23:13 +02:00
|
|
|
services.mosquitto = {
|
|
|
|
enable = true;
|
2021-05-26 18:59:37 +02:00
|
|
|
settings = {
|
|
|
|
sys_interval = 1;
|
|
|
|
};
|
2021-05-21 19:23:01 +02:00
|
|
|
listeners = [
|
|
|
|
{
|
|
|
|
inherit port;
|
2021-05-26 18:59:37 +02:00
|
|
|
users = {
|
|
|
|
password_store = {
|
|
|
|
inherit password;
|
|
|
|
};
|
|
|
|
password_file = {
|
|
|
|
passwordFile = pkgs.writeText "mqtt-password" password;
|
|
|
|
};
|
|
|
|
hashed_store = {
|
|
|
|
inherit hashedPassword;
|
|
|
|
};
|
|
|
|
hashed_file = {
|
|
|
|
hashedPasswordFile = pkgs.writeText "mqtt-hashed-password" hashedPassword;
|
|
|
|
};
|
|
|
|
|
|
|
|
reader = {
|
|
|
|
inherit password;
|
|
|
|
acl = [
|
|
|
|
"read ${topic}"
|
|
|
|
"read $SYS/#" # so we always have something to read
|
|
|
|
];
|
|
|
|
};
|
|
|
|
writer = {
|
|
|
|
inherit password;
|
|
|
|
acl = [ "write ${topic}" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
port = tlsPort;
|
|
|
|
users.client1 = {
|
|
|
|
acl = [ "read $SYS/#" ];
|
|
|
|
};
|
|
|
|
settings = {
|
|
|
|
cafile = "${snakeOil}/ca.crt";
|
|
|
|
certfile = "${snakeOil}/server.crt";
|
|
|
|
keyfile = "${snakeOil}/server.key";
|
|
|
|
require_certificate = true;
|
|
|
|
use_identity_as_username = true;
|
2021-05-21 19:23:01 +02:00
|
|
|
};
|
|
|
|
}
|
2021-10-24 19:34:25 +02:00
|
|
|
{
|
|
|
|
port = anonPort;
|
|
|
|
omitPasswordAuth = true;
|
|
|
|
settings.allow_anonymous = true;
|
|
|
|
acl = [ "pattern read #" ];
|
|
|
|
users = {
|
|
|
|
anonWriter = {
|
|
|
|
password = "<ignored>" + password;
|
|
|
|
acl = [ "write ${topic}" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2022-05-14 07:54:19 +02:00
|
|
|
{
|
|
|
|
settings.bind_interface = "eth0";
|
|
|
|
port = bindTestPort;
|
|
|
|
}
|
2021-05-21 19:23:01 +02:00
|
|
|
];
|
2019-04-24 10:23:13 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
client1 = client;
|
|
|
|
client2 = client;
|
|
|
|
};
|
|
|
|
|
2021-05-26 18:59:37 +02:00
|
|
|
testScript = ''
|
2022-05-14 07:54:19 +02:00
|
|
|
import json
|
|
|
|
|
2021-05-26 18:59:37 +02:00
|
|
|
def mosquitto_cmd(binary, user, topic, port):
|
2019-11-20 18:58:24 +01:00
|
|
|
return (
|
2021-05-26 18:59:37 +02:00
|
|
|
"mosquitto_{} "
|
2019-11-20 18:58:24 +01:00
|
|
|
"-V mqttv311 "
|
|
|
|
"-h server "
|
2021-05-26 18:59:37 +02:00
|
|
|
"-p {} "
|
|
|
|
"-u {} "
|
2019-11-20 18:58:24 +01:00
|
|
|
"-P '${password}' "
|
2021-05-26 18:59:37 +02:00
|
|
|
"-t '{}'"
|
|
|
|
).format(binary, port, user, topic)
|
2019-11-20 18:58:24 +01:00
|
|
|
|
|
|
|
|
2021-05-26 18:59:37 +02:00
|
|
|
def publish(args, user, topic="${topic}", port=${toString port}):
|
|
|
|
return "{} {}".format(mosquitto_cmd("pub", user, topic, port), args)
|
2019-11-20 18:58:24 +01:00
|
|
|
|
2021-05-26 18:59:37 +02:00
|
|
|
def subscribe(args, user, topic="${topic}", port=${toString port}):
|
2021-10-24 19:11:45 +02:00
|
|
|
return "{} -W 5 -C 1 {}".format(mosquitto_cmd("sub", user, topic, port), args)
|
2021-05-26 18:59:37 +02:00
|
|
|
|
|
|
|
def parallel(*fns):
|
|
|
|
from threading import Thread
|
|
|
|
threads = [ Thread(target=fn) for fn in fns ]
|
|
|
|
for t in threads: t.start()
|
|
|
|
for t in threads: t.join()
|
2019-11-20 18:58:24 +01:00
|
|
|
|
2022-10-26 19:21:33 +02:00
|
|
|
def wait_uuid(uuid):
|
|
|
|
server.wait_for_console_text(uuid)
|
|
|
|
return None
|
|
|
|
|
2019-11-20 18:58:24 +01:00
|
|
|
|
2019-11-20 18:33:36 +01:00
|
|
|
start_all()
|
|
|
|
server.wait_for_unit("mosquitto.service")
|
2019-06-24 07:59:53 +02:00
|
|
|
|
2022-05-14 07:54:19 +02:00
|
|
|
with subtest("bind_interface"):
|
|
|
|
addrs = dict()
|
|
|
|
for iface in json.loads(server.succeed("ip -json address show")):
|
|
|
|
for addr in iface['addr_info']:
|
|
|
|
# don't want to deal with multihoming here
|
|
|
|
assert addr['local'] not in addrs
|
|
|
|
addrs[addr['local']] = (iface['ifname'], addr['family'])
|
|
|
|
|
|
|
|
# mosquitto grabs *one* random address per type for bind_interface
|
|
|
|
(has4, has6) = (False, False)
|
|
|
|
for line in server.succeed("ss -HlptnO sport = ${toString bindTestPort}").splitlines():
|
|
|
|
items = line.split()
|
|
|
|
if "mosquitto" not in items[5]: continue
|
|
|
|
listener = items[3].rsplit(':', maxsplit=1)[0].strip('[]')
|
|
|
|
assert listener in addrs
|
|
|
|
assert addrs[listener][0] == "eth0"
|
|
|
|
has4 |= addrs[listener][1] == 'inet'
|
|
|
|
has6 |= addrs[listener][1] == 'inet6'
|
|
|
|
assert has4
|
|
|
|
assert has6
|
|
|
|
|
2021-10-24 19:11:45 +02:00
|
|
|
with subtest("check passwords"):
|
2021-05-26 18:59:37 +02:00
|
|
|
client1.succeed(publish("-m test", "password_store"))
|
|
|
|
client1.succeed(publish("-m test", "password_file"))
|
|
|
|
client1.succeed(publish("-m test", "hashed_store"))
|
|
|
|
client1.succeed(publish("-m test", "hashed_file"))
|
|
|
|
|
2021-10-24 19:11:45 +02:00
|
|
|
with subtest("check acl"):
|
2021-05-26 18:59:37 +02:00
|
|
|
client1.succeed(subscribe("", "reader", topic="$SYS/#"))
|
2021-10-24 19:11:45 +02:00
|
|
|
client1.fail(subscribe("", "writer", topic="$SYS/#"))
|
2021-05-26 18:59:37 +02:00
|
|
|
|
|
|
|
parallel(
|
|
|
|
lambda: client1.succeed(subscribe("-i 3688cdd7-aa07-42a4-be22-cb9352917e40", "reader")),
|
|
|
|
lambda: [
|
2022-10-26 19:21:33 +02:00
|
|
|
wait_uuid("3688cdd7-aa07-42a4-be22-cb9352917e40"),
|
2021-05-26 18:59:37 +02:00
|
|
|
client2.succeed(publish("-m test", "writer"))
|
|
|
|
])
|
|
|
|
|
|
|
|
parallel(
|
2021-10-24 19:11:45 +02:00
|
|
|
lambda: client1.fail(subscribe("-i 24ff16a2-ae33-4a51-9098-1b417153c712", "reader")),
|
2021-05-26 18:59:37 +02:00
|
|
|
lambda: [
|
2022-10-26 19:21:33 +02:00
|
|
|
wait_uuid("24ff16a2-ae33-4a51-9098-1b417153c712"),
|
2021-05-26 18:59:37 +02:00
|
|
|
client2.succeed(publish("-m test", "reader"))
|
|
|
|
])
|
|
|
|
|
2021-10-24 19:11:45 +02:00
|
|
|
with subtest("check tls"):
|
2021-05-26 18:59:37 +02:00
|
|
|
client1.succeed(
|
|
|
|
subscribe(
|
|
|
|
"--cafile ${snakeOil}/ca.crt "
|
|
|
|
"--cert ${snakeOil}/client1.crt "
|
|
|
|
"--key ${snakeOil}/client1.key",
|
|
|
|
topic="$SYS/#",
|
|
|
|
port=${toString tlsPort},
|
|
|
|
user="no_such_user"))
|
2021-10-24 19:34:25 +02:00
|
|
|
|
|
|
|
with subtest("check omitPasswordAuth"):
|
|
|
|
parallel(
|
|
|
|
lambda: client1.succeed(subscribe("-i fd56032c-d9cb-4813-a3b4-6be0e04c8fc3",
|
|
|
|
"anonReader", port=${toString anonPort})),
|
|
|
|
lambda: [
|
2022-10-26 19:21:33 +02:00
|
|
|
wait_uuid("fd56032c-d9cb-4813-a3b4-6be0e04c8fc3"),
|
2021-10-24 19:34:25 +02:00
|
|
|
client2.succeed(publish("-m test", "anonWriter", port=${toString anonPort}))
|
|
|
|
])
|
2019-04-24 10:23:13 +02:00
|
|
|
'';
|
|
|
|
})
|