Skip to content

Commit d157279

Browse files
authored
feat: CA creates a http.ServeMux with its routes (#15)
CA creates a "standard" mux which responds to "POST /issue" with CA.ServeHTTP. It also responds to "GET /namespace" with a plain-text UUID namespace string.
1 parent 9d490a8 commit d157279

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

cmd/bf/ca.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,19 @@ var caServeCmd = &cli.Command{
9898
"notAfter", cert.NotAfter,
9999
)
100100

101-
mux := http.NewServeMux()
102-
103-
if exposeMetrics {
104-
slog.InfoContext(ctx, "metrics enabled")
105-
mux.HandleFunc("GET /metrics", webapp.MetricsHandler)
106-
}
107-
108101
ca, err := tinyca.New(cert, key, nil)
109102
if err != nil {
110103
slog.ErrorContext(ctx, "error creating CA", "error", err)
111104
return cli.Exit("Error creating CA", 1)
112105
}
113106
defer ca.Close()
114107

115-
mux.Handle("POST /issue", ca)
108+
mux := ca.ServeMux()
116109

117-
nss := cert.Namespace.String()
118-
mux.HandleFunc("GET /namespace", func(w http.ResponseWriter, r *http.Request) {
119-
fmt.Fprint(w, nss)
120-
})
110+
if exposeMetrics {
111+
slog.InfoContext(ctx, "metrics enabled")
112+
mux.HandleFunc("GET /metrics", webapp.MetricsHandler)
113+
}
121114

122115
if webEnabled {
123116
slog.InfoContext(ctx, "web interface enabled", "staticPath", webStaticPath)
@@ -127,7 +120,7 @@ var caServeCmd = &cli.Command{
127120
hdlr := webapp.RequestLogger(mux)
128121

129122
addr := fmt.Sprintf("%s:%d", caHost, caPort)
130-
slog.InfoContext(ctx, "starting server", "address", addr, "namespace", nss)
123+
slog.InfoContext(ctx, "starting server", "address", addr, "namespace", cert.Namespace)
131124

132125
server := http.Server{Addr: addr, Handler: hdlr}
133126

tinyca/ca.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ func (ca *CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
174174
}
175175
}
176176

177+
// ServeMux returns an http.ServeMux with the CA's HTTP handler registered at "POST /issue".
178+
// The ServeMux also provides a "GET /namespace" endpoint that returns the namespace of the CA.
179+
func (ca *CA) ServeMux() *http.ServeMux {
180+
mux := http.NewServeMux()
181+
mux.Handle("POST /issue", ca)
182+
nss := ca.cert.Namespace.String()
183+
mux.HandleFunc("GET /namespace", func(w http.ResponseWriter, r *http.Request) {
184+
w.Header().Set("Content-Type", "text/plain")
185+
fmt.Fprintln(w, nss)
186+
})
187+
return mux
188+
}
189+
177190
// IssueCertificate issues a client certificate for a valid certificate request parsed from asn1CSR.
178191
func (ca *CA) IssueCertificate(asn1CSR []byte, notBefore, notAfter time.Time) ([]byte, error) {
179192
issueStart := time.Now()

0 commit comments

Comments
 (0)