fix(relay): support --enroll-method in systemd install#262
fix(relay): support --enroll-method in systemd install#262AhmedAburady wants to merge 2 commits into
Conversation
relay systemd install only wrote INFISICAL_TOKEN, so a systemd-managed relay always fell back to legacy machine-identity auth. Add --enroll-method (token|aws) and --relay-id to the install command, write the matching enrollment env vars to relay.conf, and add an INFISICAL_RELAY_ENROLLMENT_TOKEN env fallback in relay start so the flagless systemd invocation can read the token. Legacy behavior is unchanged when --enroll-method is omitted.
|
| Filename | Overview |
|---|---|
| packages/cmd/relay.go | Adds --enroll-method and --relay-id flags to relay systemd install and adds env fallback for the enrollment token in relay start; has a bug where INFISICAL_TOKEN is silently used as the enrollment token when --enroll-method=token and the flag is omitted |
| packages/relay/systemd.go | Extends InstallRelaySystemdService to write INFISICAL_RELAY_ENROLL_METHOD and corresponding credential vars; relay.conf is written with 0600 permissions; logic mirrors the cmd-layer switch cleanly |
| packages/relay/enroll.go | Adds INFISICAL_RELAY_ENROLL_METHOD_KEY constant and aligns spacing; clean, no issues |
Comments Outside Diff (1)
-
packages/cmd/relay.go, line 294-352 (link)Wrong token type silently accepted when
INFISICAL_TOKENis set in the environmentThe
tokenvariable is read unconditionally from the--tokenflag withINFISICAL_TOKENas the env fallback (line 294), beforeenrollMethodis known. When an operator runssystemd install --enroll-method=token --name=... --host=...and has a machine-identityINFISICAL_TOKENset in their shell (a common scenario for admins), the validation at line 351 passes (token != ""), and the machine-identity token is written torelay.confasINFISICAL_RELAY_ENROLLMENT_TOKEN. The systemd service then fails to start with a confusing enrollment error, not at install time.Compare with
relay start(line 114–116), which correctly falls back toINFISICAL_RELAY_ENROLLMENT_TOKEN(notINFISICAL_TOKEN) whenenrollMethod == token. The install command should do the same: inside theEnrollMethodTokenswitch case, overridetokenfromINFISICAL_RELAY_ENROLLMENT_TOKENwhen the flag was not explicitly set, before the emptiness check.Prompt To Fix With AI
This is a comment left during a code review. Path: packages/cmd/relay.go Line: 294-352 Comment: **Wrong token type silently accepted when `INFISICAL_TOKEN` is set in the environment** The `token` variable is read unconditionally from the `--token` flag with `INFISICAL_TOKEN` as the env fallback (line 294), before `enrollMethod` is known. When an operator runs `systemd install --enroll-method=token --name=... --host=...` and has a machine-identity `INFISICAL_TOKEN` set in their shell (a common scenario for admins), the validation at line 351 passes (`token != ""`), and the machine-identity token is written to `relay.conf` as `INFISICAL_RELAY_ENROLLMENT_TOKEN`. The systemd service then fails to start with a confusing enrollment error, not at install time. Compare with `relay start` (line 114–116), which correctly falls back to `INFISICAL_RELAY_ENROLLMENT_TOKEN` (not `INFISICAL_TOKEN`) when `enrollMethod == token`. The install command should do the same: inside the `EnrollMethodToken` switch case, override `token` from `INFISICAL_RELAY_ENROLLMENT_TOKEN` when the flag was not explicitly set, before the emptiness check. How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
packages/cmd/relay.go:294-352
**Wrong token type silently accepted when `INFISICAL_TOKEN` is set in the environment**
The `token` variable is read unconditionally from the `--token` flag with `INFISICAL_TOKEN` as the env fallback (line 294), before `enrollMethod` is known. When an operator runs `systemd install --enroll-method=token --name=... --host=...` and has a machine-identity `INFISICAL_TOKEN` set in their shell (a common scenario for admins), the validation at line 351 passes (`token != ""`), and the machine-identity token is written to `relay.conf` as `INFISICAL_RELAY_ENROLLMENT_TOKEN`. The systemd service then fails to start with a confusing enrollment error, not at install time.
Compare with `relay start` (line 114–116), which correctly falls back to `INFISICAL_RELAY_ENROLLMENT_TOKEN` (not `INFISICAL_TOKEN`) when `enrollMethod == token`. The install command should do the same: inside the `EnrollMethodToken` switch case, override `token` from `INFISICAL_RELAY_ENROLLMENT_TOKEN` when the flag was not explicitly set, before the emptiness check.
Reviews (1): Last reviewed commit: "fix(relay): support --enroll-method in s..." | Re-trigger Greptile
install token mode
Description
Closes #261
The bug (high level)
infisical relay startsupports--enroll-method=token(andaws), which uses a one-timeenrollment token to authenticate against the V2 relay API. This avoids the legacy
machine-identity auth path that triggers the dashboard warning:
However,
infisical relay systemd installdid not expose--enroll-method. The generatedsystemd unit runs
infisical relay startwith no flags, relying entirely on/etc/infisical/relay.conf. Since that file never contained any enrollment variables, asystemd-managed relay always fell back to legacy machine-identity auth on every boot — there
was no way to run a systemd relay using the newer enrollment flow.
The fix (low level)
packages/cmd/relay.go(relay systemd install) — added--enroll-method(token|aws)and
--relay-idflags. Auth validation is now aswitchon the enroll method:token: requires--token(treated as a one-time enrollment token)aws: requires--relay-id(falls back to theINFISICAL_RELAY_IDenv)""(legacy): unchanged behaviorpackages/relay/systemd.go(InstallRelaySystemdService) — extended to acceptenrollMethodandrelayID, and to write the matching variables torelay.conf:token→INFISICAL_RELAY_ENROLL_METHOD=token+INFISICAL_RELAY_ENROLLMENT_TOKEN=<token>aws→INFISICAL_RELAY_ENROLL_METHOD=aws+INFISICAL_RELAY_ID=<id>INFISICAL_TOKEN/INFISICAL_RELAY_AUTH_SECRETexactly as beforepackages/cmd/relay.go(relay start) — the enrollment-token path now falls back to theINFISICAL_RELAY_ENROLLMENT_TOKENenv var (it previously read the token only from--token,so a flagless systemd invocation could never supply it). Mirrors how the
awspath alreadyfalls back to
INFISICAL_RELAY_ID.packages/relay/enroll.go— added theINFISICAL_RELAY_ENROLL_METHOD_KEYconstant (theenv name was previously a string literal) and reused it in both
relay startand thesystemd installer.
Backward compatibility
When
--enroll-methodis omitted, behavior is identical to before:INFISICAL_TOKEN(org) orINFISICAL_RELAY_AUTH_SECRET(instance) is written, and no enrollment variables are added.Type of change
Testing
Built and tested on a real Linux server (systemd):
Service logs confirm the enrollment flow (no machine identity):
/etc/infisical/relay.confcontainsINFISICAL_RELAY_ENROLL_METHOD=tokenandINFISICAL_RELAY_ENROLLMENT_TOKEN=..., and noINFISICAL_TOKEN. Legacy install (no--enroll-method) was verified to still writeINFISICAL_TOKENas before.Before (current

main) — flag doesn't exist:After — enrollment install + service starts and enrolls:

Dashboard confirms Token Auth (not Machine Identity Legacy):

Checklist