Skip to content

Commit

Permalink
fix: don't mutate log variable outside closure
Browse files Browse the repository at this point in the history
  • Loading branch information
smlx committed Jan 16, 2024
1 parent b711d2b commit 7847400
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/sshportalapi/sshportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func sshportal(ctx context.Context, log *slog.Logger, c *nats.EncodedConn,
ctx, span := otel.Tracer(pkgName).Start(ctx, SubjectSSHAccessQuery)
defer span.End()
requestsCounter.Inc()
log = log.With(slog.Any("query", query))
log := log.With(slog.Any("query", query))
// sanity check the query
if query.SSHFingerprint == "" || query.NamespaceName == "" {
log.Warn("malformed sshportal query")
Expand Down
4 changes: 1 addition & 3 deletions internal/sshserver/authhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ func pubKeyAuth(log *slog.Logger, nc *nats.EncodedConn,
c *k8s.Client) ssh.PublicKeyHandler {
return func(ctx ssh.Context, key ssh.PublicKey) bool {
authAttemptsTotal.Inc()
log = log.With(
slog.String("sessionID", ctx.SessionID()),
)
log := log.With(slog.String("sessionID", ctx.SessionID()))
// parse SSH public key
pubKey, err := gossh.ParsePublicKey(key.Marshal())
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions internal/sshserver/sessionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ func sessionHandler(log *slog.Logger, c *k8s.Client,
return func(s ssh.Session) {
sessionTotal.Inc()
ctx := s.Context()
log = log.With(
slog.String("sessionID", ctx.SessionID()),
)
log := log.With(slog.String("sessionID", ctx.SessionID()))
log.Debug("starting session",
slog.Any("rawCommand", s.Command()),
slog.String("subsystem", s.Subsystem()),
Expand Down
3 changes: 1 addition & 2 deletions internal/sshtoken/authhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
func pubKeyAuth(log *slog.Logger, ldb LagoonDBService) ssh.PublicKeyHandler {
return func(ctx ssh.Context, key ssh.PublicKey) bool {
authnAttemptsTotal.Inc()
log = log.With(slog.String("sessionID", ctx.SessionID()))
log := log.With(slog.String("sessionID", ctx.SessionID()))
// parse SSH public key
pubKey, err := gossh.ParsePublicKey(key.Marshal())
if err != nil {
Expand All @@ -59,7 +59,6 @@ func pubKeyAuth(log *slog.Logger, ldb LagoonDBService) ssh.PublicKeyHandler {
authnSuccessTotal.Inc()
ctx.SetValue(userUUID, user.UUID)
log.Info("authentication successful",
slog.String("fingerprint", fingerprint),
slog.String("userID", user.UUID.String()))
return true
}
Expand Down
2 changes: 1 addition & 1 deletion internal/sshtoken/sessionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func sessionHandler(log *slog.Logger, p *rbac.Permission,
sessionTotal.Inc()
// extract required info from the session context
ctx := s.Context()
log = log.With(slog.String("sessionID", ctx.SessionID()))
log := log.With(slog.String("sessionID", ctx.SessionID()))
uid, ok := ctx.Value(userUUID).(*uuid.UUID)
if !ok {
log.Warn("couldn't get user UUID from context")
Expand Down

0 comments on commit 7847400

Please sign in to comment.