Skip to content

Commit

Permalink
fixup! Add HTTP client to certificate manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Oct 30, 2024
1 parent 6caa630 commit cf7dfe1
Show file tree
Hide file tree
Showing 29 changed files with 72 additions and 54 deletions.
2 changes: 1 addition & 1 deletion certificate-authority/service/grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions cloud2cloud-connector/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions cloud2cloud-gateway/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion cloud2cloud-gateway/test/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion cloud2cloud-gateway/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
7 changes: 3 additions & 4 deletions coap-gateway/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion device-provisioning-service/service/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions device-provisioning-service/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion grpc-gateway/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion identity-store/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion m2m-oauth-server/service/grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/net/coap/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -49,22 +50,22 @@ 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
},
) (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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions pkg/net/coap/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{}{}
})
Expand Down Expand Up @@ -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{}{}
})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions pkg/net/coap/service/tcpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/net/coap/service/udpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/net/grpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/net/http/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/net/listener/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit cf7dfe1

Please sign in to comment.