Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROX-23249: multiple emailsender issues discovered by debugging #1869

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading