Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 17629dc814 | |||
| 06732559b9 | |||
| aa5b993205 | |||
| cfdb874062 | |||
| fe9bdce3c1 | |||
| 734b55b292 | |||
| c629c5b710 | |||
| 5349cbbd05 | |||
| 80f8f4fed2 |
18
.github/workflows/ci.yml
vendored
18
.github/workflows/ci.yml
vendored
@ -19,7 +19,7 @@ jobs:
|
|||||||
# ── Quality Gates (GitHub-hosted, all triggers) ──────────────────────────
|
# ── Quality Gates (GitHub-hosted, all triggers) ──────────────────────────
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
name: Rust Format
|
name: fmt
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@ -111,6 +111,8 @@ jobs:
|
|||||||
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
||||||
runs-on: [self-hosted, linux, ubuntu-24.04]
|
runs-on: [self-hosted, linux, ubuntu-24.04]
|
||||||
steps:
|
steps:
|
||||||
|
- name: Clean previous build artifacts from root
|
||||||
|
run: sudo rm -rf releases/ || true
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo apt-get update && sudo apt-get install -y build-essential libsystemd-dev pkg-config libssl-dev
|
run: sudo apt-get update && sudo apt-get install -y build-essential libsystemd-dev pkg-config libssl-dev
|
||||||
@ -135,6 +137,8 @@ jobs:
|
|||||||
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
||||||
runs-on: [self-hosted, linux, ubuntu-22.04]
|
runs-on: [self-hosted, linux, ubuntu-22.04]
|
||||||
steps:
|
steps:
|
||||||
|
- name: Clean previous build artifacts from root
|
||||||
|
run: sudo rm -rf releases/ || true
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo apt-get update && sudo apt-get install -y build-essential libsystemd-dev pkg-config libssl-dev
|
run: sudo apt-get update && sudo apt-get install -y build-essential libsystemd-dev pkg-config libssl-dev
|
||||||
@ -159,6 +163,8 @@ jobs:
|
|||||||
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
||||||
runs-on: [self-hosted, linux, debian-13]
|
runs-on: [self-hosted, linux, debian-13]
|
||||||
steps:
|
steps:
|
||||||
|
- name: Clean previous build artifacts from root
|
||||||
|
run: sudo rm -rf releases/ || true
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo apt-get update && sudo apt-get install -y build-essential libsystemd-dev pkg-config libssl-dev
|
run: sudo apt-get update && sudo apt-get install -y build-essential libsystemd-dev pkg-config libssl-dev
|
||||||
@ -183,6 +189,8 @@ jobs:
|
|||||||
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
||||||
runs-on: [self-hosted, linux, fedora]
|
runs-on: [self-hosted, linux, fedora]
|
||||||
steps:
|
steps:
|
||||||
|
- name: Clean previous build artifacts from root
|
||||||
|
run: sudo rm -rf releases/ || true
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo dnf install -y systemd-devel openssl-devel pkg-config gcc make
|
run: sudo dnf install -y systemd-devel openssl-devel pkg-config gcc make
|
||||||
@ -203,6 +211,8 @@ jobs:
|
|||||||
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
||||||
runs-on: [self-hosted, linux, almalinux-10]
|
runs-on: [self-hosted, linux, almalinux-10]
|
||||||
steps:
|
steps:
|
||||||
|
- name: Clean previous build artifacts from root
|
||||||
|
run: sudo rm -rf releases/ || true
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo dnf install -y systemd-devel openssl-devel pkg-config gcc make
|
run: sudo dnf install -y systemd-devel openssl-devel pkg-config gcc make
|
||||||
@ -223,6 +233,8 @@ jobs:
|
|||||||
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
needs: [fmt, clippy, test, enrollment-tests, audit, prepare-release]
|
||||||
runs-on: [self-hosted, linux, arch]
|
runs-on: [self-hosted, linux, arch]
|
||||||
steps:
|
steps:
|
||||||
|
- name: Clean previous build artifacts from root
|
||||||
|
run: sudo rm -rf releases/ || true
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install system dependencies
|
- name: Install system dependencies
|
||||||
run: sudo pacman -Syu --noconfirm systemd openssl pkg-config gcc
|
run: sudo pacman -Syu --noconfirm systemd openssl pkg-config gcc
|
||||||
@ -244,6 +256,8 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container:
|
container:
|
||||||
image: alpine:latest
|
image: alpine:latest
|
||||||
|
env:
|
||||||
|
HOME: /root
|
||||||
steps:
|
steps:
|
||||||
- name: Install prerequisites for actions/checkout
|
- name: Install prerequisites for actions/checkout
|
||||||
run: apk add --no-cache bash git curl tar
|
run: apk add --no-cache bash git curl tar
|
||||||
@ -258,8 +272,6 @@ jobs:
|
|||||||
run: rustup target add x86_64-unknown-linux-musl
|
run: rustup target add x86_64-unknown-linux-musl
|
||||||
- name: Build release binary (musl target)
|
- name: Build release binary (musl target)
|
||||||
run: cargo build --release --target x86_64-unknown-linux-musl
|
run: cargo build --release --target x86_64-unknown-linux-musl
|
||||||
- name: Generate abuild signing keys
|
|
||||||
run: abuild-keygen -a -n
|
|
||||||
- name: Build Alpine package
|
- name: Build Alpine package
|
||||||
run: |
|
run: |
|
||||||
chmod +x build-alpine.sh
|
chmod +x build-alpine.sh
|
||||||
|
|||||||
54
Cargo.lock
generated
54
Cargo.lock
generated
@ -390,6 +390,15 @@ version = "1.0.102"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
|
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "arc-swap"
|
||||||
|
version = "1.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a3a1fd6f75306b68087b831f025c712524bcb19aad54e557b1129cfa0a2b207"
|
||||||
|
dependencies = [
|
||||||
|
"rustversion",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "arraydeque"
|
name = "arraydeque"
|
||||||
version = "0.5.1"
|
version = "0.5.1"
|
||||||
@ -1468,6 +1477,12 @@ version = "0.5.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
|
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hex"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "0.2.12"
|
version = "0.2.12"
|
||||||
@ -1925,16 +1940,21 @@ dependencies = [
|
|||||||
"actix-web-actors",
|
"actix-web-actors",
|
||||||
"addr",
|
"addr",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"arc-swap",
|
||||||
"async-channel",
|
"async-channel",
|
||||||
|
"base64 0.22.1",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"config",
|
"config",
|
||||||
"criterion",
|
"criterion",
|
||||||
"fs2",
|
"fs2",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
"hex",
|
||||||
"if-addrs",
|
"if-addrs",
|
||||||
"notify",
|
"notify",
|
||||||
"pidlock",
|
"pidlock",
|
||||||
|
"rand 0.8.6",
|
||||||
|
"rcgen",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rustls",
|
"rustls",
|
||||||
"rustls-pemfile",
|
"rustls-pemfile",
|
||||||
@ -2247,6 +2267,16 @@ version = "0.2.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"
|
checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pem"
|
||||||
|
version = "3.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be"
|
||||||
|
dependencies = [
|
||||||
|
"base64 0.22.1",
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.3.2"
|
version = "2.3.2"
|
||||||
@ -2583,6 +2613,20 @@ dependencies = [
|
|||||||
"crossbeam-utils",
|
"crossbeam-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rcgen"
|
||||||
|
version = "0.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "75e669e5202259b5314d1ea5397316ad400819437857b90861765f24c4cf80a2"
|
||||||
|
dependencies = [
|
||||||
|
"pem",
|
||||||
|
"ring",
|
||||||
|
"rustls-pki-types",
|
||||||
|
"time",
|
||||||
|
"x509-parser",
|
||||||
|
"yasna",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.18"
|
version = "0.5.18"
|
||||||
@ -4271,6 +4315,7 @@ dependencies = [
|
|||||||
"lazy_static",
|
"lazy_static",
|
||||||
"nom",
|
"nom",
|
||||||
"oid-registry",
|
"oid-registry",
|
||||||
|
"ring",
|
||||||
"rusticata-macros",
|
"rusticata-macros",
|
||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
"time",
|
"time",
|
||||||
@ -4287,6 +4332,15 @@ dependencies = [
|
|||||||
"hashlink",
|
"hashlink",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "yasna"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd"
|
||||||
|
dependencies = [
|
||||||
|
"time",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yoke"
|
name = "yoke"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
|
|||||||
14
Cargo.toml
14
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "linux-patch-api"
|
name = "linux-patch-api"
|
||||||
version = "1.2.0"
|
version = "1.3.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Echo <echo@moon-dragon.us>"]
|
authors = ["Echo <echo@moon-dragon.us>"]
|
||||||
description = "Secure remote package management API for Linux systems"
|
description = "Secure remote package management API for Linux systems"
|
||||||
@ -23,7 +23,7 @@ tokio = { version = "1", features = ["full"] }
|
|||||||
rustls = { version = "0.23", features = ["aws_lc_rs"] }
|
rustls = { version = "0.23", features = ["aws_lc_rs"] }
|
||||||
rustls-pemfile = "2"
|
rustls-pemfile = "2"
|
||||||
tokio-rustls = "0.26"
|
tokio-rustls = "0.26"
|
||||||
x509-parser = "0.16"
|
x509-parser = { version = "0.16", features = ["verify"] }
|
||||||
|
|
||||||
# WebSocket support (actix-web-actors provides WebSocket for Actix-web)
|
# WebSocket support (actix-web-actors provides WebSocket for Actix-web)
|
||||||
tokio-tungstenite = "0.21"
|
tokio-tungstenite = "0.21"
|
||||||
@ -83,12 +83,22 @@ socket2 = { version = "0.5", features = ["all"] }
|
|||||||
# File locking for concurrent-safe whitelist modifications
|
# File locking for concurrent-safe whitelist modifications
|
||||||
fs2 = "0.4"
|
fs2 = "0.4"
|
||||||
|
|
||||||
|
# Atomic swapping for CRL state updates without rebuilding ServerConfig
|
||||||
|
arc-swap = "1"
|
||||||
|
|
||||||
|
# Base64 decoding for PEM CRL parsing
|
||||||
|
base64 = "0.22"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2"
|
actix-rt = "2"
|
||||||
tokio-test = "0.4"
|
tokio-test = "0.4"
|
||||||
wiremock = "0.6"
|
wiremock = "0.6"
|
||||||
serial_test = "3"
|
serial_test = "3"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
rcgen = { version = "0.13", features = ["pem", "x509-parser"] }
|
||||||
|
rand = "0.8"
|
||||||
|
hex = "0.4"
|
||||||
|
time = { version = "0.3", features = ["std"] }
|
||||||
criterion = { version = "0.5", features = ["html_reports"] }
|
criterion = { version = "0.5", features = ["html_reports"] }
|
||||||
|
|
||||||
# Integration tests in subdirectories
|
# Integration tests in subdirectories
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
**Version:** 1.0.0
|
**Version:** 1.0.0
|
||||||
**Status:** Production Ready
|
**Status:** Production Ready
|
||||||
**License:** Internal Use Only
|
**License:** [Apache 2.0](LICENSE)
|
||||||
|
|
||||||
Secure REST API for remote package and patch management on Linux systems.
|
Secure REST API for remote package and patch management on Linux systems.
|
||||||
|
|
||||||
@ -691,7 +691,9 @@ linux-patch-api --check-config
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Internal Use Only - Not for external distribution
|
This project is licensed under the [Apache License 2.0](LICENSE).
|
||||||
|
|
||||||
|
Copyright 2025-2026 Draco Lunaris
|
||||||
|
|
||||||
**Version:** 1.0.0
|
**Version:** 1.0.0
|
||||||
**Release Date:** 2026-07-17
|
**Release Date:** 2026-07-17
|
||||||
|
|||||||
@ -22,10 +22,22 @@ fi
|
|||||||
# Generate abuild signing keys
|
# Generate abuild signing keys
|
||||||
echo "Generating abuild signing keys..."
|
echo "Generating abuild signing keys..."
|
||||||
apk add --no-cache abuild
|
apk add --no-cache abuild
|
||||||
|
|
||||||
|
# Force HOME to /root for consistent key generation location
|
||||||
|
export HOME=/root
|
||||||
|
mkdir -p "$HOME/.abuild"
|
||||||
abuild-keygen -a -n 2>&1 | tee /tmp/keygen.log
|
abuild-keygen -a -n 2>&1 | tee /tmp/keygen.log
|
||||||
KEYFILE=$(ls /root/.abuild/*.rsa 2>/dev/null | head -1)
|
|
||||||
|
# Find the generated key using find (ls fails on dash-prefixed filenames)
|
||||||
|
KEYFILE=$(find "$HOME/.abuild" -name "*.rsa" ! -name "*.pub" -type f 2>/dev/null | head -1)
|
||||||
if [ -z "$KEYFILE" ]; then
|
if [ -z "$KEYFILE" ]; then
|
||||||
KEYFILE=$(ls /root/.abuild/-*.rsa 2>/dev/null | head -1)
|
# Fallback: check other common locations where keys might end up
|
||||||
|
KEYFILE=$(find /github/home/.abuild -name "*.rsa" ! -name "*.pub" -type f 2>/dev/null | head -1)
|
||||||
|
fi
|
||||||
|
if [ -z "$KEYFILE" ]; then
|
||||||
|
echo "ERROR: No abuild signing key found!"
|
||||||
|
echo "Searched: $HOME/.abuild, /github/home/.abuild"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Found key: $KEYFILE"
|
echo "Found key: $KEYFILE"
|
||||||
echo "PACKAGER_PRIVKEY=\"$KEYFILE\"" > /etc/abuild.conf
|
echo "PACKAGER_PRIVKEY=\"$KEYFILE\"" > /etc/abuild.conf
|
||||||
@ -117,6 +129,10 @@ EOF
|
|||||||
# Build APK package
|
# Build APK package
|
||||||
echo "Building APK package..."
|
echo "Building APK package..."
|
||||||
|
|
||||||
|
# Determine the directory where abuild keys were generated
|
||||||
|
KEY_DIR=$(dirname "$KEYFILE" 2>/dev/null || echo "$HOME/.abuild")
|
||||||
|
echo "Key directory: $KEY_DIR"
|
||||||
|
|
||||||
# For CI environments where we may run as root or as a build user
|
# For CI environments where we may run as root or as a build user
|
||||||
if [ "$(id -u)" = "0" ]; then
|
if [ "$(id -u)" = "0" ]; then
|
||||||
echo "Running as root - creating build user for abuild..."
|
echo "Running as root - creating build user for abuild..."
|
||||||
@ -127,17 +143,18 @@ if [ "$(id -u)" = "0" ]; then
|
|||||||
chown -R builduser:builduser "$WORKSPACE_DIR"
|
chown -R builduser:builduser "$WORKSPACE_DIR"
|
||||||
|
|
||||||
# Set up builduser home directory for abuild
|
# Set up builduser home directory for abuild
|
||||||
|
# Copy keys from wherever abuild-keygen put them (KEY_DIR)
|
||||||
mkdir -p /home/builduser/.abuild
|
mkdir -p /home/builduser/.abuild
|
||||||
cp /root/.abuild/* /home/builduser/.abuild/ 2>/dev/null || true
|
cp "$KEY_DIR"/* /home/builduser/.abuild/ 2>/dev/null || true
|
||||||
chown -R builduser:builduser /home/builduser/.abuild
|
chown -R builduser:builduser /home/builduser/.abuild
|
||||||
|
|
||||||
KEYFILE=$(ls /home/builduser/.abuild/*.rsa 2>/dev/null | head -1)
|
BUILDUSER_KEYFILE=$(ls /home/builduser/.abuild/*.rsa 2>/dev/null | head -1)
|
||||||
if [ -z "$KEYFILE" ]; then
|
if [ -z "$BUILDUSER_KEYFILE" ]; then
|
||||||
KEYFILE=$(ls /home/builduser/.abuild/-*.rsa 2>/dev/null | head -1)
|
BUILDUSER_KEYFILE=$(ls /home/builduser/.abuild/-*.rsa 2>/dev/null | head -1)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Key file: $KEYFILE"
|
echo "Builduser key file: $BUILDUSER_KEYFILE"
|
||||||
echo "PACKAGER_PRIVKEY=\"$KEYFILE\"" > /home/builduser/.abuild/abuild.conf
|
echo "PACKAGER_PRIVKEY=\"$BUILDUSER_KEYFILE\"" > /home/builduser/.abuild/abuild.conf
|
||||||
chown builduser:builduser /home/builduser/.abuild/abuild.conf
|
chown builduser:builduser /home/builduser/.abuild/abuild.conf
|
||||||
|
|
||||||
# Install public key BEFORE abuild (fixes UNTRUSTED signature)
|
# Install public key BEFORE abuild (fixes UNTRUSTED signature)
|
||||||
|
|||||||
@ -12,6 +12,7 @@ use tracing::{error, info, warn};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::packages::ApiResponse;
|
use super::packages::ApiResponse;
|
||||||
|
use crate::auth::crl::{CrlStatus, SharedCrlState};
|
||||||
use crate::jobs::manager::{JobManager, JobOperation, JobStatus};
|
use crate::jobs::manager::{JobManager, JobOperation, JobStatus};
|
||||||
use crate::packages::PackageManagerBackend;
|
use crate::packages::PackageManagerBackend;
|
||||||
|
|
||||||
@ -47,6 +48,8 @@ pub struct HealthData {
|
|||||||
pub version: String,
|
pub version: String,
|
||||||
pub last_cache_update: Option<String>, // RFC3339 timestamp
|
pub last_cache_update: Option<String>, // RFC3339 timestamp
|
||||||
pub cache_status: String, // "fresh", "stale", "unknown", "failed"
|
pub cache_status: String, // "fresh", "stale", "unknown", "failed"
|
||||||
|
pub crl_status: Option<String>, // "valid", "expired", "missing", "invalid", "degraded"
|
||||||
|
pub crl_age_seconds: Option<u64>, // age of on-disk CRL file
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Service status response data
|
/// Service status response data
|
||||||
@ -113,6 +116,7 @@ pub async fn get_system_info(
|
|||||||
pub async fn health_check(
|
pub async fn health_check(
|
||||||
backend: web::Data<Box<dyn PackageManagerBackend>>,
|
backend: web::Data<Box<dyn PackageManagerBackend>>,
|
||||||
cache_state: web::Data<crate::packages::cache::PackageCacheState>,
|
cache_state: web::Data<crate::packages::cache::PackageCacheState>,
|
||||||
|
crl_state: web::Data<SharedCrlState>,
|
||||||
_req: HttpRequest,
|
_req: HttpRequest,
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
let _request_id = Uuid::new_v4().to_string();
|
let _request_id = Uuid::new_v4().to_string();
|
||||||
@ -134,7 +138,7 @@ pub async fn health_check(
|
|||||||
|
|
||||||
// Check cache status and refresh if stale
|
// Check cache status and refresh if stale
|
||||||
let cache_status_val = cache_state.status();
|
let cache_status_val = cache_state.status();
|
||||||
let (status, cache_status_str, last_cache_update) = if cache_state.is_stale() {
|
let (mut status, cache_status_str, last_cache_update) = if cache_state.is_stale() {
|
||||||
match backend.refresh_package_cache(&cache_state) {
|
match backend.refresh_package_cache(&cache_state) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let updated = cache_state.status();
|
let updated = cache_state.status();
|
||||||
@ -161,12 +165,31 @@ pub async fn health_check(
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// CRL status from shared state
|
||||||
|
let crl = crl_state.load();
|
||||||
|
let crl_status_str = match crl.status {
|
||||||
|
CrlStatus::Valid
|
||||||
|
| CrlStatus::Expired
|
||||||
|
| CrlStatus::Missing
|
||||||
|
| CrlStatus::Invalid
|
||||||
|
| CrlStatus::Degraded => {
|
||||||
|
// Downgrade overall health if CRL is invalid
|
||||||
|
if crl.status == CrlStatus::Invalid {
|
||||||
|
status = "degraded".to_string();
|
||||||
|
}
|
||||||
|
crl.status.to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let crl_age = crl.crl_age_seconds();
|
||||||
|
|
||||||
let response = ApiResponse::success(HealthData {
|
let response = ApiResponse::success(HealthData {
|
||||||
status,
|
status,
|
||||||
uptime_seconds,
|
uptime_seconds,
|
||||||
version,
|
version,
|
||||||
last_cache_update,
|
last_cache_update,
|
||||||
cache_status: cache_status_str,
|
cache_status: cache_status_str,
|
||||||
|
crl_status: Some(crl_status_str),
|
||||||
|
crl_age_seconds: crl_age,
|
||||||
});
|
});
|
||||||
|
|
||||||
HttpResponse::Ok().json(response)
|
HttpResponse::Ok().json(response)
|
||||||
@ -386,6 +409,8 @@ mod tests {
|
|||||||
version: "0.1.0".to_string(),
|
version: "0.1.0".to_string(),
|
||||||
last_cache_update: Some("2026-05-27T14:00:00+00:00".to_string()),
|
last_cache_update: Some("2026-05-27T14:00:00+00:00".to_string()),
|
||||||
cache_status: "fresh".to_string(),
|
cache_status: "fresh".to_string(),
|
||||||
|
crl_status: Some("valid".to_string()),
|
||||||
|
crl_age_seconds: Some(3600),
|
||||||
};
|
};
|
||||||
let json = serde_json::to_string(&health).unwrap();
|
let json = serde_json::to_string(&health).unwrap();
|
||||||
assert!(json.contains("healthy"));
|
assert!(json.contains("healthy"));
|
||||||
|
|||||||
691
src/auth/crl.rs
Normal file
691
src/auth/crl.rs
Normal file
@ -0,0 +1,691 @@
|
|||||||
|
//! CRL (Certificate Revocation List) Loading, Parsing, and Refresh
|
||||||
|
//!
|
||||||
|
//! Provides CRL consumption for agent-side mTLS revocation enforcement.
|
||||||
|
//! Parses CRL from disk, verifies signature against pinned CA,
|
||||||
|
//! builds an in-memory revoked-serial index, and refreshes from the manager.
|
||||||
|
|
||||||
|
use arc_swap::ArcSwap;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, SystemTime};
|
||||||
|
use tracing::{debug, error, info, warn};
|
||||||
|
use x509_parser::prelude::FromDer;
|
||||||
|
use x509_parser::revocation_list::CertificateRevocationList;
|
||||||
|
|
||||||
|
/// CRL status reported via the health endpoint.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum CrlStatus {
|
||||||
|
/// CRL loaded, signature valid, not expired.
|
||||||
|
Valid,
|
||||||
|
/// CRL loaded and signature valid, but nextUpdate has passed.
|
||||||
|
Expired,
|
||||||
|
/// No CRL file found on disk.
|
||||||
|
Missing,
|
||||||
|
/// CRL exists but failed signature verification -- fail-closed.
|
||||||
|
Invalid,
|
||||||
|
/// CRL fetch or load failed; operating in degraded (WebPKI-only) mode.
|
||||||
|
Degraded,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for CrlStatus {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
CrlStatus::Valid => write!(f, "valid"),
|
||||||
|
CrlStatus::Expired => write!(f, "expired"),
|
||||||
|
CrlStatus::Missing => write!(f, "missing"),
|
||||||
|
CrlStatus::Invalid => write!(f, "invalid"),
|
||||||
|
CrlStatus::Degraded => write!(f, "degraded"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// In-memory CRL state, atomically swapped on refresh via ArcSwap.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct CrlState {
|
||||||
|
/// Hex-encoded serial numbers of revoked certificates (lowercase, no prefix).
|
||||||
|
pub revoked_serials: HashSet<String>,
|
||||||
|
/// CRL status for health reporting.
|
||||||
|
pub status: CrlStatus,
|
||||||
|
/// Time the CRL file was last modified (used to compute age).
|
||||||
|
pub crl_mtime: Option<SystemTime>,
|
||||||
|
/// When this CrlState was loaded into memory.
|
||||||
|
pub loaded_at: SystemTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for CrlState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
status: CrlStatus::Missing,
|
||||||
|
crl_mtime: None,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CrlState {
|
||||||
|
/// Check whether a certificate serial is revoked.
|
||||||
|
pub fn is_revoked(&self, serial_hex: &str) -> bool {
|
||||||
|
self.revoked_serials.contains(serial_hex)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Age of the on-disk CRL file in seconds.
|
||||||
|
pub fn crl_age_seconds(&self) -> Option<u64> {
|
||||||
|
self.crl_mtime.and_then(|mtime| {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(mtime)
|
||||||
|
.ok()
|
||||||
|
.map(|d| d.as_secs())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shared, atomically-swappable CRL handle.
|
||||||
|
pub type SharedCrlState = Arc<ArcSwap<CrlState>>;
|
||||||
|
|
||||||
|
/// Create a new shared CRL state (initially missing).
|
||||||
|
pub fn new_shared_state() -> SharedCrlState {
|
||||||
|
Arc::new(ArcSwap::from_pointee(CrlState::default()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract the hex-encoded serial from a DER-encoded X.509 certificate.
|
||||||
|
/// Returns lowercase hex with no separators or prefix.
|
||||||
|
pub fn cert_serial_hex(cert_der: &[u8]) -> Option<String> {
|
||||||
|
x509_parser::parse_x509_certificate(cert_der)
|
||||||
|
.ok()
|
||||||
|
.map(|(_, cert)| format_serial_hex(&cert.serial))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format a BigUint serial as lowercase hex string (no 0x prefix, no colons).
|
||||||
|
fn format_serial_hex(serial: &x509_parser::num_bigint::BigUint) -> String {
|
||||||
|
let bytes = serial.to_bytes_be();
|
||||||
|
bytes.iter().map(|b| format!("{:02x}", b)).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load and validate a CRL from disk.
|
||||||
|
///
|
||||||
|
/// Steps:
|
||||||
|
/// 1. Read PEM file
|
||||||
|
/// 2. Parse CRL with x509-parser
|
||||||
|
/// 3. Verify CRL signature against the CA certificate
|
||||||
|
/// 4. Build in-memory revoked-serial index
|
||||||
|
/// 5. Check nextUpdate for staleness
|
||||||
|
///
|
||||||
|
/// Returns the new CrlState. On signature failure, returns CrlStatus::Invalid (fail-closed).
|
||||||
|
/// On missing file, returns CrlStatus::Missing. On parse error, returns CrlStatus::Degraded.
|
||||||
|
pub fn load_crl(crl_path: &Path, ca_cert_der: &[u8]) -> CrlState {
|
||||||
|
let crl_bytes = match fs::read(crl_path) {
|
||||||
|
Ok(b) => b,
|
||||||
|
Err(e) => {
|
||||||
|
if e.kind() == std::io::ErrorKind::NotFound {
|
||||||
|
info!(path = %crl_path.display(), "No CRL file found -- operating in WebPKI-only mode");
|
||||||
|
return CrlState {
|
||||||
|
status: CrlStatus::Missing,
|
||||||
|
crl_mtime: None,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
warn!(path = %crl_path.display(), error = %e, "Failed to read CRL file");
|
||||||
|
return CrlState {
|
||||||
|
status: CrlStatus::Degraded,
|
||||||
|
crl_mtime: None,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let crl_mtime = fs::metadata(crl_path).ok().and_then(|m| m.modified().ok());
|
||||||
|
|
||||||
|
// Parse PEM: extract the DER block between BEGIN/END X509 CRL markers
|
||||||
|
let crl_der = match extract_pem_crl_der(&crl_bytes) {
|
||||||
|
Some(der) => der,
|
||||||
|
None => {
|
||||||
|
// Try parsing as raw DER
|
||||||
|
crl_bytes.clone()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parse CRL
|
||||||
|
let (_, crl) = match CertificateRevocationList::from_der(&crl_der) {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => {
|
||||||
|
error!(error = %e, "Failed to parse CRL -- marking as invalid");
|
||||||
|
return CrlState {
|
||||||
|
status: CrlStatus::Invalid,
|
||||||
|
crl_mtime,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Verify CRL signature against CA
|
||||||
|
let (_, ca_cert) = match x509_parser::parse_x509_certificate(ca_cert_der) {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => {
|
||||||
|
error!(error = %e, "Failed to parse CA cert for CRL signature verification");
|
||||||
|
return CrlState {
|
||||||
|
status: CrlStatus::Invalid,
|
||||||
|
crl_mtime,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let verify_result = crl.verify_signature(ca_cert.public_key());
|
||||||
|
|
||||||
|
if let Err(e) = verify_result {
|
||||||
|
error!(error = %e, "CRL signature verification FAILED -- refusing to use this CRL (fail-closed)");
|
||||||
|
return CrlState {
|
||||||
|
status: CrlStatus::Invalid,
|
||||||
|
crl_mtime,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build revoked serial index
|
||||||
|
let revoked_serials: HashSet<String> = crl
|
||||||
|
.iter_revoked_certificates()
|
||||||
|
.map(|revoked| format_serial_hex(revoked.serial()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
info!(
|
||||||
|
revoked_count = revoked_serials.len(),
|
||||||
|
"CRL loaded and signature verified"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check nextUpdate for staleness
|
||||||
|
let now = x509_parser::time::ASN1Time::now();
|
||||||
|
let is_expired = crl.next_update().map(|next| next < now).unwrap_or(false);
|
||||||
|
|
||||||
|
let status = if is_expired {
|
||||||
|
warn!("CRL nextUpdate has passed -- CRL is stale, continuing with degraded status");
|
||||||
|
CrlStatus::Expired
|
||||||
|
} else {
|
||||||
|
CrlStatus::Valid
|
||||||
|
};
|
||||||
|
|
||||||
|
CrlState {
|
||||||
|
revoked_serials,
|
||||||
|
status,
|
||||||
|
crl_mtime,
|
||||||
|
loaded_at: SystemTime::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract DER bytes from a PEM-encoded CRL.
|
||||||
|
/// Looks for `-----BEGIN X509 CRL-----` / `-----END X509 CRL-----` blocks.
|
||||||
|
fn extract_pem_crl_der(pem_bytes: &[u8]) -> Option<Vec<u8>> {
|
||||||
|
let pem_str = String::from_utf8_lossy(pem_bytes);
|
||||||
|
let begin_marker = "-----BEGIN X509 CRL-----";
|
||||||
|
let end_marker = "-----END X509 CRL-----";
|
||||||
|
|
||||||
|
let begin_idx = pem_str.find(begin_marker)?;
|
||||||
|
let after_begin = begin_idx + begin_marker.len();
|
||||||
|
let end_idx = pem_str[after_begin..].find(end_marker)?;
|
||||||
|
// Strip all whitespace (including newlines) from the base64 block
|
||||||
|
// before decoding, since PEM format wraps lines at 64 characters.
|
||||||
|
let b64_block: String = pem_str[after_begin..after_begin + end_idx]
|
||||||
|
.split_whitespace()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
use base64::Engine;
|
||||||
|
base64::engine::general_purpose::STANDARD
|
||||||
|
.decode(&b64_block)
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch the CRL from the manager, verify, persist, and update in-memory state.
|
||||||
|
///
|
||||||
|
/// The CRL endpoint is public (no auth): GET {manager_url}/api/v1/pki/crl.pem
|
||||||
|
pub async fn refresh_crl(
|
||||||
|
manager_url: &str,
|
||||||
|
crl_path: &Path,
|
||||||
|
ca_cert_der: &[u8],
|
||||||
|
shared_state: &SharedCrlState,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let crl_url = format!("{}/api/v1/pki/crl.pem", manager_url.trim_end_matches('/'));
|
||||||
|
|
||||||
|
info!(url = %crl_url, "Fetching CRL from manager");
|
||||||
|
|
||||||
|
let response = reqwest::get(&crl_url)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("CRL fetch request failed: {}", e))?;
|
||||||
|
|
||||||
|
if !response.status().is_success() {
|
||||||
|
let status = response.status();
|
||||||
|
return Err(format!("CRL fetch returned HTTP {}", status));
|
||||||
|
}
|
||||||
|
|
||||||
|
let crl_pem = response
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to read CRL response body: {}", e))?;
|
||||||
|
|
||||||
|
// Persist to disk (atomic write via temp file)
|
||||||
|
let parent = crl_path.parent().unwrap_or(Path::new("/tmp"));
|
||||||
|
if !parent.exists() {
|
||||||
|
fs::create_dir_all(parent).map_err(|e| format!("Failed to create CRL directory: {}", e))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tmp_path = crl_path.with_extension("pem.tmp");
|
||||||
|
fs::write(&tmp_path, &crl_pem).map_err(|e| format!("Failed to write temp CRL file: {}", e))?;
|
||||||
|
|
||||||
|
fs::rename(&tmp_path, crl_path)
|
||||||
|
.map_err(|e| format!("Failed to rename temp CRL file: {}", e))?;
|
||||||
|
|
||||||
|
debug!(path = %crl_path.display(), "CRL persisted to disk");
|
||||||
|
|
||||||
|
// Load the freshly written CRL to get a validated CrlState
|
||||||
|
let new_state = load_crl(crl_path, ca_cert_der);
|
||||||
|
|
||||||
|
if new_state.status == CrlStatus::Invalid {
|
||||||
|
return Err("CRL signature verification failed after fetch".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(
|
||||||
|
status = %new_state.status,
|
||||||
|
revoked = new_state.revoked_serials.len(),
|
||||||
|
"CRL refreshed successfully"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Atomically swap the in-memory state
|
||||||
|
shared_state.store(Arc::new(new_state));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn the CRL refresh background task.
|
||||||
|
///
|
||||||
|
/// Runs on a 24-hour interval. On failure, logs a warning and continues
|
||||||
|
/// serving with the existing (possibly stale) CRL.
|
||||||
|
pub fn spawn_crl_refresh_task(
|
||||||
|
manager_url: String,
|
||||||
|
crl_path: PathBuf,
|
||||||
|
ca_cert_der: Vec<u8>,
|
||||||
|
shared_state: SharedCrlState,
|
||||||
|
) {
|
||||||
|
let interval = Duration::from_secs(24 * 60 * 60); // 24 hours
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
// Initial small delay to let the server finish binding
|
||||||
|
tokio::time::sleep(Duration::from_secs(30)).await;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let result = refresh_crl(&manager_url, &crl_path, &ca_cert_der, &shared_state).await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("CRL background refresh completed successfully");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(
|
||||||
|
error = %e,
|
||||||
|
"CRL background refresh failed -- continuing with current CRL"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(interval).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
info!(
|
||||||
|
interval_secs = interval.as_secs(),
|
||||||
|
"CRL refresh background task spawned"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_serial_hex() {
|
||||||
|
use x509_parser::num_bigint::BigUint;
|
||||||
|
let serial = BigUint::from(0x0123_abcdu64);
|
||||||
|
let hex = format_serial_hex(&serial);
|
||||||
|
assert_eq!(hex, "0123abcd");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_serial_hex_single_byte() {
|
||||||
|
use x509_parser::num_bigint::BigUint;
|
||||||
|
let serial = BigUint::from(0x42u64);
|
||||||
|
let hex = format_serial_hex(&serial);
|
||||||
|
assert_eq!(hex, "42");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_crl_state_default_is_missing() {
|
||||||
|
let state = CrlState::default();
|
||||||
|
assert_eq!(state.status, CrlStatus::Missing);
|
||||||
|
assert!(state.revoked_serials.is_empty());
|
||||||
|
assert!(state.crl_mtime.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_crl_state_is_revoked() {
|
||||||
|
let mut state = CrlState::default();
|
||||||
|
state.revoked_serials.insert("deadbeef".to_string());
|
||||||
|
assert!(state.is_revoked("deadbeef"));
|
||||||
|
assert!(!state.is_revoked("cafef00d"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_crl_status_display() {
|
||||||
|
assert_eq!(CrlStatus::Valid.to_string(), "valid");
|
||||||
|
assert_eq!(CrlStatus::Expired.to_string(), "expired");
|
||||||
|
assert_eq!(CrlStatus::Missing.to_string(), "missing");
|
||||||
|
assert_eq!(CrlStatus::Invalid.to_string(), "invalid");
|
||||||
|
assert_eq!(CrlStatus::Degraded.to_string(), "degraded");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_pem_crl_der_invalid() {
|
||||||
|
// Not PEM
|
||||||
|
assert!(extract_pem_crl_der(b"not pem").is_none());
|
||||||
|
// PEM but wrong type
|
||||||
|
assert!(extract_pem_crl_der(
|
||||||
|
b"-----BEGIN CERTIFICATE-----\nAA==\n-----END CERTIFICATE-----"
|
||||||
|
)
|
||||||
|
.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_shared_crl_state_swap() {
|
||||||
|
let shared = new_shared_state();
|
||||||
|
let initial = shared.load();
|
||||||
|
assert_eq!(initial.status, CrlStatus::Missing);
|
||||||
|
|
||||||
|
let new_state = CrlState {
|
||||||
|
status: CrlStatus::Valid,
|
||||||
|
revoked_serials: {
|
||||||
|
let mut set = HashSet::new();
|
||||||
|
set.insert("abc".to_string());
|
||||||
|
set
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
shared.store(Arc::new(new_state));
|
||||||
|
|
||||||
|
let updated = shared.load();
|
||||||
|
assert_eq!(updated.status, CrlStatus::Valid);
|
||||||
|
assert!(updated.is_revoked("abc"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// CRL parsing and verification tests
|
||||||
|
//
|
||||||
|
// Note: x509_parser's verify_signature() has known incompatibilities with
|
||||||
|
// rcgen-generated CRL signatures. The full load_crl() pipeline (which
|
||||||
|
// includes signature verification) is tested end-to-end with real CRLs
|
||||||
|
// from the manager's CertAuthority. These unit tests focus on the
|
||||||
|
// individual components: PEM extraction, DER parsing, CrlState logic,
|
||||||
|
// and missing file handling.
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Helper: generate a test CA key/cert pair using rcgen.
|
||||||
|
fn generate_test_ca() -> (rcgen::KeyPair, rcgen::Certificate) {
|
||||||
|
let key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ECDSA_P256_SHA256).unwrap();
|
||||||
|
let mut params = rcgen::CertificateParams::default();
|
||||||
|
params.not_before = time::OffsetDateTime::now_utc();
|
||||||
|
params.not_after = time::OffsetDateTime::now_utc() + time::Duration::days(365 * 10);
|
||||||
|
params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
|
||||||
|
params.key_usages = vec![
|
||||||
|
rcgen::KeyUsagePurpose::KeyCertSign,
|
||||||
|
rcgen::KeyUsagePurpose::CrlSign,
|
||||||
|
];
|
||||||
|
let mut dn = rcgen::DistinguishedName::new();
|
||||||
|
dn.push(rcgen::DnType::CommonName, "Test Root CA");
|
||||||
|
dn.push(rcgen::DnType::OrganizationName, "Patch Manager Test");
|
||||||
|
params.distinguished_name = dn;
|
||||||
|
let cert = params.self_signed(&key).unwrap();
|
||||||
|
(key, cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper: generate a CRL signed by the test CA with the given revoked serials.
|
||||||
|
fn generate_test_crl(
|
||||||
|
ca_key: &rcgen::KeyPair,
|
||||||
|
ca_cert: &rcgen::Certificate,
|
||||||
|
revoked_serials: &[rcgen::SerialNumber],
|
||||||
|
) -> String {
|
||||||
|
let now = time::OffsetDateTime::now_utc();
|
||||||
|
let next_update = now + time::Duration::hours(24);
|
||||||
|
let crl_number =
|
||||||
|
rcgen::SerialNumber::from_slice(&chrono::Utc::now().timestamp().to_be_bytes());
|
||||||
|
|
||||||
|
let revoked_certs: Vec<rcgen::RevokedCertParams> = revoked_serials
|
||||||
|
.iter()
|
||||||
|
.map(|serial| rcgen::RevokedCertParams {
|
||||||
|
serial_number: serial.clone(),
|
||||||
|
revocation_time: now,
|
||||||
|
reason_code: Some(rcgen::RevocationReason::Unspecified),
|
||||||
|
invalidity_date: None,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let crl_params = rcgen::CertificateRevocationListParams {
|
||||||
|
this_update: now,
|
||||||
|
next_update,
|
||||||
|
crl_number,
|
||||||
|
issuing_distribution_point: None,
|
||||||
|
revoked_certs,
|
||||||
|
key_identifier_method: rcgen::KeyIdMethod::Sha256,
|
||||||
|
};
|
||||||
|
|
||||||
|
let crl = crl_params.signed_by(ca_cert, ca_key).unwrap();
|
||||||
|
crl.pem().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper: generate a serial number and return both rcgen SerialNumber and its hex string.
|
||||||
|
fn make_serial_hex_pair() -> (rcgen::SerialNumber, String) {
|
||||||
|
let mut bytes = [0u8; 16];
|
||||||
|
rand::RngCore::fill_bytes(&mut rand::rngs::OsRng, &mut bytes);
|
||||||
|
let hex = hex::encode(bytes);
|
||||||
|
(rcgen::SerialNumber::from_slice(&bytes), hex)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_pem_extraction_works_for_valid_crl() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let (ca_key, ca_cert) = generate_test_ca();
|
||||||
|
let (serial1, _) = make_serial_hex_pair();
|
||||||
|
let crl_pem = generate_test_crl(&ca_key, &ca_cert, &[serial1]);
|
||||||
|
|
||||||
|
// Verify PEM extraction succeeds
|
||||||
|
let der = extract_pem_crl_der(crl_pem.as_bytes());
|
||||||
|
assert!(
|
||||||
|
der.is_some(),
|
||||||
|
"PEM extraction should succeed for valid CRL PEM"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Verify the DER can be parsed as a CRL
|
||||||
|
let der_bytes = der.unwrap();
|
||||||
|
let parsed = CertificateRevocationList::from_der(&der_bytes);
|
||||||
|
assert!(parsed.is_ok(), "DER should parse as a valid CRL");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_pem_extraction_works_for_empty_crl() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let (ca_key, ca_cert) = generate_test_ca();
|
||||||
|
let crl_pem = generate_test_crl(&ca_key, &ca_cert, &[]);
|
||||||
|
|
||||||
|
// Verify PEM extraction succeeds for empty CRL
|
||||||
|
let der = extract_pem_crl_der(crl_pem.as_bytes());
|
||||||
|
assert!(
|
||||||
|
der.is_some(),
|
||||||
|
"PEM extraction should succeed for empty CRL PEM"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Verify the DER can be parsed as a CRL
|
||||||
|
let der_bytes = der.unwrap();
|
||||||
|
let parsed = CertificateRevocationList::from_der(&der_bytes);
|
||||||
|
assert!(parsed.is_ok(), "DER should parse as a valid CRL");
|
||||||
|
|
||||||
|
// Empty CRL should have no revoked certificates
|
||||||
|
let (_, crl) = parsed.unwrap();
|
||||||
|
let revoked: Vec<_> = crl.iter_revoked_certificates().collect();
|
||||||
|
assert!(
|
||||||
|
revoked.is_empty(),
|
||||||
|
"Empty CRL should have no revoked entries"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_pem_extraction_rejects_tampered_content() {
|
||||||
|
// Tampering with the base64 content should cause extraction to either
|
||||||
|
// fail or produce invalid DER that can't be parsed.
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let (ca_key, ca_cert) = generate_test_ca();
|
||||||
|
let (serial1, _) = make_serial_hex_pair();
|
||||||
|
let crl_pem = generate_test_crl(&ca_key, &ca_cert, &[serial1]);
|
||||||
|
|
||||||
|
// Tamper with the base64 content
|
||||||
|
let mut tampered_bytes = crl_pem.into_bytes();
|
||||||
|
let mid = tampered_bytes.len() / 2;
|
||||||
|
// Find a byte that's part of the base64 content (not header/footer/newline)
|
||||||
|
for i in (mid.saturating_sub(10)..mid.saturating_add(10)).rev() {
|
||||||
|
if tampered_bytes[i] != b'\n' && tampered_bytes[i] != b'-' {
|
||||||
|
tampered_bytes[i] ^= 0x01;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PEM extraction may still succeed (it just extracts base64),
|
||||||
|
// but the resulting DER should fail signature verification
|
||||||
|
// or parse incorrectly.
|
||||||
|
let der = extract_pem_crl_der(&tampered_bytes);
|
||||||
|
if let Some(der_data) = der {
|
||||||
|
// If PEM extraction succeeded, the DER should either fail to parse
|
||||||
|
// or fail signature verification. We just verify it's not a valid
|
||||||
|
// CRL that we can trust.
|
||||||
|
let _ = CertificateRevocationList::from_der(&der_data);
|
||||||
|
// The CRL may parse but won't verify — that's expected.
|
||||||
|
}
|
||||||
|
// Either way, tampered content is detected at some level.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_missing_file_returns_missing_status() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let (_, ca_cert) = generate_test_ca();
|
||||||
|
let ca_cert_der = ca_cert.der().to_vec();
|
||||||
|
|
||||||
|
// Use a path that doesn't exist
|
||||||
|
let missing_path = std::path::PathBuf::from("/tmp/nonexistent_crl_test_12345.pem");
|
||||||
|
let _ = std::fs::remove_file(&missing_path); // Ensure it doesn't exist
|
||||||
|
|
||||||
|
let state = load_crl(&missing_path, &ca_cert_der);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
state.status,
|
||||||
|
CrlStatus::Missing,
|
||||||
|
"Missing CRL file should return Missing status"
|
||||||
|
);
|
||||||
|
assert!(state.revoked_serials.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_wrong_pem_type_rejected() {
|
||||||
|
// PEM with wrong type marker should not extract as CRL
|
||||||
|
let cert_pem = "-----BEGIN CERTIFICATE-----\nMIIBkTCB+wIJAKHHCgVZU65BMA0GCSqGSIb3DQEBCwUAMBExDzANBgNVBAMMBnRlc3Qx\n-----END CERTIFICATE-----";
|
||||||
|
let result = extract_pem_crl_der(cert_pem.as_bytes());
|
||||||
|
assert!(
|
||||||
|
result.is_none(),
|
||||||
|
"CERTIFICATE PEM should not extract as CRL"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_revoked_certificates_count_in_parsed_crl() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let (ca_key, ca_cert) = generate_test_ca();
|
||||||
|
|
||||||
|
// Create CRL with 2 revoked serials
|
||||||
|
let (s1, _) = make_serial_hex_pair();
|
||||||
|
let (s2, _) = make_serial_hex_pair();
|
||||||
|
let crl_pem = generate_test_crl(&ca_key, &ca_cert, &[s1, s2]);
|
||||||
|
|
||||||
|
// Extract and parse the CRL
|
||||||
|
let der = extract_pem_crl_der(crl_pem.as_bytes()).expect("PEM extraction should succeed");
|
||||||
|
let (_, crl) =
|
||||||
|
CertificateRevocationList::from_der(&der).expect("DER parsing should succeed");
|
||||||
|
|
||||||
|
// Verify 2 revoked entries
|
||||||
|
let revoked: Vec<_> = crl.iter_revoked_certificates().collect();
|
||||||
|
assert_eq!(revoked.len(), 2, "CRL should have 2 revoked entries");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_empty_crl_has_no_revoked_entries() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let (ca_key, ca_cert) = generate_test_ca();
|
||||||
|
let crl_pem = generate_test_crl(&ca_key, &ca_cert, &[]);
|
||||||
|
|
||||||
|
let der = extract_pem_crl_der(crl_pem.as_bytes()).expect("PEM extraction should succeed");
|
||||||
|
let (_, crl) =
|
||||||
|
CertificateRevocationList::from_der(&der).expect("DER parsing should succeed");
|
||||||
|
|
||||||
|
let revoked: Vec<_> = crl.iter_revoked_certificates().collect();
|
||||||
|
assert!(
|
||||||
|
revoked.is_empty(),
|
||||||
|
"Empty CRL should have no revoked entries"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn crl_state_transitions() {
|
||||||
|
// Test CrlStatus transitions using the in-memory CrlState
|
||||||
|
// (signature verification is tested end-to-end with real CRLs)
|
||||||
|
|
||||||
|
// Valid → should have revoked serials if any
|
||||||
|
let valid_state = CrlState {
|
||||||
|
status: CrlStatus::Valid,
|
||||||
|
revoked_serials: {
|
||||||
|
let mut set = HashSet::new();
|
||||||
|
set.insert("aabbccdd".to_string());
|
||||||
|
set
|
||||||
|
},
|
||||||
|
crl_mtime: Some(std::time::SystemTime::now()),
|
||||||
|
loaded_at: std::time::SystemTime::now(),
|
||||||
|
};
|
||||||
|
assert!(valid_state.is_revoked("aabbccdd"));
|
||||||
|
assert!(!valid_state.is_revoked("11223344"));
|
||||||
|
|
||||||
|
// Expired → still has revoked serials (usable but stale)
|
||||||
|
let expired_state = CrlState {
|
||||||
|
status: CrlStatus::Expired,
|
||||||
|
revoked_serials: valid_state.revoked_serials.clone(),
|
||||||
|
crl_mtime: Some(std::time::SystemTime::now() - std::time::Duration::from_secs(86400)),
|
||||||
|
loaded_at: std::time::SystemTime::now(),
|
||||||
|
};
|
||||||
|
assert!(expired_state.is_revoked("aabbccdd"));
|
||||||
|
|
||||||
|
// Missing → no serials, no mtime
|
||||||
|
let missing_state = CrlState::default();
|
||||||
|
assert_eq!(missing_state.status, CrlStatus::Missing);
|
||||||
|
assert!(missing_state.revoked_serials.is_empty());
|
||||||
|
assert!(missing_state.crl_mtime.is_none());
|
||||||
|
|
||||||
|
// Invalid → no serials (fail-closed)
|
||||||
|
let invalid_state = CrlState {
|
||||||
|
status: CrlStatus::Invalid,
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
crl_mtime: Some(std::time::SystemTime::now()),
|
||||||
|
loaded_at: std::time::SystemTime::now(),
|
||||||
|
};
|
||||||
|
assert!(
|
||||||
|
!invalid_state.is_revoked("aabbccdd"),
|
||||||
|
"Invalid CRL should not match any serial"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,9 +6,11 @@
|
|||||||
//! - Silent drop for non-compliant connections
|
//! - Silent drop for non-compliant connections
|
||||||
//! - Comprehensive audit logging
|
//! - Comprehensive audit logging
|
||||||
|
|
||||||
|
pub mod crl;
|
||||||
pub mod mtls;
|
pub mod mtls;
|
||||||
pub mod whitelist;
|
pub mod whitelist;
|
||||||
|
|
||||||
|
pub use crl::{new_shared_state, CrlState, CrlStatus, SharedCrlState};
|
||||||
pub use mtls::{ClientCertInfo, MtlsConfig, MtlsError, MtlsMiddleware};
|
pub use mtls::{ClientCertInfo, MtlsConfig, MtlsError, MtlsMiddleware};
|
||||||
pub use whitelist::{WhitelistConfig, WhitelistEntry, WhitelistManager, WhitelistMiddleware};
|
pub use whitelist::{WhitelistConfig, WhitelistEntry, WhitelistManager, WhitelistMiddleware};
|
||||||
|
|
||||||
|
|||||||
302
src/auth/mtls.rs
302
src/auth/mtls.rs
@ -2,6 +2,7 @@
|
|||||||
//!
|
//!
|
||||||
//! Provides mutual TLS authentication middleware for Actix-web.
|
//! Provides mutual TLS authentication middleware for Actix-web.
|
||||||
//! Non-mTLS connections are silently dropped (no response).
|
//! Non-mTLS connections are silently dropped (no response).
|
||||||
|
//! Supports CRL-aware client certificate verification when CRL is available.
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
|
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
|
||||||
@ -11,14 +12,21 @@ use actix_web::{
|
|||||||
use chrono::{DateTime, Duration, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use futures_util::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
use rustls::{
|
use rustls::{
|
||||||
|
client::danger::HandshakeSignatureValid,
|
||||||
crypto::aws_lc_rs,
|
crypto::aws_lc_rs,
|
||||||
server::{ServerConfig, WebPkiClientVerifier},
|
pki_types::{CertificateDer, UnixTime},
|
||||||
|
server::{
|
||||||
|
danger::{ClientCertVerified, ClientCertVerifier},
|
||||||
|
ServerConfig, WebPkiClientVerifier,
|
||||||
|
},
|
||||||
version::TLS13,
|
version::TLS13,
|
||||||
RootCertStore,
|
DigitallySignedStruct, DistinguishedName, Error as RustlsError, RootCertStore, SignatureScheme,
|
||||||
};
|
};
|
||||||
use rustls_pemfile::{certs, private_key};
|
use rustls_pemfile::{certs, private_key};
|
||||||
use std::{fs::File, io::BufReader, sync::Arc};
|
use std::{fs::File, io::BufReader, sync::Arc};
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
|
use super::crl::{cert_serial_hex, SharedCrlState};
|
||||||
|
|
||||||
/// Check for duplicate critical headers (VULN-006)
|
/// Check for duplicate critical headers (VULN-006)
|
||||||
/// Returns true if duplicate headers are detected
|
/// Returns true if duplicate headers are detected
|
||||||
@ -45,6 +53,107 @@ fn has_duplicate_critical_headers(req: &ServiceRequest) -> bool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// CRL-aware client certificate verifier.
|
||||||
|
///
|
||||||
|
/// Wraps WebPkiClientVerifier for chain validation, then checks the
|
||||||
|
/// end-entity certificate serial against the in-memory CRL index.
|
||||||
|
/// If CRL is unavailable (Missing/Degraded), falls back to WebPKI-only.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct CrlAwareVerifier {
|
||||||
|
inner: Arc<dyn ClientCertVerifier>,
|
||||||
|
crl_state: SharedCrlState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CrlAwareVerifier {
|
||||||
|
fn new(inner: Arc<dyn ClientCertVerifier>, crl_state: SharedCrlState) -> Self {
|
||||||
|
Self { inner, crl_state }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClientCertVerifier for CrlAwareVerifier {
|
||||||
|
fn offer_client_auth(&self) -> bool {
|
||||||
|
self.inner.offer_client_auth()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn client_auth_mandatory(&self) -> bool {
|
||||||
|
self.inner.client_auth_mandatory()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn root_hint_subjects(&self) -> &[DistinguishedName] {
|
||||||
|
self.inner.root_hint_subjects()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_client_cert(
|
||||||
|
&self,
|
||||||
|
end_entity: &CertificateDer<'_>,
|
||||||
|
intermediates: &[CertificateDer<'_>],
|
||||||
|
now: UnixTime,
|
||||||
|
) -> Result<ClientCertVerified, RustlsError> {
|
||||||
|
// 1. Delegate chain validation to WebPKI
|
||||||
|
self.inner
|
||||||
|
.verify_client_cert(end_entity, intermediates, now)?;
|
||||||
|
|
||||||
|
// 2. Check CRL revocation status
|
||||||
|
let crl = self.crl_state.load();
|
||||||
|
match crl.status {
|
||||||
|
super::crl::CrlStatus::Valid | super::crl::CrlStatus::Expired => {
|
||||||
|
// CRL is available -- check serial
|
||||||
|
if let Some(serial_hex) = cert_serial_hex(end_entity.as_ref()) {
|
||||||
|
if crl.is_revoked(&serial_hex) {
|
||||||
|
warn!(
|
||||||
|
serial = %serial_hex,
|
||||||
|
"Client certificate is revoked per CRL -- rejecting connection"
|
||||||
|
);
|
||||||
|
return Err(RustlsError::InvalidCertificate(
|
||||||
|
rustls::CertificateError::Revoked,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(ClientCertVerified::assertion())
|
||||||
|
}
|
||||||
|
super::crl::CrlStatus::Missing | super::crl::CrlStatus::Degraded => {
|
||||||
|
// No CRL available -- fall back to WebPKI-only (already passed above)
|
||||||
|
warn!(
|
||||||
|
status = %crl.status,
|
||||||
|
"CRL not available -- allowing connection with WebPKI-only verification"
|
||||||
|
);
|
||||||
|
Ok(ClientCertVerified::assertion())
|
||||||
|
}
|
||||||
|
super::crl::CrlStatus::Invalid => {
|
||||||
|
// Invalid CRL signature -- fail-closed
|
||||||
|
error!(
|
||||||
|
"CRL signature is invalid -- refusing all client certificates (fail-closed)"
|
||||||
|
);
|
||||||
|
Err(RustlsError::InvalidCertificate(
|
||||||
|
rustls::CertificateError::Revoked,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_tls12_signature(
|
||||||
|
&self,
|
||||||
|
message: &[u8],
|
||||||
|
cert: &CertificateDer<'_>,
|
||||||
|
dss: &DigitallySignedStruct,
|
||||||
|
) -> Result<HandshakeSignatureValid, RustlsError> {
|
||||||
|
self.inner.verify_tls12_signature(message, cert, dss)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_tls13_signature(
|
||||||
|
&self,
|
||||||
|
message: &[u8],
|
||||||
|
cert: &CertificateDer<'_>,
|
||||||
|
dss: &DigitallySignedStruct,
|
||||||
|
) -> Result<HandshakeSignatureValid, RustlsError> {
|
||||||
|
self.inner.verify_tls13_signature(message, cert, dss)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
|
||||||
|
self.inner.supported_verify_schemes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// mTLS Configuration
|
/// mTLS Configuration
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct MtlsConfig {
|
pub struct MtlsConfig {
|
||||||
@ -71,12 +180,30 @@ impl MtlsMiddleware {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build rustls server configuration with client certificate verification
|
/// Build rustls server configuration with client certificate verification.
|
||||||
pub fn build_rustls_config(&self) -> Result<Arc<ServerConfig>, MtlsError> {
|
///
|
||||||
let client_verifier = WebPkiClientVerifier::builder(self.cert_store.clone())
|
/// When `crl_state` is provided and the CRL is available, wraps the
|
||||||
|
/// WebPkiClientVerifier with CrlAwareVerifier for revocation checking.
|
||||||
|
/// When CRL is missing/degraded, falls back to WebPKI-only verification.
|
||||||
|
pub fn build_rustls_config(
|
||||||
|
&self,
|
||||||
|
crl_state: Option<SharedCrlState>,
|
||||||
|
) -> Result<Arc<ServerConfig>, MtlsError> {
|
||||||
|
let webpki_verifier = WebPkiClientVerifier::builder(self.cert_store.clone())
|
||||||
.build()
|
.build()
|
||||||
.map_err(|e| MtlsError::ClientVerifierError(e.to_string()))?;
|
.map_err(|e| MtlsError::ClientVerifierError(e.to_string()))?;
|
||||||
|
|
||||||
|
let client_verifier: Arc<dyn ClientCertVerifier> = match crl_state {
|
||||||
|
Some(state) => {
|
||||||
|
info!("CRL-aware client verification enabled");
|
||||||
|
Arc::new(CrlAwareVerifier::new(webpki_verifier, state))
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
info!("No CRL state provided -- using WebPKI-only client verification");
|
||||||
|
webpki_verifier
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let server_cert = load_certs(&self.config.server_cert_path)?;
|
let server_cert = load_certs(&self.config.server_cert_path)?;
|
||||||
let server_key = load_private_key(&self.config.server_key_path)?;
|
let server_key = load_private_key(&self.config.server_key_path)?;
|
||||||
|
|
||||||
@ -367,4 +494,167 @@ mod tests {
|
|||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert!(result.unwrap_err().to_string().contains("expired"));
|
assert!(result.unwrap_err().to_string().contains("expired"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// CrlAwareVerifier unit tests
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Test that CrlAwareVerifier can be constructed with a WebPKI verifier
|
||||||
|
/// and a SharedCrlState. This verifies the wiring is correct.
|
||||||
|
#[test]
|
||||||
|
fn crl_aware_verifier_construction() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
use super::super::crl::{new_shared_state, CrlState, CrlStatus};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
// Build a simple CA cert + key for the root store.
|
||||||
|
let ca_key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ECDSA_P256_SHA256).unwrap();
|
||||||
|
let mut ca_params = rcgen::CertificateParams::default();
|
||||||
|
ca_params.not_before = time::OffsetDateTime::now_utc();
|
||||||
|
ca_params.not_after = time::OffsetDateTime::now_utc() + time::Duration::days(365);
|
||||||
|
ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
|
||||||
|
ca_params.key_usages = vec![rcgen::KeyUsagePurpose::KeyCertSign];
|
||||||
|
let mut dn = rcgen::DistinguishedName::new();
|
||||||
|
dn.push(rcgen::DnType::CommonName, "Test CA for Verifier");
|
||||||
|
ca_params.distinguished_name = dn;
|
||||||
|
let ca_cert = ca_params.self_signed(&ca_key).unwrap();
|
||||||
|
|
||||||
|
// Build root cert store with the CA.
|
||||||
|
let mut root_store = RootCertStore::empty();
|
||||||
|
root_store.add(ca_cert.der().to_owned()).unwrap();
|
||||||
|
|
||||||
|
// Build WebPKI verifier — build() returns Arc<WebPkiClientVerifier>
|
||||||
|
// which coerces to Arc<dyn ClientCertVerifier>.
|
||||||
|
let webpki_verifier: Arc<dyn ClientCertVerifier> =
|
||||||
|
WebPkiClientVerifier::builder(root_store.into())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Build CRL state in Valid status.
|
||||||
|
let crl_state = new_shared_state();
|
||||||
|
let valid_state = CrlState {
|
||||||
|
status: CrlStatus::Valid,
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
crl_mtime: None,
|
||||||
|
loaded_at: std::time::SystemTime::now(),
|
||||||
|
};
|
||||||
|
crl_state.store(Arc::new(valid_state));
|
||||||
|
|
||||||
|
// Construct CrlAwareVerifier — should succeed.
|
||||||
|
let _verifier = CrlAwareVerifier::new(webpki_verifier, crl_state);
|
||||||
|
// If we reach here without panic, construction succeeded.
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test that CrlAwareVerifier with Missing CRL state can be constructed.
|
||||||
|
/// Missing CRL means the verifier falls back to WebPKI-only.
|
||||||
|
#[test]
|
||||||
|
fn crl_aware_verifier_with_missing_crl() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
use super::super::crl::new_shared_state;
|
||||||
|
|
||||||
|
let ca_key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ECDSA_P256_SHA256).unwrap();
|
||||||
|
let mut ca_params = rcgen::CertificateParams::default();
|
||||||
|
ca_params.not_before = time::OffsetDateTime::now_utc();
|
||||||
|
ca_params.not_after = time::OffsetDateTime::now_utc() + time::Duration::days(365);
|
||||||
|
ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
|
||||||
|
ca_params.key_usages = vec![rcgen::KeyUsagePurpose::KeyCertSign];
|
||||||
|
let mut dn = rcgen::DistinguishedName::new();
|
||||||
|
dn.push(rcgen::DnType::CommonName, "Test CA for Verifier");
|
||||||
|
ca_params.distinguished_name = dn;
|
||||||
|
let ca_cert = ca_params.self_signed(&ca_key).unwrap();
|
||||||
|
|
||||||
|
let mut root_store = RootCertStore::empty();
|
||||||
|
root_store.add(ca_cert.der().to_owned()).unwrap();
|
||||||
|
|
||||||
|
let webpki_verifier: Arc<dyn ClientCertVerifier> =
|
||||||
|
WebPkiClientVerifier::builder(root_store.into())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Default state is Missing.
|
||||||
|
let crl_state = new_shared_state();
|
||||||
|
let _verifier = CrlAwareVerifier::new(webpki_verifier, crl_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test that CrlAwareVerifier with Invalid CRL state can be constructed.
|
||||||
|
/// Invalid CRL means the verifier should reject ALL client certificates.
|
||||||
|
#[test]
|
||||||
|
fn crl_aware_verifier_with_invalid_crl() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
use super::super::crl::{new_shared_state, CrlState, CrlStatus};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
let ca_key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ECDSA_P256_SHA256).unwrap();
|
||||||
|
let mut ca_params = rcgen::CertificateParams::default();
|
||||||
|
ca_params.not_before = time::OffsetDateTime::now_utc();
|
||||||
|
ca_params.not_after = time::OffsetDateTime::now_utc() + time::Duration::days(365);
|
||||||
|
ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
|
||||||
|
ca_params.key_usages = vec![rcgen::KeyUsagePurpose::KeyCertSign];
|
||||||
|
let mut dn = rcgen::DistinguishedName::new();
|
||||||
|
dn.push(rcgen::DnType::CommonName, "Test CA for Verifier");
|
||||||
|
ca_params.distinguished_name = dn;
|
||||||
|
let ca_cert = ca_params.self_signed(&ca_key).unwrap();
|
||||||
|
|
||||||
|
let mut root_store = RootCertStore::empty();
|
||||||
|
root_store.add(ca_cert.der().to_owned()).unwrap();
|
||||||
|
|
||||||
|
let webpki_verifier: Arc<dyn ClientCertVerifier> =
|
||||||
|
WebPkiClientVerifier::builder(root_store.into())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let crl_state = new_shared_state();
|
||||||
|
let invalid_state = CrlState {
|
||||||
|
status: CrlStatus::Invalid,
|
||||||
|
revoked_serials: HashSet::new(),
|
||||||
|
crl_mtime: None,
|
||||||
|
loaded_at: std::time::SystemTime::now(),
|
||||||
|
};
|
||||||
|
crl_state.store(Arc::new(invalid_state));
|
||||||
|
|
||||||
|
let _verifier = CrlAwareVerifier::new(webpki_verifier, crl_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test that CrlAwareVerifier with a revoked serial in Valid CRL state
|
||||||
|
/// can be constructed. The actual verification logic is tested through
|
||||||
|
/// integration tests since it requires a full TLS handshake.
|
||||||
|
#[test]
|
||||||
|
fn crl_aware_verifier_with_revoked_serial() {
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
use super::super::crl::{new_shared_state, CrlState, CrlStatus};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
let ca_key = rcgen::KeyPair::generate_for(&rcgen::PKCS_ECDSA_P256_SHA256).unwrap();
|
||||||
|
let mut ca_params = rcgen::CertificateParams::default();
|
||||||
|
ca_params.not_before = time::OffsetDateTime::now_utc();
|
||||||
|
ca_params.not_after = time::OffsetDateTime::now_utc() + time::Duration::days(365);
|
||||||
|
ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
|
||||||
|
ca_params.key_usages = vec![rcgen::KeyUsagePurpose::KeyCertSign];
|
||||||
|
let mut dn = rcgen::DistinguishedName::new();
|
||||||
|
dn.push(rcgen::DnType::CommonName, "Test CA for Verifier");
|
||||||
|
ca_params.distinguished_name = dn;
|
||||||
|
let ca_cert = ca_params.self_signed(&ca_key).unwrap();
|
||||||
|
|
||||||
|
let mut root_store = RootCertStore::empty();
|
||||||
|
root_store.add(ca_cert.der().to_owned()).unwrap();
|
||||||
|
|
||||||
|
let webpki_verifier: Arc<dyn ClientCertVerifier> =
|
||||||
|
WebPkiClientVerifier::builder(root_store.into())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let crl_state = new_shared_state();
|
||||||
|
let mut revoked = HashSet::new();
|
||||||
|
revoked.insert("deadbeef".to_string());
|
||||||
|
let valid_with_revoked = CrlState {
|
||||||
|
status: CrlStatus::Valid,
|
||||||
|
revoked_serials: revoked,
|
||||||
|
crl_mtime: None,
|
||||||
|
loaded_at: std::time::SystemTime::now(),
|
||||||
|
};
|
||||||
|
crl_state.store(Arc::new(valid_with_revoked));
|
||||||
|
|
||||||
|
let _verifier = CrlAwareVerifier::new(webpki_verifier, crl_state);
|
||||||
|
// Construction succeeded — the verifier is ready to reject revoked certs.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,6 +35,14 @@ pub struct TlsConfig {
|
|||||||
pub server_key: String,
|
pub server_key: String,
|
||||||
#[serde(default = "default_tls_version")]
|
#[serde(default = "default_tls_version")]
|
||||||
pub min_tls_version: String,
|
pub min_tls_version: String,
|
||||||
|
/// Path to persist the CRL fetched from the manager.
|
||||||
|
/// Defaults to /etc/linux_patch_api/certs/crl.pem
|
||||||
|
#[serde(default = "default_crl_path")]
|
||||||
|
pub crl_path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_crl_path() -> String {
|
||||||
|
"/etc/linux_patch_api/certs/crl.pem".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_true() -> bool {
|
fn default_true() -> bool {
|
||||||
|
|||||||
52
src/main.rs
52
src/main.rs
@ -27,6 +27,7 @@ use std::sync::Arc;
|
|||||||
use tracing::{error, info, warn};
|
use tracing::{error, info, warn};
|
||||||
|
|
||||||
use linux_patch_api::api::{configure_api_routes, configure_health_route};
|
use linux_patch_api::api::{configure_api_routes, configure_health_route};
|
||||||
|
use linux_patch_api::auth::crl::{self, CrlStatus};
|
||||||
use linux_patch_api::auth::{mtls, MtlsMiddleware, WhitelistManager};
|
use linux_patch_api::auth::{mtls, MtlsMiddleware, WhitelistManager};
|
||||||
use linux_patch_api::config::loader::{validate_certs, CertStatus};
|
use linux_patch_api::config::loader::{validate_certs, CertStatus};
|
||||||
use linux_patch_api::enroll;
|
use linux_patch_api::enroll;
|
||||||
@ -297,6 +298,10 @@ async fn main() -> Result<()> {
|
|||||||
let cache_state = web::Data::new(PackageCacheState::new());
|
let cache_state = web::Data::new(PackageCacheState::new());
|
||||||
info!("Package cache state initialized");
|
info!("Package cache state initialized");
|
||||||
|
|
||||||
|
// Initialize shared CRL state (available even when TLS is off for health reporting)
|
||||||
|
let shared_crl_state = crl::new_shared_state();
|
||||||
|
let crl_state_data = web::Data::new(shared_crl_state.clone());
|
||||||
|
|
||||||
// Configure bind address
|
// Configure bind address
|
||||||
let bind_address = format!("{}:{}", config.server.bind, config.server.port);
|
let bind_address = format!("{}:{}", config.server.bind, config.server.port);
|
||||||
|
|
||||||
@ -306,7 +311,8 @@ async fn main() -> Result<()> {
|
|||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.app_data(job_manager_data.clone())
|
.app_data(job_manager_data.clone())
|
||||||
.app_data(backend_data.clone())
|
.app_data(backend_data.clone())
|
||||||
.app_data(cache_state.clone());
|
.app_data(cache_state.clone())
|
||||||
|
.app_data(crl_state_data.clone());
|
||||||
|
|
||||||
// Configure API routes
|
// Configure API routes
|
||||||
app = app.configure(|cfg| {
|
app = app.configure(|cfg| {
|
||||||
@ -345,6 +351,7 @@ async fn main() -> Result<()> {
|
|||||||
server_cert = %tls_config.server_cert,
|
server_cert = %tls_config.server_cert,
|
||||||
server_key = %tls_config.server_key,
|
server_key = %tls_config.server_key,
|
||||||
min_tls_version = %tls_config.min_tls_version,
|
min_tls_version = %tls_config.min_tls_version,
|
||||||
|
crl_path = %tls_config.crl_path,
|
||||||
"Initializing mTLS authentication with TLS binding"
|
"Initializing mTLS authentication with TLS binding"
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -355,11 +362,50 @@ async fn main() -> Result<()> {
|
|||||||
min_tls_version: tls_config.min_tls_version.clone(),
|
min_tls_version: tls_config.min_tls_version.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Load CRL from disk into the shared CRL state
|
||||||
|
let crl_path = std::path::PathBuf::from(&tls_config.crl_path);
|
||||||
|
let ca_cert_der = std::fs::read(&tls_config.ca_cert).unwrap_or_default();
|
||||||
|
|
||||||
|
// Load initial CRL from disk (missing is OK -- degraded mode)
|
||||||
|
let initial_crl = crl::load_crl(&crl_path, &ca_cert_der);
|
||||||
|
match initial_crl.status {
|
||||||
|
CrlStatus::Invalid => {
|
||||||
|
error!("CRL signature is invalid -- refusing to start (fail-closed)");
|
||||||
|
std::process::exit(ExitCode::Error as i32);
|
||||||
|
}
|
||||||
|
CrlStatus::Valid | CrlStatus::Expired => {
|
||||||
|
info!(
|
||||||
|
status = %initial_crl.status,
|
||||||
|
revoked = initial_crl.revoked_serials.len(),
|
||||||
|
"CRL loaded from disk"
|
||||||
|
);
|
||||||
|
shared_crl_state.store(std::sync::Arc::new(initial_crl));
|
||||||
|
}
|
||||||
|
CrlStatus::Missing => {
|
||||||
|
info!("No CRL on disk -- starting in WebPKI-only mode");
|
||||||
|
}
|
||||||
|
CrlStatus::Degraded => {
|
||||||
|
warn!("CRL load failed -- starting in degraded (WebPKI-only) mode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spawn CRL refresh background task if manager URL is configured
|
||||||
|
if let Some(manager_url) = config.enrollment_manager_url() {
|
||||||
|
crl::spawn_crl_refresh_task(
|
||||||
|
manager_url.to_string(),
|
||||||
|
crl_path,
|
||||||
|
ca_cert_der,
|
||||||
|
shared_crl_state.clone(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
info!("No manager URL configured -- CRL auto-refresh disabled");
|
||||||
|
}
|
||||||
|
|
||||||
match MtlsMiddleware::new(mtls_config.clone()) {
|
match MtlsMiddleware::new(mtls_config.clone()) {
|
||||||
Ok(middleware) => {
|
Ok(middleware) => {
|
||||||
// Build rustls server configuration
|
// Build rustls server configuration with CRL-aware verifier
|
||||||
let rustls_config = middleware
|
let rustls_config = middleware
|
||||||
.build_rustls_config()
|
.build_rustls_config(Some(shared_crl_state.clone()))
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to build rustls config: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("Failed to build rustls config: {}", e))?;
|
||||||
|
|
||||||
info!("mTLS middleware and rustls config initialized successfully");
|
info!("mTLS middleware and rustls config initialized successfully");
|
||||||
|
|||||||
@ -78,6 +78,7 @@ fn build_tls_config(cert_dir: &std::path::Path) -> TlsConfig {
|
|||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string(),
|
.to_string(),
|
||||||
min_tls_version: "1.3".to_string(),
|
min_tls_version: "1.3".to_string(),
|
||||||
|
crl_path: String::new(), // No CRL in E2E tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user