Skip to content

Commit

Permalink
Merge pull request #8 from spiffe/strideynet/propagate-spiffe-client-…
Browse files Browse the repository at this point in the history
…logs

Surface `spiffe-go` SDK logs to user
  • Loading branch information
strideynet authored Dec 18, 2024
2 parents 4d22e8b + 062919b commit 7839538
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/credential_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/credential_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions internal/spiffe_log_bridge.go
Original file line number Diff line number Diff line change
@@ -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...))
}

0 comments on commit 7839538

Please sign in to comment.