Skip to content

[Mailer] added Instance Profile support to SES Transport of Mailer #33326

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion src/Symfony/Component/Mailer/Bridge/Amazon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ CHANGELOG
-----

* [BC BREAK] Renamed and moved `Symfony\Component\Mailer\Bridge\Amazon\Http\Api\SesTransport`
to `Symfony\Component\Mailer\Bridge\Amazon\Transpor\SesApiTransport`, `Symfony\Component\Mailer\Bridge\Amazon\Http\SesTransport`
to `Symfony\Component\Mailer\Bridge\Amazon\Transport\SesApiTransport`, `Symfony\Component\Mailer\Bridge\Amazon\Http\SesTransport`
to `Symfony\Component\Mailer\Bridge\Amazon\Transport\SesHttpTransport`, `Symfony\Component\Mailer\Bridge\Amazon\Smtp\SesTransport`
to `Symfony\Component\Mailer\Bridge\Amazon\Transport\SesSmtpTransport`.
* [BC BREAK] changed `Symfony\Component\Mailer\Bridge\Amazon\Transport\SesApiTransport::__construct` username and password arguments to credential
* [BC BREAK] changed `Symfony\Component\Mailer\Bridge\Amazon\Transport\SesHttpTransport::__construct` username and password arguments to credential
* [BC BREAK] changed `Symfony\Component\Mailer\Bridge\Amazon\Transport\SesSmtpTransport::__construct` username and password arguments to credential
* Added Instance Profile support

4.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Amazon\Credential;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class ApiTokenCredential
{
private $accessKey;

private $secretKey;

private $token;

private $expiration;

public function __construct(string $accessKey, string $secretKey, string $token, \DateTime $expiration)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->token = $token;
$this->expiration = $expiration;
}

public function getAccessKey(): string
{
return $this->accessKey;
}

public function getSecretKey(): string
{
return $this->secretKey;
}

public function getToken(): string
{
return $this->token;
}

public function getExpiration(): \DateTime
{
return $this->expiration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Amazon\Credential;

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Mailer\Exception\RuntimeException;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* Based on: aws-sdk-php / Credentials/InstanceProfileProvider.php.
*
* @author Karoly Gossler <connor@connor.hu>
*/
class InstanceCredentialProvider
{
const SERVER_URI_TEMPLATE = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/%role_name%';

public function __construct(HttpClientInterface $client = null, int $retries = 3)
{
$this->retries = $retries;
$this->client = $client;

if (null === $this->client) {
if (!class_exists(HttpClient::class)) {
throw new LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
}

$this->client = HttpClient::create();
Copy link
Member

Choose a reason for hiding this comment

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

I think the default timeout is 1.0s in official SDK

}
}

public function getCredential(string $roleName): ApiTokenCredential
{
$attempts = 0;

$instanceMetadataServerURL = str_replace('%role_name%', $roleName, self::SERVER_URI_TEMPLATE);

while (true) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we try several times?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The amazon sdk does the same. I guess connecting to the 169.254.169.254 IP sometime may temporary fail, so thats why I did keep the same logic.

try {
++$attempts;

$response = $this->client->request('GET', $instanceMetadataServerURL);

if (200 === $response->getStatusCode()) {
$content = json_decode($response->getContent(), true);

if (null === $content) {
throw new RuntimeException('Unexpected instance metadata response.');
}

if ('Success' !== $content['Code']) {
$msg = sprintf('Unexpected instance profile response: %s', $content['Code']);
throw new RuntimeException($msg);
}

return new ApiTokenCredential($content['AccessKeyId'], $content['SecretAccessKey'], $content['Token'], new \DateTime($content['Expiration']));
} elseif (404 === $response->getStatusCode()) {
$attempts = $this->retries + 1;
}

sleep(pow(1.2, $attempts));
} catch (\Exception $e) {
}

if ($attempts > $this->retries) {
throw new RuntimeException('Error retrieving credentials from instance metadata server.');
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Bridge\Amazon\Credential;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class UsernamePasswordCredential
{
private $username;

private $password;

public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
}

public function getUsername(): string
{
return $this->username;
}

public function getPassword(): string
{
return $this->password;
}
}
Loading