Files

78 lines
2.3 KiB
Go
Raw Permalink Normal View History

2022-02-18 15:44:53 +01:00
package parser
import (
"errors"
"fmt"
"net/mail"
"strings"
2022-02-18 15:44:53 +01:00
2022-05-04 14:49:59 +02:00
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config"
2024-02-29 11:19:01 +01:00
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
2022-06-20 16:49:06 +02:00
"github.com/owncloud/ocis/v2/services/notifications/pkg/config"
"github.com/owncloud/ocis/v2/services/notifications/pkg/config/defaults"
"github.com/owncloud/ocis/v2/services/notifications/pkg/logging"
2022-02-18 15:44:53 +01:00
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode"
2022-02-18 15:44:53 +01:00
)
2022-04-29 20:21:14 +00:00
// ParseConfig loads configuration from known paths.
2022-02-18 15:44:53 +01:00
func ParseConfig(cfg *config.Config) error {
err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
2022-02-18 15:44:53 +01:00
if err != nil {
return err
}
2022-03-08 17:35:55 +01:00
defaults.EnsureDefaults(cfg)
2022-02-18 15:44:53 +01:00
// load all env variables relevant to the config in the current context.
if err := envdecode.Decode(cfg); err != nil {
// no environment variable set for this config is an expected "error"
if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) {
return err
}
}
2022-03-08 17:35:55 +01:00
defaults.Sanitize(cfg)
return Validate(cfg)
}
func Validate(cfg *config.Config) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
if cfg.Notifications.SMTP.Host != "" {
switch cfg.Notifications.SMTP.Encryption {
case "tls":
logger.Warn().Msg("The smtp_encryption value 'tls' is deprecated. Please use the value 'starttls' instead.")
case "ssl":
logger.Warn().Msg("The smtp_encryption value 'ssl' is deprecated. Please use the value 'ssltls' instead.")
case "starttls", "ssltls", "none":
break
default:
return fmt.Errorf(
"unknown value '%s' for 'smtp_encryption' in service %s. Allowed values are 'starttls', 'ssltls' or 'none'",
cfg.Notifications.SMTP.Encryption, cfg.Service.Name,
)
}
}
2024-02-29 11:19:01 +01:00
{
if strings.TrimSpace(cfg.Notifications.SMTP.Sender) == "" {
return fmt.Errorf("the 'smtp_sender' must be a valid single RFC 5322 address. parsing error: the address cannot be empty")
}
if s, err := mail.ParseAddress(cfg.Notifications.SMTP.Sender); err == nil {
cfg.Notifications.SMTP.Sender = s.String()
} else {
return fmt.Errorf("the 'smtp_sender' must be a valid single RFC 5322 address. parsing error %w", err)
}
}
2024-02-29 11:19:01 +01:00
if cfg.ServiceAccount.ServiceAccountID == "" {
return shared.MissingServiceAccountID(cfg.Service.Name)
}
if cfg.ServiceAccount.ServiceAccountSecret == "" {
return shared.MissingServiceAccountSecret(cfg.Service.Name)
}
2022-02-18 15:44:53 +01:00
return nil
}