Skip to content

Make iroh-console work from a second org and a second machine - #32

Merged
joshk merged 2 commits into
mainfrom
fix/iroh-console-identity
Nov 24, 2026
Merged

joshk merged 2 commits into
mainfrom
fix/iroh-console-identity

Conversation

@joshk

@joshk joshk commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

nh device iroh-console registers this machine's iroh endpoint with the current org so the hosted relay lets it through. It only worked once per user:

  • Second org → 409. nh kept one key per machine (iroh/identity). NervesHub refuses to register a key that's already held anywhere:
    • lib/nerves_hub/devices/network_identities.ex:168-169: register/3 returns {:error, :claimed_elsewhere} if (service, identifier) exists in any org.
    • lib/nerves_hub_web/controllers/api/fallback_controller.ex:271-283: that becomes a 409.
    • lib/nerves_hub/devices/network_identity.ex:72-78, 164-166: global unique index on (service, identifier). Lines 86-88 say it directly: someone working across three orgs runs three endpoints.
  • Second machine → 422. nh always registered instance default (omitted, so the server default applied). A member gets one row per (org_user_id, service, instance):
    • lib/nerves_hub/devices/network_identity.ex:79-84, 170-172: partial unique index. Its changeset error lands on org_user_id ("has already been taken") and comes back as a 422.
    • org_user_id is a membership (network_identity.ex:17-19), so this applies per org.

Server references are at nerves_hub_web upstream/main a7c5c5e44.

A third, separate bug: nh read the device's ticket from the identity's identifier. The server defines identifier as the proven key, the 64-hex endpoint id, with tickets in details (network_identity.ex:34-49; controllers/api/schemas/iroh_endpoint_schemas.ex:83). IrohConsole's NervesHubLink provider reports exactly that shape: identifier: to_string(addr.id) and details: %{"ticket" => …, "relay_urls" => …}, under instance iroh_console (lib/iroh_console/nerves_hub_link.ex:96-112). nh tried to decode a hex id as a ticket and failed.

Design decisions

  • One key per org, at <data dir>/iroh/orgs/<org>/identity. No key ever sits in two orgs.

    • It lives under orgs/ so an org named identity can't collide with the legacy file.
    • Org names allow A-Z (lib/nerves_hub/accounts/org.ex:63). Names that aren't all lowercase get a short hash suffix so Acme and acme stay apart on case-insensitive filesystems.
  • Instance nh-<host>-<first 8 hex of the key>. The server only requires a length of 1-255 after trimming (network_identity.ex:158, network_identities.ex:568-577).

    • The hostname is reduced to its first label, lowercased, [a-z0-9-], and capped at 40 characters.
    • The key prefix keeps two laptops with the same default hostname (two "MacBook-Pro"s) apart. It also means a re-created key can't collide with the stale row of the old one.
    • With no usable hostname, the instance is nh-<key prefix>.
  • Existing users. On first use in an org with no per-org key, nh checks whether the old machine key (iroh/identity) is registered with that org.

    • If it is, nh copies it into that org's slot and keeps using it. Nothing is re-registered and the existing default row keeps working.
    • Every other org gets a fresh key. The old key is never posted to a second org, and the legacy file is left in place for older nh builds.
  • Errors from registration:

    • 409 (or a 422 on service, which is the same key-taken race): nh re-checks first, in case another nh run just registered the key here. Otherwise it explains that the key is held outside this org, names the likely causes (copied data dir, renamed org) and gives the exact key file to remove.
    • 422 on org_user_id/instance: nh says the instance name is taken, looks up which endpoint holds it, and prints the nh iroh-endpoint delete <id> --org <org> command.
    • Other 422s: wrapped with context, keeping the server's message.
    • 403: nh says the role can't register endpoints and prints the nh iroh-endpoint register … command an admin can run.
  • Key files are written to a temp file and hard-linked into place. Two concurrent nh runs agree on one key, a reader never sees a half-written key, and a key that may already be registered is never overwritten.

  • A legacy key that can't be read is skipped, not reported. It could only matter to one org, so it shouldn't block every new one.

  • Device address, in order:

    1. details["ticket"]. It is refused if it names a different endpoint than identifier, because identifier is what the device proved. A ticket nh can't decode falls through to the next two.
    2. identifier as a ticket, for early IrohConsole builds that reported it there.
    3. identifier as an endpoint id plus details["relay_urls"].

    An identity with none of these gets a "cannot be dialled" error instead of "has not reported an endpoint".

  • Layout: the logic lives in internal/iroh (identity.go, register.go, addr.go). cmd/device_iroh_console.go only wires it up. iroh.Dial now takes a DeviceAddr instead of a ticket string, so go-iroh types stay inside internal/iroh.

Tests

Registration runs against an httptest NervesHub that enforces both constraints (internal/iroh/register_test.go). The tests cover:

  • a second org gets its own key, with no 409;
  • a second machine with the same hostname gets a distinct instance, with no 422;
  • migration: the legacy key is kept for its org and not re-posted, other orgs get new keys, and the legacy file is untouched;
  • a legacy key held by another org is not used, and an unreadable legacy key doesn't block a new org;
  • messages for 409, for a 409 caused by a race, for a taken instance (422), for other 422s and for 403;
  • instance naming.

Address resolution is in internal/iroh/addr_test.go. It covers the ticket in details, the legacy ticket identifier, id plus relays, an undecodable ticket falling back to relays, a mismatched ticket, nothing to dial, and malformed input. The cmd tests cover the lookup wiring.

go test ./..., go vet ./... and gofmt -l . are clean. Not tested against a live relay; internal/iroh/live_test.go is opt-in and was updated for the new Dial signature.

Not in this PR

  • Old default registrations from the previous nh aren't renamed, because the API has no update. They keep working.
  • Stale per-machine rows (for example after wiping a data dir) aren't cleaned up automatically. Remove them with nh iroh-endpoint delete.

🤖 Generated with Claude Code

joshk and others added 2 commits November 24, 2026 13:15
NervesHub lets an iroh key belong to only one organization (409
claimed_elsewhere) and a member hold one endpoint per (service, instance)
in an organization (422). nh kept one key per machine and registered it as
instance "default", so a second organization got a 409 and a second laptop a
422.

nh now keeps a key per organization under iroh/orgs/<org>/identity and
registers it under an instance named for the machine,
"nh-<host>-<key prefix>". The old per-machine key is still used for the
organization it is already registered with, so existing registrations keep
working, and is never registered anywhere else. A 409, 422 or 403 from
registration now says what happened and what to do.

The device's address is read from details["ticket"], where IrohConsole
reports it, falling back to a ticket in identifier (early builds) and to the
endpoint id plus details["relay_urls"]. A ticket naming a different endpoint
from the reported id is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Fall back to the endpoint id and relay_urls when the device's ticket
  cannot be decoded, rather than failing on a ticket encoding this build
  does not read.
- Skip an unreadable legacy key instead of failing every new organization
  over a file only one organization could use.
- Write a new key to a temp file and link it into place, so a concurrent
  nh run never reads a half-written key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joshk
joshk merged commit 5d4d997 into main Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant