-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} | ||
|
||
public function getCredential(string $roleName): ApiTokenCredential | ||
{ | ||
$attempts = 0; | ||
|
||
$instanceMetadataServerURL = str_replace('%role_name%', $roleName, self::SERVER_URI_TEMPLATE); | ||
|
||
while (true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we try several times? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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