acl, log
- separated ACL's for as plain and TLS - added logging configuration - added timestamp for logs
This commit is contained in:
@@ -28,3 +28,8 @@
|
|||||||
## 0.2.5
|
## 0.2.5
|
||||||
|
|
||||||
- Minor bug fix
|
- Minor bug fix
|
||||||
|
|
||||||
|
## 0.3.0
|
||||||
|
|
||||||
|
- Added separated ACL's
|
||||||
|
- Improved logging capabilities
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name: Visquitto
|
name: Visquitto
|
||||||
version: "0.2.5"
|
version: "0.3.0"
|
||||||
slug: visquitto
|
slug: visquitto
|
||||||
description: Second private Mosquitto broker for bitopa nodes
|
description: Second private Mosquitto broker for bitopa nodes
|
||||||
arch:
|
arch:
|
||||||
@@ -19,6 +19,10 @@ map:
|
|||||||
- ssl:ro
|
- ssl:ro
|
||||||
options:
|
options:
|
||||||
users: []
|
users: []
|
||||||
|
log_dest: []
|
||||||
|
log_type: []
|
||||||
|
nodes: []
|
||||||
|
require_certificate: false
|
||||||
cafile: mqtt-cert-manager-ca.crt
|
cafile: mqtt-cert-manager-ca.crt
|
||||||
certfile: mosquitto-server.crt
|
certfile: mosquitto-server.crt
|
||||||
keyfile: mosquitto-server.key
|
keyfile: mosquitto-server.key
|
||||||
@@ -26,6 +30,10 @@ schema:
|
|||||||
users:
|
users:
|
||||||
- username: str
|
- username: str
|
||||||
password: password
|
password: password
|
||||||
|
nodes:
|
||||||
|
log_dest: []
|
||||||
|
log_type: []
|
||||||
|
require_certificate: false
|
||||||
cafile: str
|
cafile: str
|
||||||
certfile: str
|
certfile: str
|
||||||
keyfile: str
|
keyfile: str
|
||||||
+92
-38
@@ -1,22 +1,25 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# every file the script creates is private from the start
|
|
||||||
umask 077
|
umask 077
|
||||||
|
|
||||||
CONFIG=/config
|
CONFIG=/config
|
||||||
SHARE=/share/visquitto
|
SHARE=/share/visquitto
|
||||||
|
DATA=/data/mosquitto
|
||||||
PASSWD=/data/passwd
|
PASSWD=/data/passwd
|
||||||
CERTDIR=/data/certs
|
CERTDIR=/data/certs
|
||||||
FINAL=/tmp/mosquitto.conf
|
|
||||||
|
|
||||||
mkdir -p "$SHARE" /data/mosquitto "$CERTDIR"
|
mkdir -p "$SHARE" "$DATA" "$CERTDIR"
|
||||||
chown -R mosquitto:mosquitto /data/mosquitto
|
chown -R mosquitto:mosquitto "$DATA"
|
||||||
|
|
||||||
CAFILE=$(jq -r '.cafile' /data/options.json)
|
CAFILE=$(jq -r '.cafile' /data/options.json)
|
||||||
CERTFILE=$(jq -r '.certfile' /data/options.json)
|
CERTFILE=$(jq -r '.certfile' /data/options.json)
|
||||||
KEYFILE=$(jq -r '.keyfile' /data/options.json)
|
KEYFILE=$(jq -r '.keyfile' /data/options.json)
|
||||||
|
|
||||||
|
# ---- Check plugins exist ----
|
||||||
|
for p in /usr/lib/mosquitto_acl_file.so /usr/lib/mosquitto_password_file.so; do
|
||||||
|
[ -f "$p" ] || { echo "Missing plugin $p" >&2; exit 1; }
|
||||||
|
done
|
||||||
|
|
||||||
# ---- Certificates: copied at every start, readable by mosquitto ----
|
# ---- Certificates: copied at every start, readable by mosquitto ----
|
||||||
for f in "$CAFILE" "$CERTFILE" "$KEYFILE"; do
|
for f in "$CAFILE" "$CERTFILE" "$KEYFILE"; do
|
||||||
[ -f "/ssl/$f" ] || { echo "Missing /ssl/$f" >&2; exit 1; }
|
[ -f "/ssl/$f" ] || { echo "Missing /ssl/$f" >&2; exit 1; }
|
||||||
@@ -31,49 +34,111 @@ chmod 600 "$CERTDIR/key.pem"
|
|||||||
|
|
||||||
# ---- Defaults in /config (regenerated at every start) ----
|
# ---- Defaults in /config (regenerated at every start) ----
|
||||||
cat > "$CONFIG/mosquitto.conf" <<EOF
|
cat > "$CONFIG/mosquitto.conf" <<EOF
|
||||||
|
protocol mqtt
|
||||||
allow_anonymous false
|
allow_anonymous false
|
||||||
password_file $PASSWD
|
|
||||||
|
log_dest stdout
|
||||||
|
log_type error
|
||||||
|
log_type warning
|
||||||
|
log_type notice
|
||||||
|
log_timestamp_format %Y-%m-%d %H:%M:%S
|
||||||
|
|
||||||
persistence true
|
persistence true
|
||||||
persistence_location /data/mosquitto/
|
persistence_location $DATA/
|
||||||
|
max_queued_messages 8192
|
||||||
|
|
||||||
|
plugin_load acl_file /usr/lib/mosquitto_acl_file.so
|
||||||
|
plugin_load password_file /usr/lib/mosquitto_password_file.so
|
||||||
|
|
||||||
|
include_dir $SHARE
|
||||||
|
|
||||||
|
# Plain listener (host port 1993): password users only
|
||||||
listener 1883
|
listener 1883
|
||||||
|
protocol mqtt
|
||||||
|
plugin_use password_file
|
||||||
|
plugin_opt_password_file $PASSWD
|
||||||
|
plugin_use acl_file
|
||||||
|
plugin_opt_acl_file $DATA/acl_plain
|
||||||
|
|
||||||
|
# TLS listener (host port 9993): client certificate required
|
||||||
listener 8883
|
listener 8883
|
||||||
|
protocol mqtt
|
||||||
cafile $CERTDIR/ca.pem
|
cafile $CERTDIR/ca.pem
|
||||||
certfile $CERTDIR/cert.pem
|
certfile $CERTDIR/cert.pem
|
||||||
keyfile $CERTDIR/key.pem
|
keyfile $CERTDIR/key.pem
|
||||||
tls_version tlsv1.2
|
tls_version tlsv1.2
|
||||||
|
require_certificate true
|
||||||
|
use_identity_as_username true
|
||||||
|
plugin_use password_file
|
||||||
|
plugin_opt_password_file $PASSWD
|
||||||
|
plugin_use acl_file
|
||||||
|
plugin_opt_acl_file $DATA/acl_tls
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
cat > "$CONFIG/acl.conf" <<EOF
|
# plain ACL
|
||||||
acl_file $CONFIG/accesscontrollist
|
echo "# Default ACL for the plain listener: no rules, all access denied" > "$CONFIG/acl_plain"
|
||||||
|
cat > "$CONFIG/acl_plain" <<EOF
|
||||||
|
# ============================================================
|
||||||
|
# Not certified devices
|
||||||
|
# ============================================================
|
||||||
|
user hamqtt
|
||||||
|
|
||||||
|
# Test topic
|
||||||
|
topic readwrite test
|
||||||
|
# Initial enrollment
|
||||||
|
topic write cert/enroll/+/request
|
||||||
|
topic read cert/enroll/+/issued
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Certificate manager
|
||||||
|
# ============================================================
|
||||||
|
user viracert
|
||||||
|
|
||||||
|
topic read cert/enroll/+/request
|
||||||
|
topic write cert/enroll/+/issued
|
||||||
|
topic read cert/enroll/+/renew
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
cat > "$CONFIG/accesscontrollist" <<EOF
|
# TLS ACL
|
||||||
# Default ACL: no rules, all access denied
|
echo "# Default ACL for the TLS listener: no rules, all access denied" > "$CONFIG/acl_tls"
|
||||||
|
cat > "$CONFIG/acl_tls" <<EOF
|
||||||
|
# Default rule for every certificate client. %u is the certificate CN.
|
||||||
|
pattern readwrite clients/%u/#
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# ---- User customization in /share (created once, never overwritten) ----
|
# ---- User customization in /share (created once, never overwritten) ----
|
||||||
if [ ! -f "$SHARE/mosquitto.conf" ]; then
|
if [ ! -f "$SHARE/mosquitto.conf" ]; then
|
||||||
echo "# Extra Mosquitto options, appended to the default config" > "$SHARE/mosquitto.conf"
|
echo "# Extra global Mosquitto options (loaded with include_dir, before the listeners)" > "$SHARE/mosquitto.conf"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f "$SHARE/acl.conf" ]; then
|
if [ ! -f "$SHARE/acl_plain" ]; then
|
||||||
echo "acl_file /share/visquitto/accesscontrollist" > "$SHARE/acl.conf"
|
cat > "$SHARE/acl_plain" <<EOF
|
||||||
fi
|
# Extra ACL rules for the plain listener, appended to the default, for example:
|
||||||
|
# user enroll
|
||||||
if [ ! -f "$SHARE/accesscontrollist" ]; then
|
# topic write certs/request/#
|
||||||
cat > "$SHARE/accesscontrollist" <<EOF
|
# topic read certs/response/#
|
||||||
# Visquitto ACL rules, for example:
|
EOF
|
||||||
# user myuser
|
fi
|
||||||
# topic readwrite myprivate/#
|
|
||||||
|
if [ ! -f "$SHARE/acl_tls" ]; then
|
||||||
|
cat > "$SHARE/acl_tls" <<EOF
|
||||||
|
# Extra ACL rules for the TLS listener (user = certificate CN), appended to the default, for example:
|
||||||
|
# user hamt
|
||||||
|
# topic read broadcast/#
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
chown mosquitto:mosquitto "$SHARE/mosquitto.conf" "$SHARE/acl.conf" "$SHARE/accesscontrollist" \
|
|
||||||
"$CONFIG/acl.conf" "$CONFIG/accesscontrollist"
|
|
||||||
chmod 600 "$SHARE/acl.conf" "$SHARE/accesscontrollist" "$CONFIG/acl.conf" "$CONFIG/accesscontrollist"
|
|
||||||
chmod 644 "$SHARE/mosquitto.conf"
|
chmod 644 "$SHARE/mosquitto.conf"
|
||||||
|
chmod 600 "$SHARE/acl_plain" "$SHARE/acl_tls"
|
||||||
|
|
||||||
|
# ---- Merge defaults + user ACLs into the files Mosquitto reads ----
|
||||||
|
{ cat "$CONFIG/acl_plain"; echo; cat "$SHARE/acl_plain"; echo; } > "$DATA/acl_plain"
|
||||||
|
{ cat "$CONFIG/acl_tls"; echo; cat "$SHARE/acl_tls"; echo; } > "$DATA/acl_tls"
|
||||||
|
|
||||||
|
for f in "$DATA/acl_plain" "$DATA/acl_tls"; do
|
||||||
|
chown mosquitto:mosquitto "$f"
|
||||||
|
chmod 600 "$f"
|
||||||
|
done
|
||||||
|
|
||||||
# ---- Users from the panel ----
|
# ---- Users from the panel ----
|
||||||
rm -f "$PASSWD"
|
rm -f "$PASSWD"
|
||||||
@@ -83,19 +148,8 @@ while IFS="$(printf '\t')" read -r user pass; do
|
|||||||
mosquitto_passwd -b "$PASSWD" "$user" "$pass"
|
mosquitto_passwd -b "$PASSWD" "$user" "$pass"
|
||||||
done
|
done
|
||||||
chown mosquitto:mosquitto "$PASSWD"
|
chown mosquitto:mosquitto "$PASSWD"
|
||||||
chmod 600 "$PASSWD"
|
chmod 0700 "$PASSWD"
|
||||||
|
|
||||||
[ -s "$PASSWD" ] || echo "Warning: no users defined, nobody can log in" >&2
|
[ -s "$PASSWD" ] || echo "Warning: no users defined, nobody can log in on the plain listener" >&2
|
||||||
|
|
||||||
# ---- Final config: defaults + user config + ACL ----
|
exec mosquitto -c "$CONFIG/mosquitto.conf"
|
||||||
cat "$CONFIG/mosquitto.conf" > "$FINAL"
|
|
||||||
echo >> "$FINAL"
|
|
||||||
cat "$SHARE/mosquitto.conf" >> "$FINAL"
|
|
||||||
echo >> "$FINAL"
|
|
||||||
if grep -q '^[[:space:]]*acl_file' "$SHARE/acl.conf"; then
|
|
||||||
cat "$SHARE/acl.conf" >> "$FINAL"
|
|
||||||
else
|
|
||||||
cat "$CONFIG/acl.conf" >> "$FINAL"
|
|
||||||
fi
|
|
||||||
|
|
||||||
exec mosquitto -c "$FINAL"
|
|
||||||
Reference in New Issue
Block a user