From 062919bb4690d43b7672fc281e893756664469e7 Mon Sep 17 00:00:00 2001 From: Noah Stride Date: Wed, 18 Dec 2024 13:42:10 +0000 Subject: [PATCH] Surface `spiffe-go` SDK logs to user Signed-off-by: Noah Stride --- cmd/credential_file.go | 2 ++ cmd/credential_process.go | 2 ++ internal/spiffe_log_bridge.go | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 internal/spiffe_log_bridge.go diff --git a/cmd/credential_file.go b/cmd/credential_file.go index 6788822..5413aa1 100644 --- a/cmd/credential_file.go +++ b/cmd/credential_file.go @@ -49,6 +49,7 @@ func oneshotX509CredentialFile( client, err := workloadapi.New( ctx, workloadapi.WithAddr(sf.workloadAPIAddr), + workloadapi.WithLogger(internal.NewSPIFFESlogAdapter(slog.Default())), ) if err != nil { return fmt.Errorf("creating workload api client: %w", err) @@ -144,6 +145,7 @@ func daemonX509CredentialFile( client, err := workloadapi.New( ctx, workloadapi.WithAddr(sf.workloadAPIAddr), + workloadapi.WithLogger(internal.NewSPIFFESlogAdapter(slog.Default())), ) if err != nil { return fmt.Errorf("creating workload api client: %w", err) diff --git a/cmd/credential_process.go b/cmd/credential_process.go index 79e762d..f85f5a7 100644 --- a/cmd/credential_process.go +++ b/cmd/credential_process.go @@ -7,6 +7,7 @@ import ( "os" "github.com/spf13/cobra" + "github.com/spiffe/aws-spiffe-workload-helper/internal" "github.com/spiffe/go-spiffe/v2/workloadapi" ) @@ -21,6 +22,7 @@ func newX509CredentialProcessCmd() (*cobra.Command, error) { client, err := workloadapi.New( ctx, workloadapi.WithAddr(sf.workloadAPIAddr), + workloadapi.WithLogger(internal.NewSPIFFESlogAdapter(slog.Default())), ) if err != nil { return fmt.Errorf("creating workload api client: %w", err) diff --git a/internal/spiffe_log_bridge.go b/internal/spiffe_log_bridge.go new file mode 100644 index 0000000..6264ace --- /dev/null +++ b/internal/spiffe_log_bridge.go @@ -0,0 +1,36 @@ +package internal + +import ( + "fmt" + "log/slog" + + "github.com/spiffe/go-spiffe/v2/logger" +) + +var _ logger.Logger = (*SPIFFESlogAdapter)(nil) + +// SPIFFESlogAdapter allows a slog logger to be injected into the SPIFFE GO +// SDK. +type SPIFFESlogAdapter struct { + slog *slog.Logger +} + +func NewSPIFFESlogAdapter(slog *slog.Logger) SPIFFESlogAdapter { + return SPIFFESlogAdapter{slog: slog} +} + +func (w SPIFFESlogAdapter) Debugf(format string, args ...interface{}) { + w.slog.Debug(fmt.Sprintf(format, args...)) +} + +func (w SPIFFESlogAdapter) Infof(format string, args ...interface{}) { + w.slog.Info(fmt.Sprintf(format, args...)) +} + +func (w SPIFFESlogAdapter) Warnf(format string, args ...interface{}) { + w.slog.Warn(fmt.Sprintf(format, args...)) +} + +func (w SPIFFESlogAdapter) Errorf(format string, args ...interface{}) { + w.slog.Error(fmt.Sprintf(format, args...)) +}