feat: Add a tenant-listener identity source to demo multi-tenancy without moca

Abierto
#2,091 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
go

Línea de trabajo

Start from the pattern in core/listener/vmcontrol/registry.go and handler.go, which wrap the forward proxy's handler per listener, and from config.ListenerPlan in core/config/listenerplan.go. The work depends on unmerged issues #2086, #2087 and #2088, so confirm those land first. Done means the end-to-end test in core/listener/tenantlisten passes in the core module's CI with no moca code in the build.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Parent: #2042, step 5 of the build order (#2085 → #2087 → #2086 → #2088 → #2091 → #2089 → #2090). Needs #2087 (Binding, shared mode), #2086 (Binding.Refusal) and #2088 (credresolve). It needs no moca code and no moca decision.

Problem

After #2042, Cortex serves many tenants from one process, but the only identity source is the moca driver. Nobody can see the feature without a moca KVM host. And only one user has shaped the seams, so they may fit only that user.

Goal

A small, general identity source and a small, general resolver, so that anyone can show multi-tenancy on a laptop:

  • Two clients use two local proxy addresses.
  • Each client gets its own credential for the same host.
  • A header that a client sends never changes which tenant it is.

It is built before #2089 and #2090 on purpose. If it needs anything from the moca driver, a seam is wrong, and the fix goes into #2087, #2086 or #2088 while that is still cheap.

