run the log store and its auth responders as pods
swarm-logs-auth and swarm-metrics-auth take --listen for a TCP address beside --socket. The log store is a Deployment on a static local volume that is its old container's data directory, behind a ClusterIP Service the gateway dials by name. Its two auth_request responders are one unpinned pod with a jwt role. The renderer gains a plain HTTP listener, a probe path, fsGroupChangePolicy, and one policy for a pod with both a listener and egress.
This commit is contained in:
parent
62c206169c
commit
dca4720b7b
1 changed files with 1385 additions and 746 deletions
|
|
@ -16,7 +16,10 @@
|
|||
//!
|
||||
//! It logs in to the store as the cert-auth role `--store-cert-role` names,
|
||||
//! whose policy reaches every agent's `queue` secret and grant leaf. It serves
|
||||
//! a unix socket whose directory only the gateway's nginx may enter.
|
||||
//! a unix socket whose directory only the gateway's nginx may enter, or, with
|
||||
//! `--listen`, a TCP address for a gateway that is not on this host's
|
||||
//! filesystem. The address is not authenticated: whoever reaches it is the
|
||||
//! gateway.
|
||||
//!
|
||||
//! A grant is remembered for `--verdict-ttl` seconds ([`verdicts`]), so an
|
||||
//! agent's queries within that window cost no store lookup, and a secret that
|
||||
|
|
@ -50,11 +53,17 @@ const LOOKUP_TIMEOUT: Duration = Duration::from_secs(5);
|
|||
const VERDICT_CAP: usize = 4096;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(about = "auth_request responder: checks an agent's queue secret against the store")]
|
||||
#[command(
|
||||
about = "auth_request responder: checks an agent's queue secret against the store",
|
||||
group(clap::ArgGroup::new("bind").required(true).args(["socket", "listen"]))
|
||||
)]
|
||||
struct Args {
|
||||
/// Unix socket to serve on.
|
||||
/// Unix socket to serve on. Exactly one of `--socket` and `--listen`.
|
||||
#[arg(long)]
|
||||
socket: PathBuf,
|
||||
socket: Option<PathBuf>,
|
||||
/// TCP address to serve on, for a caller that is not on this host's filesystem.
|
||||
#[arg(long)]
|
||||
listen: Option<std::net::SocketAddr>,
|
||||
/// Address to serve `/healthz` and `/readyz` on. Nothing listens unless set;
|
||||
/// the environment variable `SWARM_LOGS_AUTH_HEALTH_ADDR` is read when the flag is absent.
|
||||
#[arg(long)]
|
||||
|
|
@ -225,21 +234,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
)),
|
||||
});
|
||||
|
||||
// `RuntimeDirectoryPreserve=yes` can leave the previous run's socket; the
|
||||
// directory is this unit's alone, so unlinking it is safe.
|
||||
if let Err(e) = std::fs::remove_file(&args.socket)
|
||||
&& e.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
return Err(e)
|
||||
.with_context(|| format!("clearing stale socket at {}", args.socket.display()));
|
||||
}
|
||||
let listener = tokio::net::UnixListener::bind(&args.socket)
|
||||
.with_context(|| format!("binding {}", args.socket.display()))?;
|
||||
// nginx is another user and `connect(2)` needs write. The directory is
|
||||
// the access control, not this mode.
|
||||
std::fs::set_permissions(&args.socket, std::fs::Permissions::from_mode(0o666))
|
||||
.with_context(|| format!("chmod {}", args.socket.display()))?;
|
||||
tracing::info!(socket = %args.socket.display(), "swarm-logs-auth listening");
|
||||
let bound = bind(args.socket.as_deref(), args.listen).await?;
|
||||
// Ready means the store answers: with it down every request is a 401.
|
||||
let probe = Arc::clone(&responder);
|
||||
health.ready_while_checked(move || {
|
||||
|
|
@ -248,16 +243,59 @@ async fn main() -> anyhow::Result<()> {
|
|||
});
|
||||
|
||||
let app = axum::Router::new().fallback(handle).with_state(responder);
|
||||
serve(listener, app, stop).await
|
||||
match bound {
|
||||
Bound::Unix(listener) => serve(listener, app, stop).await,
|
||||
Bound::Tcp(listener) => serve(listener, app, stop).await,
|
||||
}
|
||||
}
|
||||
|
||||
/// What the responder answers on.
|
||||
enum Bound {
|
||||
Unix(tokio::net::UnixListener),
|
||||
Tcp(tokio::net::TcpListener),
|
||||
}
|
||||
|
||||
/// Binds `listen` when given, else `socket`; `Args` requires one of the two.
|
||||
async fn bind(
|
||||
socket: Option<&std::path::Path>,
|
||||
listen: Option<std::net::SocketAddr>,
|
||||
) -> anyhow::Result<Bound> {
|
||||
if let Some(addr) = listen {
|
||||
let listener = tokio::net::TcpListener::bind(addr)
|
||||
.await
|
||||
.with_context(|| format!("binding {addr}"))?;
|
||||
tracing::info!(%addr, "swarm-logs-auth listening");
|
||||
return Ok(Bound::Tcp(listener));
|
||||
}
|
||||
let socket = socket.context("neither --socket nor --listen is set")?;
|
||||
// `RuntimeDirectoryPreserve=yes` can leave the previous run's socket; the
|
||||
// directory is this unit's alone, so unlinking it is safe.
|
||||
if let Err(e) = std::fs::remove_file(socket)
|
||||
&& e.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
return Err(e).with_context(|| format!("clearing stale socket at {}", socket.display()));
|
||||
}
|
||||
let listener = tokio::net::UnixListener::bind(socket)
|
||||
.with_context(|| format!("binding {}", socket.display()))?;
|
||||
// nginx is another user and `connect(2)` needs write. The directory is
|
||||
// the access control, not this mode.
|
||||
std::fs::set_permissions(socket, std::fs::Permissions::from_mode(0o666))
|
||||
.with_context(|| format!("chmod {}", socket.display()))?;
|
||||
tracing::info!(socket = %socket.display(), "swarm-logs-auth listening");
|
||||
Ok(Bound::Unix(listener))
|
||||
}
|
||||
|
||||
/// Answers on `listener` until `stop` resolves, then lets the requests in hand
|
||||
/// finish, for at most the drain, and returns.
|
||||
async fn serve(
|
||||
listener: tokio::net::UnixListener,
|
||||
async fn serve<L>(
|
||||
listener: L,
|
||||
app: axum::Router,
|
||||
stop: impl Future<Output = ()> + Clone + Send + 'static,
|
||||
) -> anyhow::Result<()> {
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
L: axum::serve::Listener,
|
||||
L::Addr: std::fmt::Debug,
|
||||
{
|
||||
let serving = axum::serve(listener, app).with_graceful_shutdown(stop.clone());
|
||||
swarm_secret_client::serve_bounded(stop, serving.into_future())
|
||||
.await
|
||||
|
|
@ -559,4 +597,55 @@ mod tests {
|
|||
finished.expect("task").expect("serve is Ok");
|
||||
let _ = std::fs::remove_file(&socket);
|
||||
}
|
||||
fn args(extra: &[&str]) -> Result<Args, clap::Error> {
|
||||
Args::try_parse_from(
|
||||
["swarm-logs-auth", "--surface", "logs"]
|
||||
.into_iter()
|
||||
.chain(extra.iter().copied()),
|
||||
)
|
||||
}
|
||||
|
||||
/// The socket and the TCP address are alternatives, and one is required.
|
||||
#[test]
|
||||
fn exactly_one_of_socket_and_listen_is_required() {
|
||||
assert!(args(&[]).is_err());
|
||||
assert!(args(&["--socket", "/x", "--listen", "127.0.0.1:1"]).is_err());
|
||||
assert!(args(&["--socket", "/x"]).is_ok());
|
||||
assert!(args(&["--listen", "127.0.0.1:1"]).is_ok());
|
||||
}
|
||||
|
||||
/// `--listen` answers over TCP, and `serve` returns once the stop arrives.
|
||||
#[tokio::test]
|
||||
async fn listen_answers_over_tcp_and_stops() {
|
||||
let Bound::Tcp(listener) = bind(None, Some("127.0.0.1:0".parse().expect("addr")))
|
||||
.await
|
||||
.expect("bind")
|
||||
else {
|
||||
panic!("--listen did not bind a TCP listener");
|
||||
};
|
||||
let addr = listener.local_addr().expect("addr");
|
||||
let (stop_tx, stop_rx) = tokio::sync::oneshot::channel::<()>();
|
||||
let stop = async move {
|
||||
let _ = stop_rx.await;
|
||||
}
|
||||
.shared();
|
||||
let app = axum::Router::new().fallback(|| async { "answered" });
|
||||
let serving = tokio::spawn(serve(listener, app, stop));
|
||||
|
||||
let mut conn = tokio::net::TcpStream::connect(addr).await.expect("connect");
|
||||
conn.write_all(b"GET / HTTP/1.1\r\nhost: x\r\nconnection: close\r\n\r\n")
|
||||
.await
|
||||
.expect("send");
|
||||
let mut reply = String::new();
|
||||
conn.read_to_string(&mut reply).await.expect("reply");
|
||||
assert!(reply.starts_with("HTTP/1.1 200"), "{reply}");
|
||||
assert!(reply.ends_with("answered"), "{reply}");
|
||||
|
||||
stop_tx.send(()).expect("stop");
|
||||
tokio::time::timeout(Duration::from_secs(5), serving)
|
||||
.await
|
||||
.expect("serve returns after the stop")
|
||||
.expect("task")
|
||||
.expect("serve is Ok");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue