@@ -16,6 +16,7 @@ import (
16
16
"net"
17
17
"net/rpc"
18
18
"os"
19
+ "strings"
19
20
"sync"
20
21
"time"
21
22
@@ -152,6 +153,12 @@ type Sealer interface {
152
153
Unseal (* protocol.Operation ) ([]byte , error )
153
154
}
154
155
156
+ // ClientInfo has information on the client of the connection
157
+ type ClientInfo struct {
158
+ Name string
159
+ CertSerial string
160
+ }
161
+
155
162
// handler is associated with a connection and contains bookkeeping
156
163
// information used across goroutines. The channel tokens limits the
157
164
// concurrency: before reading a request a token is extracted, when
@@ -166,6 +173,7 @@ type handler struct {
166
173
conn net.Conn
167
174
timeout time.Duration
168
175
closed bool
176
+ c * ClientInfo
169
177
}
170
178
171
179
func (h * handler ) close (err error ) {
@@ -197,6 +205,12 @@ func (h *handler) handle(pkt *protocol.Packet, reqTime time.Time) {
197
205
} else {
198
206
resp = h .s .unlimitedDo (pkt , h .name )
199
207
}
208
+
209
+ if resp .op .ErrorVal () != protocol .ErrNone {
210
+ // log the client certificate information on the connection if the request failed so the caller is apparent
211
+ reqID , _ := getOperationRequestID (& pkt .Operation )
212
+ log .Errorf ("operation from client %s client cert serial: %s errored. sni %s ski %s cert %s request-id %s" , h .c .Name , h .c .CertSerial , resp .op .SNI , resp .op .SKI .String (), resp .op .CertID , reqID )
213
+ }
200
214
logRequestExecDuration (pkt .Operation .Opcode , start , resp .op .ErrorVal ())
201
215
respPkt := protocol.Packet {
202
216
Header : protocol.Header {
@@ -289,32 +303,61 @@ func makeErrResponse(pkt *protocol.Packet, err protocol.Error) response {
289
303
func addOperationRequestID (op * protocol.Operation ) string {
290
304
reqContext := make (map [string ]interface {})
291
305
var reqID string
292
- var gen bool
293
306
294
307
if len (op .ReqContext ) > 0 {
295
- if err := json .Unmarshal (op .ReqContext , & reqContext ); err == nil {
296
- if v , ok := reqContext ["request_id" ]; ok {
297
- return v .(string )
298
- } else {
299
- gen = true
300
- }
301
- } else {
302
- log .Errorf ("malformed operation.ReqContext %v, ignoring error" , op .ReqContext )
308
+ if decodeErr := json .Unmarshal (op .ReqContext , & reqContext ); decodeErr != nil {
309
+ log .Error (fmt .Errorf ("malformed operation.ReqContext %v, ignoring error" , op .ReqContext ))
310
+ return reqID
311
+ }
312
+ }
313
+
314
+ if v , ok := reqContext ["request_id" ]; ok {
315
+ return v .(string )
316
+ }
317
+
318
+ reqID = uuid .New ().String ()
319
+ reqContext ["request_id" ] = reqID
320
+ b , err := json .Marshal (reqContext )
321
+ if err == nil {
322
+ op .ReqContext = b
323
+ } else {
324
+ log .Errorf ("error marshaling operation.ReqContext %v, ignoring error" , reqContext )
325
+ reqID = ""
326
+ }
327
+ return reqID
328
+ }
329
+
330
+ func getOperationRequestID (op * protocol.Operation ) (reqID string , err error ) {
331
+ reqContext := make (map [string ]interface {})
332
+ if len (op .ReqContext ) == 0 {
333
+ return
334
+ }
335
+ if decodeErr := json .Unmarshal (op .ReqContext , & reqContext ); decodeErr == nil {
336
+ if v , ok := reqContext ["request_id" ]; ok {
337
+ return v .(string ), nil
303
338
}
339
+ } else {
340
+ err = fmt .Errorf ("malformed operation.ReqContext %v, ignoring error" , op .ReqContext )
341
+ log .Error (err )
342
+ return
304
343
}
344
+ return
345
+ }
305
346
306
- if len ( op . ReqContext ) == 0 || gen {
307
- reqID = uuid . New (). String ( )
308
- reqContext [ "request_id" ] = reqID
309
- b , err := json . Marshal ( reqContext )
310
- if err == nil {
311
- op . ReqContext = b
347
+ func getClientInfoFromCerts ( certs [] * x509. Certificate ) * ClientInfo {
348
+ cln := [] string ( nil )
349
+ srls := [] string ( nil )
350
+ for _ , cert := range certs {
351
+ if cert . Subject . CommonName != "" {
352
+ cln = append ( cln , cert . Subject . CommonName )
312
353
} else {
313
- log .Errorf ("error marshaling operation.ReqContext %v, ignoring error" , reqContext )
314
- reqID = ""
354
+ cln = append (cln , cert .DNSNames ... )
315
355
}
356
+ srls = append (srls , cert .SerialNumber .String ())
316
357
}
317
- return reqID
358
+ name := strings .Join (cln , " , " )
359
+ serial := strings .Join (srls , " , " )
360
+ return & ClientInfo {Name : name , CertSerial : serial }
318
361
}
319
362
320
363
func (s * Server ) unlimitedDo (pkt * protocol.Packet , connName string ) response {
@@ -328,7 +371,7 @@ func (s *Server) unlimitedDo(pkt *protocol.Packet, connName string) response {
328
371
reqID := addOperationRequestID (& pkt .Operation )
329
372
span .SetTag ("request_id" , reqID )
330
373
331
- log .Debugf ("connection %s: limited=false opcode=%s id=%d sni=%s ip=%s ski=%v request-id=%s" ,
374
+ log .Debugf ("connection %s: limited=false opcode= %s id=%d sni= %s ip= %s ski= %v request-id= %s" ,
332
375
connName ,
333
376
pkt .Operation .Opcode ,
334
377
pkt .Header .ID ,
@@ -412,14 +455,14 @@ func (s *Server) unlimitedDo(pkt *protocol.Packet, connName string) response {
412
455
413
456
sig , err := key .Sign (rand .Reader , pkt .Operation .Payload , crypto .Hash (0 ))
414
457
if err != nil {
415
- log .Errorf ("Connection: %s: sni=%s ski=%v request-id=%s: Signing error: %v: request-id:%s: " , connName , pkt .Operation .SNI , pkt .Operation .SKI , reqID , protocol .ErrCrypto , err , reqID )
458
+ log .Errorf ("Connection: %s: sni= %s ski= %v request-id= %s: Signing error: %v" , connName , pkt .Operation .SNI , pkt .Operation .SKI , reqID , protocol .ErrCrypto , err )
416
459
// This indicates that a remote keyserver is being used
417
460
var remoteConfigurationErr RemoteConfigurationErr
418
461
if errors .As (err , & remoteConfigurationErr ) {
419
- log .Errorf ("Connection %v: sni=%s ski=%v request-id=%s: %s: Signing error: %v request-id:%s \n " , connName , pkt .Operation .SNI , pkt .Operation .SKI , reqID , protocol .ErrRemoteConfiguration , err , reqID )
462
+ log .Errorf ("Connection %v: sni= %s ski= %v request-id= %s: %s: Signing error: %v\n " , connName , pkt .Operation .SNI , pkt .Operation .SKI , reqID , protocol .ErrRemoteConfiguration , err )
420
463
return makeErrResponse (pkt , protocol .ErrRemoteConfiguration )
421
464
} else {
422
- log .Errorf ("Connection %v: sni=%s ski=%v request-id=%s: %s: Signing error: %v request-id:%s \n " , connName , pkt .Operation .SNI , pkt .Operation .SKI , reqID , protocol .ErrCrypto , err , reqID )
465
+ log .Errorf ("Connection %v: sni= %s ski= %v request-id= %s: %s: Signing error: %v\n " , connName , pkt .Operation .SNI , pkt .Operation .SKI , reqID , protocol .ErrCrypto , err )
423
466
return makeErrResponse (pkt , protocol .ErrCrypto )
424
467
}
425
468
}
@@ -430,23 +473,23 @@ func (s *Server) unlimitedDo(pkt *protocol.Packet, connName string) response {
430
473
key , err := s .keys .Get (ctx , & pkt .Operation )
431
474
logKeyLoadDuration (loadStart )
432
475
if err != nil {
433
- log .Errorf ("failed to load key with sni=%s ip=%s ski=%v request-id=%s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err )
476
+ log .Errorf ("failed to load key with sni= %s ip= %s ski=%v request-id= %s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err )
434
477
return makeErrResponse (pkt , protocol .ErrInternal )
435
478
} else if key == nil {
436
- log .Errorf ("failed to load key with sni=%s ip=%s ski=%v request-id=%s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrKeyNotFound )
479
+ log .Errorf ("failed to load key with sni= %s ip= %s ski= %v request-id= %s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrKeyNotFound )
437
480
return makeErrResponse (pkt , protocol .ErrKeyNotFound )
438
481
}
439
482
440
483
if _ , ok := key .Public ().(* rsa.PublicKey ); ! ok {
441
- log .Errorf ("Connection %v: sni=%s request-id=%s: %s: Key is not RSA" , connName , pkt .Operation .SNI , reqID , protocol .ErrCrypto )
484
+ log .Errorf ("Connection %v: sni= %s request-id= %s: %s: Key is not RSA" , connName , pkt .Operation .SNI , reqID , protocol .ErrCrypto )
442
485
return makeErrResponse (pkt , protocol .ErrCrypto )
443
486
}
444
487
445
488
if rsaKey , ok := key .(* rsa.PrivateKey ); ok {
446
489
// Decrypt without removing padding; that's the client's responsibility.
447
490
ptxt , err := textbook_rsa .Decrypt (rsaKey , pkt .Operation .Payload )
448
491
if err != nil {
449
- log .Errorf ("connection %v: sni=%s ip=%s ski=%v request-id=%s: %v" , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err )
492
+ log .Errorf ("connection %v: sni= %s ip= %s ski= %v request-id= %s: %v" , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err )
450
493
return makeErrResponse (pkt , protocol .ErrCrypto )
451
494
}
452
495
return makeRespondResponse (pkt , ptxt )
@@ -493,10 +536,10 @@ func (s *Server) unlimitedDo(pkt *protocol.Packet, connName string) response {
493
536
key , err := s .keys .Get (ctx , & pkt .Operation )
494
537
logKeyLoadDuration (loadStart )
495
538
if err != nil {
496
- log .Errorf ("failed to load key with sni=%s ip=%s ski=%v request-id=%s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err )
539
+ log .Errorf ("failed to load key with sni= %s ip= %s ski= %v request-id= %s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err )
497
540
return makeErrResponse (pkt , protocol .ErrInternal )
498
541
} else if key == nil {
499
- log .Errorf ("failed to load key with sni=%s ip=%s ski=%v request-id=%s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrKeyNotFound )
542
+ log .Errorf ("failed to load key with sni= %s ip= %s ski= %v request-id= %s: %v" , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrKeyNotFound )
500
543
return makeErrResponse (pkt , protocol .ErrKeyNotFound )
501
544
}
502
545
@@ -526,17 +569,17 @@ func (s *Server) unlimitedDo(pkt *protocol.Packet, connName string) response {
526
569
}
527
570
if err != nil {
528
571
if attempts > 1 {
529
- log .Debugf ("Connection %v sni=%s ip=%s ski=%v request-id=%s : failed sign attempt: %s, %d attempt(s) left" , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err , attempts - 1 )
572
+ log .Debugf ("Connection %v sni= %s ip= %s ski= %v request-id= %s : failed sign attempt: %s, %d attempt(s) left" , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , err , attempts - 1 )
530
573
continue
531
574
} else {
532
575
tracing .LogError (span , err )
533
576
// This indicates that a remote keyserver is being used
534
577
var remoteConfigurationErr RemoteConfigurationErr
535
578
if errors .As (err , & remoteConfigurationErr ) {
536
- log .Errorf ("Connection %v sni=%s ip=%s ski=%v request-id=%s : %s: Signing error: %v\n " , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrRemoteConfiguration , err )
579
+ log .Errorf ("Connection %v sni= %s ip= %s ski= %v request-id= %s : %s: Signing error: %v\n " , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrRemoteConfiguration , err )
537
580
return makeErrResponse (pkt , protocol .ErrRemoteConfiguration )
538
581
} else {
539
- log .Errorf ("Connection %v sni=%s ip=%s ski=%v request-id=%s : %s: Signing error: %v\n " , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrCrypto , err )
582
+ log .Errorf ("Connection %v sni= %s ip= %s ski= %v request-id= %s : %s: Signing error: %v\n " , connName , pkt .Operation .SNI , pkt .Operation .ServerIP , pkt .Operation .SKI , reqID , protocol .ErrCrypto , err )
540
583
return makeErrResponse (pkt , protocol .ErrCrypto )
541
584
}
542
585
}
@@ -656,6 +699,7 @@ func (s *Server) spawn(l net.Listener, c net.Conn) {
656
699
}
657
700
connState := tconn .ConnectionState ()
658
701
certmetrics .Observe (certmetrics .CertSourceFromCerts (fmt .Sprintf ("listener: %s" , l .Addr ().String ()), connState .PeerCertificates )... )
702
+ cl := getClientInfoFromCerts (connState .PeerCertificates )
659
703
limited , err := s .config .isLimited (connState )
660
704
if err != nil {
661
705
log .Errorf ("connection %v: could not determine if limited: %v" , c .RemoteAddr (), err )
@@ -692,6 +736,7 @@ func (s *Server) spawn(l net.Listener, c net.Conn) {
692
736
conn : tconn ,
693
737
listener : l ,
694
738
timeout : timeout ,
739
+ c : cl ,
695
740
}
696
741
err = handler .loop ()
697
742
0 commit comments