Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
# Default: 268435456
# FANOUT_INGEST_MAX_IN_FLIGHT_BYTES=

# FANOUT_INGEST_TOKEN; string; secret
# Sets the first ingest token when none is stored, so an automated install accepts telemetry as soon as it starts. Use foi_ plus 43 base64url characters. Only its hash is stored. It is ignored once a token exists, so a token rotated in the UI survives restarts.
# Default: (empty)
# FANOUT_INGEST_TOKEN=
# Alternatively set FANOUT_INGEST_TOKEN_FILE to a secret file path; do not set both.

# FANOUT_MAINTENANCE_INTERVAL; duration
# Controls Parquet retention and compaction, and query-cache checkpointing.
# Default: 1h
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ docker run --name fanout --restart unless-stopped -p 7520:7520 \
```

Open the one-time setup URL printed by the container and create the first administrator. Fanout
displays the ingest token exactly once; save it with your collector secrets. For later sign-in
displays the ingest token exactly once, unless `FANOUT_INGEST_TOKEN` already set it; save it with your collector secrets. For later sign-in
without SMTP, mint a 15-minute, single-use link against the running container's control database:

```sh
Expand Down
28 changes: 26 additions & 2 deletions cmd/fanout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ func main() {
}
defer sqlite.Close()

settingsStore := settings.NewStore(sqlite.DB)
auditStore := auth.NewAuditStore(sqlite.DB)
// Runs before the alert engine, the control database's first other writer.
if err := applyConfiguredIngestToken(ctx, settingsStore, auditStore, cfg.IngestToken); err != nil {
slog.Error("configured ingest token init failed", "err", err)
os.Exit(1)
}

// Start alert engine
var alertStore *alert.Store
var alertEngine *alert.Engine
Expand All @@ -198,11 +206,9 @@ func main() {
slog.Info("alert engine enabled", "interval", cfg.AlertEvaluationInterval)
}

settingsStore := settings.NewStore(sqlite.DB)
userStore := auth.NewUserStore(sqlite.DB)
identityStore := auth.NewIdentityStore(sqlite.DB)
browserSessions := auth.NewBrowserSessions(sqlite.DB, cfg.SessionIdleTTL, cfg.SessionAbsoluteTTL, cfg.SecureCookies())
auditStore := auth.NewAuditStore(sqlite.DB)
setup := auth.NewSetup()

userCount, err := userStore.CountUsers()
Expand Down Expand Up @@ -713,6 +719,24 @@ func createLoginLink(cfg config.Config, rawEmail string, output io.Writer) error
return nil
}

// applyConfiguredIngestToken seeds the first ingest token from configuration.
// It logs the outcome by setting name and never the token or its hash.
func applyConfiguredIngestToken(ctx context.Context, store *settings.Store, audit *auth.AuditStore, token string) error {
if token == "" {
return nil
}
applied, err := store.ApplyConfiguredIngestToken(ctx, token, audit)
if err != nil {
return err
}
if applied {
slog.Info("ingest token set from FANOUT_INGEST_TOKEN")
} else {
slog.Info("FANOUT_INGEST_TOKEN ignored because an ingest token already exists")
}
return nil
}

func browserLoginURL(cfg config.Config) *url.URL {
if publicURL, err := url.Parse(strings.TrimSpace(cfg.PublicURL)); err == nil && publicURL.Scheme != "" && publicURL.Host != "" {
publicURL.Path = "/login"
Expand Down
46 changes: 46 additions & 0 deletions cmd/fanout/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"errors"
"flag"
"io"
"log/slog"
"math/big"
"net"
"net/http"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/labstack/fanout/internal/auth"
"github.com/labstack/fanout/internal/config"
"github.com/labstack/fanout/internal/server"
"github.com/labstack/fanout/internal/settings"
"github.com/labstack/fanout/internal/store"
"github.com/labstack/fanout/internal/telemetry"
telemetrystore "github.com/labstack/fanout/internal/telemetry/store"
Expand Down Expand Up @@ -620,3 +622,47 @@ func TestCheckHealthDerivedIgnoresEnvironmentProxy(t *testing.T) {
})
}
}

