Files
ocis/services/notifications/pkg/command/server.go
T

101 lines
3.0 KiB
Go
Raw Normal View History

2022-02-18 15:44:53 +01:00
package command
import (
2022-10-12 14:56:47 +02:00
"crypto/tls"
"crypto/x509"
2022-02-18 15:44:53 +01:00
"fmt"
2022-10-12 14:56:47 +02:00
"os"
2022-02-18 15:44:53 +01:00
2022-03-03 19:56:04 +01:00
"github.com/cs3org/reva/v2/pkg/events"
2022-12-19 10:18:05 +01:00
"github.com/cs3org/reva/v2/pkg/events/stream"
2022-09-12 16:59:49 +02:00
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
2022-04-19 09:44:47 +02:00
"github.com/go-micro/plugins/v4/events/natsjs"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
2022-10-12 14:56:47 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/crypto"
2022-06-20 16:49:06 +02:00
"github.com/owncloud/ocis/v2/services/notifications/pkg/channels"
"github.com/owncloud/ocis/v2/services/notifications/pkg/config"
"github.com/owncloud/ocis/v2/services/notifications/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/notifications/pkg/logging"
"github.com/owncloud/ocis/v2/services/notifications/pkg/service"
2022-02-18 15:44:53 +01:00
"github.com/urfave/cli/v2"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
2022-08-18 10:59:35 +02:00
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
2022-02-18 15:44:53 +01:00
Category: "server",
Before: func(c *cli.Context) error {
2022-08-22 08:08:18 +02:00
return configlog.ReturnFatal(parser.ParseConfig(cfg))
2022-02-18 15:44:53 +01:00
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
2022-09-19 16:49:04 +02:00
// evs defines a list of events to subscribe to
2022-02-18 15:44:53 +01:00
evs := []events.Unmarshaller{
events.ShareCreated{},
2023-01-08 13:33:48 +01:00
events.ShareExpired{},
2022-09-19 16:49:04 +02:00
events.SpaceShared{},
2022-12-20 15:20:32 +01:00
events.SpaceUnshared{},
2023-01-08 13:33:48 +01:00
events.SpaceMembershipExpired{},
2022-02-18 15:44:53 +01:00
}
2022-02-21 14:59:16 +01:00
evtsCfg := cfg.Notifications.Events
2022-10-12 14:56:47 +02:00
var tlsConf *tls.Config
if evtsCfg.EnableTLS {
var rootCAPool *x509.CertPool
if evtsCfg.TLSRootCACertificate != "" {
rootCrtFile, err := os.Open(evtsCfg.TLSRootCACertificate)
if err != nil {
return err
}
rootCAPool, err = crypto.NewCertPoolFromPEM(rootCrtFile)
if err != nil {
return err
}
evtsCfg.TLSInsecure = false
2022-10-12 14:56:47 +02:00
}
tlsConf = &tls.Config{
2022-11-03 16:08:56 +01:00
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: evtsCfg.TLSInsecure, //nolint:gosec
RootCAs: rootCAPool,
}
2022-10-12 14:56:47 +02:00
}
2022-12-19 10:18:05 +01:00
client, err := stream.Nats(
2022-10-12 14:56:47 +02:00
natsjs.TLSConfig(tlsConf),
natsjs.Address(evtsCfg.Endpoint),
natsjs.ClusterID(evtsCfg.Cluster),
)
2022-02-18 15:44:53 +01:00
if err != nil {
return err
}
2022-03-23 10:15:27 +01:00
evts, err := events.Consume(client, evtsCfg.ConsumerGroup, evs...)
2022-02-18 15:44:53 +01:00
if err != nil {
return err
}
2022-02-21 14:59:16 +01:00
channel, err := channels.NewMailChannel(*cfg, logger)
2022-02-18 16:36:18 +01:00
if err != nil {
return err
}
tm, err := pool.StringToTLSMode(cfg.Notifications.GRPCClientTLS.Mode)
if err != nil {
return err
}
gwclient, err := pool.GetGatewayServiceClient(
cfg.Notifications.RevaGateway,
pool.WithTLSCACert(cfg.Notifications.GRPCClientTLS.CACert),
pool.WithTLSMode(tm),
)
2022-09-12 16:59:49 +02:00
if err != nil {
logger.Fatal().Err(err).Str("addr", cfg.Notifications.RevaGateway).Msg("could not get reva client")
2022-09-12 16:59:49 +02:00
}
2022-11-08 09:23:05 +01:00
svc := service.NewEventsNotifier(evts, channel, logger, gwclient, cfg.Notifications.MachineAuthAPIKey, cfg.Notifications.EmailTemplatePath, cfg.WebUIURL)
2022-02-18 15:44:53 +01:00
return svc.Run()
},
}
}