Skip to content

Commit fabd72d

Browse files
committed
add tls support for all nats connections
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
1 parent 9b62a97 commit fabd72d

File tree

58 files changed

+519
-271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+519
-271
lines changed

pkg/nats/options.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package nats
2+
3+
import (
4+
"crypto/tls"
5+
6+
"github.com/nats-io/nats.go"
7+
)
8+
9+
func Secure(enableTLS, insecure bool, rootCA string) nats.Option {
10+
if enableTLS {
11+
if rootCA != "" {
12+
return nats.RootCAs(rootCA)
13+
}
14+
return nats.Secure(&tls.Config{
15+
MinVersion: tls.VersionTLS12,
16+
InsecureSkipVerify: insecure,
17+
})
18+
}
19+
return nil
20+
}

pkg/shared/shared_types.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ type HTTPServiceTLS struct {
4848
}
4949

5050
type Cache struct {
51-
Store string `yaml:"store" env:"OC_CACHE_STORE" desc:"The type of the cache store. Supported values are: 'memory', 'redis-sentinel', 'nats-js-kv', 'noop'. See the text description for details." introductionVersion:"1.0.0"`
52-
Nodes []string `yaml:"nodes" env:"OC_CACHE_STORE_NODES" desc:"A comma separated list of nodes to access the configured store. This has no effect when 'memory' store is configured. Note that the behaviour how nodes are used is dependent on the library of the configured store." introductionVersion:"1.0.0"`
53-
Database string `yaml:"database" env:"OC_CACHE_STORE_DATABASE" desc:"The database name the configured store should use." introductionVersion:"1.0.0"`
54-
Table string `yaml:"table" env:"OC_CACHE_STORE_TABLE" desc:"The database table the store should use." introductionVersion:"1.0.0"`
55-
TTL time.Duration `yaml:"ttl" env:"OC_CACHE_TTL" desc:"Time to live for events in the store. The duration can be set as number followed by a unit identifier like s, m or h." introductionVersion:"1.0.0"`
56-
DisablePersistence bool `yaml:"disable_persistence" env:"OC_CACHE_DISABLE_PERSISTENCE" desc:"Disables persistence of the cache. Only applies when store type 'nats-js-kv' is configured. Defaults to false." introductionVersion:"1.0.0"`
57-
AuthUsername string `yaml:"auth_username" env:"OC_CACHE_AUTH_USERNAME" desc:"The username to use for authentication. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
58-
AuthPassword string `yaml:"auth_password" env:"OC_CACHE_AUTH_PASSWORD" desc:"The password to use for authentication. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
51+
Store string `yaml:"store" env:"OC_CACHE_STORE" desc:"The type of the cache store. Supported values are: 'memory', 'redis-sentinel', 'nats-js-kv', 'noop'. See the text description for details." introductionVersion:"1.0.0"`
52+
Nodes []string `yaml:"nodes" env:"OC_CACHE_STORE_NODES" desc:"A comma separated list of nodes to access the configured store. This has no effect when 'memory' store is configured. Note that the behaviour how nodes are used is dependent on the library of the configured store." introductionVersion:"1.0.0"`
53+
Database string `yaml:"database" env:"OC_CACHE_STORE_DATABASE" desc:"The database name the configured store should use." introductionVersion:"1.0.0"`
54+
Table string `yaml:"table" env:"OC_CACHE_STORE_TABLE" desc:"The database table the store should use." introductionVersion:"1.0.0"`
55+
TTL time.Duration `yaml:"ttl" env:"OC_CACHE_TTL" desc:"Time to live for events in the store. The duration can be set as number followed by a unit identifier like s, m or h." introductionVersion:"1.0.0"`
56+
DisablePersistence bool `yaml:"disable_persistence" env:"OC_CACHE_DISABLE_PERSISTENCE" desc:"Disables persistence of the cache. Only applies when store type 'nats-js-kv' is configured. Defaults to false." introductionVersion:"1.0.0"`
57+
AuthUsername string `yaml:"auth_username" env:"OC_CACHE_AUTH_USERNAME" desc:"The username to use for authentication. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
58+
AuthPassword string `yaml:"auth_password" env:"OC_CACHE_AUTH_PASSWORD" desc:"The password to use for authentication. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
59+
EnableTLS bool `yaml:"enable_tls" env:"OC_CACHE_ENABLE_TLS" desc:"Enable TLS for the connection to file metadata cache." introductionVersion:"%%NEXT%%"`
60+
TLSInsecure bool `yaml:"tls_insecure" env:"OC_INSECURE;OC_CACHE_TLS_INSECURE" desc:"Whether to verify the server TLS certificates." introductionVersion:"%%NEXT%%"`
61+
TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"OC_CACHE_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided OC_CACHE_TLS_INSECURE will be seen as false." introductionVersion:"%%NEXT%%"`
5962
}
6063

6164
// Commons holds configuration that are common to all extensions. Each extension can then decide whether

services/activitylog/pkg/command/server.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"github.com/opencloud-eu/reva/v2/pkg/events"
99
"github.com/opencloud-eu/reva/v2/pkg/events/stream"
1010
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
11-
"github.com/opencloud-eu/reva/v2/pkg/store"
1211
"github.com/spf13/cobra"
13-
microstore "go-micro.dev/v4/store"
1412

1513
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
1614
"github.com/opencloud-eu/opencloud/pkg/generators"
@@ -77,15 +75,6 @@ func Server(cfg *config.Config) *cobra.Command {
7775
return err
7876
}
7977

80-
evStore := store.Create(
81-
store.Store(cfg.Store.Store),
82-
store.TTL(cfg.Store.TTL),
83-
microstore.Nodes(cfg.Store.Nodes...),
84-
microstore.Database(cfg.Store.Database),
85-
microstore.Table(cfg.Store.Table),
86-
store.Authentication(cfg.Store.AuthUsername, cfg.Store.AuthPassword),
87-
)
88-
8978
tm, err := pool.StringToTLSMode(cfg.GRPCClientTLS.Mode)
9079
if err != nil {
9180
logger.Error().Err(err).Msg("Failed to parse tls mode")
@@ -120,7 +109,6 @@ func Server(cfg *config.Config) *cobra.Command {
120109
http.Context(ctx), // NOTE: not passing this "option" leads to a panic in go-micro
121110
http.TraceProvider(tracerProvider),
122111
http.Stream(evStream),
123-
http.Store(evStore),
124112
http.GatewaySelector(gatewaySelector),
125113
http.HistoryClient(hClient),
126114
http.ValueClient(vClient),

services/activitylog/pkg/config/config.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ type Events struct {
4949

5050
// Store configures the store to use
5151
type Store struct {
52-
Store string `yaml:"store" env:"OC_PERSISTENT_STORE;ACTIVITYLOG_STORE" desc:"The type of the store. Supported values are: 'memory', 'nats-js-kv', 'redis-sentinel', 'noop'. See the text description for details." introductionVersion:"1.0.0"`
53-
Nodes []string `yaml:"nodes" env:"OC_PERSISTENT_STORE_NODES;ACTIVITYLOG_STORE_NODES" desc:"A list of nodes to access the configured store. This has no effect when 'memory' store is configured. Note that the behaviour how nodes are used is dependent on the library of the configured store. See the Environment Variable Types description for more details." introductionVersion:"1.0.0"`
54-
Database string `yaml:"database" env:"ACTIVITYLOG_STORE_DATABASE" desc:"The database name the configured store should use." introductionVersion:"1.0.0"`
55-
Table string `yaml:"table" env:"ACTIVITYLOG_STORE_TABLE" desc:"The database table the store should use." introductionVersion:"1.0.0"`
56-
TTL time.Duration `yaml:"ttl" env:"OC_PERSISTENT_STORE_TTL;ACTIVITYLOG_STORE_TTL" desc:"Time to live for events in the store. See the Environment Variable Types description for more details." introductionVersion:"1.0.0"`
57-
AuthUsername string `yaml:"username" env:"OC_PERSISTENT_STORE_AUTH_USERNAME;ACTIVITYLOG_STORE_AUTH_USERNAME" desc:"The username to authenticate with the store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
58-
AuthPassword string `yaml:"password" env:"OC_PERSISTENT_STORE_AUTH_PASSWORD;ACTIVITYLOG_STORE_AUTH_PASSWORD" desc:"The password to authenticate with the store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
52+
Store string `yaml:"store" env:"OC_PERSISTENT_STORE;ACTIVITYLOG_STORE" desc:"The type of the store. Supported values are: 'memory', 'nats-js-kv', 'redis-sentinel', 'noop'. See the text description for details." introductionVersion:"1.0.0"`
53+
Nodes []string `yaml:"nodes" env:"OC_PERSISTENT_STORE_NODES;ACTIVITYLOG_STORE_NODES" desc:"A list of nodes to access the configured store. This has no effect when 'memory' store is configured. Note that the behaviour how nodes are used is dependent on the library of the configured store. See the Environment Variable Types description for more details." introductionVersion:"1.0.0"`
54+
Database string `yaml:"database" env:"ACTIVITYLOG_STORE_DATABASE" desc:"The database name the configured store should use." introductionVersion:"1.0.0"`
55+
TTL time.Duration `yaml:"ttl" env:"OC_PERSISTENT_STORE_TTL;ACTIVITYLOG_STORE_TTL" desc:"Time to live for events in the store. See the Environment Variable Types description for more details." introductionVersion:"1.0.0"`
56+
AuthUsername string `yaml:"username" env:"OC_PERSISTENT_STORE_AUTH_USERNAME;ACTIVITYLOG_STORE_AUTH_USERNAME" desc:"The username to authenticate with the store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
57+
AuthPassword string `yaml:"password" env:"OC_PERSISTENT_STORE_AUTH_PASSWORD;ACTIVITYLOG_STORE_AUTH_PASSWORD" desc:"The password to authenticate with the store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"1.0.0"`
58+
EnableTLS bool `yaml:"enable_tls" env:"OC_PERSISTENT_STORE_ENABLE_TLS;ACTIVITYLOG_STORE_ENABLE_TLS" desc:"Enable TLS for the connection to the store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"%%NEXT%%"`
59+
TLSInsecure bool `yaml:"tls_insecure" env:"OC_INSECURE;OC_PERSISTENT_STORE_TLS_INSECURE;ACTIVITYLOG_STORE_TLS_INSECURE" desc:"Whether to verify the server TLS certificates." introductionVersion:"%%NEXT%%"`
60+
TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"OC_PERSISTENT_STORE_TLS_ROOT_CA_CERTIFICATE;ACTIVITYLOG_STORE_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided ACTIVITYLOG_STORE_TLS_INSECURE will be seen as false." introductionVersion:"%%NEXT%%"`
5961
}
6062

6163
// ServiceAccount is the configuration for the used service account

services/activitylog/pkg/config/defaults/defaultconfig.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func DefaultConfig() *config.Config {
3737
Store: "nats-js-kv",
3838
Nodes: []string{"127.0.0.1:9233"},
3939
Database: "activitylog",
40-
Table: "",
4140
},
4241
RevaGateway: shared.DefaultRevaConfig().Address,
4342
DefaultLanguage: "en",

services/activitylog/pkg/server/debug/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/opencloud-eu/opencloud/pkg/checks"
77
"github.com/opencloud-eu/opencloud/pkg/handlers"
8+
"github.com/opencloud-eu/opencloud/pkg/nats"
89
"github.com/opencloud-eu/opencloud/pkg/service/debug"
910
"github.com/opencloud-eu/opencloud/pkg/version"
1011
)
@@ -17,8 +18,13 @@ func Server(opts ...Option) (*http.Server, error) {
1718
WithLogger(options.Logger).
1819
WithCheck("http reachability", checks.NewHTTPCheck(options.Config.HTTP.Addr))
1920

21+
secureOption := nats.Secure(
22+
options.Config.Events.EnableTLS,
23+
options.Config.Events.TLSInsecure,
24+
options.Config.Events.TLSRootCACertificate,
25+
)
2026
readyHandlerConfiguration := healthHandlerConfiguration.
21-
WithCheck("nats reachability", checks.NewNatsCheck(options.Config.Events.Endpoint))
27+
WithCheck("nats reachability", checks.NewNatsCheck(options.Config.Events.Endpoint, secureOption))
2228

2329
return debug.NewService(
2430
debug.Logger(options.Logger),

services/activitylog/pkg/server/http/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ func Server(opts ...Option) (http.Service, error) {
8181
svc.Logger(options.Logger),
8282
svc.Stream(options.Stream),
8383
svc.Mux(mux),
84-
svc.Store(options.Store),
8584
svc.Config(options.Config),
8685
svc.GatewaySelector(options.GatewaySelector),
8786
svc.TraceProvider(options.TraceProvider),

services/activitylog/pkg/service/options.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/opencloud-eu/opencloud/services/activitylog/pkg/config"
1212
"github.com/opencloud-eu/reva/v2/pkg/events"
1313
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
14-
microstore "go-micro.dev/v4/store"
1514
"go.opentelemetry.io/otel/trace"
1615
)
1716

@@ -25,7 +24,6 @@ type Options struct {
2524
TraceProvider trace.TracerProvider
2625
Stream events.Stream
2726
RegisteredEvents []events.Unmarshaller
28-
Store microstore.Store
2927
GatewaySelector pool.Selectable[gateway.GatewayAPIClient]
3028
Mux *chi.Mux
3129
HistoryClient ehsvc.EventHistoryService
@@ -69,13 +67,6 @@ func RegisteredEvents(e []events.Unmarshaller) Option {
6967
}
7068
}
7169

72-
// Store configures the store to use
73-
func Store(store microstore.Store) Option {
74-
return func(o *Options) {
75-
o.Store = store
76-
}
77-
}
78-
7970
// GatewaySelector adds a grpc client selector for the gateway service
8071
func GatewaySelector(gatewaySelector pool.Selectable[gateway.GatewayAPIClient]) Option {
8172
return func(o *Options) {

services/activitylog/pkg/service/service.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"crypto/tls"
56
"encoding/base32"
67
"encoding/json"
78
"fmt"
@@ -166,6 +167,18 @@ func New(opts ...Option) (*ActivitylogService, error) {
166167
natsOptions := nats.Options{
167168
Servers: o.Config.Store.Nodes,
168169
}
170+
if o.Config.Store.EnableTLS {
171+
if o.Config.Store.TLSRootCACertificate != "" {
172+
// when root ca is configured use it. an insecure flag is ignored.
173+
nats.RootCAs(o.Config.Store.TLSRootCACertificate)(&natsOptions)
174+
} else {
175+
// enable tls and use insecure flag
176+
nats.Secure(&tls.Config{MinVersion: tls.VersionTLS12, InsecureSkipVerify: o.Config.Store.TLSInsecure})(&natsOptions)
177+
}
178+
}
179+
if o.Config.Store.AuthUsername != "" && o.Config.Store.AuthPassword != "" {
180+
nats.UserInfo(o.Config.Store.AuthUsername, o.Config.Store.AuthPassword)(&natsOptions)
181+
}
169182
conn, err := natsOptions.Connect()
170183
if err != nil {
171184
return nil, err

services/antivirus/pkg/server/debug/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/opencloud-eu/opencloud/pkg/checks"
1212
"github.com/opencloud-eu/opencloud/pkg/handlers"
13+
"github.com/opencloud-eu/opencloud/pkg/nats"
1314
"github.com/opencloud-eu/opencloud/pkg/service/debug"
1415
"github.com/opencloud-eu/opencloud/pkg/version"
1516
)
@@ -18,9 +19,14 @@ import (
1819
func Server(opts ...Option) (*http.Server, error) {
1920
options := newOptions(opts...)
2021

22+
secureOption := nats.Secure(
23+
options.Config.Events.EnableTLS,
24+
options.Config.Events.TLSInsecure,
25+
options.Config.Events.TLSRootCACertificate,
26+
)
2127
readyHandlerConfiguration := handlers.NewCheckHandlerConfiguration().
2228
WithLogger(options.Logger).
23-
WithCheck("nats reachability", checks.NewNatsCheck(options.Config.Events.Endpoint)).
29+
WithCheck("nats reachability", checks.NewNatsCheck(options.Config.Events.Endpoint, secureOption)).
2430
WithCheck("antivirus reachability", func(ctx context.Context) error {
2531
cfg := options.Config
2632
switch cfg.Scanner.Type {

0 commit comments

Comments
 (0)