feat(auth): explicit transport modes
ACAUA_TRANSPORT (local, tls, proxy) is now required; a missing or invalid value stops the daemon. local refuses a non-loopback ACAUA_PUBLISHED, tls serves HTTPS via axum-server/rustls, proxy needs ACAUA_TRUSTED_PROXIES. HSTS is sent in tls and proxy modes. The image defaults to local. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ad89d5be5
commit
dc8c7fa248
8 files changed
+430
-19
No files matched your search
+13
-2
@@ -2,8 +2,19 @@
|
|||||||
ZEEK_INTERFACE=eth0
|
ZEEK_INTERFACE=eth0
|
||||||
# Acauã image tag: latest (last release), X.Y.Z / X.Y (pinned), or edge (main).
|
# Acauã image tag: latest (last release), X.Y.Z / X.Y (pinned), or edge (main).
|
||||||
ACAUA_VERSION=latest
|
ACAUA_VERSION=latest
|
||||||
# Host address and port of the dashboard. Keep it on loopback unless an
|
# How the dashboard is reached. The daemon refuses to start without a valid mode.
|
||||||
# authenticating reverse proxy sits in front of it.
|
# local - plain HTTP on loopback only; ACAUA_PUBLISH must be 127.0.0.1 or [::1]
|
||||||
|
# tls - the daemon terminates HTTPS; set ACAUA_TLS_CERT and ACAUA_TLS_KEY
|
||||||
|
# (PEM paths inside the container, mounted by you)
|
||||||
|
# proxy - an HTTPS reverse proxy in front; set ACAUA_TRUSTED_PROXIES to its
|
||||||
|
# address as comma-separated CIDRs, e.g. 172.18.0.0/16
|
||||||
|
ACAUA_TRANSPORT=local
|
||||||
|
# Host address and port of the dashboard. Login and MFA protect it in every
|
||||||
|
# mode, but only tls and proxy encrypt the traffic: publish beyond loopback
|
||||||
|
# only with one of them.
|
||||||
ACAUA_PUBLISH=127.0.0.1:8787
|
ACAUA_PUBLISH=127.0.0.1:8787
|
||||||
|
# ACAUA_TLS_CERT=/tls/cert.pem
|
||||||
|
# ACAUA_TLS_KEY=/tls/key.pem
|
||||||
|
# ACAUA_TRUSTED_PROXIES=172.18.0.0/16
|
||||||
# Zeek image, pinned to an exact version.
|
# Zeek image, pinned to an exact version.
|
||||||
ZEEK_IMAGE=zeek/zeek:9.0.1
|
ZEEK_IMAGE=zeek/zeek:9.0.1
|
||||||
Generated
+154
@@ -8,6 +8,7 @@ version = "0.2.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"acaua-core",
|
"acaua-core",
|
||||||
"axum",
|
"axum",
|
||||||
|
"axum-server",
|
||||||
"clap",
|
"clap",
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
"ipnet",
|
"ipnet",
|
||||||
@@ -168,6 +169,15 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arc-swap"
|
||||||
|
version = "1.9.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "380962b3d93b99642e50b9febf72c071e97e8b32d86eae9cf4ef582bb960ae6e"
|
||||||
|
dependencies = [
|
||||||
|
"rustversion",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "argon2"
|
name = "argon2"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
@@ -376,6 +386,29 @@ version = "1.5.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aws-lc-rs"
|
||||||
|
version = "1.19.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "048006790404103c488e6c5616b3e7e702a71292aadda08e8f7698a9e0d2eb53"
|
||||||
|
dependencies = [
|
||||||
|
"aws-lc-sys",
|
||||||
|
"zeroize",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aws-lc-sys"
|
||||||
|
version = "0.47.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8bfbe32ac36b3c428696ce315464181bc4029d689fd58e3f2a4e58c252242f52"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"cmake",
|
||||||
|
"dunce",
|
||||||
|
"fs_extra",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum"
|
name = "axum"
|
||||||
version = "0.8.9"
|
version = "0.8.9"
|
||||||
@@ -428,6 +461,28 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "axum-server"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1df331683d982a0b9492b38127151e6453639cd34926eb9c07d4cd8c6d22bfc"
|
||||||
|
dependencies = [
|
||||||
|
"arc-swap",
|
||||||
|
"bytes",
|
||||||
|
"either",
|
||||||
|
"fs-err",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"hyper",
|
||||||
|
"hyper-util",
|
||||||
|
"pin-project-lite",
|
||||||
|
"rustls",
|
||||||
|
"rustls-pki-types",
|
||||||
|
"tokio",
|
||||||
|
"tokio-rustls",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "base32"
|
name = "base32"
|
||||||
version = "0.5.1"
|
version = "0.5.1"
|
||||||
@@ -522,6 +577,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "86e055fa6e3e40ad6187ba25b6855eeb635d5271f8e0f0e1e9a5826b8d75bea0"
|
checksum = "86e055fa6e3e40ad6187ba25b6855eeb635d5271f8e0f0e1e9a5826b8d75bea0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"find-msvc-tools",
|
"find-msvc-tools",
|
||||||
|
"jobserver",
|
||||||
|
"libc",
|
||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -609,6 +666,15 @@ version = "1.1.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"
|
checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cmake"
|
||||||
|
version = "0.1.58"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c0f78a02292a74a88ac736019ab962ece0bc380e3f977bf72e376c5d78ff0678"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cmov"
|
name = "cmov"
|
||||||
version = "0.5.4"
|
version = "0.5.4"
|
||||||
@@ -836,6 +902,18 @@ dependencies = [
|
|||||||
"strum",
|
"strum",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dunce"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.19.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "equivalent"
|
name = "equivalent"
|
||||||
version = "1.0.3"
|
version = "1.0.3"
|
||||||
@@ -906,6 +984,12 @@ dependencies = [
|
|||||||
"zlib-rs",
|
"zlib-rs",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnv"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "foldhash"
|
name = "foldhash"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@@ -921,6 +1005,16 @@ dependencies = [
|
|||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fs-err"
|
||||||
|
version = "3.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5c95b673b8f6f7235229ae11c5642d81b04c2e64c1e2fb417bc0cf73ca45f29"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fs2"
|
name = "fs2"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
@@ -931,6 +1025,21 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fs_extra"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-channel"
|
||||||
|
version = "0.3.34"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-core"
|
name = "futures-core"
|
||||||
version = "0.3.34"
|
version = "0.3.34"
|
||||||
@@ -1018,6 +1127,25 @@ dependencies = [
|
|||||||
"polyval",
|
"polyval",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "h2"
|
||||||
|
version = "0.4.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7d29020232d6aa3fb1daca64c1127cf662cf97f254ae16c18c05b8ab635fc118"
|
||||||
|
dependencies = [
|
||||||
|
"atomic-waker",
|
||||||
|
"bytes",
|
||||||
|
"fnv",
|
||||||
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
|
"http",
|
||||||
|
"indexmap",
|
||||||
|
"slab",
|
||||||
|
"tokio",
|
||||||
|
"tokio-util",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "half"
|
name = "half"
|
||||||
version = "2.7.1"
|
version = "2.7.1"
|
||||||
@@ -1131,7 +1259,9 @@ checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"atomic-waker",
|
"atomic-waker",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"h2",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
"httparse",
|
"httparse",
|
||||||
@@ -1335,6 +1465,16 @@ version = "1.0.18"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jobserver"
|
||||||
|
version = "0.1.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom 0.4.3",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.107"
|
version = "0.3.107"
|
||||||
@@ -1922,6 +2062,7 @@ version = "0.23.46"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
|
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aws-lc-rs",
|
||||||
"log",
|
"log",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"ring",
|
"ring",
|
||||||
@@ -1946,6 +2087,7 @@ version = "0.103.16"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281"
|
checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aws-lc-rs",
|
||||||
"ring",
|
"ring",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
"untrusted",
|
"untrusted",
|
||||||
@@ -2322,6 +2464,7 @@ version = "1.54.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3"
|
checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
@@ -2342,6 +2485,16 @@ dependencies = [
|
|||||||
"syn 3.0.9",
|
"syn 3.0.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio-rustls"
|
||||||
|
version = "0.26.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c9cc2678c2cdd569ef8215e2afd7954ada2ae20b4fdd2c5fe6139a3b02d105db"
|
||||||
|
dependencies = [
|
||||||
|
"rustls",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-util"
|
name = "tokio-util"
|
||||||
version = "0.7.20"
|
version = "0.7.20"
|
||||||
@@ -2351,6 +2504,7 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-sink",
|
"futures-sink",
|
||||||
|
"libc",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ LABEL org.opencontainers.image.title="Acauã" \
|
|||||||
org.opencontainers.image.source="https://git.lucasrguerra.dev.br/ciberfauna/acaua" \
|
org.opencontainers.image.source="https://git.lucasrguerra.dev.br/ciberfauna/acaua" \
|
||||||
org.opencontainers.image.licenses="MIT OR Apache-2.0"
|
org.opencontainers.image.licenses="MIT OR Apache-2.0"
|
||||||
ENV ACAUA_RUNTIME=container
|
ENV ACAUA_RUNTIME=container
|
||||||
|
ENV ACAUA_TRANSPORT=local
|
||||||
USER acaua
|
USER acaua
|
||||||
WORKDIR /data
|
WORKDIR /data
|
||||||
EXPOSE 8787
|
EXPOSE 8787
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ path = "src/main.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
acaua-core = { path = "../acaua-core" }
|
acaua-core = { path = "../acaua-core" }
|
||||||
axum = "0.8"
|
axum = "0.8"
|
||||||
|
axum-server = { version = "0.8.0", features = ["tls-rustls"] }
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { version = "4", features = ["derive"] }
|
||||||
ipnet = "2"
|
ipnet = "2"
|
||||||
mime_guess = "2"
|
mime_guess = "2"
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
//! How requests reach the daemon: this decides which client address can be
|
//! How requests reach the daemon: this decides which client address can be
|
||||||
//! believed and whether cookies may be marked `Secure`.
|
//! believed and whether cookies may be marked `Secure`.
|
||||||
|
|
||||||
|
use acaua_core::{AcauaError, Result};
|
||||||
use axum::http::HeaderMap;
|
use axum::http::HeaderMap;
|
||||||
use ipnet::IpNet;
|
use ipnet::IpNet;
|
||||||
use std::net::IpAddr;
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
// `Tls` and `Proxy` have no reader outside tests until `from_env` and TLS
|
|
||||||
// serving exist.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Transport {
|
pub enum Transport {
|
||||||
Local,
|
Local,
|
||||||
@@ -17,6 +15,71 @@ pub enum Transport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Transport {
|
impl Transport {
|
||||||
|
/// Reads the mode from the environment. There is no default: a daemon
|
||||||
|
/// that guesses how it is exposed can end up serving plain HTTP on a
|
||||||
|
/// network by accident.
|
||||||
|
pub fn from_env(env: &impl Fn(&str) -> Option<String>) -> Result<Transport> {
|
||||||
|
let get = |name: &str| env(name).filter(|v| !v.trim().is_empty());
|
||||||
|
let required = |mode: &str, name: &str| {
|
||||||
|
get(name).ok_or_else(|| {
|
||||||
|
AcauaError::InvalidInput(format!("ACAUA_TRANSPORT={mode} requires {name}"))
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let mode = get("ACAUA_TRANSPORT").ok_or_else(|| {
|
||||||
|
AcauaError::InvalidInput(
|
||||||
|
"ACAUA_TRANSPORT is not set; choose one of: local, tls, proxy".into(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
match mode.trim() {
|
||||||
|
"local" => {
|
||||||
|
if let Some(published) = env("ACAUA_PUBLISHED") {
|
||||||
|
let loopback = published
|
||||||
|
.trim()
|
||||||
|
.parse::<SocketAddr>()
|
||||||
|
.is_ok_and(|addr| addr.ip().to_canonical().is_loopback());
|
||||||
|
if !loopback {
|
||||||
|
return Err(AcauaError::InvalidInput(format!(
|
||||||
|
"ACAUA_TRANSPORT=local refuses ACAUA_PUBLISHED={published}: \
|
||||||
|
the dashboard would be reachable over plain HTTP from the network. \
|
||||||
|
Publish on 127.0.0.1 or [::1], or set ACAUA_TRANSPORT to tls or proxy"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Transport::Local)
|
||||||
|
}
|
||||||
|
"tls" => Ok(Transport::Tls {
|
||||||
|
cert: required("tls", "ACAUA_TLS_CERT")?.into(),
|
||||||
|
key: required("tls", "ACAUA_TLS_KEY")?.into(),
|
||||||
|
}),
|
||||||
|
"proxy" => {
|
||||||
|
let list = required("proxy", "ACAUA_TRUSTED_PROXIES")?;
|
||||||
|
let mut trusted = Vec::new();
|
||||||
|
for item in list.split(',').map(str::trim).filter(|i| !i.is_empty()) {
|
||||||
|
let net = item.parse::<IpNet>().map_err(|_| {
|
||||||
|
AcauaError::InvalidInput(format!(
|
||||||
|
"ACAUA_TRUSTED_PROXIES: `{item}` is not a CIDR such as 10.0.0.0/8"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
trusted.push(net);
|
||||||
|
}
|
||||||
|
if trusted.is_empty() {
|
||||||
|
return Err(AcauaError::InvalidInput(
|
||||||
|
"ACAUA_TRANSPORT=proxy requires ACAUA_TRUSTED_PROXIES".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(Transport::Proxy { trusted })
|
||||||
|
}
|
||||||
|
other => Err(AcauaError::InvalidInput(format!(
|
||||||
|
"ACAUA_TRANSPORT=`{other}` is not valid; choose one of: local, tls, proxy"
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Browsers should be pinned to HTTPS only when HTTPS is what they use.
|
||||||
|
pub fn hsts(&self) -> bool {
|
||||||
|
!matches!(self, Transport::Local)
|
||||||
|
}
|
||||||
|
|
||||||
/// The address to throttle and audit. `X-Forwarded-For` counts only when
|
/// The address to throttle and audit. `X-Forwarded-For` counts only when
|
||||||
/// the peer is a trusted proxy, and then the answer is the rightmost hop
|
/// the peer is a trusted proxy, and then the answer is the rightmost hop
|
||||||
/// that is not itself trusted: everything left of it is client-supplied.
|
/// that is not itself trusted: everything left of it is client-supplied.
|
||||||
@@ -109,6 +172,101 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn env<'a>(pairs: &'a [(&'a str, &'a str)]) -> impl Fn(&str) -> Option<String> + 'a {
|
||||||
|
move |k| {
|
||||||
|
pairs
|
||||||
|
.iter()
|
||||||
|
.find(|(name, _)| *name == k)
|
||||||
|
.map(|(_, v)| (*v).to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn err(pairs: &[(&str, &str)]) -> String {
|
||||||
|
Transport::from_env(&env(pairs)).unwrap_err().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn missing_or_invalid_mode_fails() {
|
||||||
|
assert!(err(&[]).contains("ACAUA_TRANSPORT"));
|
||||||
|
let bad = err(&[("ACAUA_TRANSPORT", "xyz")]);
|
||||||
|
assert!(bad.contains("xyz") && bad.contains("local"), "{bad}");
|
||||||
|
assert!(err(&[("ACAUA_TRANSPORT", "")]).contains("ACAUA_TRANSPORT"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn local_refuses_non_loopback_publish() {
|
||||||
|
let local = |published: &'static str| {
|
||||||
|
Transport::from_env(&env(&[
|
||||||
|
("ACAUA_TRANSPORT", "local"),
|
||||||
|
("ACAUA_PUBLISHED", published),
|
||||||
|
]))
|
||||||
|
};
|
||||||
|
for bad in [
|
||||||
|
"0.0.0.0:8787",
|
||||||
|
"192.168.1.5:8787",
|
||||||
|
"[::]:8787",
|
||||||
|
"garbage",
|
||||||
|
"",
|
||||||
|
] {
|
||||||
|
let e = local(bad).unwrap_err().to_string();
|
||||||
|
assert!(e.contains("ACAUA_PUBLISHED"), "{bad}: {e}");
|
||||||
|
}
|
||||||
|
for ok in ["127.0.0.1:8787", "[::1]:8787", "127.0.0.2:80"] {
|
||||||
|
assert!(matches!(local(ok), Ok(Transport::Local)), "{ok}");
|
||||||
|
}
|
||||||
|
assert!(matches!(
|
||||||
|
Transport::from_env(&env(&[("ACAUA_TRANSPORT", "local")])),
|
||||||
|
Ok(Transport::Local)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tls_requires_cert_and_key() {
|
||||||
|
assert!(err(&[("ACAUA_TRANSPORT", "tls")]).contains("ACAUA_TLS_CERT"));
|
||||||
|
assert!(
|
||||||
|
err(&[("ACAUA_TRANSPORT", "tls"), ("ACAUA_TLS_CERT", "/c.pem")])
|
||||||
|
.contains("ACAUA_TLS_KEY")
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
err(&[("ACAUA_TRANSPORT", "tls"), ("ACAUA_TLS_KEY", "/k.pem")])
|
||||||
|
.contains("ACAUA_TLS_CERT")
|
||||||
|
);
|
||||||
|
let t = Transport::from_env(&env(&[
|
||||||
|
("ACAUA_TRANSPORT", "tls"),
|
||||||
|
("ACAUA_TLS_CERT", "/c.pem"),
|
||||||
|
("ACAUA_TLS_KEY", "/k.pem"),
|
||||||
|
]))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(t, Transport::Tls { cert, key }
|
||||||
|
if cert.as_path() == std::path::Path::new("/c.pem") && key.as_path() == std::path::Path::new("/k.pem")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proxy_requires_trusted_list() {
|
||||||
|
let base = ("ACAUA_TRANSPORT", "proxy");
|
||||||
|
assert!(err(&[base]).contains("ACAUA_TRUSTED_PROXIES"));
|
||||||
|
assert!(err(&[base, ("ACAUA_TRUSTED_PROXIES", " , ")]).contains("ACAUA_TRUSTED_PROXIES"));
|
||||||
|
let bad = err(&[base, ("ACAUA_TRUSTED_PROXIES", "10.0.0.0/8,nope")]);
|
||||||
|
assert!(bad.contains("nope"), "{bad}");
|
||||||
|
let t = Transport::from_env(&env(&[
|
||||||
|
base,
|
||||||
|
("ACAUA_TRUSTED_PROXIES", "10.0.0.0/8, 172.18.0.2/32"),
|
||||||
|
]))
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(t, Transport::Proxy { trusted } if trusted.len() == 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hsts_only_in_tls_and_proxy() {
|
||||||
|
assert!(!Transport::Local.hsts());
|
||||||
|
assert!(proxy().hsts());
|
||||||
|
assert!(Transport::Tls {
|
||||||
|
cert: "c".into(),
|
||||||
|
key: "k".into()
|
||||||
|
}
|
||||||
|
.hsts());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn only_local_cookies_lack_secure() {
|
fn only_local_cookies_lack_secure() {
|
||||||
assert!(!Transport::Local.cookie_secure());
|
assert!(!Transport::Local.cookie_secure());
|
||||||
|
|||||||
+64
-13
@@ -5,20 +5,24 @@ use acaua_core::fusion::Alert;
|
|||||||
use acaua_core::storage::{DatasetSummary, Storage};
|
use acaua_core::storage::{DatasetSummary, Storage};
|
||||||
use acaua_core::update::UpdateStatus;
|
use acaua_core::update::UpdateStatus;
|
||||||
use acaua_core::zeek::{self, AnalysisReport};
|
use acaua_core::zeek::{self, AnalysisReport};
|
||||||
use acaua_core::{update, Result};
|
use acaua_core::{update, AcauaError, Result};
|
||||||
use axum::body::Body;
|
use axum::body::Body;
|
||||||
|
use axum::extract::Request;
|
||||||
use axum::extract::{DefaultBodyLimit, Query, State};
|
use axum::extract::{DefaultBodyLimit, Query, State};
|
||||||
use axum::http::{header, Method, StatusCode, Uri};
|
use axum::http::{header, HeaderValue, Method, StatusCode, Uri};
|
||||||
use axum::middleware::from_fn_with_state;
|
use axum::middleware::{from_fn_with_state, Next};
|
||||||
use axum::response::{IntoResponse, Response};
|
use axum::response::{IntoResponse, Response};
|
||||||
use axum::routing::{delete, get, patch, post, MethodRouter};
|
use axum::routing::{delete, get, patch, post, MethodRouter};
|
||||||
use axum::{Json, Router};
|
use axum::{Json, Router};
|
||||||
|
use axum_server::tls_rustls::RustlsConfig;
|
||||||
|
use axum_server::Handle;
|
||||||
use rust_embed::RustEmbed;
|
use rust_embed::RustEmbed;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
||||||
|
use std::time::Duration;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::sync::Semaphore;
|
use tokio::sync::Semaphore;
|
||||||
use tower_http::compression::CompressionLayer;
|
use tower_http::compression::CompressionLayer;
|
||||||
@@ -233,11 +237,25 @@ pub fn build_app(state: AppState) -> Router {
|
|||||||
// Bounds every `Json` extractor before anything looks at the input.
|
// Bounds every `Json` extractor before anything looks at the input.
|
||||||
.layer(DefaultBodyLimit::max(auth_http::MAX_BODY_BYTES))
|
.layer(DefaultBodyLimit::max(auth_http::MAX_BODY_BYTES))
|
||||||
.layer(from_fn_with_state(state.clone(), require_auth))
|
.layer(from_fn_with_state(state.clone(), require_auth))
|
||||||
|
.layer(from_fn_with_state(state.clone(), hsts))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
.layer(CompressionLayer::new())
|
.layer(CompressionLayer::new())
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds `Strict-Transport-Security` when the transport is HTTPS. It sits
|
||||||
|
/// outside `require_auth`, so refusals carry it too.
|
||||||
|
async fn hsts(State(state): State<AppState>, req: Request, next: Next) -> Response {
|
||||||
|
let mut response = next.run(req).await;
|
||||||
|
if state.transport.hsts() {
|
||||||
|
response.headers_mut().insert(
|
||||||
|
header::STRICT_TRANSPORT_SECURITY,
|
||||||
|
HeaderValue::from_static("max-age=31536000"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
response
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Health {
|
struct Health {
|
||||||
status: &'static str,
|
status: &'static str,
|
||||||
@@ -389,22 +407,55 @@ async fn shutdown_signal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn serve(bind: &str, data_dir: PathBuf) -> Result<()> {
|
pub async fn serve(bind: &str, data_dir: PathBuf) -> Result<()> {
|
||||||
|
// Decide the exposure before touching the data directory or printing a
|
||||||
|
// setup code: a refusal must leave nothing behind.
|
||||||
|
let transport = Transport::from_env(&|name| std::env::var(name).ok())?;
|
||||||
let _ = tracing_subscriber::fmt().try_init();
|
let _ = tracing_subscriber::fmt().try_init();
|
||||||
std::fs::create_dir_all(&data_dir)?;
|
std::fs::create_dir_all(&data_dir)?;
|
||||||
let auth = Arc::new(AuthService::open(&data_dir, None)?);
|
let auth = Arc::new(AuthService::open(&data_dir, None)?);
|
||||||
if let Some(code) = auth.ensure_bootstrap(auth_http::unix_now())? {
|
if let Some(code) = auth.ensure_bootstrap(auth_http::unix_now())? {
|
||||||
eprintln!("Acauã: código de configuração inicial (válido por 1 hora): {code}");
|
eprintln!("Acauã: código de configuração inicial (válido por 1 hora): {code}");
|
||||||
}
|
}
|
||||||
let state = AppState::new(Arc::new(Storage::open(&data_dir)?), auth, Transport::Local);
|
let tls = match &transport {
|
||||||
let app = build_app(state);
|
Transport::Tls { cert, key } => Some((cert.clone(), key.clone())),
|
||||||
let listener = TcpListener::bind(bind).await?;
|
_ => None,
|
||||||
println!("Acauã listening on http://{bind}");
|
};
|
||||||
axum::serve(
|
let state = AppState::new(Arc::new(Storage::open(&data_dir)?), auth, transport);
|
||||||
listener,
|
let service = build_app(state).into_make_service_with_connect_info::<SocketAddr>();
|
||||||
app.into_make_service_with_connect_info::<SocketAddr>(),
|
match tls {
|
||||||
)
|
None => {
|
||||||
.with_graceful_shutdown(shutdown_signal())
|
let listener = TcpListener::bind(bind).await?;
|
||||||
.await?;
|
println!("Acauã listening on http://{bind}");
|
||||||
|
axum::serve(listener, service)
|
||||||
|
.with_graceful_shutdown(shutdown_signal())
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Some((cert, key)) => {
|
||||||
|
let addr: SocketAddr = bind.parse().map_err(|_| {
|
||||||
|
AcauaError::InvalidInput(format!(
|
||||||
|
"--bind `{bind}` must be an IP:port when ACAUA_TRANSPORT=tls"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let config = RustlsConfig::from_pem_file(&cert, &key)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
AcauaError::InvalidInput(format!(
|
||||||
|
"cannot load ACAUA_TLS_CERT/ACAUA_TLS_KEY: {e}"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let handle = Handle::new();
|
||||||
|
let stopper = handle.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
shutdown_signal().await;
|
||||||
|
stopper.graceful_shutdown(Some(Duration::from_secs(10)));
|
||||||
|
});
|
||||||
|
println!("Acauã listening on https://{bind}");
|
||||||
|
axum_server::bind_rustls(addr, config)
|
||||||
|
.handle(handle)
|
||||||
|
.serve(service)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -339,6 +339,33 @@ async fn html_request_redirects_to_login_api_gets_401() {
|
|||||||
assert_eq!(r.status, StatusCode::UNAUTHORIZED);
|
assert_eq!(r.status, StatusCode::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn hsts_header_only_in_tls_and_proxy() {
|
||||||
|
for (transport, hsts) in [
|
||||||
|
(Transport::Local, false),
|
||||||
|
(
|
||||||
|
Transport::Tls {
|
||||||
|
cert: "c".into(),
|
||||||
|
key: "k".into(),
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Transport::Proxy {
|
||||||
|
trusted: vec!["10.0.0.0/8".parse().unwrap()],
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
),
|
||||||
|
] {
|
||||||
|
let (_d, _a, app) = fresh(transport);
|
||||||
|
let r = send(&app, empty(Method::GET, "/api/health")).await;
|
||||||
|
assert_eq!(
|
||||||
|
r.headers.contains_key(header::STRICT_TRANSPORT_SECURITY),
|
||||||
|
hsts
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn cookie_flags() {
|
async fn cookie_flags() {
|
||||||
for (transport, secure) in [
|
for (transport, secure) in [
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ services:
|
|||||||
acaua:
|
acaua:
|
||||||
image: ${ACAUA_IMAGE:-git.lucasrguerra.dev.br/ciberfauna/acaua}:${ACAUA_VERSION:-latest}
|
image: ${ACAUA_IMAGE:-git.lucasrguerra.dev.br/ciberfauna/acaua}:${ACAUA_VERSION:-latest}
|
||||||
build: .
|
build: .
|
||||||
|
environment:
|
||||||
|
ACAUA_TRANSPORT: ${ACAUA_TRANSPORT:-local}
|
||||||
|
# Informs the daemon where the port is published; `local` refuses a
|
||||||
|
# non-loopback address.
|
||||||
|
ACAUA_PUBLISHED: ${ACAUA_PUBLISH:-127.0.0.1:8787}
|
||||||
|
ACAUA_TLS_CERT: ${ACAUA_TLS_CERT:-}
|
||||||
|
ACAUA_TLS_KEY: ${ACAUA_TLS_KEY:-}
|
||||||
|
ACAUA_TRUSTED_PROXIES: ${ACAUA_TRUSTED_PROXIES:-}
|
||||||
ports:
|
ports:
|
||||||
- "${ACAUA_PUBLISH:-127.0.0.1:8787}:8787"
|
- "${ACAUA_PUBLISH:-127.0.0.1:8787}:8787"
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
Reference in new issue
Block a user