Skip to content

Commit

Permalink
ROX-23249: multiple emailsender issues discovered by debugging (#1869)
Browse files Browse the repository at this point in the history
fixed some issue discovered by local debugging
  • Loading branch information
johannes94 authored Jun 10, 2024
1 parent 69e60e1 commit ed33c31
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions dp-terraform/helm/rhacs-terraform/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ emailsender:
clusterId: ""
clusterName: ""
environment: ""
senderAddress: "[email protected]"
authConfigFromKubernetes: true
resources:
requests:
Expand Down
4 changes: 2 additions & 2 deletions emailsender/cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func main() {
glog.Errorf("Failed to initialise SES Client: %v", err)
os.Exit(1)
}
temporarySenderName := "[email protected]"
emailSender := email.NewEmailSender(temporarySenderName, sesClient)

emailSender := email.NewEmailSender(cfg.SenderAddress, sesClient)
emailHandler := api.NewEmailHandler(emailSender)

router, err := api.SetupRoutes(cfg.AuthConfig, emailHandler)
Expand Down
1 change: 1 addition & 0 deletions emailsender/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"[email protected]"`
AuthConfig AuthConfig
}

Expand Down
4 changes: 2 additions & 2 deletions emailsender/pkg/api/emailhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
14 changes: 13 additions & 1 deletion emailsender/pkg/email/sender.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
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,
ses: ses,
}
}

// 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 ([email protected] 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)
Expand Down

0 comments on commit ed33c31

Please sign in to comment.