interceptors.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package server
  13. import (
  14. "context"
  15. "log"
  16. "time"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/credentials"
  19. "google.golang.org/grpc/peer"
  20. "google.golang.org/grpc/tap"
  21. )
  22. // LoggingUnaryInterceptor logs all RPC calls with connection details.
  23. func LoggingUnaryInterceptor(verbose bool) grpc.UnaryServerInterceptor {
  24. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
  25. start := time.Now()
  26. // Get peer information
  27. p, ok := peer.FromContext(ctx)
  28. var peerAddr string
  29. if ok {
  30. peerAddr = p.Addr.String()
  31. if verbose {
  32. LogTLSConnectionInfo(p)
  33. }
  34. } else {
  35. peerAddr = "unknown"
  36. }
  37. log.Printf("[RPC] --> %s from %s", info.FullMethod, peerAddr)
  38. // Call the handler
  39. resp, err := handler(ctx, req)
  40. duration := time.Since(start)
  41. if err != nil {
  42. log.Printf("[RPC] <-- %s failed in %v: %v", info.FullMethod, duration, err)
  43. } else {
  44. log.Printf("[RPC] <-- %s succeeded in %v", info.FullMethod, duration)
  45. }
  46. return resp, err
  47. }
  48. }
  49. // ConnectionTapHandler logs every connection attempt with detailed information.
  50. func ConnectionTapHandler(ctx context.Context, info *tap.Info) (context.Context, error) {
  51. log.Printf("[CONNECTION] New connection attempt: %+v", info)
  52. return ctx, nil
  53. }
  54. // LogTLSConnectionInfo logs detailed TLS handshake information.
  55. func LogTLSConnectionInfo(p *peer.Peer) {
  56. if p == nil {
  57. log.Printf("[TLS] No peer information available")
  58. return
  59. }
  60. log.Printf("[TLS] Connection from: %s", p.Addr.String())
  61. authInfo := p.AuthInfo
  62. if authInfo == nil {
  63. log.Printf("[TLS] WARNING: No auth info - connection may not be using TLS")
  64. return
  65. }
  66. tlsInfo, ok := authInfo.(credentials.TLSInfo)
  67. if !ok {
  68. log.Printf("[TLS] WARNING: Auth info is not TLS type: %T", authInfo)
  69. return
  70. }
  71. state := tlsInfo.State
  72. log.Printf("[TLS] Handshake complete: version=0x%04x (%s), cipher=0x%04x, resumed=%v",
  73. state.Version, TLSVersionName(state.Version), state.CipherSuite, state.DidResume)
  74. if state.ServerName != "" {
  75. log.Printf("[TLS] SNI server name: %s", state.ServerName)
  76. }
  77. // Log peer certificates
  78. if len(state.PeerCertificates) > 0 {
  79. log.Printf("[TLS] Peer presented %d certificate(s)", len(state.PeerCertificates))
  80. for i, cert := range state.PeerCertificates {
  81. log.Printf("[TLS] Cert %d: Subject=%s, Issuer=%s, NotBefore=%s, NotAfter=%s",
  82. i, cert.Subject, cert.Issuer, cert.NotBefore, cert.NotAfter)
  83. if len(cert.DNSNames) > 0 {
  84. log.Printf("[TLS] Cert %d: DNS names=%v", i, cert.DNSNames)
  85. }
  86. }
  87. } else {
  88. log.Printf("[TLS] WARNING: No peer certificates - mTLS may not be working")
  89. }
  90. // Log verified chains
  91. if len(state.VerifiedChains) > 0 {
  92. log.Printf("[TLS] Successfully verified %d certificate chain(s)", len(state.VerifiedChains))
  93. } else {
  94. log.Printf("[TLS] WARNING: No verified certificate chains")
  95. }
  96. }