Skip to content

Commit f185ce6

Browse files
authored
Merge pull request #20941 from ahrtr/20251117_tokens_3.6
[release-3.6] Print token fingerprint instead of the original tokens in log messages
2 parents d2809cf + 554dc70 commit f185ce6

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

server/auth/jwt.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ func (t *tokenJWT) assign(ctx context.Context, username string, revision uint64)
115115
return "", err
116116
}
117117

118-
t.lg.Debug(
119-
"created/assigned a new JWT token",
120-
zap.String("user-name", username),
121-
zap.Uint64("revision", revision),
122-
zap.String("token", token),
123-
)
118+
if ce := t.lg.Check(zap.DebugLevel, "created/assigned a new JWT token"); ce != nil {
119+
tokenFingerprint := redactToken(token)
120+
ce.Write(zap.String("user-name", username),
121+
zap.Uint64("revision", revision),
122+
zap.String("token-fingerprint", tokenFingerprint))
123+
}
124124
return token, err
125125
}
126126

server/auth/simple_token.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ func (t *tokenSimple) assignSimpleTokenToUser(username, token string) {
131131

132132
_, ok := t.simpleTokens[token]
133133
if ok {
134+
tokenFingerprint := redactToken(token)
134135
t.lg.Panic(
135136
"failed to assign already-used simple token to a user",
136137
zap.String("user-name", username),
137-
zap.String("token", token),
138+
zap.String("token-fingerprint", tokenFingerprint),
138139
)
139140
}
140141

server/auth/store.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ package auth
1717
import (
1818
"bytes"
1919
"context"
20+
"crypto/sha256"
2021
"encoding/base64"
22+
"encoding/hex"
2123
"errors"
2224
"sort"
2325
"strings"
@@ -349,11 +351,10 @@ func (as *authStore) Authenticate(ctx context.Context, username, password string
349351
return nil, err
350352
}
351353

352-
as.lg.Debug(
353-
"authenticated a user",
354-
zap.String("user-name", username),
355-
zap.String("token", token),
356-
)
354+
if ce := as.lg.Check(zap.DebugLevel, "authenticated a user"); ce != nil {
355+
tokenFingerprint := redactToken(token)
356+
ce.Write(zap.String("user-name", username), zap.String("token-fingerprint", tokenFingerprint))
357+
}
357358
return &pb.AuthenticateResponse{Token: token}, nil
358359
}
359360

@@ -1074,7 +1075,8 @@ func (as *authStore) AuthInfoFromCtx(ctx context.Context) (*AuthInfo, error) {
10741075
token := ts[0]
10751076
authInfo, uok := as.authInfoFromToken(ctx, token)
10761077
if !uok {
1077-
as.lg.Warn("invalid auth token", zap.String("token", token))
1078+
tokenFingerprint := redactToken(token)
1079+
as.lg.Warn("invalid auth token", zap.String("token-fingerprint", tokenFingerprint))
10781080
return nil, ErrInvalidAuthToken
10791081
}
10801082

@@ -1228,3 +1230,8 @@ func (as *authStore) setupMetricsReporter() {
12281230
}
12291231
reportCurrentAuthRevMu.Unlock()
12301232
}
1233+
1234+
func redactToken(token string) string {
1235+
sum := sha256.Sum256([]byte(token))
1236+
return hex.EncodeToString(sum[:])[:12]
1237+
}

0 commit comments

Comments
 (0)