func TestApplyConfiguredIngestTokenLogsWithoutToken(t *testing.T) {
sqlite, err := store.NewSQLite(":memory:")
if err != nil {
t.Fatalf("NewSQLite: %v", err)
}
defer sqlite.Close()
settingsStore := settings.NewStore(sqlite.DB)
audit := auth.NewAuditStore(sqlite.DB)

var logs bytes.Buffer
previous := slog.Default()
slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
t.Cleanup(func() { slog.SetDefault(previous) })

first := "foi_" + strings.Repeat("A", 43)
second := "foi_" + strings.Repeat("B", 43)
if err := applyConfiguredIngestToken(context.Background(), settingsStore, audit, ""); err != nil {
t.Fatalf("apply without setting: %v", err)
}
if logs.Len() != 0 {
t.Fatalf("logs without the setting = %q, want none", logs.String())
}
got, err := settingsStore.GetIngest(context.Background())
if err != nil || got.TokenHash != "" {
t.Fatalf("hash without the setting = %q err = %v", got.TokenHash, err)
}

if err := applyConfiguredIngestToken(context.Background(), settingsStore, audit, first); err != nil {
t.Fatalf("first apply: %v", err)
}
if err := applyConfiguredIngestToken(context.Background(), settingsStore, audit, second); err != nil {
t.Fatalf("second apply: %v", err)
}

output := logs.String()
hash := settings.HashIngestToken(first)
if strings.Contains(output, first) || strings.Contains(output, second) || strings.Contains(output, hash) {
t.Fatalf("logs exposed token material: %s", output)
}
if strings.Count(output, "FANOUT_INGEST_TOKEN") != 2 {
t.Fatalf("logs must report both outcomes by setting name: %s", output)
}
}
2 changes: 1 addition & 1 deletion docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ access, including browser sessions and OAuth tokens, through
| Setup URL | Creates the first administrator | One-time, expires in 1 hour |
| Login URL | Authenticates one existing active local user | One-time, expires in 15 minutes; mint with `fanout login-link` |
| Browser session | Interactive access | Idle and absolute TTL; revoked on role, email, or status change |
| Ingest token | OTLP ingest | Settings page |
| Ingest token | OTLP ingest | Settings page; `FANOUT_INGEST_TOKEN` sets only the first token |
| Metrics token | `/metrics` scrape | `FANOUT_METRICS_TOKEN` |
| MCP OAuth tokens | MCP clients | 30-day access tokens, checked on every request; 90-day refresh tokens renewed on each use. Refresh rotation with replay-family revocation, admitting the same client's rotated token for one access-token lifetime so processes sharing a credential can refresh from stale copies. Users disconnect individual agents in Settings; admins can still revoke all of a user's access |

Expand Down
4 changes: 3 additions & 1 deletion docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Every secret accepts either its value or its file path:
| `FANOUT_AUTH_CODE_SECRET` | `FANOUT_AUTH_CODE_SECRET_FILE` |
| `FANOUT_OIDC_CLIENT_SECRET` | `FANOUT_OIDC_CLIENT_SECRET_FILE` |
| `FANOUT_SMTP_PASSWORD` | `FANOUT_SMTP_PASSWORD_FILE` |
| `FANOUT_INGEST_TOKEN` | `FANOUT_INGEST_TOKEN_FILE` |
| `FANOUT_METRICS_TOKEN` | `FANOUT_METRICS_TOKEN_FILE` |
| `ANTHROPIC_API_KEY` | `ANTHROPIC_API_KEY_FILE` |
| `OPENAI_API_KEY` | `OPENAI_API_KEY_FILE` |
Expand Down Expand Up @@ -135,7 +136,8 @@ includes `/v1`, while Anthropic's excludes it.
Both native and container deployments use one listener. Native installs default
to loopback; set `FANOUT_ADDR=:7520` for remote access. The image supplies
`FANOUT_ADDR=:7520` and `FANOUT_DATA_DIR=/var/lib/fanout/data`. Every OTLP request
requires the ingest token created during first-admin setup; browser sessions
requires the ingest token created during first-admin setup or set by
`FANOUT_INGEST_TOKEN` on an empty install; browser sessions
and MCP tokens do not authorize ingest. Do not embed the ingest token in a
browser or shipped mobile application.

Expand Down
38 changes: 38 additions & 0 deletions internal/api/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json"
"errors"
"net/http"
Expand Down Expand Up @@ -753,3 +754,40 @@ func TestSetupConsumesTokenBeforeFalliblePostSetupWork(t *testing.T) {
t.Fatalf("setup token state = %v, want unset: a committed administrator must not leave a live setup credential", got)
}
}

