Skip to content

Add Session Token to Amazon Mailer #42982

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

Merged
merged 1 commit into from
Sep 13, 2021

Conversation

Jubeki
Copy link
Contributor

@Jubeki Jubeki commented Sep 11, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR There doesn't appear to be a Amazon/SES section in the docs right now

This PR provide's support for Amazons separate Session Token feature in both the API and HTTPS transports. I've added the ability to set the session token via a query parameter in the DSN.

Right now in Laravel users have the ability to use a Session Token together with temporary Access and Secret keys for sending emails. But unfortunately Symfony Mailer, which we're switching to for the upcoming Laravel v9 release doesn't has this feature yet. That's why I decided to send in this PR so both Symfony and Laravel users can enjoy this feature from SES / Amazon. This PR is needed for laravel/framework#38481

Documentation for the Session Token with temporary credentials can be found here: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html
Documentation for creating AsyncAws/Ses/SesClient can be found here: https://async-aws.com/configuration.html#sessiontoken

Current setup through Laravel can be found here: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Mail/MailManager.php#L258-L293
(It seems like Laravel has not documented this feature yet. I will be sending a PR to the Laravel docs and then update this PR accordingly)

@driesvints maybe you can also take a look at this PR.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@Jubeki
Copy link
Contributor Author

Jubeki commented Sep 11, 2021

I don't know why the PhpUnit with PHP 8.0 fails but it seems unrelated to my PR. Maybe somebody can trigger a rerun.

Error:

1) Symfony\Component\Messenger\Bridge\Doctrine\Tests\Transport\DoctrineIntegrationTest::testConnectionSendAndGet
710
Doctrine\DBAL\Exception\ReadOnlyException: An exception occurred while executing a query: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database

@Jubeki Jubeki changed the title Add Session Token to Amazon Mailer [Mailer] Add Session Token to Amazon Mailer Sep 11, 2021
@jderusse
Copy link
Member

jderusse commented Sep 11, 2021

Hey, thank you for this PR. Is it a duplicate of #40193 ?

Having a session_token in a DSN sounds weird to me. As you pointed in the AWS documentation, Session Tokens are temporary credentials, but DSN is a configuration, that is not the best candidate for temporary configuration.

That's said. I realize that the TransportFactory takes a Dsn instance in the parameter, so, even if the users don't use string configuration (ie. ses://key:secret@default), they have to create an instance of Dsn class event when credentials (session_token) are fetched programmaticaly.

Note: In the Laravel PR you linked, the DSN is built from the $config variable, which looks suspicious to me. I've the feeling that you are expecting to store temporary credentials in a config file...

Why not using Env variable like suggested in #40193 ?

@Jubeki
Copy link
Contributor Author

Jubeki commented Sep 11, 2021

Yeah it is a duplicate of the PR.

In Laravel the token is passed through the config right now. I am not sure why though and I am not sure when and where it is explicitly set.

It was introduced in the following PR for SES and it seems like it is intended for the config:
laravel/framework#23766

It was expanded to SQS and S3 in the following PR: (unrelated for this PR though)
laravel/framework#24746

I would forward the question to @driesvints and @taylorotwell
Maybe they have a reason for doing it. Also in Regards to Laravel Vapor which uses AWS Lambda.

I think it would be possible using ENV. But I am unsure what happens if you maybe change the SESSION_TOKEN between two different SesTransports.

Example you want to send an email with one configuration to the users and then send another internal email to the admins using another configuration. (Probably unlikely but possible)

Copy link
Member

@jderusse jderusse left a comment

Choose a reason for hiding this comment

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

regarding laravel/framework#23766 the simplest way is to NOT convert env variable in config, and let SDK deal with the env variable instead.

 return [
     'ses' => [
-        'key'    => env('AWS_ACCESS_KEY_ID'),
-        'secret' => env('AWS_SECRET_ACCESS_KEY'),
-        'token'  => env('AWS_SESSION_TOKEN'),
         'region' => env('AWS_REGION', 'eu-west-1'),  // e.g. us-east-1
     ],
 ];

but still... People fetching config in another way (lets say people use an internal mechanism to fetch the session token that is not an env variable) will be stuck. I'm 👍 for this PR

@carsonbot carsonbot changed the title [Mailer] Add Session Token to Amazon Mailer Add Session Token to Amazon Mailer Sep 11, 2021
@Jubeki
Copy link
Contributor Author

Jubeki commented Sep 11, 2021

Using the config has another advantage:
It doesn't matter if you use temporary credentials or not because the token is only attached if it exists.
Therefor you only need one setup method just with different credentials.

Because the access_key and secret is loaded from the config in laravel. It doesn't matter how the env variables is named in the .env file.

Before 2018 the variables in the default config also were named differently and were then renamed:
laravel/laravel@87667b2#diff-a7bf72e66282b81a3685555cf2fa6fbb80ba61953539e8d6e80b603eb58e0709

Some people don't upgrade the default configuration for the configs. That would be an addionational upgrade step.

Copy link
Contributor

@driesvints driesvints left a comment

Choose a reason for hiding this comment

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

Thanks @Jubeki! Just what we need 👍

@fabpot fabpot force-pushed the add-session-token-to-amazon-mailer branch from 93eb5e0 to fdeec77 Compare September 13, 2021 15:21
@fabpot
Copy link
Member

fabpot commented Sep 13, 2021

Thank you @Jubeki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants