Skip to content

Commit

Permalink
fix: Clean up web static files loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthb committed May 27, 2024
1 parent 682c43e commit e5f963b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
10 changes: 8 additions & 2 deletions cmd/bf/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var caServeCmd = &cli.Command{
Name: "web-static-path",
Usage: "read web static files from `PATH`",
Sources: cli.EnvVars("WEB_STATIC_PATH"),
Value: "embed",
Destination: &webStaticPath,
},
&cli.BoolFlag{
Expand All @@ -86,11 +87,16 @@ var caServeCmd = &cli.Command{
if err != nil {
return cli.Exit(fmt.Sprintf("Error reading cert/key: %s", err), 1)
}
slog.DebugContext(
ctx, "loaded CA certificate and private key",
"notBefore", cert.NotBefore,
"notAfter", cert.NotAfter,
)

mux := http.NewServeMux()

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

Expand All @@ -107,7 +113,7 @@ var caServeCmd = &cli.Command{
})

if webEnabled {
slog.DebugContext(ctx, "web enabled", "staticFiles", webStaticPath)
slog.InfoContext(ctx, "web interface enabled", "staticPath", webStaticPath)
webapp.AddRoutes(mux, webStaticPath, cert.Namespace)
}

Expand Down
8 changes: 5 additions & 3 deletions internal/webapp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
// local filesystem. Otherwise, it will serve them from the embedded filesystem.
func AddRoutes(mux *http.ServeMux, staticFilesPath string, ns uuid.UUID) {
index := Index(ns)
static := http.FileServer(http.FS(web.Static))
if staticFilesPath != "" {
static = http.FileServer(http.Dir("web/static"))
var static http.Handler
if staticFilesPath == "embed" {
static = http.FileServer(http.FS(web.Static))
} else {
static = http.FileServer(http.Dir(staticFilesPath))
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
Expand Down
21 changes: 12 additions & 9 deletions tinyca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package tinyca

import (
"context"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
Expand Down Expand Up @@ -73,33 +74,35 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
nb := r.URL.Query().Get("not-before")
na := r.URL.Query().Get("not-after")

ctx := r.Context()

notBefore, notAfter, err := ParseValidity(nb, na)
if err != nil {
writeHTTPError(w, err.Error(), http.StatusBadRequest)
writeHTTPError(ctx, w, err.Error(), http.StatusBadRequest)
return
}

contentType, _, err := webapp.GetContentType(r.Header, webapp.MimeTypeText)
if err != nil {
msg := fmt.Sprintf("error parsing Content-Type header: %s", err)
writeHTTPError(w, msg, http.StatusBadRequest)
writeHTTPError(ctx, w, msg, http.StatusBadRequest)
return
}

if ct := contentType; ct != webapp.MimeTypeText && ct != webapp.MimeTypeBytes {
msg := fmt.Sprintf("unsupported Content-Type %s", ct)
writeHTTPError(w, msg, http.StatusUnsupportedMediaType)
writeHTTPError(ctx, w, msg, http.StatusUnsupportedMediaType)
return
}

body, err := io.ReadAll(r.Body)
if err != nil {
writeHTTPError(w, err.Error(), http.StatusInternalServerError)
writeHTTPError(ctx, w, err.Error(), http.StatusInternalServerError)
return
}
csr, err := readCsr(contentType, body)
if err != nil {
writeHTTPError(w, err.Error(), http.StatusBadRequest)
writeHTTPError(ctx, w, err.Error(), http.StatusBadRequest)
return
}

Expand All @@ -114,7 +117,7 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if errors.Is(err, bifrost.ErrNamespaceMismatch) {
statusCode = http.StatusForbidden
}
writeHTTPError(w, err.Error(), statusCode)
writeHTTPError(ctx, w, err.Error(), statusCode)
return
}

Expand Down Expand Up @@ -148,7 +151,7 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if err != nil {
slog.Error("error writing certificate response", "err", err)
slog.ErrorContext(ctx, "error writing certificate response", "err", err)
}

ca.requestsDuration.Update(time.Since(startTime).Seconds())
Expand Down Expand Up @@ -225,7 +228,7 @@ func readCsr(contentType string, body []byte) ([]byte, error) {
return asn1Data, nil
}

func writeHTTPError(w http.ResponseWriter, msg string, statusCode int) {
slog.Error(msg, "statusCode", statusCode)
func writeHTTPError(ctx context.Context, w http.ResponseWriter, msg string, statusCode int) {
slog.ErrorContext(ctx, msg, "statusCode", statusCode)
http.Error(w, msg, statusCode)
}

0 comments on commit e5f963b

Please sign in to comment.