1
|
|
|
<?php |
2
|
|
|
namespace Elastica\Transport; |
3
|
|
|
|
4
|
|
|
use Aws\Credentials\CredentialProvider; |
5
|
|
|
use Aws\Credentials\Credentials; |
6
|
|
|
use Aws\Signature\SignatureV4; |
7
|
|
|
use Elastica\Connection; |
8
|
|
|
use Elastica\Request; |
9
|
|
|
use GuzzleHttp; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
use GuzzleHttp\HandlerStack; |
12
|
|
|
use GuzzleHttp\Middleware; |
13
|
|
|
use Psr\Http\Message\RequestInterface; |
14
|
|
|
|
15
|
|
|
class AwsAuthV4 extends Guzzle |
16
|
|
|
{ |
17
|
|
|
protected function _getGuzzleClient($baseUrl, $persistent = true, Request $request) |
18
|
|
|
{ |
19
|
|
|
if (!$persistent || !self::$_guzzleClientConnection) { |
20
|
|
|
$stack = HandlerStack::create(GuzzleHttp\choose_handler()); |
21
|
|
|
$stack->push($this->getSigningMiddleware(), 'sign'); |
22
|
|
|
|
23
|
|
|
self::$_guzzleClientConnection = new Client([ |
24
|
|
|
'base_uri' => $baseUrl, |
25
|
|
|
'handler' => $stack, |
26
|
|
|
'headers' => [ |
27
|
|
|
'Content-Type' => $request->getContentType(), |
28
|
|
|
], |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return self::$_guzzleClientConnection; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function _getBaseUrl(Connection $connection) |
36
|
|
|
{ |
37
|
|
|
$this->initializePortAndScheme(); |
38
|
|
|
|
39
|
|
|
return parent::_getBaseUrl($connection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function getSigningMiddleware() |
43
|
|
|
{ |
44
|
|
|
$region = $this->getConnection()->hasParam('aws_region') |
45
|
|
|
? $this->getConnection()->getParam('aws_region') |
46
|
|
|
: getenv('AWS_REGION'); |
47
|
|
|
$signer = new SignatureV4('es', $region); |
48
|
|
|
$credProvider = $this->getCredentialProvider(); |
49
|
|
|
|
50
|
|
|
return Middleware::mapRequest(function (RequestInterface $req) use ( |
51
|
|
|
$signer, |
52
|
|
|
$credProvider |
53
|
|
|
) { |
54
|
|
|
return $signer->signRequest($req, $credProvider()->wait()); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getCredentialProvider() |
59
|
|
|
{ |
60
|
|
|
$connection = $this->getConnection(); |
61
|
|
|
if ($connection->hasParam('aws_secret_access_key')) { |
62
|
|
|
return CredentialProvider::fromCredentials(new Credentials( |
63
|
|
|
$connection->getParam('aws_access_key_id'), |
64
|
|
|
$connection->getParam('aws_secret_access_key'), |
65
|
|
|
$connection->hasParam('aws_session_token') |
66
|
|
|
? $connection->getParam('aws_session_token') |
67
|
|
|
: null |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return CredentialProvider::defaultProvider(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function initializePortAndScheme() |
75
|
|
|
{ |
76
|
|
|
$connection = $this->getConnection(); |
77
|
|
|
if (true === $this->isSslRequired($connection)) { |
78
|
|
|
$this->_scheme = 'https'; |
79
|
|
|
$connection->setPort(443); |
80
|
|
|
} else { |
81
|
|
|
$this->_scheme = 'http'; |
82
|
|
|
$connection->setPort(80); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Connection $conn |
88
|
|
|
* @param bool $default |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
private function isSslRequired(Connection $conn, $default = false) |
93
|
|
|
{ |
94
|
|
|
return $conn->hasParam('ssl') |
95
|
|
|
? (bool) $conn->getParam('ssl') |
96
|
|
|
: $default; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|