oficina / equipo Public

hostedproxy: Task 7 review 1, fake controller holds the report rule

The fake controller accepts a certification report only for an issued,
unrevoked serial of a live certification launch named by the report,
signed by the certificate's key, once per launch. Independent report
cases use fresh certification launches; a second report, a runner
launch, another launch's name and a stopped launch are refused.
Redeem and renew answer NotAfterNS on the controller's clock. The
forged-header assertions use the ruling's wording.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev ab498539a6f3442f29dde057065eebd1bbc523d9 parent c8e772e
3 files changed, +190 −66
  • docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md +15 −0
  • internal/hostedproxy/e2e_test.go +90 −50
  • internal/hostedproxy/proxytest/harness.go +85 −16
diff --git a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md
index 18fb28b5..f48437ad 100644
--- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md
+++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md
@@ -302,3 +302,18 @@ Mutation checks (each applied by script, the named test run, then restored and c
22. (the fix) Close an unparseable CONNECT silently again (`if false && ...` on the new branch, `egress.go`). Command: `CGO_ENABLED=0 go test ./internal/hostedproxy -run '^TestFrontingMatrix$' -count=1`. Observed: FAIL, `TestFrontingMatrix/CONNECT_to_host%2Eexample: e2e_test.go:142: CONNECT instance%2Eexample.test:443 = 0, unexpected EOF; want 400`. Restored.
Also run: `go test ./internal/hostedproxy/... ./cmd/equipo-hosted-proxy -count=1 -race` once, ok (3.4 s and 1.1 s); `go test ./internal/isolatedjob -run TestProviderTypesStayInAdapter -count=1` ok; staticcheck v0.7.0 on `./internal/hostedproxy/...` clean; gofmt clean; `make budget build vet` ok (127 directories within budget).
+
+### Task 7 review round 1 (t7-r1.md: finding 1 by ruling, findings 2 and 3 fixed)
+
+1. Forged identity headers (ruling, plan amended in c8e772e0): forged headers never reach the controller, because the proxy builds its own forwarded request, and a report sent to the controller directly with identity headers is refused. The test is unchanged in substance; its assertions now carry that wording ("forged identity headers never reach the controller", "a direct report with identity headers"), and the direct refusal is paired with the same report accepted without the header, so the header is shown to be the reason.
+2. The fake controller now holds Task 13's report rule. In order: no identity header; a serial it issued, unrevoked; its launch live (in the controller, not stopped, before its deadline, same attempt, VM and epoch) else `not_live`; a certification launch (`Launch.Certification`) else `not_certification`; the report's `launch_id` the launch's else `launch_mismatch`; the certificate key's signature else `bad_signature`; once per launch else `already_reported`. `FakeController.Stop` takes a launch out of the feed and revokes its serials. `Harness.CertVM(id)` adds a fresh live certification launch, polls the proxy and issues its VM a certificate; `proxytest.CertReport(launch, note)` is a report naming a launch. TestCertReportThroughProxy now uses fresh launches for independent cases: c-1 reports (intact, credited to c-1) and its second report is refused `already_reported`; c-2 reports with forged headers (none reach the controller; credited to c-2); c-3 directly with an identity header (400 `identity_header`) then without (200); the redeemed runner launch l-1 is refused `not_certification`; c-4 naming c-9 `launch_mismatch`; c-5 with c-6's serial `serial_mismatch` at the proxy and `bad_signature` at the controller; c-5 with another key `bad_signature`; c-5 revoked, `revoked` at both; stopped c-7 refused at the controller. Exactly three reports are accepted.
+3. Redeem and renew answer `NotAfterNS` as ctlproxy does: the controller's monotonic time at issue plus the certificate's lifetime (`issueLocked` returns it). `VM.NotAfterNS` keeps the redeem answer; TestOpenSocketClosesAtCertExpiry checks the redeem answer is Mono plus 20 s, TestRenewalReplayRefused checks the renewal's against the renewed certificate.
+
+Mutation checks (each applied by script to `proxytest/harness.go`, the named test run, then restored and checked with `cmp`):
+
+23. Accept a second report per launch (`if false && c.reported[...]`). Command: `CGO_ENABLED=0 go test ./internal/hostedproxy -run '^TestCertReportThroughProxy$' -count=1`. Observed: FAIL, `e2e_test.go:486: a second report for c-1 = 200 , want the controller's 403 already_reported`, `e2e_test.go:505: accepted reports = [...]`. Restored.
+24. Accept any launch kind (`case false && !l.Certification`). Same command. Observed: FAIL, `e2e_test.go:525: report from a runner launch = 200 , want 403 not_certification`, `e2e_test.go:576: the controller accepted 4 reports, want c-1, c-2 and c-3`. Restored.
+25. Ignore the report's launch ID (`false && named.LaunchID != ...`). Same command. Observed: FAIL, `e2e_test.go:530: report naming another launch = 200 , want 403 launch_mismatch`, `e2e_test.go:576: ... accepted 4 reports`. Restored.
+26. `NotAfterNS: 1` again. Command: `... -run '^(TestOpenSocketClosesAtCertExpiry|TestRenewalReplayRefused)$' -count=1`. Observed: FAIL, `e2e_test.go:347: redeem answered not_after_ns 1, want the controller's mono 3620000000000`, `e2e_test.go:426: renew answered not_after_ns 1, want the controller's mono 4500000000000`. Restored.
+
+Also run: the brief's green command ok (0.16 s); `TestCertReport|TestRenewalReplay|TestOpenSocket` `-count=20` ok; `go test ./internal/hostedproxy/... ./cmd/equipo-hosted-proxy -count=1 -race` once, ok (3.5 s and 1.1 s); whole package 1.0 s; staticcheck v0.7.0 clean; gofmt clean; `make budget build vet` ok (127 directories within budget; `internal/hostedproxy` 1882 source and 2148 test lines, `proxytest` 1178).
diff --git a/internal/hostedproxy/e2e_test.go b/internal/hostedproxy/e2e_test.go
index ddb5339c..f6dd5a75 100644
--- a/internal/hostedproxy/e2e_test.go
+++ b/internal/hostedproxy/e2e_test.go
@@ -343,6 +343,9 @@ func TestOpenSocketClosesAtCertExpiry(t *testing.T) {
if got := vm.Cert.Leaf.NotAfter.Sub(h.Clock.Wall()); got != life {
t.Fatalf("certificate lifetime %v, want %v", got, life)
}
+ if want := int64(h.Clock.Mono() + life); vm.NotAfterNS != want {
+ t.Errorf("redeem answered not_after_ns %d, want the controller's mono %d", vm.NotAfterNS, want)
+ }
ws := openSocket(t, h, vm)
mono := h.Clock.Mono()
at := h.Proxy.Feed().Deadline(vm.Identity(t), mono)
@@ -419,6 +422,9 @@ func TestRenewalReplayRefused(t *testing.T) {
t.Fatal(err)
}
renewed := proxytest.VMFromDER(t, key, r.Cert)
+ if want := int64(h.Clock.Mono() + renewed.Cert.Leaf.NotAfter.Sub(h.Clock.Wall())); r.NotAfterNS != want || want <= int64(h.Clock.Mono()) {
+ t.Errorf("renew answered not_after_ns %d, want the controller's mono %d", r.NotAfterNS, want)
+ }
if status, code := bootCode(t, h, vm, "/renew", captured, nil); status != http.StatusForbidden || code != "already_renewed" {
t.Errorf("replay before the next feed = %d %s, want the controller's 403 already_renewed", status, code)
@@ -443,96 +449,130 @@ func TestRenewalReplayRefused(t *testing.T) {
}
// TestCertReportThroughProxy: a report signed with the VM's certificate
-// key reaches the controller byte for byte and is credited to the
-// launch the serial was issued for. Identity headers the VM adds never
-// reach the controller, which refuses any that do; a report for another
-// serial, or signed by another key, or for a revoked serial, is refused.
+// key reaches the controller byte for byte and is credited to the launch
+// the serial was issued for. Forged identity headers never reach the
+// controller, because the proxy builds its own forwarded request, and a
+// report sent to the controller directly with identity headers is
+// refused. The fake controller holds Task 13's rule: a live
+// certification launch, the report naming that launch, the certificate's
+// own key, an unrevoked serial, once per launch.
func TestCertReportThroughProxy(t *testing.T) {
h := proxytest.New(t)
- vm := h.Redeem(t)
- report := []byte(`{"launch_id":"l-1","root":[],"user":[],"sandbox":[]}`)
- body := proxytest.ReportBody(t, vm.Serial(), report, vm.Key)
- if status, code := bootCode(t, h, vm, "/cert-report", body, nil); status != http.StatusOK {
+ ctx := context.Background()
+ route := "POST /v1/proxy/cert-report"
+ sent := func(vm *proxytest.VM, body []byte, extra http.Header) (int, string) {
+ t.Helper()
+ return bootCode(t, h, vm, "/cert-report", body, extra)
+ }
+
+ // The positive case: c-1's VM reports once; a second report for the
+ // launch is refused.
+ c1 := h.CertVM(t, "c-1")
+ report := proxytest.CertReport("c-1", "first")
+ body := proxytest.ReportBody(t, c1.Serial(), report, c1.Key)
+ if status, code := sent(c1, body, nil); status != http.StatusOK {
t.Fatalf("report = %d %s", status, code)
}
- got := h.Controller.Received("POST /v1/proxy/cert-report")
+ got := h.Controller.Received(route)
if len(got) != 1 || string(got[0].Body) != string(body) {
t.Fatalf("the controller got %d reports, want the VM's body intact", len(got))
}
- if r := h.Controller.Reports(); len(r) != 1 || r[0].Launch.LaunchID != proxytest.LaunchID || r[0].Serial != vm.Serial() ||
+ if r := h.Controller.Reports(); len(r) != 1 || r[0].Launch.LaunchID != "c-1" || r[0].Serial != c1.Serial() ||
string(r[0].Body) != string(report) {
t.Fatalf("accepted reports = %+v", r)
}
+ again := proxytest.ReportBody(t, c1.Serial(), proxytest.CertReport("c-1", "second"), c1.Key)
+ if status, code := sent(c1, again, nil); status != http.StatusForbidden || code != "already_reported" {
+ t.Errorf("a second report for c-1 = %d %s, want the controller's 403 already_reported", status, code)
+ }
- // Forged identity headers: the proxy forwards none of them, so the
- // report is credited by its serial alone ...
- forged := http.Header{"X-Equipo-Launch": {"l-9"}, "X-Equipo-Vm": {"vm-9"}, "X-Equipo-Serial": {"ab"},
- "X-Forwarded-For": {"10.0.0.9"}, hostedwire.HeaderKeyID: {"controller"}, "Equipo-Launch": {"l-9"}}
- second := proxytest.ReportBody(t, vm.Serial(), []byte("second"), vm.Key)
- if status, code := bootCode(t, h, vm, "/cert-report", second, forged); status != http.StatusOK {
+ // Forged identity headers never reach the controller: the proxy builds
+ // its own forwarded request, so c-2's report is credited by its serial.
+ c2 := h.CertVM(t, "c-2")
+ forged := http.Header{"X-Equipo-Launch": {"c-1"}, "X-Equipo-Vm": {"vm-c-1"}, "X-Equipo-Serial": {c1.Serial()},
+ "X-Forwarded-For": {"10.0.0.9"}, hostedwire.HeaderKeyID: {"controller"}, "Equipo-Launch": {"c-1"}}
+ if status, code := sent(c2, proxytest.ReportBody(t, c2.Serial(), proxytest.CertReport("c-2", "forged"), c2.Key), forged); status != 200 {
t.Fatalf("report with forged headers = %d %s", status, code)
}
- for _, r := range h.Controller.Received("POST /v1/proxy/cert-report") {
- for name := range forged {
- if v := r.Header.Values(name); name != hostedwire.HeaderKeyID && len(v) != 0 || name == hostedwire.HeaderKeyID &&
- (len(v) != 1 || v[0] != string(proxytest.ProxyKey)) {
- t.Errorf("the controller got %s: %q", name, v)
- }
+ last := h.Controller.Received(route)
+ for name := range forged {
+ v := last[len(last)-1].Header.Values(name)
+ if name == hostedwire.HeaderKeyID && (len(v) != 1 || v[0] != string(proxytest.ProxyKey)) || name != hostedwire.HeaderKeyID && len(v) != 0 {
+ t.Errorf("forged identity headers never reach the controller: it got %s %q", name, v)
}
}
- if r := h.Controller.Reports(); len(r) != 2 || r[1].Launch.LaunchID != proxytest.LaunchID || r[1].Launch.VMRef != proxytest.VMRef {
+ if r := h.Controller.Reports(); len(r) != 2 || r[1].Launch.LaunchID != "c-2" || r[1].Launch.VMRef != "vm-c-2" {
t.Fatalf("accepted reports = %+v", r)
}
- // ... and a request that did carry one is refused by the controller.
- direct := http.Header{"X-Equipo-Launch": {"l-9"}}
- if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", second, direct); status != 400 ||
+
+ // A report sent to the controller directly with identity headers is
+ // refused; the same report without them is accepted.
+ c3 := h.CertVM(t, "c-3")
+ direct := proxytest.ReportBody(t, c3.Serial(), proxytest.CertReport("c-3", "direct"), c3.Key)
+ if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", direct, http.Header{"X-Equipo-Launch": {"c-3"}}); status != 400 ||
code != "identity_header" {
- t.Errorf("a report with an identity header at the controller = %d %s, want 400 identity_header", status, code)
+ t.Errorf("a direct report with identity headers = %d %s, want 400 identity_header", status, code)
+ }
+ if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", direct, nil); status != 200 {
+ t.Errorf("the same report without them = %d %s, want 200", status, code)
}
- // Another serial: another launch's VM certificate, signed with this
- // VM's key. The proxy refuses it; so does the controller.
- h.Controller.SetLaunch(proxytest.Launch{LaunchID: "l-2", AttemptID: "a-2", VMRef: "vm-2", Epoch: 1, Code: "unused",
- Services: []hostedwire.Service{{Host: host, Port: 443, Class: hostedwire.ServiceInstance}}})
- otherVM := h.Issue(t, "l-2")
- if err := h.Proxy.Poll(context.Background()); err != nil {
- t.Fatal(err)
+ // A launch that is not a certification launch, and a report naming
+ // another launch: refused by the controller.
+ runner := h.Redeem(t)
+ if status, code := sent(runner, proxytest.ReportBody(t, runner.Serial(), proxytest.CertReport(proxytest.LaunchID, "x"), runner.Key),
+ nil); status != 403 || code != "not_certification" {
+ t.Errorf("report from a runner launch = %d %s, want 403 not_certification", status, code)
+ }
+ c4 := h.CertVM(t, "c-4")
+ if status, code := sent(c4, proxytest.ReportBody(t, c4.Serial(), proxytest.CertReport("c-9", "x"), c4.Key), nil); status != 403 ||
+ code != "launch_mismatch" {
+ t.Errorf("report naming another launch = %d %s, want 403 launch_mismatch", status, code)
}
- before := len(h.Controller.Received("POST /v1/proxy/cert-report"))
- theirs := proxytest.ReportBody(t, otherVM.Serial(), report, vm.Key)
- if status, code := bootCode(t, h, vm, "/cert-report", theirs, nil); status != http.StatusForbidden || code != "serial_mismatch" {
+
+ // Another serial: c-5's VM reports with c-6's serial, signed with its
+ // own key. The proxy refuses it; so does the controller.
+ c5, c6 := h.CertVM(t, "c-5"), h.CertVM(t, "c-6")
+ before := len(h.Controller.Received(route))
+ theirs := proxytest.ReportBody(t, c6.Serial(), proxytest.CertReport("c-6", "x"), c5.Key)
+ if status, code := sent(c5, theirs, nil); status != http.StatusForbidden || code != "serial_mismatch" {
t.Errorf("report for another serial = %d %s, want 403 serial_mismatch", status, code)
}
- if n := len(h.Controller.Received("POST /v1/proxy/cert-report")); n != before {
+ if n := len(h.Controller.Received(route)); n != before {
t.Errorf("the controller got the report for another serial")
}
- if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", theirs, nil); status != 403 ||
- code != "bad_signature" {
+ if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", theirs, nil); status != 403 || code != "bad_signature" {
t.Errorf("report for another serial at the controller = %d %s, want 403 bad_signature", status, code)
}
// Another key: the proxy forwards it (it never vouches for a report);
// the controller refuses it and the VM gets its signed refusal.
stranger, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
- if status, code := bootCode(t, h, vm, "/cert-report", proxytest.ReportBody(t, vm.Serial(), report, stranger), nil); status != 403 ||
+ if status, code := sent(c5, proxytest.ReportBody(t, c5.Serial(), proxytest.CertReport("c-5", "x"), stranger), nil); status != 403 ||
code != "bad_signature" {
t.Errorf("report signed by another key = %d %s, want the controller's 403 bad_signature", status, code)
}
- // A revoked serial: refused by the proxy once the feed says so, and
- // by the controller.
- h.Controller.Revoke(vm.Serial())
- if err := h.Proxy.Poll(context.Background()); err != nil {
+ // A revoked serial: refused by the proxy once the feed says so, and by
+ // the controller. A stopped launch: refused by the controller.
+ h.Controller.Revoke(c5.Serial())
+ if err := h.Proxy.Poll(ctx); err != nil {
t.Fatal(err)
}
- third := proxytest.ReportBody(t, vm.Serial(), []byte("third"), vm.Key)
- if status, code := bootCode(t, h, vm, "/cert-report", third, nil); status != 403 || code != hostedproxy.ReasonRevoked {
+ revoked := proxytest.ReportBody(t, c5.Serial(), proxytest.CertReport("c-5", "x"), c5.Key)
+ if status, code := sent(c5, revoked, nil); status != 403 || code != hostedproxy.ReasonRevoked {
t.Errorf("report over a revoked certificate = %d %s, want 403 revoked", status, code)
}
- if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", third, nil); status != 403 || code != "revoked" {
+ if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", revoked, nil); status != 403 || code != "revoked" {
t.Errorf("report for a revoked serial at the controller = %d %s, want 403 revoked", status, code)
}
- if n := len(h.Controller.Reports()); n != 2 {
- t.Errorf("the controller accepted %d reports, want 2", n)
+ c7 := h.CertVM(t, "c-7")
+ h.Controller.Stop("c-7")
+ stopped := proxytest.ReportBody(t, c7.Serial(), proxytest.CertReport("c-7", "x"), c7.Key)
+ if status, code := h.Controller.SignedCall(t, h.ProxyKey, "/v1/proxy/cert-report", stopped, nil); status != 403 {
+ t.Errorf("report from a stopped launch at the controller = %d %s, want 403", status, code)
+ }
+ if r := h.Controller.Reports(); len(r) != 3 {
+ t.Errorf("the controller accepted %d reports, want c-1, c-2 and c-3", len(r))
}
}
diff --git a/internal/hostedproxy/proxytest/harness.go b/internal/hostedproxy/proxytest/harness.go
index 9bf01d02..62abadf9 100644
--- a/internal/hostedproxy/proxytest/harness.go
+++ b/internal/hostedproxy/proxytest/harness.go
@@ -214,6 +214,8 @@ func (l *Log) Wait(t testing.TB, what string, from int, fn func(LogLine) bool) L
// Launch is one launch the fake controller knows. Deadline (wall) caps the
// certificates it issues, as the controller's launch deadline does.
+// Certification marks a certification launch, the only kind that may
+// send a report; Stopped is set by Stop.
type Launch struct {
LaunchID, AttemptID string
VMRef isolatedjob.Ref
@@ -221,6 +223,8 @@ type Launch struct {
Code string
Deadline time.Time
Services []hostedwire.Service
+ Certification bool
+ Stopped bool
}
// issued is one serial the fake controller signed.
@@ -249,8 +253,9 @@ type Received struct {
// FakeController is the controller's proxy side: it verifies the proxy's
// signature on every request, signs every answer for its nonce, serves the
// feed, redeems proxy codes, renews and revokes certificates, and accepts
-// certification reports by Task 13's rule (a serial it issued, unrevoked,
-// signed by that certificate's key; no identity in any header).
+// certification reports by Task 13's rule: no identity in any header; a
+// serial it issued, unrevoked, for a live certification launch whose ID
+// is the report's; signed by that certificate's key; once per launch.
type FakeController struct {
clock *FakeMono
srv *httptest.Server
@@ -267,6 +272,7 @@ type FakeController struct {
feedSer int64
nonces map[string]bool
reports []Report
+ reported map[string]bool // launch -> a report was accepted
received []Received
}
@@ -287,7 +293,8 @@ func newFakeController(t testing.TB, clock *FakeMono, proxyPub ed25519.PublicKey
}
caKey, ca, pool := newCA(t, "fake controller client CA")
c := &FakeController{clock: clock, key: key, ca: ca, caKey: caKey, caPool: pool,
- launches: map[string]*Launch{}, used: map[string]bool{}, certs: map[string]*issued{}, nonces: map[string]bool{}}
+ launches: map[string]*Launch{}, used: map[string]bool{}, certs: map[string]*issued{}, nonces: map[string]bool{},
+ reported: map[string]bool{}}
c.verifier = &hostedwire.Verifier{Keys: map[hostedwire.KeyID]hostedwire.VerifyKey{ProxyKey: {Public: proxyPub, Role: hostedwire.RoleProxy}},
Nonces: c, Now: clock.Wall}
c.srv = httptest.NewUnstartedServer(http.HandlerFunc(c.serve))
@@ -335,6 +342,19 @@ func (c *FakeController) SetEpoch(launchID string, epoch int64) {
c.launches[launchID].Epoch = epoch
}
+// Stop stops a launch: it leaves the feed and every serial issued for it
+// is revoked.
+func (c *FakeController) Stop(launchID string) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.launches[launchID].Stopped = true
+ for _, s := range c.certs {
+ if s.launch.LaunchID == launchID {
+ s.revoked = true
+ }
+ }
+}
+
// Revoke revokes serial, as a stop does.
func (c *FakeController) Revoke(serial string) {
c.mu.Lock()
@@ -370,12 +390,16 @@ func (c *FakeController) Received(route string) []Received {
func (c *FakeController) Issue(launchID string, pub *ecdsa.PublicKey) ([]byte, error) {
c.mu.Lock()
defer c.mu.Unlock()
- return c.issueLocked(c.launches[launchID], pub)
+ der, _, err := c.issueLocked(c.launches[launchID], pub)
+ return der, err
}
-func (c *FakeController) issueLocked(l *Launch, pub *ecdsa.PublicKey) ([]byte, error) {
- if l == nil {
- return nil, errors.New("no such launch")
+// issueLocked signs the certificate and returns it with its NotAfter on
+// the controller's monotonic clock, as RedeemResponse.NotAfterNS carries
+// it (ctlproxy: now.T plus the certificate's lifetime).
+func (c *FakeController) issueLocked(l *Launch, pub *ecdsa.PublicKey) ([]byte, int64, error) {
+ if l == nil || l.Stopped {
+ return nil, 0, errors.New("no such launch")
}
now := c.clock.Wall()
notAfter := now.Add(ctlproxy.CertLifetime)
@@ -384,11 +408,11 @@ func (c *FakeController) issueLocked(l *Launch, pub *ecdsa.PublicKey) ([]byte, e
}
notAfter = notAfter.Truncate(time.Second)
if !notAfter.After(now) {
- return nil, errors.New("launch past its deadline")
+ return nil, 0, errors.New("launch past its deadline")
}
n, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
- return nil, err
+ return nil, 0, err
}
n.Add(n, big.NewInt(1))
id := ctlproxy.LaunchIdentity{LaunchID: l.LaunchID, AttemptID: l.AttemptID, VMRef: l.VMRef, Epoch: l.Epoch}
@@ -397,10 +421,10 @@ func (c *FakeController) issueLocked(l *Launch, pub *ecdsa.PublicKey) ([]byte, e
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, BasicConstraintsValid: true, URIs: []*url.URL{id.URI()}},
c.ca, pub, c.caKey)
if err != nil {
- return nil, err
+ return nil, 0, err
}
c.certs[n.Text(16)] = &issued{launch: id, pub: pub, notAfter: notAfter}
- return der, nil
+ return der, int64(c.clock.Mono() + notAfter.Sub(now)), nil
}
// feed is the current feed: every launch, every revoked or renewed serial.
@@ -410,6 +434,9 @@ func (c *FakeController) feed() hostedwire.ProxyFeed {
TTLMS: ctlproxy.FeedLifetime.Milliseconds(), Launches: []hostedwire.FeedLaunch{}, Revoked: []string{}}
for _, id := range slices.Sorted(maps.Keys(c.launches)) {
l := c.launches[id]
+ if l.Stopped {
+ continue
+ }
f.Launches = append(f.Launches, hostedwire.FeedLaunch{LaunchID: l.LaunchID, AttemptID: l.AttemptID, VMRef: l.VMRef,
Epoch: l.Epoch, Services: slices.Clone(l.Services), ProxyCodeHash: ctlproxy.CodeHash(l.Code)})
}
@@ -474,7 +501,7 @@ func (c *FakeController) route(r *http.Request, body []byte) (any, error) {
}
l, ok := c.launches[q.LaunchID]
switch {
- case !ok || l.VMRef != q.VMRef:
+ case !ok || l.Stopped || l.VMRef != q.VMRef:
return nil, refuse("not_live")
case subtle.ConstantTimeCompare([]byte(q.ProxyCode), []byte(l.Code)) != 1:
return nil, refuse("wrong_code")
@@ -505,7 +532,7 @@ func (c *FakeController) route(r *http.Request, body []byte) (any, error) {
}
l, ok := c.launches[old.launch.LaunchID]
switch {
- case !ok || l.VMRef != old.launch.VMRef:
+ case !ok || l.Stopped || l.VMRef != old.launch.VMRef:
return nil, refuse("not_live")
case l.Epoch != old.launch.Epoch:
return nil, refuse("wrong_epoch")
@@ -528,10 +555,27 @@ func (c *FakeController) route(r *http.Request, body []byte) (any, error) {
case s.revoked:
return nil, refuse("revoked")
}
+ l, ok := c.launches[s.launch.LaunchID]
+ var named struct {
+ LaunchID string `json:"launch_id"`
+ }
+ switch {
+ case !ok || l.Stopped || !l.Deadline.IsZero() && !c.clock.Wall().Before(l.Deadline) ||
+ l.AttemptID != s.launch.AttemptID || l.VMRef != s.launch.VMRef || l.Epoch != s.launch.Epoch:
+ return nil, refuse("not_live")
+ case !l.Certification:
+ return nil, refuse("not_certification")
+ case json.Unmarshal(q.Report, &named) != nil || named.LaunchID != l.LaunchID:
+ return nil, refuse("launch_mismatch")
+ }
sum := sha256.Sum256(hostedwire.CertReportMessage(q.Serial, q.Report))
if !ecdsa.VerifyASN1(s.pub, sum[:], q.Sig) {
return nil, refuse("bad_signature")
}
+ if c.reported[l.LaunchID] {
+ return nil, refuse("already_reported")
+ }
+ c.reported[l.LaunchID] = true
c.reports = append(c.reports, Report{Launch: s.launch, Serial: q.Serial, Body: slices.Clone(q.Report)})
return struct{}{}, nil
}
@@ -539,11 +583,11 @@ func (c *FakeController) route(r *http.Request, body []byte) (any, error) {
}
func (c *FakeController) answer(l *Launch, pub *ecdsa.PublicKey) (any, error) {
- der, err := c.issueLocked(l, pub)
+ der, notAfterNS, err := c.issueLocked(l, pub)
if err != nil {
return nil, refuse("not_live")
}
- return hostedwire.RedeemResponse{Cert: der, NotAfterNS: 1}, nil
+ return hostedwire.RedeemResponse{Cert: der, NotAfterNS: notAfterNS}, nil
}
func csrKey(der []byte) (*ecdsa.PublicKey, error) {
@@ -813,6 +857,9 @@ func (resolver) LookupNetIP(_ context.Context, network, host string) ([]netip.Ad
type VM struct {
Key *ecdsa.PrivateKey
Cert tls.Certificate
+ // NotAfterNS is the controller's answer for a redeemed certificate:
+ // its NotAfter on the controller's monotonic clock.
+ NotAfterNS int64
}
// Serial is the certificate's serial as the controller writes it.
@@ -867,7 +914,9 @@ func (h *Harness) Redeem(t testing.TB) *VM {
if err := json.Unmarshal(out, &r); err != nil {
t.Fatal(err)
}
- return VMFromDER(t, key, r.Cert)
+ vm := VMFromDER(t, key, r.Cert)
+ vm.NotAfterNS = r.NotAfterNS
+ return vm
}
// Issue has the controller issue a certificate for launchID straight away
@@ -882,6 +931,26 @@ func (h *Harness) Issue(t testing.TB, launchID string) *VM {
return VMFromDER(t, key, der)
}
+// CertVM adds a fresh live certification launch, launchID (VM
+// "vm-"+launchID, epoch 1, the service host), polls the proxy so its feed
+// holds it, and issues the launch's VM a certificate.
+func (h *Harness) CertVM(t testing.TB, launchID string) *VM {
+ t.Helper()
+ h.Controller.SetLaunch(Launch{LaunchID: launchID, AttemptID: "a-" + launchID, VMRef: isolatedjob.Ref("vm-" + launchID), Epoch: 1,
+ Code: strings.Repeat("D", len(ctlproxy.CodePlaceholder)), Certification: true,
+ Services: []hostedwire.Service{{Host: Host, Port: hostedproxy.ServicePort, Class: hostedwire.ServiceInstance}}})
+ if err := h.Proxy.Poll(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ return h.Issue(t, launchID)
+}
+
+// CertReport is a report body naming launchID, as Task 13's Report does.
+func CertReport(launchID, note string) []byte {
+ raw, _ := json.Marshal(map[string]string{"launch_id": launchID, "note": note})
+ return raw
+}
+
// RenewBody is a renewal request for vm's serial with a fresh key.
func RenewBody(t testing.TB, vm *VM) (*ecdsa.PrivateKey, []byte) {
t.Helper()