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:
Abhishek K 2024-08-22 13:03:49 +05:30 committed by GitHub
parent fe57a6a42d
commit 1924da2956
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 18 additions and 89 deletions

View file

@ -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"`

View file

@ -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

View file

@ -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(&params)
if err != nil {
return fmt.Errorf("failed sending mail via resend: %w", err)
}
return nil
}

View file

@ -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

View file

@ -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

View file

@ -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")