diff --git a/certificate-authority/service/grpc/service.go b/certificate-authority/service/grpc/service.go index cf1c579d9..d28f36b2f 100644 --- a/certificate-authority/service/grpc/service.go +++ b/certificate-authority/service/grpc/service.go @@ -20,7 +20,7 @@ func New(config Config, clientApplicationServer *CertificateAuthorityServer, val if err != nil { return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - server, err := server.New(config.BaseConfig, fileWatcher, logger, opts...) + server, err := server.New(config.BaseConfig, fileWatcher, logger, tracerProvider, opts...) if err != nil { return nil, err } diff --git a/cloud2cloud-connector/service/service.go b/cloud2cloud-connector/service/service.go index 976e93e9b..e777af946 100644 --- a/cloud2cloud-connector/service/service.go +++ b/cloud2cloud-connector/service/service.go @@ -195,10 +195,9 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg return nil, fmt.Errorf("cannot create open telemetry collector client: %w", err) } otelClient.AddCloseFunc(cancel) - tracerProvider := otelClient.GetTracerProvider() - listener, err := listener.New(config.APIs.HTTP.Connection, fileWatcher, logger) + listener, err := listener.New(config.APIs.HTTP.Connection, fileWatcher, logger, tracerProvider) if err != nil { otelClient.Close() return nil, fmt.Errorf("cannot create http server: %w", err) diff --git a/cloud2cloud-gateway/service/service.go b/cloud2cloud-gateway/service/service.go index d55894234..ffea10235 100644 --- a/cloud2cloud-gateway/service/service.go +++ b/cloud2cloud-gateway/service/service.go @@ -231,10 +231,9 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg if err != nil { return nil, fmt.Errorf("cannot create open telemetry collector client: %w", err) } - tracerProvider := otelClient.GetTracerProvider() - listener, err := listener.New(config.APIs.HTTP.Connection, fileWatcher, logger) + listener, err := listener.New(config.APIs.HTTP.Connection, fileWatcher, logger, tracerProvider) if err != nil { return nil, fmt.Errorf("cannot create http server: %w", err) } diff --git a/cloud2cloud-gateway/test/events.go b/cloud2cloud-gateway/test/events.go index a09168917..3437a9133 100644 --- a/cloud2cloud-gateway/test/events.go +++ b/cloud2cloud-gateway/test/events.go @@ -19,6 +19,7 @@ import ( "github.com/plgd-dev/kit/v2/codec/json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/trace/noop" ) type EventsServer struct { @@ -108,7 +109,7 @@ func NewEventsServer(t *testing.T, uri string) *EventsServer { fileWatcher, err := fsnotify.NewWatcher(logger) require.NoError(t, err) - certManager, err := server.New(listenCfg.TLS, fileWatcher, logger) + certManager, err := server.New(listenCfg.TLS, fileWatcher, logger, noop.NewTracerProvider()) require.NoError(t, err) listener, err := tls.Listen("tcp", listenCfg.Addr, certManager.GetTLSConfig()) diff --git a/cloud2cloud-gateway/test/test.go b/cloud2cloud-gateway/test/test.go index 500dbee87..29343c7c7 100644 --- a/cloud2cloud-gateway/test/test.go +++ b/cloud2cloud-gateway/test/test.go @@ -19,6 +19,7 @@ import ( "github.com/plgd-dev/hub/v2/test/config" testHttp "github.com/plgd-dev/hub/v2/test/http" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/trace/noop" ) func MakeStorageConfig() service.StorageConfig { @@ -116,7 +117,7 @@ func NewTestListener(t *testing.T) (net.Listener, func()) { fileWatcher, err := fsnotify.NewWatcher(logger) require.NoError(t, err) - certManager, err := server.New(listenCfg.TLS, fileWatcher, logger) + certManager, err := server.New(listenCfg.TLS, fileWatcher, logger, noop.NewTracerProvider()) require.NoError(t, err) listener, err := tls.Listen("tcp", listenCfg.Addr, certManager.GetTLSConfig()) diff --git a/coap-gateway/service/service.go b/coap-gateway/service/service.go index 484cc06ed..8adad487c 100644 --- a/coap-gateway/service/service.go +++ b/coap-gateway/service/service.go @@ -227,7 +227,6 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg return nil, fmt.Errorf("cannot create open telemetry collector client: %w", err) } otelClient.AddCloseFunc(cancel) - tracerProvider := otelClient.GetTracerProvider() queue, err := queue.New(config.TaskQueue) @@ -342,7 +341,7 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg tracerProvider: tracerProvider, } - ss, err := s.createServices(fileWatcher, logger) + ss, err := s.createServices(fileWatcher, logger, tracerProvider) if err != nil { nats.Close() return nil, fmt.Errorf("cannot create services: %w", err) @@ -577,7 +576,7 @@ func (s *Service) authMiddleware(next mux.Handler) mux.Handler { } // createServices setup services for coap-gateway. -func (s *Service) createServices(fileWatcher *fsnotify.Watcher, logger log.Logger) (*service.Service, error) { +func (s *Service) createServices(fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (*service.Service, error) { setHandlerError := func(uri string, err error) error { return fmt.Errorf("failed to set %v handler: %w", uri, err) } @@ -617,7 +616,7 @@ func (s *Service) createServices(fileWatcher *fsnotify.Watcher, logger log.Logge return nil, setHandlerError(plgdtime.ResourceURI, err) } - services, err := coapService.New(s.ctx, s.config.APIs.COAP.Config, m, fileWatcher, logger, + services, err := coapService.New(s.ctx, s.config.APIs.COAP.Config, m, fileWatcher, logger, tracerProvider, coapService.WithOnNewConnection(s.coapConnOnNew), coapService.WithOnInactivityConnection(s.onInactivityConnection), coapService.WithMessagePool(s.messagePool), diff --git a/device-provisioning-service/service/http/service.go b/device-provisioning-service/service/http/service.go index ea0eabbf4..48450538a 100644 --- a/device-provisioning-service/service/http/service.go +++ b/device-provisioning-service/service/http/service.go @@ -31,7 +31,7 @@ func New(ctx context.Context, serviceName string, config Config, fileWatcher *fs if err != nil { return nil, fmt.Errorf("cannot create validator: %w", err) } - listener, err := listener.New(config.Connection, fileWatcher, logger) + listener, err := listener.New(config.Connection, fileWatcher, logger, tracerProvider) if err != nil { validator.Close() return nil, fmt.Errorf("cannot create grpc server: %w", err) diff --git a/device-provisioning-service/service/service.go b/device-provisioning-service/service/service.go index 7d9ec3a19..74f8f5ca6 100644 --- a/device-provisioning-service/service/service.go +++ b/device-provisioning-service/service/service.go @@ -185,7 +185,7 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg enrollmentGroupsCache: enrollmentGroupsCache, } - ss, err := s.createServices(fileWatcher, logger) + ss, err := s.createServices(fileWatcher, logger, tracerProvider) if err != nil { if httpService != nil { httpService.Close() @@ -327,7 +327,7 @@ func (server *Service) toHandler(h func(ctx context.Context, req *mux.Message, s } // createServices setups coap server -func (server *Service) createServices(fileWatcher *fsnotify.Watcher, logger log.Logger) (*service.Service, error) { +func (server *Service) createServices(fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (*service.Service, error) { setHandlerError := func(uri string, err error) error { return fmt.Errorf("failed to set %v handler: %w", uri, err) } @@ -351,7 +351,7 @@ func (server *Service) createServices(fileWatcher *fsnotify.Watcher, logger log. return nil, setHandlerError(uri.CloudConfiguration, err) } - return coapService.New(server.ctx, server.config.APIs.COAP.Config, m, fileWatcher, logger, + return coapService.New(server.ctx, server.config.APIs.COAP.Config, m, fileWatcher, logger, tracerProvider, coapService.WithOnNewConnection(server.coapConnOnNew), coapService.WithOnInactivityConnection(server.onInactivityConnection), coapService.WithMessagePool(server.messagePool), diff --git a/grpc-gateway/service/service.go b/grpc-gateway/service/service.go index f54474da8..a7fae809c 100644 --- a/grpc-gateway/service/service.go +++ b/grpc-gateway/service/service.go @@ -34,7 +34,7 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg validator.Close() return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - server, err := server.New(config.APIs.GRPC.Config.BaseConfig, fileWatcher, logger, opts...) + server, err := server.New(config.APIs.GRPC.Config.BaseConfig, fileWatcher, logger, tracerProvider, opts...) if err != nil { validator.Close() otelClient.Close() diff --git a/identity-store/service/service.go b/identity-store/service/service.go index debca7d13..ae3241d4c 100644 --- a/identity-store/service/service.go +++ b/identity-store/service/service.go @@ -75,7 +75,7 @@ func NewService(persistence Persistence, publisher *publisher.Publisher, ownerCl } func NewServer(ctx context.Context, cfg Config, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, publisher *publisher.Publisher, grpcOpts ...grpc.ServerOption) (*Server, error) { - grpcServer, err := server.New(cfg.APIs.GRPC.BaseConfig, fileWatcher, logger, grpcOpts...) + grpcServer, err := server.New(cfg.APIs.GRPC.BaseConfig, fileWatcher, logger, tracerProvider, grpcOpts...) if err != nil { return nil, fmt.Errorf("cannot create grpc listener: %w", err) } diff --git a/m2m-oauth-server/service/grpc/service.go b/m2m-oauth-server/service/grpc/service.go index 60744f758..184212fc2 100644 --- a/m2m-oauth-server/service/grpc/service.go +++ b/m2m-oauth-server/service/grpc/service.go @@ -20,7 +20,7 @@ func New(config Config, m2mOAuthServiceServer *M2MOAuthServiceServer, validator if err != nil { return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - server, err := server.New(config.BaseConfig, fileWatcher, logger, opts...) + server, err := server.New(config.BaseConfig, fileWatcher, logger, tracerProvider, opts...) if err != nil { return nil, err } diff --git a/pkg/net/coap/service/service.go b/pkg/net/coap/service/service.go index b0e7df78c..23eea7785 100644 --- a/pkg/net/coap/service/service.go +++ b/pkg/net/coap/service/service.go @@ -16,6 +16,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/fsnotify" "github.com/plgd-dev/hub/v2/pkg/log" "github.com/plgd-dev/hub/v2/pkg/service" + "go.opentelemetry.io/otel/trace" ) func BlockWiseTransferSZXFromString(s string) (blockwise.SZX, error) { @@ -49,7 +50,7 @@ func closeOnError(services []service.APIService, logger log.Logger) { } } -func newService(protocol Protocol, config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, opts ...interface { +func newService(protocol Protocol, config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, opts ...interface { coapTcpServer.Option coapDtlsServer.Option coapUdpServer.Option @@ -57,14 +58,14 @@ func newService(protocol Protocol, config Config, serviceOpts Options, fileWatch ) (service.APIService, error) { switch protocol { case TCP: - coapServer, err := newTCPServer(config, serviceOpts, fileWatcher, logger, opts...) + coapServer, err := newTCPServer(config, serviceOpts, fileWatcher, logger, tracerProvider, opts...) if err != nil { return nil, fmt.Errorf("cannot create tcp server: %w", err) } return coapServer, nil case UDP: if config.TLS.IsEnabled() { - coapServer, err := newDTLSServer(config, serviceOpts, fileWatcher, logger, opts...) + coapServer, err := newDTLSServer(config, serviceOpts, fileWatcher, logger, tracerProvider, opts...) if err != nil { return nil, fmt.Errorf("cannot create dtls server: %w", err) } @@ -90,7 +91,7 @@ func makeOnInactivityConnection(logger log.Logger) func(conn mux.Conn) { } // New creates server. -func New(ctx context.Context, config Config, router *mux.Router, fileWatcher *fsnotify.Watcher, logger log.Logger, opt ...func(*Options)) (*service.Service, error) { +func New(ctx context.Context, config Config, router *mux.Router, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, opt ...func(*Options)) (*service.Service, error) { err := config.Validate() if err != nil { return nil, err @@ -131,7 +132,7 @@ func New(ctx context.Context, config Config, router *mux.Router, fileWatcher *fs if protocol == UDP && !config.BlockwiseTransfer.Enabled { logger.Warnf("It's possible that UDP messages bigger than MTU (1500) will be dropped, since apis.coap.blockwiseTransfer.enabled is set to false.") } - service, err := newService(protocol, config, serviceOpts, fileWatcher, logger, opts...) + service, err := newService(protocol, config, serviceOpts, fileWatcher, logger, tracerProvider, opts...) if err != nil { closeOnError(services, logger) return nil, err diff --git a/pkg/net/coap/service/service_test.go b/pkg/net/coap/service/service_test.go index 32d000615..e1ff0ebb2 100644 --- a/pkg/net/coap/service/service_test.go +++ b/pkg/net/coap/service/service_test.go @@ -18,6 +18,7 @@ import ( "github.com/plgd-dev/hub/v2/test/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/trace/noop" "go.uber.org/atomic" ) @@ -101,7 +102,7 @@ func TestNew(t *testing.T) { logger := log.NewLogger(log.MakeDefaultConfig()) fileWatcher, err := fsnotify.NewWatcher(logger) require.NoError(t, err) - got, err := New(context.Background(), tt.args.config, router, fileWatcher, logger, tt.args.options...) + got, err := New(context.Background(), tt.args.config, router, fileWatcher, logger, noop.NewTracerProvider(), tt.args.options...) if tt.wantErr { require.Error(t, err) return @@ -146,7 +147,7 @@ func TestOnClientInactivityTCP(t *testing.T) { defer cancel() closeChan := make(chan struct{}, 2) - got, err := New(ctx, cfg, router, fileWatcher, logger, WithOnNewConnection(func(conn mux.Conn) { + got, err := New(ctx, cfg, router, fileWatcher, logger, noop.NewTracerProvider(), WithOnNewConnection(func(conn mux.Conn) { conn.AddOnClose(func() { closeChan <- struct{}{} }) @@ -211,7 +212,7 @@ func TestOnClientInactivityUDP(t *testing.T) { defer cancel() closeChan := make(chan struct{}, 2) - got, err := New(ctx, cfg, router, fileWatcher, logger, WithOnNewConnection(func(conn mux.Conn) { + got, err := New(ctx, cfg, router, fileWatcher, logger, noop.NewTracerProvider(), WithOnNewConnection(func(conn mux.Conn) { conn.AddOnClose(func() { closeChan <- struct{}{} }) @@ -277,7 +278,7 @@ func TestOnClientInactivityCustomTCP(t *testing.T) { var numInactiveClients atomic.Int32 closeChan := make(chan struct{}, 2) - got, err := New(ctx, cfg, router, fileWatcher, logger, WithOnInactivityConnection(func(conn mux.Conn) { + got, err := New(ctx, cfg, router, fileWatcher, logger, noop.NewTracerProvider(), WithOnInactivityConnection(func(conn mux.Conn) { numInactiveClients.Inc() errC := conn.Close() require.NoError(t, errC) @@ -348,7 +349,7 @@ func TestOnClientInactivityCustomUDP(t *testing.T) { var numInactiveClients atomic.Int32 closeChan := make(chan struct{}, 2) - got, err := New(ctx, cfg, router, fileWatcher, logger, WithOnInactivityConnection(func(conn mux.Conn) { + got, err := New(ctx, cfg, router, fileWatcher, logger, noop.NewTracerProvider(), WithOnInactivityConnection(func(conn mux.Conn) { numInactiveClients.Inc() errC := conn.Close() require.NoError(t, errC) diff --git a/pkg/net/coap/service/tcpServer.go b/pkg/net/coap/service/tcpServer.go index 96a9439a8..10df1fa68 100644 --- a/pkg/net/coap/service/tcpServer.go +++ b/pkg/net/coap/service/tcpServer.go @@ -13,6 +13,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/fsnotify" "github.com/plgd-dev/hub/v2/pkg/log" certManagerServer "github.com/plgd-dev/hub/v2/pkg/security/certManager/server" + "go.opentelemetry.io/otel/trace" ) type tcpServer struct { @@ -30,7 +31,7 @@ func (s *tcpServer) Close() error { return nil } -func newTCPListener(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger) (coapTcpServer.Listener, func(), error) { +func newTCPListener(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (coapTcpServer.Listener, func(), error) { if !config.TLS.IsEnabled() { listener, err := net.NewTCPListener("tcp", config.Addr) if err != nil { @@ -45,7 +46,7 @@ func newTCPListener(config Config, serviceOpts Options, fileWatcher *fsnotify.Wa } var closeListener fn.FuncList - coapsTLS, err := certManagerServer.New(config.TLS.Embedded, fileWatcher, logger) + coapsTLS, err := certManagerServer.New(config.TLS.Embedded, fileWatcher, logger, tracerProvider) if err != nil { return nil, nil, fmt.Errorf("cannot create tls cert manager: %w", err) } @@ -67,13 +68,13 @@ func newTCPListener(config Config, serviceOpts Options, fileWatcher *fsnotify.Wa return listener, closeListener.ToFunction(), nil } -func newTCPServer(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, opts ...interface { +func newTCPServer(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, opts ...interface { coapTcpServer.Option coapDtlsServer.Option coapUdpServer.Option }, ) (*tcpServer, error) { - listener, closeListener, err := newTCPListener(config, serviceOpts, fileWatcher, logger) + listener, closeListener, err := newTCPListener(config, serviceOpts, fileWatcher, logger, tracerProvider) if err != nil { return nil, fmt.Errorf("cannot create listener: %w", err) } diff --git a/pkg/net/coap/service/udpServer.go b/pkg/net/coap/service/udpServer.go index 5ae9a9c85..629264ee4 100644 --- a/pkg/net/coap/service/udpServer.go +++ b/pkg/net/coap/service/udpServer.go @@ -15,6 +15,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/fsnotify" "github.com/plgd-dev/hub/v2/pkg/log" certManagerServer "github.com/plgd-dev/hub/v2/pkg/security/certManager/server" + "go.opentelemetry.io/otel/trace" ) type dtlsServer struct { @@ -101,9 +102,9 @@ func TLSConfigToDTLSConfig(tlsConfig *tls.Config) *dtls.Config { } } -func newDTLSListener(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger) (coapDtlsServer.Listener, func(), error) { +func newDTLSListener(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (coapDtlsServer.Listener, func(), error) { var closeListener fn.FuncList - coapsTLS, err := certManagerServer.New(config.TLS.Embedded, fileWatcher, logger) + coapsTLS, err := certManagerServer.New(config.TLS.Embedded, fileWatcher, logger, tracerProvider) if err != nil { return nil, nil, fmt.Errorf("cannot create tls cert manager: %w", err) } @@ -127,13 +128,13 @@ func newDTLSListener(config Config, serviceOpts Options, fileWatcher *fsnotify.W return listener, closeListener.ToFunction(), nil } -func newDTLSServer(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, opts ...interface { +func newDTLSServer(config Config, serviceOpts Options, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, opts ...interface { coapTcpServer.Option coapDtlsServer.Option coapUdpServer.Option }, ) (*dtlsServer, error) { - listener, closeListener, err := newDTLSListener(config, serviceOpts, fileWatcher, logger) + listener, closeListener, err := newDTLSListener(config, serviceOpts, fileWatcher, logger, tracerProvider) if err != nil { return nil, fmt.Errorf("cannot create listener: %w", err) } diff --git a/pkg/net/grpc/server/server.go b/pkg/net/grpc/server/server.go index 7a7c5d30d..e0ffc410d 100644 --- a/pkg/net/grpc/server/server.go +++ b/pkg/net/grpc/server/server.go @@ -6,16 +6,17 @@ import ( "github.com/plgd-dev/hub/v2/pkg/fsnotify" "github.com/plgd-dev/hub/v2/pkg/log" "github.com/plgd-dev/hub/v2/pkg/security/certManager/server" + "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/credentials" ) -func New(config BaseConfig, fileWatcher *fsnotify.Watcher, logger log.Logger, opts ...grpc.ServerOption) (*Server, error) { +func New(config BaseConfig, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider, opts ...grpc.ServerOption) (*Server, error) { err := config.Validate() if err != nil { return nil, fmt.Errorf("invalid config: %w", err) } - tls, err := server.New(config.TLS, fileWatcher, logger) + tls, err := server.New(config.TLS, fileWatcher, logger, tracerProvider) if err != nil { return nil, fmt.Errorf("cannot create cert manager %w", err) } diff --git a/pkg/net/http/service/service.go b/pkg/net/http/service/service.go index a2aef07c3..00c200a68 100644 --- a/pkg/net/http/service/service.go +++ b/pkg/net/http/service/service.go @@ -25,7 +25,7 @@ type Service struct { // New parses configuration and creates new http service func New(config Config) (*Service, error) { - listener, err := listener.New(config.HTTPConnection, config.FileWatcher, config.Logger) + listener, err := listener.New(config.HTTPConnection, config.FileWatcher, config.Logger, config.TraceProvider) if err != nil { return nil, fmt.Errorf("cannot create grpc server: %w", err) } diff --git a/pkg/net/listener/server.go b/pkg/net/listener/server.go index 2a276d626..5b14e8b19 100644 --- a/pkg/net/listener/server.go +++ b/pkg/net/listener/server.go @@ -9,6 +9,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/fsnotify" "github.com/plgd-dev/hub/v2/pkg/log" "github.com/plgd-dev/hub/v2/pkg/security/certManager/server" + "go.opentelemetry.io/otel/trace" ) // Server handles gRPC requests to the service. @@ -19,8 +20,8 @@ type Server struct { // NewServer instantiates a listen server. // When passing addr with an unspecified port or ":", use Addr(). -func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger) (*Server, error) { - certManager, err := server.New(config.TLS, fileWatcher, logger) +func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger, tracerProvider trace.TracerProvider) (*Server, error) { + certManager, err := server.New(config.TLS, fileWatcher, logger, tracerProvider) if err != nil { return nil, fmt.Errorf("cannot create cert manager %w", err) } diff --git a/pkg/security/certManager/general/certManager.go b/pkg/security/certManager/general/certManager.go index 367a3143f..e99bfc9aa 100644 --- a/pkg/security/certManager/general/certManager.go +++ b/pkg/security/certManager/general/certManager.go @@ -45,6 +45,9 @@ func (c Config) Validate() error { if c.KeyFile == "" { return fmt.Errorf("keyFile('%v')", c.KeyFile) } + if err := c.CRL.Validate(); err != nil { + return fmt.Errorf("CRL configuration is invalid: %w", err) + } return nil } diff --git a/pkg/security/certManager/server/certManager.go b/pkg/security/certManager/server/certManager.go index 154710147..a4c70b154 100644 --- a/pkg/security/certManager/server/certManager.go +++ b/pkg/security/certManager/server/certManager.go @@ -11,7 +11,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/security/certManager/general" pkgTls "github.com/plgd-dev/hub/v2/pkg/security/tls" "github.com/plgd-dev/hub/v2/pkg/strings" - "go.opentelemetry.io/otel/trace/noop" + "go.opentelemetry.io/otel/trace" ) // Config provides configuration of a file based Server Certificate manager. CAPool can be a string or an array of strings. @@ -72,7 +72,7 @@ func (c *CertManager) Close() { } // New creates a new certificate manager which watches for certs in a filesystem -func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger) (*CertManager, error) { +func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger, tp trace.TracerProvider) (*CertManager, error) { if !config.validated { if err := config.Validate(); err != nil { return nil, err @@ -85,8 +85,7 @@ func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger) (*Cert ClientCertificateRequired: config.ClientCertificateRequired, UseSystemCAPool: false, CRL: config.CRL, - // TODO: use real trace provider - }, fileWatcher, logger.With(log.CertManagerKey, "server"), noop.NewTracerProvider()) + }, fileWatcher, logger.With(log.CertManagerKey, "server"), tp) if err != nil { return nil, err } diff --git a/pkg/security/certManager/server/certManager_test.go b/pkg/security/certManager/server/certManager_test.go index 8693ed6e1..8ba786acf 100644 --- a/pkg/security/certManager/server/certManager_test.go +++ b/pkg/security/certManager/server/certManager_test.go @@ -11,6 +11,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/security/certManager/server" testX509 "github.com/plgd-dev/hub/v2/test/security/x509" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/trace/noop" "gopkg.in/yaml.v3" ) @@ -153,7 +154,7 @@ func TestNew(t *testing.T) { // cert manager fileWatcher, err := fsnotify.NewWatcher(logger) require.NoError(t, err) - mng, err := server.New(config, fileWatcher, logger) + mng, err := server.New(config, fileWatcher, logger, noop.NewTracerProvider()) require.NoError(t, err) tlsConfig := mng.GetTLSConfig() diff --git a/pkg/security/tls/client.go b/pkg/security/tls/client.go index 18234279a..60a3b732d 100644 --- a/pkg/security/tls/client.go +++ b/pkg/security/tls/client.go @@ -15,7 +15,7 @@ type ClientConfig struct { KeyFile urischeme.URIScheme `yaml:"keyFile" json:"keyFile" description:"file name of private key in PEM format"` CertFile urischeme.URIScheme `yaml:"certFile" json:"certFile" description:"file name of certificate in PEM format"` UseSystemCAPool bool `yaml:"useSystemCAPool" json:"useSystemCaPool" description:"use system certification pool"` - CRL CRLConfig `yaml:"crl" json:"json"` + CRL CRLConfig `yaml:"crl" json:"crl"` caPoolArray []urischeme.URIScheme `yaml:"-" json:"-"` validated bool @@ -39,6 +39,9 @@ func (c *ClientConfig) Validate() error { if c.KeyFile == "" { return fmt.Errorf("keyFile('%v')", c.KeyFile) } + if err := c.CRL.Validate(); err != nil { + return fmt.Errorf("CRL configuration is invalid: %w", err) + } c.validated = true return nil } @@ -60,8 +63,11 @@ func (c *ClientConfig) CAPoolFilePathArray() ([]string, error) { func (c *ClientConfig) Equals(c2 ClientConfig) bool { caPool1, ok1 := strings.ToStringArray(c.CAPool) + if !ok1 { + return false + } caPool2, ok2 := strings.ToStringArray(c2.CAPool) - if !ok1 || !ok2 { + if !ok2 { return false } return slices.Equal(caPool1, caPool2) && diff --git a/pkg/security/tls/crl.go b/pkg/security/tls/crl.go index 21372684f..95ad44f2e 100644 --- a/pkg/security/tls/crl.go +++ b/pkg/security/tls/crl.go @@ -125,6 +125,9 @@ func (c *CRLConfig) Equals(c2 CRLConfig) bool { if c.HTTP == nil { return c2.HTTP == nil } + if c2.HTTP == nil { + return false + } tls := c.HTTP.GetTLS() return c.HTTP.GetMaxIdleConns() == c2.HTTP.GetMaxIdleConns() && c.HTTP.GetMaxConnsPerHost() == c2.HTTP.GetMaxConnsPerHost() && diff --git a/resource-aggregate/service/service.go b/resource-aggregate/service/service.go index e07452c02..8f2eb6669 100644 --- a/resource-aggregate/service/service.go +++ b/resource-aggregate/service/service.go @@ -110,7 +110,7 @@ func newGrpcServer(ctx context.Context, config GRPCConfig, fileWatcher *fsnotify return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - grpcServer, err := server.New(config.BaseConfig, fileWatcher, logger, opts...) + grpcServer, err := server.New(config.BaseConfig, fileWatcher, logger, tracerProvider, opts...) if err != nil { validator.Close() return nil, fmt.Errorf("cannot create grpc server: %w", err) diff --git a/resource-directory/service/service.go b/resource-directory/service/service.go index ea3f9b292..d8931e584 100644 --- a/resource-directory/service/service.go +++ b/resource-directory/service/service.go @@ -38,7 +38,7 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg validator.Close() return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - server, err := server.New(config.APIs.GRPC.BaseConfig, fileWatcher, logger, opts...) + server, err := server.New(config.APIs.GRPC.BaseConfig, fileWatcher, logger, tracerProvider, opts...) if err != nil { otelClient.Close() validator.Close() diff --git a/snippet-service/service/grpc/service.go b/snippet-service/service/grpc/service.go index 533b15ae5..7b1bf7c15 100644 --- a/snippet-service/service/grpc/service.go +++ b/snippet-service/service/grpc/service.go @@ -20,7 +20,7 @@ func New(config Config, snippetServiceServer *SnippetServiceServer, validator *v if err != nil { return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - server, err := server.New(config.BaseConfig, fileWatcher, logger, opts...) + server, err := server.New(config.BaseConfig, fileWatcher, logger, tracerProvider, opts...) if err != nil { return nil, err } diff --git a/test/coap-gateway/service/service.go b/test/coap-gateway/service/service.go index 9586308ef..ef12f8ee5 100644 --- a/test/coap-gateway/service/service.go +++ b/test/coap-gateway/service/service.go @@ -24,6 +24,7 @@ import ( "github.com/plgd-dev/hub/v2/pkg/log" certManagerServer "github.com/plgd-dev/hub/v2/pkg/security/certManager/server" "github.com/plgd-dev/hub/v2/pkg/sync/task/queue" + "go.opentelemetry.io/otel/trace/noop" ) // Service is a configuration of coap-gateway @@ -55,7 +56,7 @@ func newTCPListener(config COAPConfig, fileWatcher *fsnotify.Watcher, logger log } var closeListener fn.FuncList - coapsTLS, err := certManagerServer.New(config.TLS.Config, fileWatcher, logger) + coapsTLS, err := certManagerServer.New(config.TLS.Config, fileWatcher, logger, noop.NewTracerProvider()) if err != nil { return nil, nil, fmt.Errorf("cannot create tls cert manager: %w", err) } diff --git a/test/oauth-server/service/service.go b/test/oauth-server/service/service.go index 8fa0b26f7..12546274f 100644 --- a/test/oauth-server/service/service.go +++ b/test/oauth-server/service/service.go @@ -33,7 +33,7 @@ func New(ctx context.Context, config Config, fileWatcher *fsnotify.Watcher, logg otelClient.AddCloseFunc(cancel) tracerProvider := otelClient.GetTracerProvider() - listener, err := listener.New(config.APIs.HTTP.Connection, fileWatcher, logger) + listener, err := listener.New(config.APIs.HTTP.Connection, fileWatcher, logger, tracerProvider) if err != nil { otelClient.Close() return nil, fmt.Errorf("cannot create http server: %w", err) diff --git a/tools/grpc-reflection/service/service.go b/tools/grpc-reflection/service/service.go index 01844c5af..418373818 100644 --- a/tools/grpc-reflection/service/service.go +++ b/tools/grpc-reflection/service/service.go @@ -25,7 +25,7 @@ func New(config Config, fileWatcher *fsnotify.Watcher, logger log.Logger) (*serv if err != nil { return nil, fmt.Errorf("cannot create grpc server options: %w", err) } - server, err := server.New(config.APIs.GRPC.BaseConfig, fileWatcher, logger, opts...) + server, err := server.New(config.APIs.GRPC.BaseConfig, fileWatcher, logger, noop.NewTracerProvider(), opts...) if err != nil { return nil, err }