Skip to content

Commit 1f36f88

Browse files
committed
chore: Log HTTP errors
1 parent c1ae993 commit 1f36f88

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

tinyca/ca.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,41 +71,35 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7171
startTime := time.Now()
7272

7373
nb := r.URL.Query().Get("not-before")
74-
if nb == "" {
75-
nb = "now"
76-
}
7774
na := r.URL.Query().Get("not-after")
78-
if na == "" {
79-
na = "+1h"
80-
}
8175

8276
notBefore, notAfter, err := ParseValidity(nb, na)
8377
if err != nil {
84-
http.Error(w, err.Error(), http.StatusBadRequest)
78+
writeHTTPError(w, err.Error(), http.StatusBadRequest)
8579
return
8680
}
8781

8882
contentType, _, err := webapp.GetContentType(r.Header, webapp.MimeTypeText)
8983
if err != nil {
90-
e := fmt.Sprintf("error parsing Content-Type header: %s", err)
91-
http.Error(w, e, http.StatusBadRequest)
84+
msg := fmt.Sprintf("error parsing Content-Type header: %s", err)
85+
writeHTTPError(w, msg, http.StatusBadRequest)
9286
return
9387
}
9488

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

10195
body, err := io.ReadAll(r.Body)
10296
if err != nil {
103-
http.Error(w, err.Error(), http.StatusInternalServerError)
97+
writeHTTPError(w, err.Error(), http.StatusInternalServerError)
10498
return
10599
}
106100
csr, err := readCsr(contentType, body)
107101
if err != nil {
108-
http.Error(w, err.Error(), http.StatusBadRequest)
102+
writeHTTPError(w, err.Error(), http.StatusBadRequest)
109103
return
110104
}
111105

@@ -120,7 +114,7 @@ func (ca CA) ServeHTTP(w http.ResponseWriter, r *http.Request) {
120114
if errors.Is(err, bifrost.ErrNamespaceMismatch) {
121115
statusCode = http.StatusForbidden
122116
}
123-
http.Error(w, err.Error(), statusCode)
117+
writeHTTPError(w, err.Error(), statusCode)
124118
return
125119
}
126120

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

163-
func readCsr(contentType string, body []byte) ([]byte, error) {
164-
asn1Data := body
165-
switch contentType {
166-
case webapp.MimeTypeBytes:
167-
// DER encoded
168-
case "", webapp.MimeTypeText:
169-
// PEM
170-
block, _ := pem.Decode(body)
171-
if block == nil {
172-
return nil, fmt.Errorf("bifrost: error decoding certificate request PEM block")
173-
}
174-
asn1Data = block.Bytes
175-
}
176-
return asn1Data, nil
177-
}
178-
179157
// IssueCertificate issues a client certificate for a certificate request.
180158
// The certificate is issued with the Subject Common Name set to the
181159
// UUID of the client public key and the Subject Organization
@@ -230,3 +208,24 @@ func (ca CA) IssueCertificate(asn1CSR []byte, template *x509.Certificate) ([]byt
230208
ca.issuedTotal.Inc()
231209
return certBytes, nil
232210
}
211+
212+
func readCsr(contentType string, body []byte) ([]byte, error) {
213+
asn1Data := body
214+
switch contentType {
215+
case webapp.MimeTypeBytes:
216+
// DER encoded
217+
case "", webapp.MimeTypeText:
218+
// PEM
219+
block, _ := pem.Decode(body)
220+
if block == nil {
221+
return nil, fmt.Errorf("bifrost: error decoding certificate request PEM block")
222+
}
223+
asn1Data = block.Bytes
224+
}
225+
return asn1Data, nil
226+
}
227+
228+
func writeHTTPError(w http.ResponseWriter, msg string, statusCode int) {
229+
slog.Error(msg, "statusCode", statusCode)
230+
http.Error(w, msg, statusCode)
231+
}

0 commit comments

Comments
 (0)