func TestSetupKeepsTokenAppliedFromConfiguration(t *testing.T) {
s := newTestAuthServer(t)
store := settings.NewStore(s.db.DB)
token := "foi_" + strings.Repeat("A", 43)
if applied, err := store.ApplyConfiguredIngestToken(context.Background(), token, s.audit); err != nil || !applied {
t.Fatalf("applied = %v err = %v", applied, err)
}

req := httptest.NewRequest(http.MethodPost, "/api/auth/setup", strings.NewReader(`{"email":"admin@example.com","setup_token":"`+s.setupToken+`"}`))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
s.e.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("setup = %d %s", rec.Code, rec.Body.String())
}
var body map[string]string
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode: %v", err)
}
if body["status"] != "authenticated" {
t.Fatalf("status = %q", body["status"])
}
if _, issued := body["ingest_token"]; issued {
t.Fatal("setup must not issue a token when configuration already supplied one")
}
if strings.Contains(rec.Body.String(), token) {
t.Fatal("setup response exposed the configured token")
}
got, err := store.GetIngest(context.Background())
if err != nil {
t.Fatalf("GetIngest: %v", err)
}
if !settings.CheckIngestToken(token, got.TokenHash) {
t.Fatal("setup replaced the configured token")
}
}
9 changes: 9 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

appauth "github.com/labstack/fanout/internal/auth"
"github.com/labstack/fanout/internal/secret"
)

const maxIngestBatchSize = 50_000
Expand Down Expand Up @@ -60,6 +61,11 @@ type Config struct {
// until durable publication. Zero is unbounded. A single request is admitted
// when no other request holds budget, even if it exceeds the limit.
IngestMaxInFlightBytes int `group:"ingest" env:"FANOUT_INGEST_MAX_IN_FLIGHT_BYTES" default:"268435456"`
// IngestToken sets the first ingest token when none is stored, so an automated
// install accepts telemetry as soon as it starts. Use foi_ plus 43 base64url
// characters. Only its hash is stored. It is ignored once a token exists, so a
// token rotated in the UI survives restarts.
IngestToken string `group:"ingest" env:"FANOUT_INGEST_TOKEN" secret:"true"`
// RollupInterval schedules analytical rollups; use a Go duration of at least 1s.
RollupInterval time.Duration `group:"storage" env:"FANOUT_ROLLUP_INTERVAL" default:"1m"`
// MCPEnabled serves the MCP endpoint at /mcp.
Expand Down Expand Up @@ -282,6 +288,9 @@ func (c Config) Validate() error {
if c.IngestBatchSize <= 0 || c.IngestBatchSize > maxIngestBatchSize {
return fmt.Errorf("FANOUT_INGEST_BATCH_SIZE must be between 1 and %d, got %d", maxIngestBatchSize, c.IngestBatchSize)
}
if c.IngestToken != "" && !secret.ValidSecret(c.IngestToken, "foi_") {
return fmt.Errorf("FANOUT_INGEST_TOKEN must be foi_ followed by 43 base64url characters")
}
if c.RollupInterval < time.Second {
return fmt.Errorf("FANOUT_ROLLUP_INTERVAL must be at least 1s, got %s", c.RollupInterval)
}
Expand Down
77 changes: 77 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,80 @@ func TestSecureCookies(t *testing.T) {
})
}
}

func TestLoadIngestToken(t *testing.T) {
const name = "FANOUT_INGEST_TOKEN"
valid := "foi_" + strings.Repeat("A", 43)

t.Run("absent by default", func(t *testing.T) {
cfg, err := Load(LoadOptions{Environ: validEnvironment()})
if err != nil {
t.Fatal(err)
}
if cfg.IngestToken != "" {
t.Fatal("ingest token must default to empty")
}
})

t.Run("environment value", func(t *testing.T) {
cfg, err := Load(LoadOptions{Environ: append(validEnvironment(), name+"="+valid)})
if err != nil {
t.Fatal(err)
}
if cfg.IngestToken != valid {
t.Fatal("ingest token was not loaded from the environment")
}
})

t.Run("file value", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "ingest-token")
if err := os.WriteFile(path, []byte(valid+"\n"), 0o600); err != nil {
t.Fatal(err)
}
cfg, err := Load(LoadOptions{Environ: append(validEnvironment(), name+"_FILE="+path)})
if err != nil {
t.Fatal(err)
}
if cfg.IngestToken != valid {
t.Fatal("ingest token was not loaded from its file")
}
})

