Skip to content

Commit

Permalink
Make sender alias configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kurlov committed Aug 27, 2024
1 parent 6c007c7 commit ac06a9f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions dp-terraform/helm/rhacs-terraform/templates/emailsender.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ spec:
value: {{ .Values.fleetshardSync.environment }}
- name: SENDER_ADDRESS
value: {{ .Values.emailsender.senderAddress }}
- name: SENDER_ALIAS
value: { { .Values.emailsender.senderAlias } }
- name: EMAIL_PROVIDER
value: {{ .Values.emailsender.emailProvider }}
- name: HTTPS_CERT_FILE
Expand Down
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 @@ -81,6 +81,7 @@ emailsender:
clusterName: ""
environment: ""
senderAddress: "[email protected]"
senderAlias: "RHACS Cloud Service"
authConfigFromKubernetes: true
emailProvider: "AWS_SES"
resources:
Expand Down
1 change: 1 addition & 0 deletions emailsender/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Config struct {
KubernetesSvcURL string `env:"KUBERNETES_SVC_URL" envDefault:"https://kubernetes.default.svc"`
KubernetesJWKSPath string `env:"KUBERNETES_JWKS_PATH" envDefault:"openid/v1/jwks"`
SenderAddress string `env:"SENDER_ADDRESS" envDefault:"[email protected]"`
SenderAlias string `env:"SENDER_ALIAS" envDefault:"RHACS Cloud Service"`
LimitEmailPerTenant int `env:"LIMIT_EMAIL_PER_TENANT" envDefault:"250"`
SesMaxBackoffDelay time.Duration `env:"SES_MAX_BACKOFF_DELAY" envDefault:"5s"`
SesMaxAttempts int `env:"SES_MAX_ATTEMPTS" envDefault:"3"`
Expand Down
15 changes: 9 additions & 6 deletions emailsender/pkg/email/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"github.com/stackrox/acs-fleet-manager/emailsender/pkg/metrics"
)

const fromTemplate = "From: RHACS Cloud Service <%s>\r\n"

const (
// EmailProviderAWSSES is the type name for the AWSEmailSender implementation of the Sender interface
EmailProviderAWSSES = "AWS_SES"
// EmailProviderLog is the type name for the LogEmailSender implementation of the Sender interface
EmailProviderLog = "LOG"

toFormat = "To: %s\r\n"
fromFormat = "From: %s <%s>\r\n"
)

// Sender defines the interface to send emails
Expand All @@ -32,19 +33,20 @@ func NewEmailSender(ctx context.Context, cfg *config.Config, rateLimiter RateLim
return &LogEmailSender{
from: cfg.SenderAddress,
}, nil
default:
case EmailProviderAWSSES:
// EmailProviderAWSSES is the default
ses, err := NewSES(ctx, cfg.SesMaxBackoffDelay, cfg.SesMaxAttempts)
if err != nil {
return nil, err
}
return &AWSMailSender{
from: cfg.SenderAddress,
fromAlias: cfg.SenderAlias,
ses: ses,
rateLimiter: rateLimiter,
}, nil

}
panic("Unknown email provider: " + cfg.EmailProvider)
}

// LogEmailSender is a Sender implementation that logs email messages to glog
Expand All @@ -61,6 +63,7 @@ func (l *LogEmailSender) Send(ctx context.Context, to []string, rawMessage []byt
// AWSMailSender is the default implementation for the Sender interface
type AWSMailSender struct {
from string
fromAlias string
ses *SES
rateLimiter RateLimiter
}
Expand All @@ -73,8 +76,8 @@ func (s *AWSMailSender) Send(ctx context.Context, to []string, rawMessage []byte
metrics.DefaultInstance().IncThrottledSendEmail(tenantID)
return fmt.Errorf("rate limit exceeded for tenant: %s", tenantID)
}
fromBytes := []byte(fmt.Sprintf(fromTemplate, s.from))
toBytes := []byte(fmt.Sprintf("To: %s\r\n", strings.Join(to, ",")))
fromBytes := []byte(fmt.Sprintf(fromFormat, s.fromAlias, s.from))
toBytes := []byte(fmt.Sprintf(toFormat, strings.Join(to, ",")))

raw := bytes.Join([][]byte{fromBytes, toBytes, rawMessage}, nil)
metrics.DefaultInstance().IncSendEmail(tenantID)
Expand Down
5 changes: 5 additions & 0 deletions emailsender/pkg/email/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (m *MockedRateLimiter) PersistEmailSendEvent(tenantID string) error {

func TestSend_Success(t *testing.T) {
from := "[email protected]"
fromAlias := "RHACS Cloud Service"
to := []string{"[email protected]", "[email protected]"}
subject := "Test subject"
textBody := "text body"
Expand Down Expand Up @@ -62,6 +63,7 @@ func TestSend_Success(t *testing.T) {
mockedSES := &SES{sesClient: mockClient}
mockedSender := AWSMailSender{
from,
fromAlias,
mockedSES,
mockedRateLimiter,
}
Expand All @@ -87,6 +89,7 @@ func TestSend_LimitExceeded(t *testing.T) {
mockedSES := &SES{sesClient: mockClient}
mockedSender := AWSMailSender{
"[email protected]",
"from-alias",
mockedSES,
mockedRateLimiter,
}
Expand All @@ -100,6 +103,7 @@ func TestSend_LimitExceeded(t *testing.T) {

func TestSendAppendsFromAndTo(t *testing.T) {
from := "[email protected]"
fromAlias := "RHACS Cloud Service"
to := []string{"[email protected]", "[email protected]"}
textBody := "text body"
tenantID := "test-tenant-id"
Expand All @@ -126,6 +130,7 @@ func TestSendAppendsFromAndTo(t *testing.T) {
mockedSES := &SES{sesClient: mockClient}
sender := AWSMailSender{
from,
fromAlias,
mockedSES,
mockedRateLimiter,
}
Expand Down

0 comments on commit ac06a9f

Please sign in to comment.