Skip to content

Commit 0c70290

Browse files
committed
Remove mtcd executable and add 'mtc ca serve' subcommand
1 parent 09eaab6 commit 0c70290

File tree

4 files changed

+50
-44
lines changed

4 files changed

+50
-44
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,14 @@ authentication path
386386

387387
This is indeed the root of the `0`th batch, and so this certificate is valid.
388388

389-
### Spin up an HTTP server with `mtcd`
389+
### Run CA server
390390

391-
Run an HTTP server in the background to serve static files, accept queue
392-
requests, periodically issue new batches of certificate, and serve issued
393-
certificates.
394-
395-
```
396-
$ go install github.com/bwesterb/mtc/cmd/mtcd@v0.1.2
397-
```
391+
Run an HTTP server to serve static files, accept queue requests, periodically
392+
issue new batches of certificate, and serve issued certificates.
398393

399394
Start the server.
400395
```
401-
$ mtcd --listen-addr 8080 --ca-path .
396+
$ mtc ca --ca-path . serve --listen-addr localhost:8080
402397
```
403398

404399
Get and inspect CA parameters.

ca/ca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ func (h *Handle) Issue() error {
648648
}
649649

650650
func (h *Handle) issue(dt time.Time) error {
651-
slog.Info("Starting issuance", "time", dt)
651+
slog.Info("Starting issuance", "time", dt.UTC())
652652

653653
expectedStored := h.params.StoredBatches(dt)
654654
expectedActive := h.params.ActiveBatches(dt)

cmd/mtc/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,21 @@ func handleCaShowQueue(cc *cli.Context) error {
395395
return nil
396396
}
397397

398+
func handleCaServe(cc *cli.Context) error {
399+
path := cc.String("ca-path")
400+
listenAddr := cc.String("listen-addr")
401+
if listenAddr == "" {
402+
h, err := ca.Open(path)
403+
if err != nil {
404+
return err
405+
}
406+
listenAddr = h.Params().HttpServer
407+
h.Close()
408+
}
409+
s := NewServer(path, listenAddr)
410+
return s.Serve()
411+
}
412+
398413
func handleCaNew(cc *cli.Context) error {
399414
if cc.Args().Len() != 2 {
400415
err := cli.ShowSubcommandHelp(cc)
@@ -839,6 +854,18 @@ func main() {
839854
},
840855
),
841856
},
857+
{
858+
Name: "serve",
859+
Usage: "start CA server",
860+
Action: handleCaServe,
861+
Flags: append(
862+
assertionFlags(true),
863+
&cli.StringFlag{
864+
Name: "listen-addr",
865+
Usage: "Address for the server to listen on, in the form 'host:port'",
866+
},
867+
),
868+
},
842869
},
843870
},
844871
{

cmd/mtcd/main.go renamed to cmd/mtc/server.go

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,37 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76
"log/slog"
87
"os"
98
"os/signal"
10-
gopath "path"
119
"syscall"
1210
"time"
1311

14-
"github.com/bwesterb/mtc"
1512
"github.com/bwesterb/mtc/ca"
1613
"github.com/bwesterb/mtc/http"
1714
"golang.org/x/sync/errgroup"
1815
)
1916

20-
func main() {
21-
var path, listenAddr string
22-
23-
flag.StringVar(&path, "ca-path", ".", "the path to the CA state. Defaults to the current directory.")
24-
flag.StringVar(&listenAddr, "listen-addr", "", "the TCP address for the server to listen on, in the form 'host:port'.")
25-
flag.Parse()
17+
type Server struct {
18+
path string
19+
listenAddr string
20+
}
2621

27-
if listenAddr == "" {
28-
var p mtc.CAParams
29-
buf, err := os.ReadFile(gopath.Join(path, "www", "mtc", "v1", "ca-params"))
30-
if err != nil {
31-
slog.Error("failed to read ca-params", slog.Any("err", err))
32-
os.Exit(1)
33-
}
34-
if err := p.UnmarshalBinary(buf); err != nil {
35-
slog.Error("failed to unmarshal ca-params", slog.Any("err", err))
36-
os.Exit(1)
37-
}
38-
listenAddr = p.HttpServer
22+
func NewServer(path, listenAddr string) *Server {
23+
return &Server{
24+
path: path,
25+
listenAddr: listenAddr,
3926
}
27+
}
4028

29+
func (s *Server) Serve() error {
4130
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGQUIT, syscall.SIGTERM)
4231
defer cancel()
4332

44-
srv := http.NewServer(path, listenAddr)
33+
slog.Info("Starting server", slog.Any("listenAddr", s.listenAddr))
4534

46-
slog.Info("starting mtcd")
35+
srv := http.NewServer(s.path, s.listenAddr)
4736

4837
g, ctx := errgroup.WithContext(ctx)
4938

@@ -64,25 +53,20 @@ func main() {
6453
})
6554

6655
g.Go(func() error {
67-
h, err := ca.Open(path)
68-
if err != nil {
69-
slog.Error("could not start issuance loop", slog.Any("err", err))
70-
return nil
71-
}
72-
h.Close()
73-
if err := issue(path, ctx); err != nil {
56+
if err := issuanceLoop(s.path, ctx); err != nil {
7457
return fmt.Errorf("could not start issuance loop: %w", err)
7558
}
7659
return nil
7760
})
7861

7962
if err := g.Wait(); err != nil {
80-
slog.Info("unexpected errgroup error, exiting", slog.Any("err", err))
81-
os.Exit(1)
63+
return fmt.Errorf("unexpected errgroup error: %w", err)
8264
}
65+
66+
return nil
8367
}
8468

85-
func issue(path string, ctx context.Context) error {
69+
func issuanceLoop(path string, ctx context.Context) error {
8670
h, err := ca.Open(path)
8771
if err != nil {
8872
return err
@@ -97,7 +81,7 @@ func issue(path string, ctx context.Context) error {
9781
batchTime := params.NextBatchAt(time.Now())
9882
now := time.Now()
9983
if batchTime.After(now) {
100-
slog.Info("Sleeping until next batch", slog.Any("at", batchTime.UTC()))
84+
slog.Info("Sleeping until next batch is ready to issue", slog.Any("at", batchTime.UTC()))
10185
select {
10286
case <-ctx.Done():
10387
return nil

0 commit comments

Comments
 (0)