@@ -17,6 +17,7 @@ import (
1717 "os"
1818 "os/exec"
1919 "path/filepath"
20+ "reflect"
2021 "strconv"
2122 "strings"
2223 "sync"
@@ -164,6 +165,10 @@ func sessionHandler(session ssh.Session) {
164165}
165166
166167func publicKeyHandler (ctx ssh.Context , key ssh.PublicKey ) bool {
168+ // FIXME: the "ssh.Context" is not thread-safe, so db operations should use the immutable parent "Context"
169+ // TODO: Remove after https://github.com/gliderlabs/ssh/pull/211
170+ parentCtx := reflect .ValueOf (ctx ).Elem ().FieldByName ("Context" ).Interface ().(context.Context )
171+
167172 if log .IsDebug () { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
168173 log .Debug ("Handle Public Key: Fingerprint: %s from %s" , gossh .FingerprintSHA256 (key ), ctx .RemoteAddr ())
169174 }
@@ -189,7 +194,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
189194 // look for the exact principal
190195 principalLoop:
191196 for _ , principal := range cert .ValidPrincipals {
192- pkey , err := asymkey_model .SearchPublicKeyByContentExact (ctx , principal )
197+ pkey , err := asymkey_model .SearchPublicKeyByContentExact (parentCtx , principal )
193198 if err != nil {
194199 if asymkey_model .IsErrKeyNotExist (err ) {
195200 log .Debug ("Principal Rejected: %s Unknown Principal: %s" , ctx .RemoteAddr (), principal )
@@ -246,7 +251,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
246251 log .Debug ("Handle Public Key: %s Fingerprint: %s is not a certificate" , ctx .RemoteAddr (), gossh .FingerprintSHA256 (key ))
247252 }
248253
249- pkey , err := asymkey_model .SearchPublicKeyByContent (ctx , strings .TrimSpace (string (gossh .MarshalAuthorizedKey (key ))))
254+ pkey , err := asymkey_model .SearchPublicKeyByContent (parentCtx , strings .TrimSpace (string (gossh .MarshalAuthorizedKey (key ))))
250255 if err != nil {
251256 if asymkey_model .IsErrKeyNotExist (err ) {
252257 log .Warn ("Unknown public key: %s from %s" , gossh .FingerprintSHA256 (key ), ctx .RemoteAddr ())
0 commit comments