This driver is a demo and a test fixture, not a security boundary. Any local process can connect to any loopback port, so the port does not prove who the caller is (#2042, "Security model", G1).

Shared contract

This issue implements pipeline.Binding (Admit, Refusal) and registers a credresolve resolver, as #2042's "Shared contract" defines them. It turns on shared mode (Config.SharedMode). #2089 later adds Binding.Record, and gives this driver an empty Record.

Today

Read at Cortex main 7f3eec7 (2026-11-23).

What Where
The pattern to copy vmcontrol serves one http.Server for each listener, with a handler that wraps the forward proxy's handler (core/listener/vmcontrol/registry.go:115-127, handler.go:42-55, 189-222). After #2087 the wrapper stamps pipeline.WithBinding.
The forward proxy's handler forwardproxy.Server.Handler() (core/listener/forwardproxy/server.go:433)
A credential directory static-inject's FileResolver refuses any key that leaves the directory (core/plugins/staticinject/resolver.go). Its key_by: host keys on pctx.Host, which can be host:port (plugin.go:202, 269). Its old sources require a bearer from the client (:189-191) and answer 401 for a missing file (:207-209). source: resolver (#2088) does neither.
The listener plan config.ListenerPlan (core/config/listenerplan.go:20-51) feeds reserveListeners, reportListenFailure and agentop's port preflight
The moca channel It is turned on by the environment only (cmd/cortex/vmcontrol.go:46-50)
Where tests run The core module's CI runs go test ./... with no tags (ci.yaml:68). cmd/cortex tests run once per profile (ci.yaml:455-466), local included.
A loopback check httpx.LoopbackHost (core/listener/httpx/loopback.go:18)
Where listener config lives config.ListenerConfig (core/config/config.go:955)
Profiles with static-inject full, fullcpex, lite, envoy, hostegress (scripts/profile-tags/profiles.go:85, plus #2085). Not local.

Change

  1. A tenant-listener identity source in the general core, package core/listener/tenantlisten. Config:

    listener:
      roles: [forward]
      tenants:
        - name: alice
          addr: 127.0.0.1:47610
          credentials_dir: /path/to/alice
        - name: bob
          addr: 127.0.0.1:47611
          credentials_dir: /path/to/bob
    
    • For each entry, Cortex opens one listener and serves the forward proxy's handler on it, wrapped so that each request carries the tenant's Binding. Add the tenant addresses to config.ListenerPlan, so the existing reservation, error report and port preflight see them, and a clash with another listener is refused.
    • Admit returns Caller{SessionID: "tenant-<name>", Subject: <name>, Claims: {"tenant": <name>}}. It removes Proxy-Authorization and the session headers (a local copy of the session.*Header constants that vmcontrol strips. Do not import or move vmcontrol's unexported list), so headers never change the caller.
    • Because Subject is set, pctx.Identity is not nil (#2087). In this demo only static-inject runs, so nothing reads it. The doc says that tokenexchange, tokenexchangeaiac and opa would take the subject from it.
    • Refusal returns its input unchanged, so the client gets Cortex's own shape, plugin headers included.
    • cmd/cortex sets Config.SharedMode when listener.tenants is not empty, so every shared-mode rule of #2087 applies: no session store, no session API, no transparent listener, loopback addresses, and 403 no_binding on the TCP forward proxy.
    • Start-up refuses each of these, with an error that names the setting:
      • An addr that is not loopback.
      • Two entries with the same name or the same addr.
      • A name that does not match [a-z0-9-]+.
      • A credentials_dir that is not absolute or does not exist.
      • listener.tenants in envoy-sidecar mode (config.Validate).
      • listener.tenants together with MOCA_EGRESS_CONTROL_SOCK. One identity source at a time. This check is in cmd/cortex, because the moca channel is set by the environment only.
  2. A directory resolver, registered as credresolve.Register("tenant-dir", …). For a caller and a host, it reads the caller's tenant directory, with the same path rules as FileResolver:

    • A file named <host> is a credential for port 443. A file named <host>:<port> is a credential for that port. Each file gives one Credential{Name: <file name>, Port: <port>, PathPrefix: "/", Headers: [{Authorization, "Bearer <value>"}]}. The value is trimmed.
    • No file for the host returns an empty list with a TTL of 60 s, so the host stays an opaque tunnel. The process-wide cache of #2088 (credresolve.Get) holds it.
  3. No moca names. The driver uses only pipeline.Caller, pipeline.Binding, credresolve and Config.SharedMode. cmd/cortex sets SharedMode for it in the same places as for the moca driver (#2087 item 11). It imports nothing from core/listener/vmcontrol or cmd/cortex/vmcontrol*.go.

  4. The driver is compiled in every build and is inert when listener.tenants is empty. No preset sets it.

  5. A short doc, docs/multi-tenant-demo.md:

    • Build cmd/cortex with the full profile (local has no static-inject).
    • A config with two tenants, two credential directories, static-inject with source: resolver, resolver: tenant-dir, and tls_bridge with a CA from files (generate_ca is refused in shared mode). The clients trust that CA.
    • two HTTPS_PROXY values, and the commands that show each result in "Tests".

    The doc says that the session store is off in this mode, so agentop shows nothing during the demo. The results show in the echoed headers.

Not in scope

  • Real identity: OS users, Unix sockets with file permissions, mTLS client certificates. Each is a later identity source, if someone needs it.
  • Grants, Rebind and expiry (#2090).
  • Hot reload of listener.tenants. Every listener field needs a restart.

Tests

One end-to-end Go test in core/listener/tenantlisten. It imports core/plugins/staticinject directly, because the core module's CI runs with no tags. It builds the TLS bridge with a CA written to a temp dir, and a local TLS upstream (httptest) that echoes the request headers. The credential files are named 127.0.0.1:<upstream port>:

  • A request through alice's address gets alice's credential, and one through bob's gets bob's. The host is the same.
  • A host with no file in a tenant's directory stays an opaque tunnel: no interception and no credential.
  • A client that sends a session header, X-Forwarded-User or its own Authorization is still its listener's tenant. Its Authorization is replaced when a credential applies, and passes unchanged when none applies.
  • Plain HTTP to a host with a credential: 403 plaintext_credential, in JSON.
  • A request on the TCP forward proxy: 403 no_binding.
  • Across tenants: alice and bob in parallel, many requests each. Every echoed header belongs to the right tenant.
  • Each start-up refusal in item 1 fires once, with an error that names the setting.
  • A guard test: go list -deps ./core/listener/tenantlisten contains no vmcontrol package.

Done when

  • The test above passes in the core module's CI, with no moca code in the build.
  • The doc's commands show two tenants on one laptop with one Cortex process.
  • The driver needed no change to the moca driver. Any change to a seam is in the PR's description.
Lenguaje dominante
Go
Estrellas
31
Forks
49
Merge medio
9 h 43 min
PR fusionados (30 d)
367

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rossoctl/cortex

Todos los issues de rossoctl/cortex

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.