Skip to content

Commit

Permalink
feat: CA creates a http.ServeMux with its routes (#15)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ananthb authored Jun 17, 2024
1 parent 9d490a8 commit d157279
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 6 additions & 13 deletions cmd/bf/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,19 @@ var caServeCmd = &cli.Command{
"notAfter", cert.NotAfter,
)

mux := http.NewServeMux()

if exposeMetrics {
slog.InfoContext(ctx, "metrics enabled")
mux.HandleFunc("GET /metrics", webapp.MetricsHandler)
}

ca, err := tinyca.New(cert, key, nil)
if err != nil {
slog.ErrorContext(ctx, "error creating CA", "error", err)
return cli.Exit("Error creating CA", 1)
}
defer ca.Close()

mux.Handle("POST /issue", ca)
mux := ca.ServeMux()

nss := cert.Namespace.String()
mux.HandleFunc("GET /namespace", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, nss)
})
if exposeMetrics {
slog.InfoContext(ctx, "metrics enabled")
mux.HandleFunc("GET /metrics", webapp.MetricsHandler)
}

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

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

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

Expand Down
13 changes: 13 additions & 0 deletions tinyca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ func (ca *CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// ServeMux returns an http.ServeMux with the CA's HTTP handler registered at "POST /issue".
// The ServeMux also provides a "GET /namespace" endpoint that returns the namespace of the CA.
func (ca *CA) ServeMux() *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("POST /issue", ca)
nss := ca.cert.Namespace.String()
mux.HandleFunc("GET /namespace", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, nss)
})
return mux
}

// IssueCertificate issues a client certificate for a valid certificate request parsed from asn1CSR.
func (ca *CA) IssueCertificate(asn1CSR []byte, notBefore, notAfter time.Time) ([]byte, error) {
issueStart := time.Now()
Expand Down

0 comments on commit d157279

Please sign in to comment.