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

Cross Account - sourceArn #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to create a struct called arns that consists of those three arns and add that as member to this struct. Makes handling the arguments for a lot of the function calls a bit simpler.

}

// 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)
}
}
21 changes: 20 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("f", "", "The SourceARN (when using with cross accounts) ")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would change the description of all three flags to the following (for consistency with setName):

  • Amazon SES SourceARN
  • Amazon SES FromARN
  • Amazon SES ReturnPathARN

fromArn = flag.String("o", "", "The FromARN (when using with cross accounts)")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would switch the flags for sourceArn and fromArn, since f is better associated with from.

rPathArn = flag.String("p", "", "The ReturnPathARN (when using with cross accounts)")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, we should call this variable returnPathArn.

)

var ipMap map[string]bool
Expand Down Expand Up @@ -79,11 +82,27 @@ func configure() error {
return errors.New("Denied recipient emails: " + err.Error())
}
}
if *sourceArn != "" {
if *fromArn == "" {
fromArn = sourceArn
}
if *rPathArn == "" {
rPathArn = sourceArn
}
} else {
sourceArn = nil
}
if *fromArn == "" {
fromArn = nil
}
if *rPathArn == "" {
rPathArn = nil
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on why you're setting the variables to nil here?

switch *relayAPI {
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