Skip to content

Commit

Permalink
SES: add SourceArn, FromArn and ReturnPathArn blueimp#16
Browse files Browse the repository at this point in the history
  • Loading branch information
clark42 committed Nov 17, 2021
1 parent 7fff45b commit 4987028
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
12 changes: 12 additions & 0 deletions internal/relay/ses/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Client struct {
setName *string
allowFromRegExp *regexp.Regexp
denyToRegExp *regexp.Regexp
sourceArn *string
fromArn *string
returnPathArn *string
}

// Send uses the client SESAPI to send email data
Expand All @@ -40,6 +43,9 @@ func (c Client) Send(
Source: &from,
Destinations: allowedRecipients,
RawMessage: &ses.RawMessage{Data: data},
SourceArn: c.sourceArn,
FromArn: c.fromArn,
ReturnPathArn: c.returnPathArn,
})
relay.Log(origin, &from, allowedRecipients, err)
if err != nil {
Expand All @@ -54,11 +60,17 @@ func New(
configurationSetName *string,
allowFromRegExp *regexp.Regexp,
denyToRegExp *regexp.Regexp,
sourceArn *string,
fromArn *string,
returnPathArn *string,
) Client {
return Client{
sesAPI: ses.New(session.Must(session.NewSession())),
setName: configurationSetName,
allowFromRegExp: allowFromRegExp,
denyToRegExp: denyToRegExp,
sourceArn: sourceArn,
fromArn: fromArn,
returnPathArn: returnPathArn,
}
}
45 changes: 39 additions & 6 deletions internal/relay/ses/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func sendHelper(
configurationSetName *string,
allowFromRegExp *regexp.Regexp,
denyToRegExp *regexp.Regexp,
sourceArn *string,
fromArn *string,
returnPathArn *string,
apiErr error,
) (email *ses.SendRawEmailInput, out []byte, err []byte, sendErr error) {
outReader, outWriter, _ := os.Pipe()
Expand All @@ -58,6 +61,9 @@ func sendHelper(
setName: configurationSetName,
allowFromRegExp: allowFromRegExp,
denyToRegExp: denyToRegExp,
sourceArn: sourceArn,
fromArn: fromArn,
returnPathArn: returnPathArn,
}
testData.err = apiErr
sendErr = c.Send(origin, from, to, data)
Expand All @@ -75,7 +81,10 @@ func TestSend(t *testing.T) {
to := []string{"bob@example.org"}
data := []byte{'T', 'E', 'S', 'T'}
setName := ""
input, out, err, _ := sendHelper(&origin, from, to, data, &setName, nil, nil, nil)
sourceArn := ""
fromArn := ""
returnPathArn := ""
input, out, err, _ := sendHelper(&origin, from, to, data, &setName, nil, nil, &sourceArn, &fromArn, &returnPathArn, nil)
if *input.Source != from {
t.Errorf(
"Unexpected source: %s. Expected: %s",
Expand Down Expand Up @@ -115,7 +124,10 @@ func TestSendWithMultipleRecipients(t *testing.T) {
to := []string{"bob@example.org", "charlie@example.org"}
data := []byte{'T', 'E', 'S', 'T'}
setName := ""
input, out, err, _ := sendHelper(&origin, from, to, data, &setName, nil, nil, nil)
sourceArn := ""
fromArn := ""
returnPathArn := ""
input, out, err, _ := sendHelper(&origin, from, to, data, &setName, nil, nil, &sourceArn, &fromArn, &returnPathArn, nil)
if len(input.Destinations) != 2 {
t.Errorf(
"Unexpected number of destinations: %d. Expected: %d",
Expand Down Expand Up @@ -144,8 +156,11 @@ func TestSendWithDeniedSender(t *testing.T) {
to := []string{"bob@example.org", "charlie@example.org"}
data := []byte{'T', 'E', 'S', 'T'}
setName := ""
sourceArn := ""
fromArn := ""
returnPathArn := ""
regexp, _ := regexp.Compile(`^admin@example\.org$`)
input, out, err, sendErr := sendHelper(&origin, from, to, data, &setName, regexp, nil, nil)
input, out, err, sendErr := sendHelper(&origin, from, to, data, &setName, regexp, nil, &sourceArn, &fromArn, &returnPathArn, nil)
if input != nil {
t.Errorf(
"Unexpected number of destinations: %d. Expected: %d",
Expand All @@ -170,8 +185,11 @@ func TestSendWithDeniedRecipient(t *testing.T) {
to := []string{"bob@example.org", "charlie@example.org"}
data := []byte{'T', 'E', 'S', 'T'}
setName := ""
sourceArn := ""
fromArn := ""
returnPathArn := ""
regexp, _ := regexp.Compile(`^bob@example\.org$`)
input, out, err, sendErr := sendHelper(&origin, from, to, data, &setName, nil, regexp, nil)
input, out, err, sendErr := sendHelper(&origin, from, to, data, &setName, nil, regexp, &sourceArn, &fromArn, &returnPathArn, nil)
if len(input.Destinations) != 1 {
t.Errorf(
"Unexpected number of destinations: %d. Expected: %d",
Expand Down Expand Up @@ -203,8 +221,11 @@ func TestSendWithApiError(t *testing.T) {
to := []string{"bob@example.org"}
data := []byte{'T', 'E', 'S', 'T'}
setName := ""
sourceArn := ""
fromArn := ""
returnPathArn := ""
apiErr := errors.New("API failure")
input, out, err, sendErr := sendHelper(&origin, from, to, data, &setName, nil, nil, apiErr)
input, out, err, sendErr := sendHelper(&origin, from, to, data, &setName, nil, nil, &sourceArn, &fromArn, &returnPathArn, apiErr)
if *input.Source != from {
t.Errorf(
"Unexpected source: %s. Expected: %s",
Expand Down Expand Up @@ -245,7 +266,10 @@ func TestNew(t *testing.T) {
setName := ""
allowFromRegExp, _ := regexp.Compile(`^admin@example\.org$`)
denyToRegExp, _ := regexp.Compile(`^bob@example\.org$`)
client := New(&setName, allowFromRegExp, denyToRegExp)
sourceArn := ""
fromArn := ""
returnPathArn := ""
client := New(&setName, allowFromRegExp, denyToRegExp, &sourceArn, &fromArn, &returnPathArn)
_, ok := interface{}(client).(relay.Client)
if !ok {
t.Error("Unexpected: client is not a relay.Client")
Expand All @@ -259,4 +283,13 @@ func TestNew(t *testing.T) {
if client.denyToRegExp != denyToRegExp {
t.Errorf("Unexpected denyToRegExp: %s", client.denyToRegExp)
}
if client.sourceArn != &sourceArn {
t.Errorf("Unexpected sourceArn: %s", *client.sourceArn)
}
if client.fromArn != &fromArn {
t.Errorf("Unexpected fromArn: %s", *client.fromArn)
}
if client.returnPathArn != &returnPathArn {
t.Errorf("Unexpected returnPathArn: %s", *client.returnPathArn)
}
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var (
user = flag.String("u", "", "Authentication username")
allowFrom = flag.String("l", "", "Allowed sender emails regular expression")
denyTo = flag.String("d", "", "Denied recipient emails regular expression")
sourceArn = flag.String("sourcearn", "", "The ARN of the identity that is associated with the sending authorization policy that permits you to send for the email address specified in the Source parameter of SendRawEmail.")
fromArn = flag.String("fromarn", "", "The ARN of the identity that is associated with the sending authorization policy that permits you to specify a particular 'From' address in the header of the raw email.")
rPathArn = flag.String("returnpatharn", "", "The ARN of the identity that is associated with the sending authorization policy that permits you to use the email address specified in the ReturnPath parameter of SendRawEmail.")
)

var ipMap map[string]bool
Expand Down Expand Up @@ -83,7 +86,7 @@ func configure() error {
case "pinpoint":
relayClient = pinpointrelay.New(setName, allowFromRegExp, denyToRegExp)
case "ses":
relayClient = sesrelay.New(setName, allowFromRegExp, denyToRegExp)
relayClient = sesrelay.New(setName, allowFromRegExp, denyToRegExp, sourceArn, fromArn, rPathArn)
default:
return errors.New("Invalid relay API: " + *relayAPI)
}
Expand Down

0 comments on commit 4987028

Please sign in to comment.