From 2e8bc3e8e814d2b38d8f5407a7bd99839ffca466 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Mon, 21 Feb 2022 14:59:16 +0100 Subject: [PATCH] make notifications service configurable --- notifications/pkg/channels/channels.go | 26 ++++++++++++++--------- notifications/pkg/command/server.go | 9 ++++---- notifications/pkg/config/config.go | 25 ++++++++++++++++++++++ notifications/pkg/config/defaultconfig.go | 15 +++++++++++++ 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/notifications/pkg/channels/channels.go b/notifications/pkg/channels/channels.go index 335067950..c78068ca2 100644 --- a/notifications/pkg/channels/channels.go +++ b/notifications/pkg/channels/channels.go @@ -7,45 +7,51 @@ import ( gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" "github.com/cs3org/reva/pkg/rgrpc/todo/pool" + "github.com/owncloud/ocis/notifications/pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" ) +// Channel defines the methods of a communication channel. type Channel interface { + // Todo(c0rby): Do we need a PrepareMessage method? + // Maybe channels need to format the message or will the caller + // of SendMessage do that? + // SendMessage sends a message in a channel specific way. SendMessage(receiver, msg string) error } -func NewMailChanel(logger log.Logger) (Channel, error) { - gc, err := pool.GetGatewayServiceClient("localhost:9142") +// NewMailChannel instantiates a new mail communication channel. +func NewMailChannel(cfg config.Config, logger log.Logger) (Channel, error) { + gc, err := pool.GetGatewayServiceClient(cfg.Notifications.RevaGateway) if err != nil { logger.Error().Err(err).Msg("could not get gateway client") return nil, err } return Mail{ gatewayClient: gc, + conf: cfg, }, nil } +// Mail is the communcation channel for email. type Mail struct { gatewayClient gateway.GatewayAPIClient + conf config.Config } func (m Mail) SendMessage(receiver, msg string) error { + smtpConf := m.conf.Notifications.SMTP res, err := m.gatewayClient.Authenticate(context.Background(), &gateway.AuthenticateRequest{ Type: "machine", ClientId: "userid:" + receiver, - ClientSecret: "change-me-please", + ClientSecret: m.conf.Notifications.MachineAuthSecret, }) if err != nil { return err } - from := "god" - password := "godisdead" to := []string{res.User.Mail} - host := "localhost" - port := "1025" body := []byte(msg) - auth := smtp.PlainAuth("", from, password, host) - - return smtp.SendMail(host+":"+port, auth, from, to, body) + auth := smtp.PlainAuth("", smtpConf.Sender, smtpConf.Password, smtpConf.Host) + return smtp.SendMail(smtpConf.Host+":"+smtpConf.Port, auth, smtpConf.Sender, to, body) } diff --git a/notifications/pkg/command/server.go b/notifications/pkg/command/server.go index af1380591..7f22fa1cb 100644 --- a/notifications/pkg/command/server.go +++ b/notifications/pkg/command/server.go @@ -26,21 +26,20 @@ func Server(cfg *config.Config) *cli.Command { Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) - group := "notifications" - evs := []events.Unmarshaller{ events.ShareCreated{}, } - client, err := server.NewNatsStream(nats.Address("127.0.0.1:4222"), nats.ClusterID("test-cluster")) + evtsCfg := cfg.Notifications.Events + client, err := server.NewNatsStream(nats.Address(evtsCfg.Endpoint), nats.ClusterID(evtsCfg.Cluster)) if err != nil { return err } - evts, err := events.Consume(client, group, evs...) + evts, err := events.Consume(client, evtsCfg.ConsumerGroup, evs...) if err != nil { return err } - channel, err := channels.NewMailChanel(logger) + channel, err := channels.NewMailChannel(*cfg, logger) if err != nil { return err } diff --git a/notifications/pkg/config/config.go b/notifications/pkg/config/config.go index 254832cd1..8ac7da5d8 100644 --- a/notifications/pkg/config/config.go +++ b/notifications/pkg/config/config.go @@ -15,5 +15,30 @@ type Config struct { Log *Log `ocisConfig:"log"` Debug Debug `ocisConfig:"debug"` + Notifications Notifications `ocisConfig:"notifications"` + Context context.Context } + +// Notifications definces the config options for the notifications service. +type Notifications struct { + SMTP SMTP `ocisConfig:"SMTP"` + Events Events `ocisConfig:"events"` + RevaGateway string `ocisConfig:"reva_gateway" env:"REVA_GATEWAY;NOTIFICATIONS_REVA_GATEWAY"` + MachineAuthSecret string `ocisConfig:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;NOTIFICATIONS_MACHINE_AUTH_API_KEY"` +} + +// SMTP combines the smtp configuration options. +type SMTP struct { + Host string `ocisConfig:"smtp_host" env:"NOTIFICATIONS_SMTP_HOST"` + Port string `ocisConfig:"smtp_port" env:"NOTIFICATIONS_SMTP_PORT"` + Sender string `ocisConfig:"smtp_sender" env:"NOTIFICATIONS_SMTP_SENDER"` + Password string `ocisConfig:"smtp_password" env:"NOTIFICATIONS_SMTP_PASSWORD"` +} + +// Events combines the configuration options for the event bus. +type Events struct { + Endpoint string `ocisConfig:"events_endpoint" env:"NOTIFICATIONS_EVENTS_ENDPOINT"` + Cluster string `ocisConfig:"events_cluster" env:"NOTIFICATIONS_EVENTS_CLUSTER"` + ConsumerGroup string `ocisConfig:"events_group" env:"NOTIFICATIONS_EVENTS_GROUP"` +} diff --git a/notifications/pkg/config/defaultconfig.go b/notifications/pkg/config/defaultconfig.go index e98e72ff6..2d9169f7b 100644 --- a/notifications/pkg/config/defaultconfig.go +++ b/notifications/pkg/config/defaultconfig.go @@ -8,5 +8,20 @@ func DefaultConfig() *Config { Service: Service{ Name: "notifications", }, + Notifications: Notifications{ + SMTP: SMTP{ + Host: "127.0.0.1", + Port: "1025", + Sender: "god@example.com", + Password: "godisdead", + }, + Events: Events{ + Endpoint: "127.0.0.1:4222", + Cluster: "test-cluster", + ConsumerGroup: "notifications", + }, + RevaGateway: "127.0.0.1:9142", + MachineAuthSecret: "change-me-please", + }, } }