Skip to content

Commit efb88c5

Browse files
committed
display partial hash of Vault key
1 parent b787814 commit efb88c5

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

internal/keystore/vault/log.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package vault
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"fmt"
47
"log/slog"
58
"net/http"
69
"time"
10+
11+
vaultapi "github.com/hashicorp/vault/api"
712
)
813

914
type loggingTransport struct {
@@ -21,26 +26,27 @@ func (lt *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error)
2126

2227
// don't log health checks
2328
if req.URL.Path != "/v1/sys/health" {
29+
auth := obfuscateToken(req.Header.Get(vaultapi.AuthHeaderName))
2430
switch {
2531
case err != nil:
2632
slog.Debug("HTTP error",
2733
slog.String("method", req.Method),
2834
slog.String("url", req.URL.String()),
29-
slog.String("auth", obfuscateToken(req.Header.Get("X-Vault-Token"))),
35+
slog.String("auth", auth),
3036
slog.Duration("duration", time.Since(start)),
3137
slog.String("error", err.Error()))
3238
case resp.StatusCode >= 300:
3339
slog.Debug("HTTP error response",
3440
slog.String("method", req.Method),
3541
slog.String("url", req.URL.String()),
36-
slog.String("auth", obfuscateToken(req.Header.Get("X-Vault-Token"))),
42+
slog.String("auth", auth),
3743
slog.Duration("duration", time.Since(start)),
3844
slog.String("status", resp.Status))
3945
default:
4046
slog.Debug("HTTP success response",
4147
slog.String("method", req.Method),
4248
slog.String("url", req.URL.String()),
43-
slog.String("auth", obfuscateToken(req.Header.Get("X-Vault-Token"))),
49+
slog.String("auth", auth),
4450
slog.Duration("duration", time.Since(start)),
4551
slog.String("status", resp.Status))
4652
}
@@ -50,12 +56,9 @@ func (lt *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error)
5056
}
5157

5258
func obfuscateToken(token string) string {
53-
switch {
54-
case len(token) == 0:
59+
if len(token) == 0 {
5560
return ""
56-
case len(token) > 8:
57-
return "***" + token[len(token)-4:]
58-
default:
59-
return "***"
6061
}
62+
hash := sha256.Sum256([]byte(token))
63+
return fmt.Sprintf("%s (hashed)", hex.EncodeToString(hash[:16]))
6164
}

0 commit comments

Comments
 (0)