Skip to content

Commit

Permalink
Move recipientRegex field to SES Config object
Browse files Browse the repository at this point in the history
  • Loading branch information
ClayBenson94 committed Oct 16, 2024
1 parent bbf80ab commit a30c9fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
23 changes: 11 additions & 12 deletions pkg/appses/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,28 @@ import (

// Config is email configs used only for SES
type Config struct {
SourceARN string
Source string
SourceARN string
Source string
RecipientRegex *regexp.Regexp // if not nil, a regex that a recipient must match in order to be sent to
}

// Sender is an implementation for sending email with the SES Go SDK
// It lives in package "email" for now, but can be pulled out and imported
// if necessary for testing
type Sender struct {
client *ses.SES
config Config
environment appconfig.Environment
recipientRegex *regexp.Regexp // if not nil, a regex that a recipient must match in order to be sent to
client *ses.SES
config Config
environment appconfig.Environment
}

// NewSender constructs a Sender
func NewSender(config Config, environment appconfig.Environment, recipientRegex *regexp.Regexp) Sender {
func NewSender(config Config, environment appconfig.Environment) Sender {
sesSession := session.Must(session.NewSession())
client := ses.New(sesSession)
return Sender{
client,
config,
environment,
recipientRegex,
}
}

Expand All @@ -61,10 +60,10 @@ func (s Sender) Send(ctx context.Context, emailData email.Email) error {
}

// If a filter has been configured, filter out any addresses that don't match our allow-list
if s.recipientRegex != nil {
emailData.ToAddresses = filterAddresses(emailData.ToAddresses, s.recipientRegex)
emailData.CcAddresses = filterAddresses(emailData.CcAddresses, s.recipientRegex)
emailData.BccAddresses = filterAddresses(emailData.BccAddresses, s.recipientRegex)
if s.config.RecipientRegex != nil {
emailData.ToAddresses = filterAddresses(emailData.ToAddresses, s.config.RecipientRegex)
emailData.CcAddresses = filterAddresses(emailData.CcAddresses, s.config.RecipientRegex)
emailData.BccAddresses = filterAddresses(emailData.BccAddresses, s.config.RecipientRegex)
}

input := &ses.SendEmailInput{
Expand Down
7 changes: 4 additions & 3 deletions pkg/appses/ses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ func TestSESTestSuite(t *testing.T) {
}

sesConfig := Config{
SourceARN: config.GetString(appconfig.AWSSESSourceARNKey),
Source: config.GetString(appconfig.AWSSESSourceKey),
SourceARN: config.GetString(appconfig.AWSSESSourceARNKey),
Source: config.GetString(appconfig.AWSSESSourceKey),
RecipientRegex: nil,
}

sender := NewSender(sesConfig, env, nil)
sender := NewSender(sesConfig, env)

sesTestSuite := &SESTestSuite{
Suite: suite.Suite{},
Expand Down
16 changes: 14 additions & 2 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package server

import (
"fmt"
"regexp"
"time"

"go.uber.org/zap"

"github.com/cms-enterprise/easi-app/pkg/appconfig"
"github.com/cms-enterprise/easi-app/pkg/appses"
"github.com/cms-enterprise/easi-app/pkg/email"
Expand Down Expand Up @@ -76,9 +79,18 @@ func (s Server) NewSESConfig() appses.Config {
s.checkRequiredConfig(appconfig.AWSSESSourceARNKey)
s.checkRequiredConfig(appconfig.AWSSESSourceKey)

// Parse the regex config if configured (not required)
sesRegexString := s.Config.GetString(appconfig.SESRecipientRegexKey)
var sesRegex *regexp.Regexp
if sesRegexString != "" { // only attempt to parse if it's a non-empty string
sesRegex = regexp.MustCompile(sesRegexString)
s.logger.Info("successfully parsed ses regex:", zap.String("parsedRegex", sesRegex.String()))
}

return appses.Config{
SourceARN: s.Config.GetString(appconfig.AWSSESSourceARNKey),
Source: s.Config.GetString(appconfig.AWSSESSourceKey),
SourceARN: s.Config.GetString(appconfig.AWSSESSourceARNKey),
Source: s.Config.GetString(appconfig.AWSSESSourceKey),
RecipientRegex: sesRegex,
}
}

Expand Down
10 changes: 1 addition & 9 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -139,15 +138,8 @@ func (s *Server) routes() {
var emailClient email.Client
switch {
case s.environment.Deployed():
// Parse the regex config and pass it to the Sender if configured
sesRegexString := s.Config.GetString(appconfig.SESRecipientRegexKey)
var sesRegex *regexp.Regexp
if sesRegexString != "" { // only attempt to parse if it's a non-empty string
sesRegex = regexp.MustCompile(sesRegexString)
s.logger.Info("successfully parsed ses regex:", zap.String("parsedRegex", sesRegex.String()))
}
sesConfig := s.NewSESConfig()
sesSender := appses.NewSender(sesConfig, s.environment, sesRegex)
sesSender := appses.NewSender(sesConfig, s.environment)
emailClient, err = email.NewClient(emailConfig, sesSender)
if err != nil {
s.logger.Fatal("Failed to create email client", zap.Error(err))
Expand Down

0 comments on commit a30c9fa

Please sign in to comment.