Skip to content

Commit

Permalink
use request ID logger where possible (#209)
Browse files Browse the repository at this point in the history
* use request ID logger where possible

Signed-off-by: Bob Callaway <bob.callaway@gmail.com>

* swap last statement to use context logger

Signed-off-by: Bob Callaway <bob.callaway@gmail.com>
  • Loading branch information
bobcallaway authored Oct 22, 2021
1 parent 892a5c2 commit 067e55b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
11 changes: 6 additions & 5 deletions pkg/api/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

func SigningCertHandler(params operations.SigningCertParams, principal *oidc.IDToken) middleware.Responder {
ctx := params.HTTPRequest.Context()
logger := log.ContextLogger(ctx)

// none of the following cases should happen if the authentication path is working correctly; checking to be defensive
if principal == nil {
Expand All @@ -61,7 +62,7 @@ func SigningCertHandler(params operations.SigningCertParams, principal *oidc.IDT
case "googleca":
PemCertificate, PemCertificateChain, err = GoogleCASigningCertHandler(ctx, subj, publicKeyPEM)
case "pkcs11ca":
PemCertificate, PemCertificateChain, err = Pkcs11CASigningCertHandler(subj, publicKey)
PemCertificate, PemCertificateChain, err = Pkcs11CASigningCertHandler(ctx, subj, publicKey)
default:
return handleFulcioAPIError(params, http.StatusInternalServerError, err, genericCAError)
}
Expand All @@ -70,7 +71,7 @@ func SigningCertHandler(params operations.SigningCertParams, principal *oidc.IDT
}

// Submit to CTL
log.Logger.Info("Submitting CTL inclusion for OIDC grant: ", subj.Value)
logger.Info("Submitting CTL inclusion for OIDC grant: ", subj.Value)
var sctBytes []byte
ctURL := viper.GetString("ct-log-url")
if ctURL != "" {
Expand All @@ -83,10 +84,10 @@ func SigningCertHandler(params operations.SigningCertParams, principal *oidc.IDT
if err != nil {
return handleFulcioAPIError(params, http.StatusInternalServerError, err, failedToMarshalSCT)
}
log.Logger.Info("CTL Submission Signature Received: ", sct.Signature)
log.Logger.Info("CTL Submission ID Received: ", sct.ID)
logger.Info("CTL Submission Signature Received: ", sct.Signature)
logger.Info("CTL Submission ID Received: ", sct.ID)
} else {
log.Logger.Info("Skipping CT log upload.")
logger.Info("Skipping CT log upload.")
}

metricNewEntries.Inc()
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/googleca_signing_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
)

func GoogleCASigningCertHandler(ctx context.Context, subj *challenges.ChallengeResult, publicKey []byte) (string, []string, error) {
logger := log.ContextLogger(ctx)

parent := viper.GetString("gcp_private_ca_parent")

Expand All @@ -42,7 +43,7 @@ func GoogleCASigningCertHandler(ctx context.Context, subj *challenges.ChallengeR
privca = googleca.GithubWorkflowSubject(subj.Value)
}
req := googleca.Req(parent, privca, publicKey)
log.Logger.Infof("requesting cert from %s for %v", parent, Subject)
logger.Infof("requesting cert from %s for %v", parent, Subject)

resp, err := googleca.Client().CreateCertificate(ctx, req)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/api/pkcs11ca_signing_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package api

import (
"context"
"crypto/x509"
"encoding/pem"
"os"
Expand All @@ -28,7 +29,8 @@ import (
"github.com/spf13/viper"
)

func Pkcs11CASigningCertHandler(subj *challenges.ChallengeResult, publicKey []byte) (string, []string, error) {
func Pkcs11CASigningCertHandler(ctx context.Context, subj *challenges.ChallengeResult, publicKey []byte) (string, []string, error) {
logger := log.ContextLogger(ctx)

p11Ctx, err := pkcs11.InitHSMCtx()
if err != nil {
Expand All @@ -53,7 +55,7 @@ func Pkcs11CASigningCertHandler(subj *challenges.ChallengeResult, publicKey []by
}
block, _ := pem.Decode(pubPEMData)
if block == nil || block.Type != "CERTIFICATE" {
log.Logger.Fatal("failed to decode PEM block containing certificate")
logger.Fatal("failed to decode PEM block containing certificate")
}
rootCA, err = x509.ParseCertificate(block.Bytes)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ func WithRequestID(ctx context.Context, id string) context.Context {
}

func RequestIDLogger(r *http.Request) *zap.SugaredLogger {
return ContextLogger(r.Context())
}

func ContextLogger(ctx context.Context) *zap.SugaredLogger {
proposedLogger := Logger
if r != nil {
if ctxRequestID, ok := r.Context().Value(middleware.RequestIDKey).(string); ok {
if ctx != nil {
if ctxRequestID, ok := ctx.Value(middleware.RequestIDKey).(string); ok {
proposedLogger = proposedLogger.With(zap.String("requestID", ctxRequestID))
}
}
Expand Down

0 comments on commit 067e55b

Please sign in to comment.