Skip to content

Commit 55bf5a4

Browse files
committed
Add cert logging for acme/certificate api
1 parent e8c5a3b commit 55bf5a4

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

Diff for: acme/api/handler.go

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package api
22

33
import (
44
"context"
5+
"crypto/x509"
6+
"encoding/pem"
57
"fmt"
68
"net/http"
79

@@ -162,6 +164,18 @@ func (h *Handler) GetCertificate(w http.ResponseWriter, r *http.Request) {
162164
return
163165
}
164166

167+
block, _ := pem.Decode(certBytes)
168+
if block == nil {
169+
api.WriteError(w, acme.ServerInternalErr(errors.New("failed to decode any certificates from generated certBytes")))
170+
return
171+
}
172+
cert, err := x509.ParseCertificate(block.Bytes)
173+
if err != nil {
174+
api.WriteError(w, acme.Wrap(err, "failed to parse generated leaf certificate"))
175+
return
176+
}
177+
178+
api.LogCertificate(w, cert)
165179
w.Header().Set("Content-Type", "application/pem-certificate-chain; charset=utf-8")
166180
w.Write(certBytes)
167181
}

Diff for: acme/api/handler_test.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,43 @@ func TestHandlerGetCertificate(t *testing.T) {
526526
problem: acme.ServerInternalErr(errors.New("force")),
527527
}
528528
},
529+
"fail/decode-leaf-for-loggger": func(t *testing.T) test {
530+
acc := &acme.Account{ID: "accID"}
531+
ctx := context.WithValue(context.Background(), acme.AccContextKey, acc)
532+
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
533+
return test{
534+
auth: &mockAcmeAuthority{
535+
getCertificate: func(accID, id string) ([]byte, error) {
536+
assert.Equals(t, accID, acc.ID)
537+
assert.Equals(t, id, certID)
538+
return []byte("foo"), nil
539+
},
540+
},
541+
ctx: ctx,
542+
statusCode: 500,
543+
problem: acme.ServerInternalErr(errors.New("failed to decode any certificates from generated certBytes")),
544+
}
545+
},
546+
"fail/parse-x509-leaf-for-logger": func(t *testing.T) test {
547+
acc := &acme.Account{ID: "accID"}
548+
ctx := context.WithValue(context.Background(), acme.AccContextKey, acc)
549+
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
550+
return test{
551+
auth: &mockAcmeAuthority{
552+
getCertificate: func(accID, id string) ([]byte, error) {
553+
assert.Equals(t, accID, acc.ID)
554+
assert.Equals(t, id, certID)
555+
return pem.EncodeToMemory(&pem.Block{
556+
Type: "CERTIFICATE REQUEST",
557+
Bytes: []byte("foo"),
558+
}), nil
559+
},
560+
},
561+
ctx: ctx,
562+
statusCode: 500,
563+
problem: acme.ServerInternalErr(errors.New("failed to parse generated leaf certificate")),
564+
}
565+
},
529566
"ok": func(t *testing.T) test {
530567
acc := &acme.Account{ID: "accID"}
531568
ctx := context.WithValue(context.Background(), acme.AccContextKey, acc)
@@ -565,7 +602,7 @@ func TestHandlerGetCertificate(t *testing.T) {
565602
prob := tc.problem.ToACME()
566603

567604
assert.Equals(t, ae.Type, prob.Type)
568-
assert.Equals(t, ae.Detail, prob.Detail)
605+
assert.HasPrefix(t, ae.Detail, prob.Detail)
569606
assert.Equals(t, ae.Identifier, prob.Identifier)
570607
assert.Equals(t, ae.Subproblems, prob.Subproblems)
571608
assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})

Diff for: api/api.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ func logOtt(w http.ResponseWriter, token string) {
395395
}
396396
}
397397

398-
func logCertificate(w http.ResponseWriter, cert *x509.Certificate) {
398+
// LogCertificate add certificate fields to the log message.
399+
func LogCertificate(w http.ResponseWriter, cert *x509.Certificate) {
399400
if rl, ok := w.(logging.ResponseLogger); ok {
400401
m := map[string]interface{}{
401402
"serial": cert.SerialNumber,
@@ -413,7 +414,10 @@ func logCertificate(w http.ResponseWriter, cert *x509.Certificate) {
413414
if err != nil || len(rest) > 0 {
414415
break
415416
}
416-
m["provisioner"] = fmt.Sprintf("%s (%s)", val.Name, val.CredentialID)
417+
m["provisioner"] = fmt.Sprintf("%s", val.Name)
418+
if len(val.CredentialID) > 0 {
419+
m["provisioner"] = fmt.Sprintf("%s (%s)", m["provisioner"], val.CredentialID)
420+
}
417421
break
418422
}
419423
}

Diff for: api/rekey.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (h *caHandler) Rekey(w http.ResponseWriter, r *http.Request) {
5454
caPEM = certChainPEM[1]
5555
}
5656

57-
logCertificate(w, certChain[0])
57+
LogCertificate(w, certChain[0])
5858
JSONStatus(w, &SignResponse{
5959
ServerPEM: certChainPEM[0],
6060
CaPEM: caPEM,

Diff for: api/renew.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (h *caHandler) Renew(w http.ResponseWriter, r *http.Request) {
2525
caPEM = certChainPEM[1]
2626
}
2727

28-
logCertificate(w, certChain[0])
28+
LogCertificate(w, certChain[0])
2929
JSONStatus(w, &SignResponse{
3030
ServerPEM: certChainPEM[0],
3131
CaPEM: caPEM,

Diff for: api/revoke.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (h *caHandler) Revoke(w http.ResponseWriter, r *http.Request) {
9191
// TODO: should probably be checking if the certificate was revoked here.
9292
// Will need to thread that request down to the authority, so will need
9393
// to add API for that.
94-
logCertificate(w, opts.Crt)
94+
LogCertificate(w, opts.Crt)
9595
opts.MTLS = true
9696
}
9797

Diff for: api/sign.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (h *caHandler) Sign(w http.ResponseWriter, r *http.Request) {
7979
if len(certChainPEM) > 1 {
8080
caPEM = certChainPEM[1]
8181
}
82-
logCertificate(w, certChain[0])
82+
LogCertificate(w, certChain[0])
8383
JSONStatus(w, &SignResponse{
8484
ServerPEM: certChainPEM[0],
8585
CaPEM: caPEM,

0 commit comments

Comments
 (0)