hosted: cert bundle -empty writes the empty certificate set
The dev drill's withdrawal (7.2) needs {"certificates":[]} for the
controller to reload. bundle -empty writes it, with the same -cert-keys
and -out; with a -cert it is a usage error, and the plain form still
refuses to write a set with no certificate (Task 34, the coordinator's
ruling on open question 3).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+59
−2
cmd/equipo-hosted-certify/bundle_test.go+47 −0cmd/equipo-hosted-certify/main.go+12 −2
diff --git a/cmd/equipo-hosted-certify/bundle_test.go b/cmd/equipo-hosted-certify/bundle_test.go| index af02fedf..5102b0f5 100644 |
| --- a/cmd/equipo-hosted-certify/bundle_test.go |
| +++ b/cmd/equipo-hosted-certify/bundle_test.go |
| @@ -1,9 +1,11 @@ |
| package main |
| import ( |
| + "bytes" |
| "context" |
| "crypto/ed25519" |
| "encoding/base64" |
| + "encoding/json" |
| "os" |
| "path/filepath" |
| "strings" |
| @@ -123,3 +125,48 @@ func TestBundleRefuses(t *testing.T) { |
| t.Errorf("bundle with every argument: %v", err) |
| } |
| } |
| + |
| +// TestBundleEmpty (Task 34, the coordinator's ruling on open question 3): |
| +// -empty writes the empty set, {"certificates":[]}, which the controller |
| +// loads as no certificate (the dev drill's withdrawal, 7.2). The plain |
| +// form still refuses to write a set with no certificate in it |
| +// (TestBundleRefuses), and -empty with a -cert is a usage error. |
| +func TestBundleEmpty(t *testing.T) { |
| + keys := certKeys(t, "paul-1", paulKey.Public().(ed25519.PublicKey)) |
| + out := filepath.Join(t.TempDir(), "certificates.json") |
| + code, stdout, stderr := runCmd(t, "bundle", "-cert-keys", keys, "-empty", "-out", out) |
| + if code != 0 { |
| + t.Fatalf("bundle -empty = %d %s %s", code, stdout, stderr) |
| + } |
| + got, err := os.ReadFile(out) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var compact bytes.Buffer |
| + if err := json.Compact(&compact, got); err != nil || compact.String() != `{"certificates":[]}` { |
| + t.Fatalf("bundle -empty wrote %s (%v)", got, err) |
| + } |
| + keysJSON, _ := os.ReadFile(keys) |
| + if n, err := ctlcert.NewSet(nil).Load(keysJSON, got); err != nil || n != 0 { |
| + t.Fatalf("the controller loads the empty set: %d, %v", n, err) |
| + } |
| + if !strings.Contains(stdout, `"certificates":0`) { |
| + t.Errorf("bundle -empty printed %s", stdout) |
| + } |
| + a := signed(t, "l-1") |
| + both := filepath.Join(t.TempDir(), "certificates.json") |
| + if code, stdout, stderr := runCmd(t, "bundle", "-cert-keys", keys, "-empty", "-cert", a, "-out", both); code != exitUsage { |
| + t.Errorf("bundle -empty -cert = %d %s %s, want %d", code, stdout, stderr, exitUsage) |
| + } |
| + if _, err := os.Stat(both); !os.IsNotExist(err) { |
| + t.Errorf("bundle -empty -cert wrote %s (%v)", both, err) |
| + } |
| + for _, args := range [][]string{ |
| + {"bundle", "-empty", "-out", "o.json"}, |
| + {"bundle", "-cert-keys", "k.json", "-empty"}, |
| + } { |
| + if _, err := parseCommand(context.Background(), args, time.Now); err == nil || !strings.Contains(err.Error(), "required") { |
| + t.Errorf("%v: %v, want a refusal naming it required", args, err) |
| + } |
| + } |
| +} |
diff --git a/cmd/equipo-hosted-certify/main.go b/cmd/equipo-hosted-certify/main.go| index c32fb322..f17e0bce 100644 |
| --- a/cmd/equipo-hosted-certify/main.go |
| +++ b/cmd/equipo-hosted-certify/main.go |
| @@ -30,6 +30,10 @@ |
| // {"certificates":[...]}) from certificates sign wrote, one per -cert, |
| // in order. Each must be one certificate that verifies under the key |
| // file, and none may be listed twice; otherwise nothing is written. |
| +// bundle -cert-keys FILE -empty -out FILE |
| +// writes the empty set, {"certificates":[]}, which withdraws every |
| +// certificate at the next reload. Without -empty a set with no |
| +// certificate is refused. |
| // reload (controller flags) POST /v1/admin/reload |
| // alert-test (controller flags) POST /v1/admin/alert-test |
| // |
| @@ -189,11 +193,17 @@ func parseCommand(ctx context.Context, args []string, now func() time.Time) (fun |
| fs.Var(&certs, "cert", "a certificate sign wrote; repeat for each one the set keeps") |
| keys := fs.String("cert-keys", "", "the certificate key file (controller/cert-keys.json)") |
| out := fs.String("out", "", "where to write the set (controller/certificates.json)") |
| + empty := fs.Bool("empty", false, "write the empty set, withdrawing every certificate (no -cert)") |
| if err := parse(fs, args[1:]); err != nil { |
| return nil, err |
| } |
| - if len(certs) == 0 || *keys == "" || *out == "" { |
| - return nil, usage("-cert, -cert-keys and -out are required") |
| + switch { |
| + case *empty && len(certs) > 0: |
| + return nil, usage("-empty writes the empty set; it takes no -cert") |
| + case *empty && (*keys == "" || *out == ""): |
| + return nil, usage("-cert-keys and -out are required") |
| + case !*empty && (len(certs) == 0 || *keys == "" || *out == ""): |
| + return nil, usage("-cert, -cert-keys and -out are required (or -empty for the empty set)") |
| } |
| return func() (any, error) { return bundle(certs, *keys, *out) }, nil |
| case "reload", "alert-test": |