Laravel version update
Laravel version update
This commit is contained in:
64
vendor/aws/aws-sdk-php/src/Credentials/AssumeRoleCredentialProvider.php
vendored
Normal file
64
vendor/aws/aws-sdk-php/src/Credentials/AssumeRoleCredentialProvider.php
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace Aws\Credentials;
|
||||
|
||||
use Aws\Exception\CredentialsException;
|
||||
use Aws\Result;
|
||||
use Aws\Sts\StsClient;
|
||||
use GuzzleHttp\Promise\PromiseInterface;
|
||||
|
||||
/**
|
||||
* Credential provider that provides credentials via assuming a role
|
||||
* More Information, see: http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sts-2011-06-15.html#assumerole
|
||||
*/
|
||||
class AssumeRoleCredentialProvider
|
||||
{
|
||||
const ERROR_MSG = "Missing required 'AssumeRoleCredentialProvider' configuration option: ";
|
||||
|
||||
/** @var StsClient */
|
||||
private $client;
|
||||
|
||||
/** @var array */
|
||||
private $assumeRoleParams;
|
||||
|
||||
/**
|
||||
* The constructor requires following configure parameters:
|
||||
* - client: a StsClient
|
||||
* - assume_role_params: Parameters used to make assumeRole call
|
||||
*
|
||||
* @param array $config Configuration options
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
if (!isset($config['assume_role_params'])) {
|
||||
throw new \InvalidArgumentException(self::ERROR_MSG . "'assume_role_params'.");
|
||||
}
|
||||
|
||||
if (!isset($config['client'])) {
|
||||
throw new \InvalidArgumentException(self::ERROR_MSG . "'client'.");
|
||||
}
|
||||
|
||||
$this->client = $config['client'];
|
||||
$this->assumeRoleParams = $config['assume_role_params'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads assume role credentials.
|
||||
*
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
$client = $this->client;
|
||||
return $client->assumeRoleAsync($this->assumeRoleParams)
|
||||
->then(function (Result $result) {
|
||||
return $this->client->createCredentials($result);
|
||||
})->otherwise(function (\RuntimeException $exception) {
|
||||
throw new CredentialsException(
|
||||
"Error in retrieving assume role credentials.",
|
||||
0,
|
||||
$exception
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -50,37 +50,28 @@ class CredentialProvider
|
||||
/**
|
||||
* Create a default credential provider that first checks for environment
|
||||
* variables, then checks for the "default" profile in ~/.aws/credentials,
|
||||
* then tries to make GET Request to fetch credentials if Ecs environment
|
||||
* variable is presented, and finally checks for EC2 instance profile
|
||||
* credentials.
|
||||
* then checks for "profile default" profile in ~/.aws/config (which is
|
||||
* the default profile of AWS CLI), then tries to make a GET Request to
|
||||
* fetch credentials if Ecs environment variable is presented, and finally
|
||||
* checks for EC2 instance profile credentials.
|
||||
*
|
||||
* This provider is automatically wrapped in a memoize function that caches
|
||||
* previously provided credentials.
|
||||
*
|
||||
* @param array $config Optional array of instance profile credentials
|
||||
* @param array $config Optional array of ecs/instance profile credentials
|
||||
* provider options.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public static function defaultProvider(array $config = [])
|
||||
{
|
||||
$instanceProfileProvider = self::instanceProfile($config);
|
||||
$ecsCredentialProvider = self::ecsCredentials($config);
|
||||
|
||||
if (isset($config['credentials'])
|
||||
&& $config['credentials'] instanceof CacheInterface
|
||||
) {
|
||||
$instanceProfileProvider = self::cache(
|
||||
$instanceProfileProvider,
|
||||
$config['credentials']
|
||||
);
|
||||
}
|
||||
$localCredentialProviders = self::localCredentialProviders();
|
||||
$remoteCredentialProviders = self::remoteCredentialProviders($config);
|
||||
|
||||
return self::memoize(
|
||||
self::chain(
|
||||
self::env(),
|
||||
self::ini(),
|
||||
$ecsCredentialProvider,
|
||||
$instanceProfileProvider
|
||||
call_user_func_array(
|
||||
'self::chain',
|
||||
array_merge($localCredentialProviders, $remoteCredentialProviders)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -179,7 +170,7 @@ class CredentialProvider
|
||||
* Defaults to using a simple file-based cache when none provided.
|
||||
*
|
||||
* @param callable $provider Credentials provider function to wrap
|
||||
* @param CacheInterface $cache (optional) Cache to store credentials
|
||||
* @param CacheInterface $cache Cache to store credentials
|
||||
* @param string|null $cacheKey (optional) Cache key to use
|
||||
*
|
||||
* @return callable
|
||||
@@ -228,7 +219,7 @@ class CredentialProvider
|
||||
$secret = getenv(self::ENV_SECRET);
|
||||
if ($key && $secret) {
|
||||
return Promise\promise_for(
|
||||
new Credentials($key, $secret, getenv(self::ENV_SESSION))
|
||||
new Credentials($key, $secret, getenv(self::ENV_SESSION) ?: NULL)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -266,14 +257,26 @@ class CredentialProvider
|
||||
return new EcsCredentialProvider($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Credential provider that creates credentials using assume role
|
||||
*
|
||||
* @param array $config Array of configuration data
|
||||
* @return callable
|
||||
* @see Aws\Credentials\AssumeRoleCredentialProvider for $config details.
|
||||
*/
|
||||
public static function assumeRole(array $config=[])
|
||||
{
|
||||
return new AssumeRoleCredentialProvider($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Credentials provider that creates credentials using an ini file stored
|
||||
* in the current user's home directory.
|
||||
*
|
||||
* @param string|null $profile Profile to use. If not specified will use
|
||||
* the "default" profile.
|
||||
* the "default" profile in "~/.aws/credentials".
|
||||
* @param string|null $filename If provided, uses a custom filename rather
|
||||
* than looking in the home directory for the
|
||||
* than looking in the home directory.
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
@@ -317,6 +320,56 @@ class CredentialProvider
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Local credential providers returns a list of local credential providers
|
||||
* in following order:
|
||||
* - credentials from environment variables
|
||||
* - 'default' profile in '.aws/credentials' file
|
||||
* - 'profile default' profile in '.aws/config' file
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function localCredentialProviders()
|
||||
{
|
||||
return [
|
||||
self::env(),
|
||||
self::ini(),
|
||||
self::ini('profile default', self::getHomeDir() . '/.aws/config')
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remote credential providers returns a list of credentials providers
|
||||
* for the remote endpoints such as EC2 or ECS Roles.
|
||||
*
|
||||
* @param array $config Array of configuration data.
|
||||
*
|
||||
* @return array
|
||||
* @see Aws\Credentials\InstanceProfileProvider for $config details.
|
||||
* @see Aws\Credentials\EcsCredentialProvider for $config details.
|
||||
*/
|
||||
private static function remoteCredentialProviders(array $config = [])
|
||||
{
|
||||
if (!empty(getenv(EcsCredentialProvider::ENV_URI))) {
|
||||
$providers['ecs'] = self::ecsCredentials($config);
|
||||
}
|
||||
$providers['instance'] = self::instanceProfile($config);
|
||||
|
||||
if (isset($config['credentials'])
|
||||
&& $config['credentials'] instanceof CacheInterface
|
||||
) {
|
||||
foreach ($providers as $key => $provider) {
|
||||
$providers[$key] = self::cache(
|
||||
$provider,
|
||||
$config['credentials'],
|
||||
'aws_cached_' . $key . '_credentials'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the environment's HOME directory if available.
|
||||
*
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
namespace Aws\Credentials;
|
||||
|
||||
use Aws\Exception\CredentialsException;
|
||||
use GuzzleHttp\Promise;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use GuzzleHttp\Promise\PromiseInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
@@ -14,19 +12,24 @@ use Psr\Http\Message\ResponseInterface;
|
||||
*/
|
||||
class EcsCredentialProvider
|
||||
{
|
||||
const SERVER_URI = 'http://169.254.170.2/';
|
||||
const SERVER_URI = 'http://169.254.170.2';
|
||||
const ENV_URI = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
|
||||
|
||||
/** @var callable */
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* The constructor accepts following options:
|
||||
* - timeout: (optional) Connection timeout, in seconds, default 1.0
|
||||
* - client: An EcsClient to make request from
|
||||
*
|
||||
* @param array $config Configuration options
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->timeout = isset($config['timeout']) ? $config['timeout'] : 1.0;
|
||||
$this->client = isset($config['client'])
|
||||
? $config['client'] // internal use only
|
||||
? $config['client']
|
||||
: \Aws\default_http_handler();
|
||||
}
|
||||
|
||||
@@ -36,39 +39,16 @@ class EcsCredentialProvider
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
return $this->request($this->getEcsUri());
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch credential URI from ECS environment variable
|
||||
*
|
||||
* @return string Returns ECS URI
|
||||
* @throws CredentialsException If the credential URI path cannot be found
|
||||
*/
|
||||
private function getEcsUri()
|
||||
{
|
||||
$creds_uri = getenv(self::ENV_URI);
|
||||
|
||||
if (!$creds_uri) {
|
||||
throw new CredentialsException(
|
||||
"Unable to find an ECS environment variable value for "
|
||||
. self::ENV_URI
|
||||
);
|
||||
}
|
||||
return $creds_uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return PromiseInterface Returns a promise that is fulfilled with the
|
||||
* body of the response as a string.
|
||||
*/
|
||||
private function request($url)
|
||||
{
|
||||
$client = $this->client;
|
||||
$request = new Request('GET', new Uri(self::SERVER_URI . $url));
|
||||
return $client($request)->then(function (ResponseInterface $response) {
|
||||
$request = new Request('GET', self::getEcsUri());
|
||||
return $client(
|
||||
$request,
|
||||
[
|
||||
'timeout' => $this->timeout,
|
||||
'proxy' => '',
|
||||
]
|
||||
)->then(function (ResponseInterface $response) {
|
||||
$result = $this->decodeResult((string) $response->getBody());
|
||||
return new Credentials(
|
||||
$result['AccessKeyId'],
|
||||
@@ -77,6 +57,7 @@ class EcsCredentialProvider
|
||||
strtotime($result['Expiration'])
|
||||
);
|
||||
})->otherwise(function ($reason) {
|
||||
$reason = is_array($reason) ? $reason['exception'] : $reason;
|
||||
$msg = $reason->getMessage();
|
||||
throw new CredentialsException(
|
||||
"Error retrieving credential from ECS ($msg)"
|
||||
@@ -84,6 +65,17 @@ class EcsCredentialProvider
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch credential URI from ECS environment variable
|
||||
*
|
||||
* @return string Returns ECS URI
|
||||
*/
|
||||
private function getEcsUri()
|
||||
{
|
||||
$creds_uri = getenv(self::ENV_URI);
|
||||
return self::SERVER_URI . $creds_uri;
|
||||
}
|
||||
|
||||
private function decodeResult($response)
|
||||
{
|
||||
$result = json_decode($response, true);
|
||||
|
||||
@@ -15,6 +15,8 @@ class InstanceProfileProvider
|
||||
const SERVER_URI = 'http://169.254.169.254/latest/';
|
||||
const CRED_PATH = 'meta-data/iam/security-credentials/';
|
||||
|
||||
const ENV_DISABLE = 'AWS_EC2_METADATA_DISABLED';
|
||||
|
||||
/** @var string */
|
||||
private $profile;
|
||||
|
||||
@@ -67,6 +69,13 @@ class InstanceProfileProvider
|
||||
*/
|
||||
private function request($url)
|
||||
{
|
||||
$disabled = getenv(self::ENV_DISABLE) ?: false;
|
||||
if (strcasecmp($disabled, 'true') === 0) {
|
||||
throw new CredentialsException(
|
||||
$this->createErrorMessage('EC2 metadata server access disabled')
|
||||
);
|
||||
}
|
||||
|
||||
$fn = $this->client;
|
||||
$request = new Request('GET', self::SERVER_URI . $url);
|
||||
|
||||
@@ -77,7 +86,7 @@ class InstanceProfileProvider
|
||||
$reason = $reason['exception'];
|
||||
$msg = $reason->getMessage();
|
||||
throw new CredentialsException(
|
||||
$this->createErrorMessage($msg, 0, $reason)
|
||||
$this->createErrorMessage($msg)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user