diff --git a/docs/full-stack-setup-design.md b/docs/full-stack-setup-design.md index b3c48cb..aac612b 100644 --- a/docs/full-stack-setup-design.md +++ b/docs/full-stack-setup-design.md @@ -1335,7 +1335,7 @@ flip themselves. | `GET` | `/setup/:id` | four URLs + **VTA DID (1a)** + **dids admin-enroll URL (3e)** + **VTC DID (5b)** + `dids_enroll_used` / `vtc_install_used` + per-step status + (once) the admin keys and the VTC install credentials | | `GET` | `/setup/:id/logs` | `?source=` gains `mediator_p1\|mediator_p2\|dids_p1\|dids_p2\|dids_invite\|dids_load_did\|vta_register_dids\|import_admin_did\|vtc_setup_key\|vtc_acl_grant\|vtc_setup\|mediator\|dids\|vtc` | | `DELETE` | `/setup/:id` | tear down all 4 DNS records + all four components' resources ([§13](#13-teardown)) | -| `POST` | `/setup/:id/dids/reissue-enroll` | regenerate the single-use dids admin enrollment URL (`did-hosting-daemon invite`); scales the dids Deployment to 0, waits for its pod gone, runs the invite Job, then scales back to 1 (always, even on failure) | +| `POST` | `/setup/:id/dids/reissue-enroll` | regenerate the single-use dids admin enrollment URL (`did-hosting-daemon invite`); before the original link is opened it keeps the setup admin DID, and afterwards it generates a unique `did:key` subject (discarding its private key) for a separate passkey-only backup administrator; scales the dids Deployment to 0, waits for its pod gone, runs the invite Job, then scales back to 1 (always, even on failure) | | `POST` | `/setup/:id/dids/enroll-ack` | frontend marks `dids_enroll_used = true` once the user opens the enrollment URL | | `POST` | `/setup/:id/vtc/reissue-install` | **required, not a nice-to-have** — the setup-minted install token lives 15 minutes ([§6](#6-per-step-jobs)). Remints a fresh install URL **and claim code** via `vtc admin invite --did <5c>`, mirroring `ReissueDidsEnroll`: scale `fs-{sid}-vtc` to 0, wait pod gone, run the Job, scale back to 1 (via `defer`, so the VTC restarts even on failure). Parses the `Install URL (one-shot):` and `Claim code …:` lines (stderr; K8s captures both streams), updates `vtc_install_url` + `vtc_claim_code`, resets `vtc_install_used` | | `POST` | `/setup/:id/vtc/install-ack` | frontend marks `vtc_install_used = true` once the user opens `install_url` — mirrors `AckDidsEnroll` | diff --git a/internal/apidocs/openapi.yaml b/internal/apidocs/openapi.yaml index b4c686a..af40ae7 100644 --- a/internal/apidocs/openapi.yaml +++ b/internal/apidocs/openapi.yaml @@ -4065,7 +4065,9 @@ paths: Exists for the platform stack: it is owned by a passkey-less system account, so the user-facing route can never be called for it — and without this one an admin can see the stack's single-use enrollment link - but never replace a spent one. + but never replace a spent one. Once the original link has been opened, + the replacement targets a newly generated `did:key` subject for a + separate passkey-only backup administrator. tags: [Admin] security: [{ CookieAuthAdmin: [] }] parameters: @@ -4080,7 +4082,8 @@ paths: properties: dids_admin_enroll_url: { type: string } dids_admin_enroll_claim_code: { type: string } - required: [dids_admin_enroll_url, dids_admin_enroll_claim_code] + dids_admin_did: { type: string } + required: [dids_admin_enroll_url, dids_admin_enroll_claim_code, dids_admin_did] "401": { description: Missing or invalid token, content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } } "404": { description: Session not found, content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } } "409": { description: The dids daemon isn't provisioned yet, content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } } @@ -7377,9 +7380,11 @@ paths: description: | full_stack only. Regenerates the single-use dids admin-panel enrollment URL and its second-channel claim code - (re-runs `did-hosting-daemon invite --role admin`) — useful if the original URL - (returned once from `step_dids_invite` / `GET /setup/{id}`) was lost or already - consumed. Scales the dids Deployment to 0 and waits for its pod to terminate + (re-runs `did-hosting-daemon invite --role admin`). Before the original link is + opened, the replacement targets the setup-generated admin DID. Afterwards it + targets a newly generated `did:key` subject so the invite can enroll a + separate passkey-only backup administrator. Scales the dids Deployment + to 0 and waits for its pod to terminate (releasing the exclusive lock on the daemon's local store), runs the invite Job, then scales the daemon back to 1 — the daemon is restarted even if the job itself fails. On success, resets `dids_enroll_used` to false. @@ -7407,7 +7412,10 @@ paths: dids_admin_enroll_claim_code: type: string example: "7KQ4-MX2P-9TDA" - required: [dids_admin_enroll_url, dids_admin_enroll_claim_code] + dids_admin_did: + type: string + example: "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK" + required: [dids_admin_enroll_url, dids_admin_enroll_claim_code, dids_admin_did] "400": description: Session is not a full_stack session content: diff --git a/internal/handler/setup_fullstack.go b/internal/handler/setup_fullstack.go index e43c2f7..c877d89 100644 --- a/internal/handler/setup_fullstack.go +++ b/internal/handler/setup_fullstack.go @@ -13,6 +13,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "github.com/ic3software/vtafarm-api/internal/capacity" + "github.com/ic3software/vtafarm-api/internal/didkey" "github.com/ic3software/vtafarm-api/internal/k8s" "github.com/ic3software/vtafarm-api/internal/middleware" "github.com/ic3software/vtafarm-api/internal/model" @@ -31,6 +32,13 @@ func shellQuote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" } +func didsEnrollmentSubject(session *model.SetupSession) (string, error) { + if !session.DidsEnrollUsed { + return session.DIDHostingAdminDid, nil + } + return didkey.Generate() +} + // strVal dereferences a nullable model field, returning "" for nil — the // full_stack output/image columns are *string (NULL until set; see // model.SetupSession). @@ -545,7 +553,10 @@ func (h *SetupHandler) logsFullStack(c *gin.Context, session *model.SetupSession // ReissueDidsEnroll handles POST /api/v1/setup/:id/dids/reissue-enroll // (design §12, optional endpoint) — regenerates the single-use dids admin -// enrollment URL by re-running `did-hosting-daemon invite`. +// enrollment URL by re-running `did-hosting-daemon invite`. Once the original +// invite has been opened, it targets a fresh did:key whose private key is +// intentionally discarded: the DID is only a unique subject for a separate +// passkey administrator. // // The dids daemon holds an exclusive lock on its local (Fjall) store for as // long as it's running, so the invite Job can't open the same store @@ -579,6 +590,11 @@ func (h *SetupHandler) reissueDidsEnroll(c *gin.Context, session *model.SetupSes c.JSON(http.StatusServiceUnavailable, gin.H{"error": "k8s not configured"}) return } + subjectDID, err := didsEnrollmentSubject(session) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate admin DID"}) + return + } release, err := h.acquireSessionMaintenance(session.ID) if err != nil { status := http.StatusBadGateway @@ -621,7 +637,7 @@ func (h *SetupHandler) reissueDidsEnroll(c *gin.Context, session *model.SetupSes h.k8s.DeleteComponentJob(ctx, ns, jobName) // clear a previous (TTL'd) run if still present - cmd := fmt.Sprintf("did-hosting-daemon invite --role admin --did %s", shellQuote(session.DIDHostingAdminDid)) + cmd := fmt.Sprintf("did-hosting-daemon invite --role admin --did %s", shellQuote(subjectDID)) if err := h.k8s.CreateComponentJob(ctx, ns, k8s.ComponentJobSpec{ Name: jobName, Image: session.DidsImage, @@ -662,6 +678,7 @@ func (h *SetupHandler) reissueDidsEnroll(c *gin.Context, session *model.SetupSes c.JSON(http.StatusOK, gin.H{ "dids_admin_enroll_url": enrollURL, "dids_admin_enroll_claim_code": claimCode, + "dids_admin_did": subjectDID, }) } diff --git a/internal/handler/setup_fullstack_reissue_test.go b/internal/handler/setup_fullstack_reissue_test.go new file mode 100644 index 0000000..c883b17 --- /dev/null +++ b/internal/handler/setup_fullstack_reissue_test.go @@ -0,0 +1,42 @@ +package handler + +import ( + "strings" + "testing" + + "github.com/ic3software/vtafarm-api/internal/model" +) + +func TestDidsEnrollmentSubject(t *testing.T) { + const original = "did:key:z6MkOriginal" + + t.Run("keeps original DID before enrollment link is opened", func(t *testing.T) { + session := &model.SetupSession{DIDHostingAdminDid: original} + got, err := didsEnrollmentSubject(session) + if err != nil { + t.Fatalf("didsEnrollmentSubject() error = %v", err) + } + if got != original { + t.Fatalf("didsEnrollmentSubject() = %q, want %q", got, original) + } + }) + + t.Run("creates unique did:key after enrollment link is opened", func(t *testing.T) { + session := &model.SetupSession{DIDHostingAdminDid: original, DidsEnrollUsed: true} + first, err := didsEnrollmentSubject(session) + if err != nil { + t.Fatalf("didsEnrollmentSubject() error = %v", err) + } + second, err := didsEnrollmentSubject(session) + if err != nil { + t.Fatalf("didsEnrollmentSubject() error = %v", err) + } + const prefix = "did:key:z6Mk" + if !strings.HasPrefix(first, prefix) { + t.Fatalf("didsEnrollmentSubject() = %q, want prefix %q", first, prefix) + } + if first == second { + t.Fatalf("generated duplicate DIDs: %q", first) + } + }) +}