diff --git a/README.md b/README.md index 4f56ad9..6db935f 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ idle keep-alive connections close after 120 s. (for example one with per-record acks). `AUTH_HEADER` gates who may inject records — set it (the chart wires it from the -same Secret as the API's webhook token). Without it the relay accepts any POST. +same Secret as the API's webhook token). Without it the relay refuses to start +unless `ALLOW_UNAUTHENTICATED=true` is set, which accepts any POST. ## Configuration (environment variables) @@ -73,7 +74,8 @@ same Secret as the API's webhook token). Without it the relay accepts any POST. | `FACILITY` | `local0` | `kern`/`user`/`daemon`/`auth`/…/`local0`–`local7` | | `SEVERITY` | `info` | `emerg`/`alert`/`crit`/`err`/`warning`/`notice`/`info`/`debug` | | `SYSLOG_HOSTNAME` | OS hostname | RFC 5424 `HOSTNAME` field | -| `AUTH_HEADER` | — | if set, inbound requests must carry this exact `Authorization` header | +| `AUTH_HEADER` | — | required unless `ALLOW_UNAUTHENTICATED=true`; inbound requests must carry this exact `Authorization` header | +| `ALLOW_UNAUTHENTICATED` | — | `true` lets the relay start without `AUTH_HEADER` and accept any POST | ## Run via the Helm chart diff --git a/bridge_test.go b/bridge_test.go index e0de81c..492d21a 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -1006,3 +1006,26 @@ func TestForwarderSendHonoursContextWhileBusy(t *testing.T) { t.Fatalf("send error = %v, want one wrapping context.Canceled", err) } } + +func TestRequireAuth(t *testing.T) { + if err := requireAuth(config{}); err == nil { + t.Fatal("started without AUTH_HEADER and without ALLOW_UNAUTHENTICATED") + } + if err := requireAuth(config{authHeader: "Bearer x"}); err != nil { + t.Fatalf("rejected a configured token: %v", err) + } + if err := requireAuth(config{allowUnauth: true}); err != nil { + t.Fatalf("rejected an explicit opt-in: %v", err) + } +} + +func TestLoadConfig_AllowUnauthenticated(t *testing.T) { + t.Setenv("ALLOW_UNAUTHENTICATED", "true") + if !loadConfig().allowUnauth { + t.Fatal("ALLOW_UNAUTHENTICATED=true not read") + } + t.Setenv("ALLOW_UNAUTHENTICATED", "yes") + if loadConfig().allowUnauth { + t.Fatal("only the exact value true may open the intake") + } +} diff --git a/main.go b/main.go index 392bb0e..04752f7 100644 --- a/main.go +++ b/main.go @@ -102,15 +102,18 @@ const ( const livenessProbe = 5 * time.Millisecond type config struct { - listen string - syslogAddr string - network string // tcp | udp - useTLS bool - appName string - facility string - severity string - hostname string - authHeader string + listen string + syslogAddr string + network string // tcp | udp + useTLS bool + appName string + facility string + severity string + hostname string + authHeader string + // allowUnauth keeps the old open intake when AUTH_HEADER is empty. It + // must be asked for explicitly (ALLOW_UNAUTHENTICATED=true). + allowUnauth bool dialTimeout time.Duration readTimeout time.Duration } @@ -126,6 +129,7 @@ func loadConfig() config { severity: envOr("SEVERITY", "info"), hostname: envOr("SYSLOG_HOSTNAME", ""), authHeader: envOr("AUTH_HEADER", ""), + allowUnauth: envOr("ALLOW_UNAUTHENTICATED", "") == "true", dialTimeout: 5 * time.Second, readTimeout: defaultReadTimeout, } @@ -427,11 +431,27 @@ func (f *forwarder) write(frame []byte) error { // run wires OS signal handling to serve. main's thin wrapper; the testable // lifecycle lives in serve. func run(cfg config) error { + if err := requireAuth(cfg); err != nil { + return err + } ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() return serve(ctx, cfg) } +// requireAuth refuses to start an intake anyone in the cluster could post +// forged audit records to: AUTH_HEADER must be set unless the operator opts in +// to an unauthenticated relay with ALLOW_UNAUTHENTICATED=true. +func requireAuth(cfg config) error { + if cfg.authHeader == "" && !cfg.allowUnauth { + return errors.New("AUTH_HEADER is required; set ALLOW_UNAUTHENTICATED=true to accept unauthenticated POSTs") + } + if cfg.authHeader == "" { + slog.Warn("AUTH_HEADER is not set: accepting unauthenticated POSTs because ALLOW_UNAUTHENTICATED=true") + } + return nil +} + // serve builds the relay and runs the HTTP server until ctx is cancelled (then // it drains with a 5s deadline) or ListenAndServe fails. func serve(ctx context.Context, cfg config) error { diff --git a/specs.md b/specs.md index db3c132..14f194a 100644 --- a/specs.md +++ b/specs.md @@ -42,7 +42,7 @@ Single flat package (`main`): `main.go` (relay + config + server logic), `bridge - TLS: wraps TCP in verified TLS 1.2+; `SYSLOG_TLS=true` (requires `SYSLOG_NETWORK=tcp`) **Auth:** -- Optional bearer token: if `AUTH_HEADER` environment variable is set, the HTTP request `Authorization` header must match exactly (constant-time comparison); absence of the env var = any POST accepted +- Optional bearer token: if `AUTH_HEADER` environment variable is set, the HTTP request `Authorization` header must match exactly (constant-time comparison of SHA-256 digests); without it the relay refuses to start unless `ALLOW_UNAUTHENTICATED=true`, which accepts any POST ## Key invariants @@ -61,7 +61,7 @@ Single flat package (`main`): `main.go` (relay + config + server logic), `bridge ## Security considerations -- **Auth boundary:** optional bearer token (`AUTH_HEADER` env var) gates who may inject records (constant-time comparison; absence = open) +- **Auth boundary:** optional bearer token (`AUTH_HEADER` env var) gates who may inject records (constant-time comparison; required unless `ALLOW_UNAUTHENTICATED=true` opts in to an open intake) - **TLS transport:** `SYSLOG_TLS=true` wraps the TCP connection in verified TLS 1.2+, protecting audit events in transit - **Trust boundary:** API ← audit-syslog-bridge ← SIEM/syslog collector. The API rate-limits webhook submissions; the bridge forwards verbatim and is stateless, so it does not amplify or replay events - **DoS mitigation:** inbound body capped at 64 KiB; each request must arrive in full (headers and body) within 15 s (`http.Server.ReadTimeout`, with a 10 s header bound) and idle keep-alive connections close after 120 s; the per-frame write deadline bounds the collector side, so a misbehaving client/collector cannot wedge the process