From 5488d438cd534ad72f5a1462e80c43987229463d Mon Sep 17 00:00:00 2001 From: jkoberg Date: Thu, 13 Oct 2022 13:14:18 +0200 Subject: [PATCH] tls for experimental-only services Signed-off-by: jkoberg tls for ocdav Signed-off-by: jkoberg --- services/experimental/pkg/config/config.go | 4 +++ .../pkg/service/http/v0/service.go | 33 ++++++++++++++++--- services/ocdav/pkg/command/server.go | 2 +- services/ocdav/pkg/config/config.go | 3 ++ services/postprocessing/pkg/command/server.go | 27 +++++++++++++-- services/postprocessing/pkg/config/config.go | 3 ++ services/storage-users/pkg/revaconfig/user.go | 12 ++++--- 7 files changed, 73 insertions(+), 11 deletions(-) diff --git a/services/experimental/pkg/config/config.go b/services/experimental/pkg/config/config.go index 9d48f89a202..8f456d4e6a0 100644 --- a/services/experimental/pkg/config/config.go +++ b/services/experimental/pkg/config/config.go @@ -2,6 +2,7 @@ package config import ( "context" + "github.com/owncloud/ocis/v2/ocis-pkg/shared" ) @@ -26,6 +27,9 @@ type Config struct { type Events struct { Endpoint string `yaml:"endpoint" env:"EXPERIMENTAL_EVENTS_ENDPOINT" desc:"The address of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture."` Cluster string `yaml:"cluster" env:"EXPERIMENTAL_EVENTS_CLUSTER" desc:"The clusterID of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture. Mandatory when using NATS as event system."` + + TLSInsecure bool `yaml:"tls_insecure" env:"OCIS_INSECURE;SEARCH_EVENTS_TLS_INSECURE" desc:"Whether to verify the server TLS certificates."` + TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"SEARCH_EVENTS_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided SEARCH_EVENTS_TLS_INSECURE will be seen as false."` } // TokenManager is the config for using the reva token manager diff --git a/services/experimental/pkg/service/http/v0/service.go b/services/experimental/pkg/service/http/v0/service.go index 60b67a2442c..5627184d6e6 100644 --- a/services/experimental/pkg/service/http/v0/service.go +++ b/services/experimental/pkg/service/http/v0/service.go @@ -1,11 +1,15 @@ package svc import ( + "crypto/tls" + "crypto/x509" "net/http" + "os" "github.com/cs3org/reva/v2/pkg/events/server" "github.com/go-chi/chi/v5" "github.com/go-micro/plugins/v4/events/natsjs" + ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto" "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" searchSvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0" "github.com/owncloud/ocis/v2/services/experimental/pkg/activities" @@ -17,9 +21,30 @@ func NewService(opts ...Option) (Experimental, error) { options := newOptions(opts...) logger := options.Logger cfg := options.Config - es, err := server.NewNatsStream( - natsjs.Address(cfg.Events.Endpoint), - natsjs.ClusterID(cfg.Events.Cluster), + + evtsCfg := cfg.Events + var rootCAPool *x509.CertPool + if evtsCfg.TLSRootCACertificate != "" { + rootCrtFile, err := os.Open(evtsCfg.TLSRootCACertificate) + if err != nil { + return Experimental{}, err + } + + rootCAPool, err = ociscrypto.NewCertPoolFromPEM(rootCrtFile) + if err != nil { + return Experimental{}, err + } + evtsCfg.TLSInsecure = false + } + + tlsConf := &tls.Config{ + InsecureSkipVerify: evtsCfg.TLSInsecure, //nolint:gosec + RootCAs: rootCAPool, + } + bus, err := server.NewNatsStream( + natsjs.TLSConfig(tlsConf), + natsjs.Address(evtsCfg.Endpoint), + natsjs.ClusterID(evtsCfg.Cluster), ) if err != nil { return Experimental{}, err @@ -38,7 +63,7 @@ func NewService(opts ...Option) (Experimental, error) { logger, ) - if err := activities.NewActivitiesService(r, es, logger, cfg.Activities); err != nil { + if err := activities.NewActivitiesService(r, bus, logger, cfg.Activities); err != nil { return svc, err } diff --git a/services/ocdav/pkg/command/server.go b/services/ocdav/pkg/command/server.go index 6c697507334..6a4fb6779bd 100644 --- a/services/ocdav/pkg/command/server.go +++ b/services/ocdav/pkg/command/server.go @@ -61,7 +61,7 @@ func Server(cfg *config.Config) *cli.Command { ocdav.VersionString(cfg.Status.VersionString), ocdav.Edition(cfg.Status.Edition), ocdav.MachineAuthAPIKey(cfg.MachineAuthAPIKey), - ocdav.Events(cfg.Events.Endpoint, cfg.Events.Cluster), + ocdav.Events(cfg.Events.Endpoint, cfg.Events.Cluster, cfg.Events.TLSRootCACertificate, cfg.Events.TLSInsecure), ocdav.Broker(broker.NoOp{}), ocdav.ForceRescan(cfg.ForceRescanPeriod, cfg.PollingTimeout), // ocdav.FavoriteManager() // FIXME needs a proper persistence implementation https://github.com/owncloud/ocis/issues/1228 diff --git a/services/ocdav/pkg/config/config.go b/services/ocdav/pkg/config/config.go index e8bc52d9661..6911bdf7a4d 100644 --- a/services/ocdav/pkg/config/config.go +++ b/services/ocdav/pkg/config/config.go @@ -98,4 +98,7 @@ type Status struct { type Events struct { Endpoint string `yaml:"endpoint" env:"OCDAV_EVENTS_ENDPOINT" desc:"Endpoint of the event system."` Cluster string `yaml:"cluster" env:"OCDAV_EVENTS_CLUSTER" desc:"Cluster ID of the event system."` + + TLSInsecure bool `yaml:"tls_insecure" env:"OCIS_INSECURE;SEARCH_EVENTS_TLS_INSECURE" desc:"Whether to verify the server TLS certificates."` + TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"SEARCH_EVENTS_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided SEARCH_EVENTS_TLS_INSECURE will be seen as false."` } diff --git a/services/postprocessing/pkg/command/server.go b/services/postprocessing/pkg/command/server.go index 4ad747005e8..5c95b42ebe2 100644 --- a/services/postprocessing/pkg/command/server.go +++ b/services/postprocessing/pkg/command/server.go @@ -1,11 +1,14 @@ package command import ( + "crypto/tls" + "crypto/x509" "fmt" "os" "github.com/cs3org/reva/v2/pkg/events/server" "github.com/go-micro/plugins/v4/events/natsjs" + ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/parser" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/logging" @@ -31,14 +34,34 @@ func Server(cfg *config.Config) *cli.Command { logger := logging.Configure(cfg.Service.Name, cfg.Log) evtsCfg := cfg.Postprocessing.Events - client, err := server.NewNatsStream( + var rootCAPool *x509.CertPool + if evtsCfg.TLSRootCACertificate != "" { + rootCrtFile, err := os.Open(evtsCfg.TLSRootCACertificate) + if err != nil { + return err + } + + rootCAPool, err = ociscrypto.NewCertPoolFromPEM(rootCrtFile) + if err != nil { + return err + } + evtsCfg.TLSInsecure = false + } + + tlsConf := &tls.Config{ + InsecureSkipVerify: evtsCfg.TLSInsecure, //nolint:gosec + RootCAs: rootCAPool, + } + bus, err := server.NewNatsStream( + natsjs.TLSConfig(tlsConf), natsjs.Address(evtsCfg.Endpoint), natsjs.ClusterID(evtsCfg.Cluster), ) if err != nil { return err } - svc, err := service.NewPostprocessingService(client, logger, cfg.Postprocessing) + + svc, err := service.NewPostprocessingService(bus, logger, cfg.Postprocessing) if err != nil { return err } diff --git a/services/postprocessing/pkg/config/config.go b/services/postprocessing/pkg/config/config.go index a1d1b67bdd7..c81915e1e66 100644 --- a/services/postprocessing/pkg/config/config.go +++ b/services/postprocessing/pkg/config/config.go @@ -32,4 +32,7 @@ type Postprocessing struct { type Events struct { Endpoint string `yaml:"endpoint" env:"POSTPROCESSING_EVENTS_ENDPOINT" desc:"Endpoint of the event system."` Cluster string `yaml:"cluster" env:"POSTPROCESSING_EVENTS_CLUSTER" desc:"Cluster ID of the event system."` + + TLSInsecure bool `yaml:"tls_insecure" env:"OCIS_INSECURE;SEARCH_EVENTS_TLS_INSECURE" desc:"Whether to verify the server TLS certificates."` + TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"SEARCH_EVENTS_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided SEARCH_EVENTS_TLS_INSECURE will be seen as false."` } diff --git a/services/storage-users/pkg/revaconfig/user.go b/services/storage-users/pkg/revaconfig/user.go index 8ac33fc8d09..478fc93c5f3 100644 --- a/services/storage-users/pkg/revaconfig/user.go +++ b/services/storage-users/pkg/revaconfig/user.go @@ -102,8 +102,10 @@ func UserDrivers(cfg *config.Config) map[string]interface{} { "cache_database": cfg.Cache.Database, }, "events": map[string]interface{}{ - "natsaddress": cfg.Events.Addr, - "natsclusterid": cfg.Events.ClusterID, + "natsaddress": cfg.Events.Addr, + "natsclusterid": cfg.Events.ClusterID, + "tlsinsecure": cfg.Events.TLSInsecure, + "tlsrootcacertificate": cfg.Events.TLSRootCaCertPath, }, "tokens": map[string]interface{}{ "transfer_shared_secret": cfg.Commons.TransferSecret, @@ -142,8 +144,10 @@ func UserDrivers(cfg *config.Config) map[string]interface{} { "cache_database": cfg.Cache.Database, }, "events": map[string]interface{}{ - "natsaddress": cfg.Events.Addr, - "natsclusterid": cfg.Events.ClusterID, + "natsaddress": cfg.Events.Addr, + "natsclusterid": cfg.Events.ClusterID, + "tlsinsecure": cfg.Events.TLSInsecure, + "tlsrootcacertificate": cfg.Events.TLSRootCaCertPath, }, "tokens": map[string]interface{}{ "transfer_shared_secret": cfg.Commons.TransferSecret,