mirror of
https://github.com/gravitl/netmaker.git
synced 2024-11-10 09:32:32 +08:00
Net 1227 v1 (#3063)
* generalise smtp config * copy over smtp vars * env new line * fix master key api access * comment user tests * fix network and user invite for master key access * remove email sender type
This commit is contained in:
parent
fe57a6a42d
commit
1924da2956
6 changed files with 18 additions and 89 deletions
|
@ -95,8 +95,8 @@ type ServerConfig struct {
|
|||
EndpointDetection bool `json:"endpoint_detection"`
|
||||
AllowedEmailDomains string `yaml:"allowed_email_domains"`
|
||||
EmailSenderAddr string `json:"email_sender_addr"`
|
||||
EmailSenderAuth string `json:"email_sender_auth"`
|
||||
EmailSenderType string `json:"email_sender_type"`
|
||||
EmailSenderUser string `json:"email_sender_user"`
|
||||
EmailSenderPassword string `json:"email_sender_password"`
|
||||
SmtpHost string `json:"smtp_host"`
|
||||
SmtpPort int `json:"smtp_port"`
|
||||
MetricInterval string `yaml:"metric_interval"`
|
||||
|
|
|
@ -16,24 +16,19 @@ const (
|
|||
)
|
||||
|
||||
func init() {
|
||||
switch EmailSenderType(servercfg.EmailSenderType()) {
|
||||
case Smtp:
|
||||
smtpSender := &SmtpSender{
|
||||
SmtpHost: servercfg.GetSmtpHost(),
|
||||
SmtpPort: servercfg.GetSmtpPort(),
|
||||
SenderEmail: servercfg.GetSenderEmail(),
|
||||
SendUser: servercfg.GetSenderUser(),
|
||||
SenderPass: servercfg.GetEmaiSenderAuth(),
|
||||
}
|
||||
if smtpSender.SendUser == "" {
|
||||
smtpSender.SendUser = smtpSender.SenderEmail
|
||||
}
|
||||
client = smtpSender
|
||||
|
||||
case Resend:
|
||||
client = NewResendEmailSenderFromConfig()
|
||||
smtpSender := &SmtpSender{
|
||||
SmtpHost: servercfg.GetSmtpHost(),
|
||||
SmtpPort: servercfg.GetSmtpPort(),
|
||||
SenderEmail: servercfg.GetSenderEmail(),
|
||||
SendUser: servercfg.GetSenderUser(),
|
||||
SenderPass: servercfg.GetEmaiSenderPassword(),
|
||||
}
|
||||
client = GetClient()
|
||||
if smtpSender.SendUser == "" {
|
||||
smtpSender.SendUser = smtpSender.SenderEmail
|
||||
}
|
||||
client = smtpSender
|
||||
|
||||
}
|
||||
|
||||
// EmailSender - an interface for sending emails based on notifications and mail templates
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gravitl/netmaker/servercfg"
|
||||
"github.com/resendlabs/resend-go"
|
||||
)
|
||||
|
||||
// ResendEmailSender - implementation of EmailSender using Resend (https://resend.com)
|
||||
type ResendEmailSender struct {
|
||||
client ResendClient
|
||||
from string
|
||||
}
|
||||
|
||||
// ResendClient - dependency interface for resend client
|
||||
type ResendClient interface {
|
||||
Send(*resend.SendEmailRequest) (resend.SendEmailResponse, error)
|
||||
}
|
||||
|
||||
// NewResendEmailSender - constructs a ResendEmailSender
|
||||
func NewResendEmailSender(client ResendClient, from string) ResendEmailSender {
|
||||
return ResendEmailSender{client: client, from: from}
|
||||
}
|
||||
|
||||
// NewResendEmailSender - constructs a ResendEmailSender from config
|
||||
// TODO let main.go handle this and use dependency injection instead of calling this function
|
||||
func NewResendEmailSenderFromConfig() ResendEmailSender {
|
||||
key, from := servercfg.GetEmaiSenderAuth(), servercfg.GetSenderEmail()
|
||||
resender := resend.NewClient(key)
|
||||
return NewResendEmailSender(resender.Emails, from)
|
||||
}
|
||||
|
||||
// SendEmail - sends an email using resend-go (https://github.com/resendlabs/resend-go)
|
||||
func (es ResendEmailSender) SendEmail(ctx context.Context, notification Notification, email Mail) error {
|
||||
var (
|
||||
from = es.from
|
||||
to = notification.RecipientMail
|
||||
subject = email.GetSubject(notification)
|
||||
body = email.GetBody(notification)
|
||||
)
|
||||
params := resend.SendEmailRequest{
|
||||
From: from,
|
||||
To: []string{to},
|
||||
Subject: subject,
|
||||
Html: body,
|
||||
}
|
||||
_, err := es.client.Send(¶ms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed sending mail via resend: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -86,5 +86,4 @@ EMAIL_SENDER_ADDR=
|
|||
EMAIL_SENDER_USER=
|
||||
# sender smtp password
|
||||
EMAIL_SENDER_PASSWORD=
|
||||
# mail sender type (smtp or resend)
|
||||
EMAIL_SENDER_TYPE=smtp
|
||||
|
||||
|
|
|
@ -254,7 +254,7 @@ save_config() { (
|
|||
"CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "VERBOSITY"
|
||||
"DEBUG_MODE" "REST_BACKEND" "DISABLE_REMOTE_IP_CHECK" "TELEMETRY" "ALLOWED_EMAIL_DOMAINS" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
|
||||
"FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT" "JWT_VALIDITY_DURATION" "RAC_AUTO_DISABLE" "CACHING_ENABLED" "ENDPOINT_DETECTION"
|
||||
"SMTP_HOST" "SMTP_PORT" "EMAIL_SENDER_ADDR" "EMAIL_SENDER_USER" "EMAIL_SENDER_PASSWORD" "EMAIL_SENDER_TYPE")
|
||||
"SMTP_HOST" "SMTP_PORT" "EMAIL_SENDER_ADDR" "EMAIL_SENDER_USER" "EMAIL_SENDER_PASSWORD")
|
||||
for name in "${toCopy[@]}"; do
|
||||
save_config_item $name "${!name}"
|
||||
done
|
||||
|
|
|
@ -279,32 +279,22 @@ func GetSenderUser() string {
|
|||
v := ""
|
||||
if fromEnv := os.Getenv("EMAIL_SENDER_USER"); fromEnv != "" {
|
||||
v = fromEnv
|
||||
} else if fromCfg := config.Config.Server.EmailSenderAddr; fromCfg != "" {
|
||||
} else if fromCfg := config.Config.Server.EmailSenderUser; fromCfg != "" {
|
||||
v = fromCfg
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func GetEmaiSenderAuth() string {
|
||||
func GetEmaiSenderPassword() string {
|
||||
v := ""
|
||||
if fromEnv := os.Getenv("EMAIL_SENDER_PASSWORD"); fromEnv != "" {
|
||||
v = fromEnv
|
||||
} else if fromCfg := config.Config.Server.EmailSenderAddr; fromCfg != "" {
|
||||
} else if fromCfg := config.Config.Server.EmailSenderPassword; fromCfg != "" {
|
||||
v = fromCfg
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func EmailSenderType() string {
|
||||
s := ""
|
||||
if fromEnv := os.Getenv("EMAIL_SENDER_TYPE"); fromEnv != "" {
|
||||
s = fromEnv
|
||||
} else if fromCfg := config.Config.Server.EmailSenderType; fromCfg != "" {
|
||||
s = fromCfg
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// GetOwnerEmail - gets the owner email (saas)
|
||||
func GetOwnerEmail() string {
|
||||
return os.Getenv("SAAS_OWNER_EMAIL")
|
||||
|
|
Loading…
Reference in a new issue