From f589bc9e94c21b2bedfb731e954ee1e3594526a2 Mon Sep 17 00:00:00 2001 From: Johannes Malsam Date: Fri, 7 Jun 2024 09:04:34 +0200 Subject: [PATCH] fixed some issue discovered by local debugging --- dp-terraform/helm/rhacs-terraform/values.yaml | 1 + emailsender/cmd/app/main.go | 4 ++-- emailsender/config/config.go | 1 + emailsender/pkg/api/emailhandler.go | 4 ++-- emailsender/pkg/email/sender.go | 14 +++++++++++++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/dp-terraform/helm/rhacs-terraform/values.yaml b/dp-terraform/helm/rhacs-terraform/values.yaml index f5360faf7b..9b4ecefb9e 100644 --- a/dp-terraform/helm/rhacs-terraform/values.yaml +++ b/dp-terraform/helm/rhacs-terraform/values.yaml @@ -76,6 +76,7 @@ emailsender: clusterId: "" clusterName: "" environment: "" + senderAddress: "noreply@mail.rhacs-dev.com" authConfigFromKubernetes: true resources: requests: diff --git a/emailsender/cmd/app/main.go b/emailsender/cmd/app/main.go index 0f06c7dce4..11a5378a23 100644 --- a/emailsender/cmd/app/main.go +++ b/emailsender/cmd/app/main.go @@ -44,8 +44,8 @@ func main() { glog.Errorf("Failed to initialise SES Client: %v", err) os.Exit(1) } - temporarySenderName := "noreply@mail.acs.rhcloud.com" - emailSender := email.NewEmailSender(temporarySenderName, sesClient) + + emailSender := email.NewEmailSender(cfg.SenderAddress, sesClient) emailHandler := api.NewEmailHandler(emailSender) router, err := api.SetupRoutes(cfg.AuthConfig, emailHandler) diff --git a/emailsender/config/config.go b/emailsender/config/config.go index 6b769da707..6e887c4043 100644 --- a/emailsender/config/config.go +++ b/emailsender/config/config.go @@ -36,6 +36,7 @@ type Config struct { MetricsAddress string `env:"METRICS_ADDRESS" envDefault:":9090"` AuthConfigFile string `env:"AUTH_CONFIG_FILE" envDefault:"config/emailsender-authz.yaml"` AuthConfigFromKubernetes bool `env:"AUTH_CONFIG_FROM_KUBERNETES" envDefault:"false"` + SenderAddress string `env:"SENDER_ADDRESS" envDefault:"noreply@mail.rhacs-dev.com"` AuthConfig AuthConfig } diff --git a/emailsender/pkg/api/emailhandler.go b/emailsender/pkg/api/emailhandler.go index 5c399e0853..5f0563397f 100644 --- a/emailsender/pkg/api/emailhandler.go +++ b/emailsender/pkg/api/emailhandler.go @@ -18,8 +18,8 @@ type EmailHandler struct { // SendEmailRequest represents API requests for sending email type SendEmailRequest struct { - To []string - RawMessage []byte + To []string `json:"to"` + RawMessage []byte `json:"rawMessage"` } type Envelope map[string]interface{} diff --git a/emailsender/pkg/email/sender.go b/emailsender/pkg/email/sender.go index 95e87e4d21..f353e5f7be 100644 --- a/emailsender/pkg/email/sender.go +++ b/emailsender/pkg/email/sender.go @@ -1,20 +1,27 @@ package email import ( + "bytes" "context" "fmt" + "github.com/golang/glog" ) +const fromTemplate = "From: RHACS Cloud Service <%s>\r\n" + +// Sender defines the interface to send emails type Sender interface { Send(ctx context.Context, to []string, rawMessage []byte) error } +// MailSender is the default implementation for the Sender interface type MailSender struct { from string ses *SES } +// NewEmailSender returns a new MailSender instance func NewEmailSender(from string, ses *SES) *MailSender { return &MailSender{ from: from, @@ -22,8 +29,13 @@ func NewEmailSender(from string, ses *SES) *MailSender { } } +// Send sends an email to the given AWS SES func (s *MailSender) Send(ctx context.Context, to []string, rawMessage []byte) error { - _, err := s.ses.SendRawEmail(ctx, s.from, to, rawMessage) + // Even though AWS adds the from handler we need to set it the the message to show + // an alias in email inboxes that is more human friendly (noreply@rhacs-dev.com vs. RHACS Cloud Service) + fromBytes := []byte(fmt.Sprintf(fromTemplate, s.from)) + raw := bytes.Join([][]byte{fromBytes, rawMessage}, nil) + _, err := s.ses.SendRawEmail(ctx, s.from, to, raw) if err != nil { glog.Errorf("Failed sending email: %v", err) return fmt.Errorf("failed to send email: %v", err)