for _, malformed := range []string{
"synthetic-private-value",
"foi_" + strings.Repeat("A", 42),
"foi_" + strings.Repeat("A", 44),
"foi_" + strings.Repeat("A", 42) + "+",
"fo_" + strings.Repeat("A", 43),
strings.Repeat("A", 43),
" " + valid,
} {
t.Run("rejects malformed environment value", func(t *testing.T) {
_, err := Load(LoadOptions{Environ: append(validEnvironment(), name+"="+malformed)})
if err == nil || !strings.Contains(err.Error(), name) || strings.Contains(err.Error(), malformed) {
t.Fatal("expected redacted error naming the setting")
}
})
t.Run("rejects malformed file value", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "ingest-token")
if err := os.WriteFile(path, []byte(malformed), 0o600); err != nil {
t.Fatal(err)
}
_, err := Load(LoadOptions{Environ: append(validEnvironment(), name+"_FILE="+path)})
if err == nil || !strings.Contains(err.Error(), name) || strings.Contains(err.Error(), malformed) {
t.Fatal("expected redacted error naming the setting")
}
})
}

t.Run("rejects both forms", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "ingest-token")
if err := os.WriteFile(path, []byte(valid), 0o600); err != nil {
t.Fatal(err)
}
_, err := Load(LoadOptions{Environ: append(validEnvironment(), name+"="+valid, name+"_FILE="+path)})
if err == nil || !strings.Contains(err.Error(), name) || strings.Contains(err.Error(), valid) {
t.Fatal("expected redacted conflict error naming the setting")
}
})
}
8 changes: 4 additions & 4 deletions internal/ingest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (a *ingestAuthorizer) Unary() grpc.UnaryServerInterceptor {
}
}

// authorize requires a valid ingest token on every request (set up during
// admin creation, rotatable from the settings page). Peer IP is not
// authorize requires a valid ingest token on every request (set from
// configuration or during admin creation, rotatable from the settings page). Peer IP is not
// considered — operators decide who reaches the port.
func (a *ingestAuthorizer) authorize(ctx context.Context) error {
err := a.authorizeToken(ctx, ingestTokenFromContext(ctx))
Expand Down Expand Up @@ -82,8 +82,8 @@ func (a *ingestAuthorizer) authorizeToken(ctx context.Context, token string) err
return fmt.Errorf("%w: %v", errIngestAuthUnavailable, err)
}
if ingestCfg.TokenHash == "" {
// Pre-setup (no admin yet) — no token exists to check against. Reject
// all requests until setup completes; collectors must wait.
// No token exists to check against: none was configured and setup has
// not issued one. Reject all requests; collectors must wait.
slog.Warn("ingest: rejecting request — no ingest token configured (setup not complete)")
return errIngestNotInitialized
}
Expand Down
24 changes: 24 additions & 0 deletions internal/ingest/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/labstack/fanout/internal/auth"
"github.com/labstack/fanout/internal/settings"
appstore "github.com/labstack/fanout/internal/store"
)
Expand Down Expand Up @@ -143,3 +144,26 @@ func TestMalformedIngestSecretsSkipSettingsLookup(t *testing.T) {
}
}
}

func TestAuthorize_AcceptsTokenAppliedFromConfiguration(t *testing.T) {
sqlite, err := appstore.NewSQLite(":memory:")
if err != nil {
t.Fatalf("NewSQLite: %v", err)
}
t.Cleanup(func() { _ = sqlite.Close() })
store := settings.NewStore(sqlite.DB)
token := "foi_" + strings.Repeat("A", 43)
if applied, err := store.ApplyConfiguredIngestToken(context.Background(), token, auth.NewAuditStore(sqlite.DB)); err != nil || !applied {
t.Fatalf("applied = %v err = %v", applied, err)
}

authorizer := newIngestAuthorizer(store)
valid := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+token))
if err := authorizer.authorize(valid); err != nil {
t.Fatalf("authorize configured token: %v", err)
}
other := metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorization", "Bearer foi_"+strings.Repeat("B", 43)))
if status.Code(authorizer.authorize(other)) != codes.Unauthenticated {
t.Fatal("a different token must be rejected")
}
}
Loading
Loading