Skip to main content

Gateway

selvara-gateway is a small service next to the dashboard. It holds the keys the dashboard must not hold, signs on the dashboard's request within limits of its own, and relays tunnel traffic between technicians and hosts.

What it holds and why​

The dashboard is a web application with a database, a login form and an API. If someone takes it over, they should not be able to run arbitrary commands on every host it manages. So the keys that hosts trust do not live there:

KeyFile in the data directoryWhat trusts it
Job-signing key (Ed25519)job-key.pemEvery agent. Its public half is pinned in each host's config.yaml as job_key at installation; an agent runs no job and opens no session whose signature does not verify against it.
SSH user CAssh-user-casshd on hosts that accept Selvara user certificates.
SSH host CAssh-host-caSSH clients that verify hosts by Selvara host certificates.
Relay token keyrelay-token.keyOnly the gateway itself, to check the tokens tunnel clients present.

The gateway generates each of these on its first start, with mode 0600, in the data directory (/data in the container). They never leave it: the dashboard only ever gets signatures and public keys.

Before it signs anything, the gateway applies its own checks, and the dashboard cannot change them:

  • Jobs — only job types on its allowlist (GATEWAY_JOB_TYPES), valid for at most 7 days, with an issuedAt within 5 minutes of the gateway's clock so nothing can be signed now for use next month.
  • Host policies — signed only when GATEWAY_REMOTE_ACTIONS is true; the actions must be known ones, the version positive, and a policy is signed under a type of its own that no host accepts as a job (and the job endpoint refuses to sign it). Each one is logged like a job, with the account that asked.
  • Rates — at most GATEWAY_RATE_PER_SYSTEM signatures per minute for one host or one account, and GATEWAY_RATE_GLOBAL for all of them together. A burst beyond that is refused with HTTP 429, not queued.
  • User certificates — valid for at most 8 hours, the key id is the Selvara account id (one token, at most 128 characters), no critical options, and only the permit-pty and permit-port-forwarding extensions. The principals are plain login names, at most 8, and never root or Administrator unless GATEWAY_DENIED_PRINCIPALS says so. The request names the host, and each principal is signed as <login>@<system id>: the host accepts only its own id (see SSH CA), so a certificate issued for one host logs in nowhere else. The same list of denied names applies to the local logins of an sshca.install principal map, so a map cannot let another principal log in as root.
  • Host certificates — valid for 90 days, for at most 16 exact DNS names or IP addresses. A name with a pattern character (*, ?, [, !) is refused, because OpenSSH reads a host certificate's principals as patterns and a wildcard would vouch for every host it matches. GATEWAY_HOST_DOMAINS narrows the names further to the domains your hosts live in.
  • Tunnels and desktop sessions — at most 8 hours per session.

Tunnel grants (session.tunnel) are signed only by the tunnel endpoint, and desktop grants (session.desktop) only by the desktop endpoint, never as ordinary jobs, so they always carry a session the relay knows about.

The gateway's own log​

Every signature and every refusal is appended to audit.jsonl in the data directory, one JSON object per line: when, what (job, user-cert, host-cert, tunnel, desktop, relay), the outcome (signed, refused, opened, closed), the reason for a refusal, the requesting address and the account the dashboard named, and the details — job id and type, certificate serial and principals, tunnel target. A line is written and synced to disk before the signature it records is handed out; if the log cannot be written, the gateway signs nothing, and it relays no tunnel it could not record opening.

Anyone who can reach the relay may send it a bad token, so those failures are not all logged: the relay endpoints allow 120 requests a minute per address, and unauthenticated refusals add at most one line per address and minute, and 60 a minute in all. The address is the connection's own, or, for a connection from a proxy named in GATEWAY_TRUSTED_PROXIES, the last X-Forwarded-For entry that proxy appended; from anyone else the header is ignored, so a caller cannot give every request an address of its own. A flood therefore cannot fill the volume and, with it, stop every signature.

{"time":"2026-10-03T12:00:00Z","operation":"job","outcome":"signed","remote":"172.20.0.4:51234","requestedBy":"acc_1","systemId":"sys_42","detail":{"jobId":"job_9","type":"patch.apply","issuedAt":"2026-10-03T12:00:00Z","expiresAt":"2026-10-04T12:00:00Z"}}

The dashboard keeps an audit log of its own. This one is the record a compromised dashboard cannot edit. Read it with:

docker run --rm -v selvara_gateway_data:/data:ro alpine tail -n 50 /data/audit.jsonl

Every line carries prev, the SHA-256 of the line before it. A line changed or removed in the middle breaks the chain from there on, and

docker compose exec gateway /usr/local/bin/selvara-gateway -verify-audit

reads the whole log and says where it breaks. A log written before the chain existed is accepted up to its last old line. Cutting lines off the end leaves a valid chain, so the gateway also prints the hash of the last line when it starts and stops (audit log head ... in the container log): keep the container log somewhere the data volume's owner cannot edit, and compare. The log is not rotated; plan the volume's size for it.

Deployment​

The shipped docker-compose.yml runs it as the gateway service from the image ghcr.io/daniel-hiller/selvara-gateway, built from deploy/gateway.Dockerfile. The image is a static binary on a distroless base, runs as an unprivileged user and keeps its data on the gateway_data volume.

The dashboard talks to it over the internal Compose network at http://gateway:8080, authenticated by a token both share. Generate it once and add it to .env:

echo "GATEWAY_TOKEN=$(openssl rand -base64 48)" >> .env
echo "GATEWAY_PUBLIC_URL=https://gateway.example.com" >> .env
docker compose up -d

Compose refuses to start without GATEWAY_TOKEN; the gateway itself refuses a token shorter than 32 characters. The app waits for the gateway to be healthy before it starts.

The first start logs the files it created and the job key:

gateway: created job-key.pem in /data
gateway: created ssh-user-ca in /data
gateway: created ssh-host-ca in /data
gateway: created relay-token.key in /data
gateway: job key 9Hk2…=

A later start that logs created job-key.pem again means the data directory was lost or not mounted. Every host pinned to the old key will refuse what the new one signs — stop and restore the backup instead of carrying on.

Settings​

VariablePurposeDefault
GATEWAY_TOKENShared with the dashboard; every call to the signing API must present it as bearer token. At least 32 characters.none, required
GATEWAY_PUBLIC_URLPublic base URL of the relay, e.g. https://gateway.example.com. Tunnel clients and agents connect there.unset: no tunnel can be opened
GATEWAY_JOB_TYPESComma-separated job types the gateway signs: exact names, or a prefix ending in .*.every job type of the actions an agent knows, and session.terminal
GATEWAY_RATE_PER_SYSTEMSignatures per minute for one host or account.30
GATEWAY_RATE_GLOBALSignatures per minute in total.600
GATEWAY_HOST_DOMAINSComma-separated domains host certificates may be issued for, e.g. corp.example,lab.example. IP addresses are not limited.unset: any exact name
GATEWAY_DENIED_PRINCIPALSComma-separated login names no user certificate is issued for; none lifts the restriction.root,Administrator
GATEWAY_TRUSTED_PROXIESComma-separated addresses or networks of the reverse proxies in front of the gateway, e.g. 10.0.0.5,172.18.0.0/16. Only their X-Forwarded-For is believed for the relay's rate limits. Set it when a proxy terminates TLS, or every client shares the proxy's limit.unset: the connection's address
GATEWAY_REMOTE_ACTIONStrue lets the gateway sign host policies, so that the dashboard can decide which actions hosts allow whose file has no actions key; see Agent Settings. Read only here, never from the dashboard. Anything but true leaves it off.unset: off
GATEWAY_DATA_DIRWhere keys and log live./data
GATEWAY_LISTENListen address.:8080

Narrowing GATEWAY_JOB_TYPES is how an instance that only patches makes sure no one can ever have a script signed, whatever happens to the dashboard: GATEWAY_JOB_TYPES=patch.*,reboot.*.

The public keys​

Hosts pin the job key, and sshd trusts the user CA. The dashboard reads all three public keys from the gateway; to read them yourself:

docker exec selvara-app sh -c \
'wget -qO- --header "Authorization: Bearer $GATEWAY_TOKEN" http://gateway:8080/v1/keys'
{"jobKey":"9Hk2…=","userCaKey":"ssh-ed25519 AAAA…","hostCaKey":"ssh-ed25519 AAAA…"}

The relay​

A tunnel connects a technician's machine to an address behind an agent, for example a customer's RDP server. Neither side can reach the other, so both connect outwards to the gateway:

  1. The dashboard asks the gateway for a tunnel token. The gateway signs a session grant naming the host, the target and the end of the session, and returns it together with a client token that is valid for two minutes.
  2. The dashboard sends the grant to the agent over its live link, and the client token to the technician's client.
  3. Both connect to the relay over WebSocket — the client to /v1/relay/client with its token, the agent to /v1/relay/agent with the grant. The agent checks the target against its own tunnel.allow first. (For a session through a host's own agent the grant is a desktop grant: the agent checks that it allows desktop and dials its own loopback; see the desktop endpoint.)
  4. The gateway pairs them by session and copies bytes both ways.

A session ends when either side closes, after 15 minutes without traffic, or at the grant's end, 8 hours at most, unless the dashboard removed that limit for it. A side whose counterpart does not arrive within 60 seconds is closed. Every session is relayed once: neither the token nor the grant opens a second connection.

The desktop endpoint​

A session through a host's own agent (the agent's desktop action) is signed by an endpoint of its own, POST /v1/desktop/token, so that its audit entries and its rate limit stay apart from tunnels'. The request names the session, the host, the account, the protocol (rdp, vnc, ssh or web), a port and a validity, and no host: the gateway writes 127.0.0.1 into the signed grant itself, and a host sent anyway is ignored. The answer has the same shape as a tunnel token. The gateway refuses a request for another protocol, a port outside 1 to 65535 or a validity beyond the eight-hour ceiling, signs a grant of type session.desktop that the job endpoint refuses to sign as an ordinary job, and logs every grant with the operation desktop and the protocol and port. It has its own per-host rate bucket and shares the global budget with the other endpoints.

The dashboard and the gateway ship together. A gateway from before 3.4.0 answers 404 on this path, which the dashboard shows as desktop_not_allowed; update both (Upgrading).

Removing the time limit​

When a person removes the time limit of a remote desktop or SSH session in the browser (Remote Desktop), the dashboard calls POST /v1/desktop/lift with the session and the account. The gateway then stops cutting that relay at its end; the 15-minute idle rule and either side closing still apply. It answers 404 session_not_relayed for a session that is not being relayed right now, and logs every lift with the operation relay.lift. A gateway without the endpoint answers 404 and the limit stays.

Reverse proxy​

Agents and technicians must reach the relay from outside; nothing else of the gateway should be reachable from there. The signing API is only for the app, which reaches it over the Compose network. The compose file publishes the gateway on 127.0.0.1:3100; give it its own name and forward only the relay paths.

Caddy:

gateway.example.com {
handle /v1/relay/* {
reverse_proxy 127.0.0.1:3100
}
handle {
respond 404
}
}

nginx — the read timeout must exceed the relay's 15-minute idle limit, or the proxy ends quiet sessions first:

location /v1/relay/ {
proxy_pass http://127.0.0.1:3100;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_read_timeout 20m;
proxy_send_timeout 20m;
}
location / {
return 404;
}

($connection_upgrade is the map shown in Installation.)

Set GATEWAY_PUBLIC_URL to the address this proxy serves. The gateway writes it into every grant and token, so a wrong value shows up as tunnels that never connect, not as an error at start.

Backups​

Back up the gateway_data volume. It is not in the database dump, and it cannot be recreated: a new job key means editing job_key in the config.yaml of every host, and a new user CA means redistributing the CA to every sshd that trusts it. Losing the directory means re-pinning every host.

docker run --rm -v selvara_gateway_data:/data:ro -v "$PWD":/backup alpine \
tar czf /backup/selvara-gateway-$(date +%F).tgz -C /data .

The keys only change if you delete them, so one copy taken after the first start covers them; back up again for the log. Keep the archive encrypted and away from the database dumps — anyone holding it can sign jobs every host will run.

To restore, onto an empty volume, before the gateway starts for the first time:

docker compose create gateway
docker run --rm -v selvara_gateway_data:/data -v "$PWD":/backup alpine \
sh -c 'tar xzf /backup/selvara-gateway-2026-10-03.tgz -C /data && chown -R 65532:65532 /data && chmod 700 /data && chmod 600 /data/*'
docker compose up -d

The start must not log created job-key.pem; if it does, the archive did not land in the volume.

Rotating the token​

Change GATEWAY_TOKEN in .env and recreate both containers: docker compose up -d app gateway. Calls in flight during the switch fail and are refused rather than signed.