Skip to content

Commit

Permalink
chore: Log HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthb committed Mar 8, 2024
1 parent c1ae993 commit 1f36f88
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions tinyca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,35 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()

nb := r.URL.Query().Get("not-before")
if nb == "" {
nb = "now"
}
na := r.URL.Query().Get("not-after")
if na == "" {
na = "+1h"
}

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

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

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

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

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

Expand Down Expand Up @@ -160,22 +154,6 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ca.requestsDuration.Update(time.Since(startTime).Seconds())
}

func readCsr(contentType string, body []byte) ([]byte, error) {
asn1Data := body
switch contentType {
case webapp.MimeTypeBytes:
// DER encoded
case "", webapp.MimeTypeText:
// PEM
block, _ := pem.Decode(body)
if block == nil {
return nil, fmt.Errorf("bifrost: error decoding certificate request PEM block")
}
asn1Data = block.Bytes
}
return asn1Data, nil
}

// IssueCertificate issues a client certificate for a certificate request.
// The certificate is issued with the Subject Common Name set to the
// UUID of the client public key and the Subject Organization
Expand Down Expand Up @@ -230,3 +208,24 @@ func (ca CA) IssueCertificate(asn1CSR []byte, template *x509.Certificate) ([]byt
ca.issuedTotal.Inc()
return certBytes, nil
}

func readCsr(contentType string, body []byte) ([]byte, error) {
asn1Data := body
switch contentType {
case webapp.MimeTypeBytes:
// DER encoded
case "", webapp.MimeTypeText:
// PEM
block, _ := pem.Decode(body)
if block == nil {
return nil, fmt.Errorf("bifrost: error decoding certificate request PEM block")
}
asn1Data = block.Bytes
}
return asn1Data, nil
}

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

0 comments on commit 1f36f88

Please sign in to comment.