Repository navigation
Make iroh-console work from a second org and a second machine - #32
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
nh device iroh-consoleregisters this machine's iroh endpoint with the current org so the hosted relay lets it through. It only worked once per user:iroh/identity). NervesHub refuses to register a key that's already held anywhere:lib/nerves_hub/devices/network_identities.ex:168-169:register/3returns{: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.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 onorg_user_id("has already been taken") and comes back as a 422.org_user_idis a membership (network_identity.ex:17-19), so this applies per org.Server references are at nerves_hub_web
upstream/maina7c5c5e44.A third, separate bug: nh read the device's ticket from the identity's
identifier. The server definesidentifieras the proven key, the 64-hex endpoint id, with tickets indetails(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)anddetails: %{"ticket" => …, "relay_urls" => …}, under instanceiroh_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.orgs/so an org namedidentitycan't collide with the legacy file.A-Z(lib/nerves_hub/accounts/org.ex:63). Names that aren't all lowercase get a short hash suffix soAcmeandacmestay 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).[a-z0-9-], and capped at 40 characters.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.defaultrow keeps working.Errors from registration:
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.org_user_id/instance: nh says the instance name is taken, looks up which endpoint holds it, and prints thenh iroh-endpoint delete <id> --org <org>command.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:
details["ticket"]. It is refused if it names a different endpoint thanidentifier, becauseidentifieris what the device proved. A ticket nh can't decode falls through to the next two.identifieras a ticket, for early IrohConsole builds that reported it there.identifieras an endpoint id plusdetails["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.goonly wires it up.iroh.Dialnow takes aDeviceAddrinstead of a ticket string, so go-iroh types stay insideinternal/iroh.Tests
Registration runs against an httptest NervesHub that enforces both constraints (
internal/iroh/register_test.go). The tests cover: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 ./...andgofmt -l .are clean. Not tested against a live relay;internal/iroh/live_test.gois opt-in and was updated for the newDialsignature.Not in this PR
defaultregistrations from the previous nh aren't renamed, because the API has no update. They keep working.nh iroh-endpoint delete.🤖 Generated with Claude Code