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

Custom SES email config from the builder #91

Merged
merged 2 commits into from
Feb 5, 2025
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
13 changes: 10 additions & 3 deletions proto/builder/builder.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions proto/builder/builder.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,50 @@ webrpc = v1 # TODO, use v2
name = sequence-builder
version = v0.1.0


# db table: "email_templates"
struct EmailTemplate
- id: uint64
+ go.field.name = ID
+ go.tag.db = id,omitempty

- templateType?: EmailTemplateType
+ go.field.name = TemplateType
+ go.tag.db = template_type

- projectId: uint64
+ go.field.name = ProjectID
+ go.tag.db = project_id

- subject: string
+ go.tag.db = subject

- introText: string
+ go.tag.db = intro_text

- logoUrl: string
+ go.field.name = LogoURL
+ go.tag.db = logo_url

- template?: string
+ go.field.name = Template
+ go.tag.db = template

- fromEmail?: string
+ go.tag.db = from_email
- placeholders: []string
+ go.tag.db = placeholders

- sesConfig?: SESSettings
+ go.tag.db = ses_config
- createdAt: timestamp
+ json = createdAt
+ go.tag.db = created_at

- updatedAt: timestamp
+ json = updatedAt
+ go.tag.db = updated_at

- deletedAt?: timestamp
+ go.tag.db = deleted_at,omitempty
+ go.tag.json = deletedAt,omitempty
+ go.field.type = *time.Time

struct SESSettings
- accessRoleARN: string
+ go.field.name = AccessRoleARN
- sourceARN: string
+ go.field.name = SourceARN

enum EmailTemplateType: uint8
- UNKNOWN
- LOGIN
Expand Down
10 changes: 10 additions & 0 deletions rpc/auth/email/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func (p *AuthProvider) InitiateAuth(
HTML: strings.Replace(*tpl.Template, "{auth_code}", secretCode, 1),
Text: tpl.IntroText + "\n\n" + secretCode,
}

if tpl.FromEmail != nil {
msg.Source = *tpl.FromEmail
}

if tpl.SesConfig != nil {
msg.SourceARN = tpl.SesConfig.SourceARN
msg.AccessRoleARN = tpl.SesConfig.AccessRoleARN
}

if err := p.Sender.Send(ctx, msg); err != nil {
return nil, fmt.Errorf("failed to send email: %w", err)
}
Expand Down
11 changes: 7 additions & 4 deletions rpc/auth/email/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
)

type Message struct {
Recipient string
Subject string
HTML string
Text string
Recipient string
Subject string
HTML string
Text string
Source string
SourceARN string
AccessRoleARN string
}

type Sender interface {
Expand Down
52 changes: 30 additions & 22 deletions rpc/auth/email/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,47 @@ import (
)

type sesSender struct {
client *ses.Client
source *string
sourceARN *string
cfg config.SESConfig
awsCfg aws.Config
}

func NewSESSender(awsCfg aws.Config, cfg config.SESConfig) Sender {
if cfg.AccessRoleARN != "" {
sender := &sesSender{
cfg: cfg,
awsCfg: awsCfg,
}

return sender
}

func (s *sesSender) Send(ctx context.Context, msg *Message) error {
awsCfg := s.awsCfg
accessRoleARN := s.cfg.AccessRoleARN
if msg.AccessRoleARN != "" {
accessRoleARN = msg.AccessRoleARN
}
if accessRoleARN != "" {
stsClient := sts.NewFromConfig(awsCfg)
creds := stscreds.NewAssumeRoleProvider(stsClient, cfg.AccessRoleARN)
creds := stscreds.NewAssumeRoleProvider(stsClient, accessRoleARN)
awsCfg.Credentials = aws.NewCredentialsCache(creds)
}

if cfg.Region != "" {
awsCfg.Region = cfg.Region
if s.cfg.Region != "" {
awsCfg.Region = s.cfg.Region
}

sender := &sesSender{
client: ses.NewFromConfig(awsCfg),
}
client := ses.NewFromConfig(awsCfg)

if cfg.Source != "" {
sender.source = &cfg.Source
source := &s.cfg.Source
if msg.Source != "" {
source = &msg.Source
}

if cfg.SourceARN != "" {
sender.sourceARN = &cfg.SourceARN
sourceARN := &s.cfg.SourceARN
if msg.SourceARN != "" {
sourceARN = &msg.SourceARN
}

return sender
}

func (s *sesSender) Send(ctx context.Context, msg *Message) error {
_, err := s.client.SendEmail(ctx, &ses.SendEmailInput{
_, err := client.SendEmail(ctx, &ses.SendEmailInput{
Destination: &types.Destination{
ToAddresses: []string{msg.Recipient},
},
Expand All @@ -64,8 +72,8 @@ func (s *sesSender) Send(ctx context.Context, msg *Message) error {
Charset: aws.String("UTF-8"),
},
},
Source: s.source,
SourceArn: s.sourceARN,
Source: source,
SourceArn: sourceARN,
})
return err
}