caelum0x a515e13dab feat(ledger-core): enforce tenant-scoped authorization on ledger-core's own gRPC surface
docs/THREAT-MODEL.md names this "the biggest one" sub-gap of item 1:
cli/tui/agent (and gateway's own forwarding, card-issuance,
webhook-ingestion, tenant-registry) all dial ledger-core directly,
bypassing gateway's internal/tenantauth entirely, so any operator or
compromised caller could act as any tenant_id against the system's
actual source of truth regardless of what gateway enforces upstream.

Add services/ledger-core/src/tenantauth.rs (authorize_tenant /
authorize_operator, checking x-api-key / authorization: Bearer headers
against a new src/authclient.rs that dials Auth's VerifyAPIKey/
VerifyJWT directly) and call it from every LedgerCore RPC handler.
RATION_LEDGER_REQUIRE_TENANT_AUTH defaults to true; ledger-core refuses
to start with it on but no AUTH_GRPC_ADDR configured.

Every direct ledger-core caller now mints and presents its own
operator-scoped credential: cli/tui/agent (reusing their existing
gateway credential), and new internal/authclient packages for gateway,
card-issuance, webhook-ingestion and tenant-registry.

Verified end-to-end against the live demo stack (seed, simulate-deposit,
status, and the agent's poll/bridge-funding loop all succeed with the
new enforcement on; zero Unauthenticated/PermissionDenied anywhere in
any service's logs).
2026-10-23 18:33:33 +03:00

89 lines
3.8 KiB
Rust

use std::path::PathBuf;
fn main() {
// --- Compile the C write-ahead log (services/wal) directly from its
// sources. We deliberately do NOT depend on services/wal's own
// Makefile/libwal.a output: compiling wal.c and crc32.c ourselves via
// the `cc` crate keeps this crate self-contained and buildable with
// nothing but `cargo build`.
let wal_dir = PathBuf::from("../wal");
let wal_c = wal_dir.join("wal.c");
let crc32_c = wal_dir.join("crc32.c");
cc::Build::new()
.file(&wal_c)
.file(&crc32_c)
.include(&wal_dir)
.flag_if_supported("-std=c11")
.warnings(true)
.compile("wal");
println!("cargo:rerun-if-changed={}", wal_c.display());
println!("cargo:rerun-if-changed={}", crc32_c.display());
println!("cargo:rerun-if-changed={}", wal_dir.join("wal.h").display());
println!(
"cargo:rerun-if-changed={}",
wal_dir.join("frame.h").display()
);
println!(
"cargo:rerun-if-changed={}",
wal_dir.join("crc32.h").display()
);
// --- Compile proto/*.proto with tonic-build. ledger-core only serves
// the LedgerCore service, but we compile every proto in the shared
// contracts directory (as common.proto and gateway.proto are part of
// the same package family and common.proto is imported by the
// others) so a single source of generated Rust types stays in sync
// with the Go stubs in pkg/pb.
let proto_dir = PathBuf::from("../../proto");
let mut proto_files: Vec<PathBuf> = std::fs::read_dir(&proto_dir)
.expect("failed to read proto/ directory — expected it next to services/")
.filter_map(|entry| entry.ok())
.map(|entry| entry.path())
.filter(|path| path.extension().and_then(|e| e.to_str()) == Some("proto"))
.collect();
proto_files.sort();
for proto_file in &proto_files {
println!("cargo:rerun-if-changed={}", proto_file.display());
}
// audit.proto and auth.proto are compiled in their own pass with
// client stubs on top of the usual server stubs: ledger-core is a
// *client* of both AuditLog (docs/SCALE-EXPANSION.md section 8,
// src/audit_mirror.rs) and Auth (docs/THREAT-MODEL.md's "Known
// gaps" item 1's "the biggest one" sub-gap, src/authclient.rs —
// ledger-core verifies every tenant-scoped caller's own credential
// directly, rather than trusting whatever's already passed
// gateway's tenantauth) in production, and never runs either as a
// server, but each module's own test suite stands up a fake server
// (implementing the generated server trait) to exercise the client
// against — so both sides are generated here, unlike every other
// proto file below, which only ever needs server stubs for the
// service(s) ledger-core actually implements.
let audit_proto = proto_dir.join("audit.proto");
let auth_proto = proto_dir.join("auth.proto");
let (client_protos, server_only_protos): (Vec<PathBuf>, Vec<PathBuf>) = proto_files
.into_iter()
.partition(|p| p == &audit_proto || p == &auth_proto);
assert!(
client_protos.contains(&audit_proto) && client_protos.contains(&auth_proto),
"expected both {} and {} to exist alongside the other proto/*.proto files",
audit_proto.display(),
auth_proto.display()
);
tonic_build::configure()
.build_server(true)
.build_client(false)
.compile_protos(&server_only_protos, std::slice::from_ref(&proto_dir))
.expect("failed to compile proto/*.proto (server-only stubs) with tonic-build");
tonic_build::configure()
.build_server(true)
.build_client(true)
.compile_protos(&client_protos, &[proto_dir])
.expect("failed to compile proto/audit.proto and proto/auth.proto (client+server stubs) with tonic-build");
}