server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package server
  14. import (
  15. "log"
  16. "time"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/credentials"
  19. "google.golang.org/grpc/keepalive"
  20. )
  21. // Options holds configuration options for creating a gRPC server.
  22. type Options struct {
  23. EnableTLS bool
  24. Verbose bool
  25. }
  26. // ServerOptions is kept as a compatibility alias for existing callers.
  27. //
  28. //nolint:revive // Compatibility alias for the previous exported API.
  29. type ServerOptions = Options
  30. // NewGRPCServer creates a new gRPC server with standard configuration.
  31. func NewGRPCServer(opts Options) (*grpc.Server, error) {
  32. var grpcOpts []grpc.ServerOption
  33. // Add keepalive parameters for better connection diagnostics
  34. grpcOpts = append(grpcOpts,
  35. grpc.KeepaliveParams(keepalive.ServerParameters{
  36. MaxConnectionIdle: 15 * time.Minute,
  37. MaxConnectionAge: 30 * time.Minute,
  38. MaxConnectionAgeGrace: 5 * time.Minute,
  39. Time: 5 * time.Minute,
  40. Timeout: 1 * time.Minute,
  41. }),
  42. grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
  43. MinTime: 5 * time.Second, // Allow pings every 5s (client sends every 10s)
  44. PermitWithoutStream: true,
  45. }),
  46. )
  47. if opts.Verbose {
  48. log.Printf("[CONFIG] Keepalive configured: idle=15m, age=30m, time=5m, timeout=1m, minPing=5s")
  49. }
  50. // Add connection tap handler for verbose mode
  51. if opts.Verbose {
  52. grpcOpts = append(grpcOpts, grpc.InTapHandle(ConnectionTapHandler))
  53. log.Printf("[CONFIG] Connection tap handler enabled")
  54. }
  55. // Add RPC interceptors: metrics and logging
  56. grpcOpts = append(grpcOpts, grpc.ChainUnaryInterceptor(
  57. MetricsUnaryInterceptor(),
  58. LoggingUnaryInterceptor(opts.Verbose),
  59. ))
  60. log.Printf("[CONFIG] RPC metrics and logging interceptors enabled")
  61. // Configure TLS if enabled
  62. if opts.EnableTLS {
  63. log.Printf("[TLS] Loading TLS configuration...")
  64. tlsConfig, err := LoadTLSConfig(DefaultTLSConfig())
  65. if err != nil {
  66. return nil, err
  67. }
  68. // Log TLS configuration details
  69. log.Printf("[TLS] Configuration loaded successfully")
  70. log.Printf("[TLS] Min TLS version: 0x%04x (%s)", tlsConfig.MinVersion, TLSVersionName(tlsConfig.MinVersion))
  71. log.Printf("[TLS] Max TLS version: 0x%04x (%s)", tlsConfig.MaxVersion, TLSVersionName(tlsConfig.MaxVersion))
  72. log.Printf("[TLS] Client auth required: %v", tlsConfig.ClientAuth == 4) // tls.RequireAndVerifyClientCert
  73. if tlsConfig.ClientCAs != nil {
  74. subjects := tlsConfig.ClientCAs.Subjects()
  75. log.Printf("[TLS] Client CA pool has %d certificate(s)", len(subjects))
  76. if opts.Verbose {
  77. for i, subject := range subjects {
  78. log.Printf("[TLS] CA %d: %s", i, string(subject))
  79. }
  80. }
  81. } else {
  82. log.Printf("[TLS] WARNING: No client CA pool configured")
  83. }
  84. if len(tlsConfig.Certificates) > 0 {
  85. log.Printf("[TLS] Server has %d certificate(s)", len(tlsConfig.Certificates))
  86. for i, cert := range tlsConfig.Certificates {
  87. if len(cert.Certificate) > 0 {
  88. log.Printf("[TLS] Cert %d: raw certificate data present", i)
  89. }
  90. }
  91. } else {
  92. log.Printf("[TLS] WARNING: No server certificates configured")
  93. }
  94. grpcOpts = append(grpcOpts, grpc.Creds(credentials.NewTLS(tlsConfig)))
  95. log.Printf("[TLS] mTLS enabled for provider server")
  96. } else {
  97. log.Printf("[SECURITY] WARNING: TLS DISABLED - NOT SUITABLE FOR PRODUCTION")
  98. log.Printf("[SECURITY] All traffic will be transmitted in PLAINTEXT")
  99. }
  100. return grpc.NewServer(grpcOpts...), nil
  101. }