From 853f0841c9ab85bb27815638dad7fa182c5baa24 Mon Sep 17 00:00:00 2001 From: Malte Poll <1780588+malt3@users.noreply.github.com> Date: Mon, 8 Jan 2024 18:13:30 +0100 Subject: [PATCH] logger: use env-aware slog handler for subsystems --- coordinator/coordapi.go | 4 +++- coordinator/intercom.go | 4 +++- coordinator/main.go | 8 ++++++-- coordinator/mesh.go | 4 +++- initializer/main.go | 8 ++++++-- internal/attestation/snp/cachedClient.go | 4 +++- internal/attestation/snp/issuer.go | 3 ++- internal/attestation/snp/validator.go | 6 ++++-- tools/parsesnp.go | 8 ++++++-- 9 files changed, 36 insertions(+), 13 deletions(-) diff --git a/coordinator/coordapi.go b/coordinator/coordapi.go index a9d79ca29c..07c90d7da2 100644 --- a/coordinator/coordapi.go +++ b/coordinator/coordapi.go @@ -11,6 +11,7 @@ import ( "github.com/edgelesssys/nunki/internal/attestation/snp" "github.com/edgelesssys/nunki/internal/coordapi" "github.com/edgelesssys/nunki/internal/grpc/atlscredentials" + "github.com/edgelesssys/nunki/internal/logger" "github.com/edgelesssys/nunki/internal/manifest" "github.com/edgelesssys/nunki/internal/memstore" "google.golang.org/grpc" @@ -36,12 +37,13 @@ func newCoordAPIServer(mSGetter manifestSetGetter, caGetter certChainGetter, log grpc.Creds(credentials), grpc.KeepaliveParams(keepalive.ServerParameters{Time: 15 * time.Second}), ) + handler := logger.NewHandler(log.Handler(), "coordapi") s := &coordAPIServer{ grpc: grpcServer, policyTextStore: memstore.New[manifest.HexString, manifest.Policy](), manifSetGetter: mSGetter, caChainGetter: caGetter, - logger: log.WithGroup("coordapi"), + logger: slog.New(handler), } coordapi.RegisterCoordAPIServer(s.grpc, s) return s diff --git a/coordinator/intercom.go b/coordinator/intercom.go index 3b34459988..640a162aa8 100644 --- a/coordinator/intercom.go +++ b/coordinator/intercom.go @@ -11,6 +11,7 @@ import ( "github.com/edgelesssys/nunki/internal/attestation/snp" "github.com/edgelesssys/nunki/internal/grpc/atlscredentials" "github.com/edgelesssys/nunki/internal/intercom" + "github.com/edgelesssys/nunki/internal/logger" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/keepalive" @@ -37,11 +38,12 @@ func newIntercomServer(meshAuth *meshAuthority, caGetter certChainGetter, log *s grpc.Creds(credentials), grpc.KeepaliveParams(keepalive.ServerParameters{Time: 15 * time.Second}), ) + handler := logger.NewHandler(log.Handler(), "intercom") s := &intercomServer{ grpc: grpcServer, certGet: meshAuth, caChainGetter: caGetter, - logger: log.WithGroup("intercom"), + logger: slog.New(handler), } intercom.RegisterIntercomServer(s.grpc, s) return s diff --git a/coordinator/main.go b/coordinator/main.go index bca49b91c2..6f9bdfee06 100644 --- a/coordinator/main.go +++ b/coordinator/main.go @@ -3,13 +3,13 @@ package main import ( "errors" "fmt" - "log/slog" "net" "os" "github.com/edgelesssys/nunki/internal/ca" "github.com/edgelesssys/nunki/internal/coordapi" "github.com/edgelesssys/nunki/internal/intercom" + "github.com/edgelesssys/nunki/internal/logger" ) func main() { @@ -19,7 +19,11 @@ func main() { } func run() (retErr error) { - logger := slog.Default() + logger, err := logger.Default() + if err != nil { + fmt.Fprintf(os.Stderr, "Error: creating logger: %v\n", err) + return err + } defer func() { if retErr != nil { logger.Error(retErr.Error()) diff --git a/coordinator/mesh.go b/coordinator/mesh.go index 1dad23b68d..d8d8c9fb8f 100644 --- a/coordinator/mesh.go +++ b/coordinator/mesh.go @@ -13,6 +13,7 @@ import ( "github.com/edgelesssys/nunki/internal/appendable" "github.com/edgelesssys/nunki/internal/ca" + "github.com/edgelesssys/nunki/internal/logger" "github.com/edgelesssys/nunki/internal/manifest" "github.com/google/go-sev-guest/abi" "github.com/google/go-sev-guest/kds" @@ -29,11 +30,12 @@ type meshAuthority struct { } func newMeshAuthority(ca *ca.CA, log *slog.Logger) *meshAuthority { + handler := logger.NewHandler(log.Handler(), "mesh-authority") return &meshAuthority{ ca: ca, certs: make(map[string][]byte), manifests: new(appendable.Appendable[manifest.Manifest]), - logger: log.WithGroup("mesh-authority"), + logger: slog.New(handler), } } diff --git a/initializer/main.go b/initializer/main.go index f1b727e184..8e22df4d69 100644 --- a/initializer/main.go +++ b/initializer/main.go @@ -11,7 +11,6 @@ import ( "encoding/pem" "errors" "fmt" - "log/slog" "net" "os" "time" @@ -20,6 +19,7 @@ import ( "github.com/edgelesssys/nunki/internal/attestation/snp" "github.com/edgelesssys/nunki/internal/grpc/dialer" "github.com/edgelesssys/nunki/internal/intercom" + "github.com/edgelesssys/nunki/internal/logger" ) func main() { @@ -29,7 +29,11 @@ func main() { } func run() (retErr error) { - logger := slog.Default() + logger, err := logger.Default() + if err != nil { + fmt.Fprintf(os.Stderr, "Error: creating logger: %v\n", err) + return err + } defer func() { if retErr != nil { logger.Error(retErr.Error()) diff --git a/internal/attestation/snp/cachedClient.go b/internal/attestation/snp/cachedClient.go index 2695d77170..2b6e9b6977 100644 --- a/internal/attestation/snp/cachedClient.go +++ b/internal/attestation/snp/cachedClient.go @@ -3,6 +3,7 @@ package snp import ( "log/slog" + "github.com/edgelesssys/nunki/internal/logger" "github.com/edgelesssys/nunki/internal/memstore" "github.com/google/go-sev-guest/verify/trust" ) @@ -16,9 +17,10 @@ type cachedKDSHTTPClient struct { func newCachedKDSHTTPClient(log *slog.Logger) *cachedKDSHTTPClient { trust.DefaultHTTPSGetter() + handler := logger.NewHandler(log.Handler(), "cached-kds-http-client") return &cachedKDSHTTPClient{ HTTPSGetter: trust.DefaultHTTPSGetter(), - logger: log.WithGroup("cached-kds-http-client"), + logger: slog.New(handler), cache: memstore.New[string, cacheEntry](), } } diff --git a/internal/attestation/snp/issuer.go b/internal/attestation/snp/issuer.go index cfebb98e2d..73918bd730 100644 --- a/internal/attestation/snp/issuer.go +++ b/internal/attestation/snp/issuer.go @@ -14,6 +14,7 @@ import ( "fmt" "log/slog" + "github.com/edgelesssys/nunki/internal/logger" "github.com/google/go-sev-guest/client" ) @@ -24,7 +25,7 @@ type Issuer struct { // NewIssuer returns a new Issuer. func NewIssuer(log *slog.Logger) *Issuer { - return &Issuer{logger: log.WithGroup("snp-issuer")} + return &Issuer{logger: slog.New(logger.NewHandler(log.Handler(), "snp-issuer"))} } // OID returns the OID of the issuer. diff --git a/internal/attestation/snp/validator.go b/internal/attestation/snp/validator.go index 4c4cd1ff17..83847878b5 100644 --- a/internal/attestation/snp/validator.go +++ b/internal/attestation/snp/validator.go @@ -14,6 +14,7 @@ import ( "fmt" "log/slog" + "github.com/edgelesssys/nunki/internal/logger" "github.com/google/go-sev-guest/abi" "github.com/google/go-sev-guest/proto/sevsnp" "github.com/google/go-sev-guest/validate" @@ -51,9 +52,10 @@ func (v *StaticValidateOptsGenerator) SNPValidateOpts(_ *sevsnp.Report) (*valida // NewValidator returns a new Validator. func NewValidator(optsGen validateOptsGenerator, log *slog.Logger) *Validator { + handler := logger.NewHandler(log.Handler(), "snp-validator") return &Validator{ validateOptsGen: optsGen, - logger: log.WithGroup("snp-validator"), + logger: slog.New(handler), } } @@ -63,7 +65,7 @@ func NewValidatorWithCallbacks(optsGen validateOptsGenerator, log *slog.Logger, validateOptsGen: optsGen, callbackers: callbacks, kdsGetter: newCachedKDSHTTPClient(log), - logger: log.WithGroup("snp-validator"), + logger: slog.New(logger.NewHandler(log.Handler(), "snp-validator")), } } diff --git a/tools/parsesnp.go b/tools/parsesnp.go index 2e06be43de..e8f6469bac 100644 --- a/tools/parsesnp.go +++ b/tools/parsesnp.go @@ -5,9 +5,9 @@ import ( "encoding/hex" "fmt" "io" - "log/slog" "os" + "github.com/edgelesssys/nunki/internal/logger" "github.com/google/go-sev-guest/abi" ) @@ -51,7 +51,11 @@ func main() { } func run() (retErr error) { - logger := slog.Default() + logger, err := logger.Default() + if err != nil { + fmt.Fprintf(os.Stderr, "Error: creating logger: %v\n", err) + return err + } defer func() { if retErr != nil { logger.Error(retErr.Error())