Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"log"
"log/slog"
"os"
"strings"
"time"

Expand All @@ -21,16 +22,23 @@ import (
)

const (
apiTimeout = 5 * time.Second
spiffeSocket = "unix:///spiffe-workload-api/spire-agent.sock"
timeFormat = time.RFC3339
apiTimeout = 5 * time.Second
defaultSpiffeSocket = "unix:///spiffe-workload-api/spire-agent.sock"
timeFormat = time.RFC3339
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), apiTimeout)
defer cancel()

client, err := workloadapi.New(ctx, workloadapi.WithAddr(spiffeSocket), workloadapi.WithLogger(logger.Std))
// Get socket path from environment variable, with a fallback to the default.
// SPIFFE_ENDPOINT_SOCKET is the standard environment variable for this.
socketPath := os.Getenv("SPIFFE_ENDPOINT_SOCKET")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think go-spiffe automatically reads this env var.

if socketPath == "" {
socketPath = defaultSpiffeSocket
}

client, err := workloadapi.New(ctx, workloadapi.WithAddr(socketPath), workloadapi.WithLogger(logger.Std))
Comment on lines +34 to +41

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better debuggability, it would be helpful to log which socket path is being used to connect to the SPIFFE Workload API. Since this is a debug container, having this information in the logs can be very valuable. This can be done by adding a single log line after determining the socket path.

Suggested change
// Get socket path from environment variable, with a fallback to the default.
// SPIFFE_ENDPOINT_SOCKET is the standard environment variable for this.
socketPath := os.Getenv("SPIFFE_ENDPOINT_SOCKET")
if socketPath == "" {
socketPath = defaultSpiffeSocket
}
client, err := workloadapi.New(ctx, workloadapi.WithAddr(socketPath), workloadapi.WithLogger(logger.Std))
// Get socket path from environment variable, with a fallback to the default.
// SPIFFE_ENDPOINT_SOCKET is the standard environment variable for this.
socketPath := os.Getenv("SPIFFE_ENDPOINT_SOCKET")
if socketPath == "" {
socketPath = defaultSpiffeSocket
}
slog.Info("Connecting to SPIFFE Workload API", "socket_path", socketPath)
client, err := workloadapi.New(ctx, workloadapi.WithAddr(socketPath), workloadapi.WithLogger(logger.Std))


if err != nil {
log.Fatalf("Unable to create workload API client: %v", err)
Expand Down