22
33namespace Elasticquent ;
44
5+ use GuzzleHttp \Psr7 \Request ;
6+ use Aws \Signature \SignatureV4 ;
7+ use Aws \Credentials \Credentials ;
8+ use GuzzleHttp \Ring \Future \CompletedFutureArray ;
9+ use GuzzleHttp \Psr7 \Uri ;
10+ use Psr \Http \Message \ResponseInterface ;
11+
512trait ElasticquentClientTrait
613{
714
@@ -19,11 +26,72 @@ public function getElasticSearchClient()
1926
2027 // elasticsearch v2.0 using builder
2128 if (class_exists ('\Elasticsearch\ClientBuilder ' )) {
29+ $ awsConfig = $ this ->getElasticConfig ('aws ' );
30+ if ( ! empty ($ awsConfig ) && array_get ($ this ->getElasticConfig ('aws ' ), 'iam ' , false )) {
31+ if ($ handler = $ this ->getAwsESHandler ()) {
32+ array_set ($ config , 'handler ' , $ handler );
33+ }
34+ }
35+
2236 return \Elasticsearch \ClientBuilder::fromConfig ($ config );
2337 }
2438
2539 // elasticsearch v1
2640 return new \Elasticsearch \Client ($ config );
2741 }
2842
43+
44+ /**
45+ * @return bool|\Closure
46+ */
47+ private function getAwsESHandler ()
48+ {
49+ $ awsConfig = $ this ->getElasticConfig ('aws ' );
50+ if (empty ($ awsConfig )) {
51+ return false ;
52+ }
53+
54+ $ key = array_get ($ awsConfig , 'key ' );
55+ $ secret = array_get ($ awsConfig , 'secret ' );
56+ $ region = array_get ($ awsConfig , 'region ' , 'us-west-2 ' );
57+
58+ $ psr7Handler = \Aws \default_http_handler ();
59+ $ signer = new SignatureV4 ('es ' , $ region );
60+
61+ $ handler = function (array $ request ) use (
62+ $ psr7Handler ,
63+ $ signer ,
64+ $ key ,
65+ $ secret
66+ ) {
67+ // Amazon ES listens on standard ports (443 for HTTPS, 80 for HTTP).
68+ $ request ['headers ' ]['host ' ][0 ] = parse_url ($ request ['headers ' ]['host ' ][0 ], PHP_URL_HOST );
69+
70+ $ credentials = new Credentials ($ key , $ secret );
71+
72+ // Create a PSR-7 request from the array passed to the handler
73+ $ psr7Request = new Request ($ request ['http_method ' ],
74+ (new Uri ($ request ['uri ' ]))->withScheme ($ request ['scheme ' ])->withHost ($ request ['headers ' ]['host ' ][0 ]),
75+ $ request ['headers ' ], $ request ['body ' ]);
76+
77+ // Sign the PSR-7 request with credentials from the environment
78+ $ signedRequest = $ signer ->signRequest ($ psr7Request , $ credentials );
79+
80+ // Send the signed request to Amazon ES
81+ /** @var ResponseInterface $response */
82+ $ response = $ psr7Handler ($ signedRequest )->wait ();
83+
84+ // Convert the PSR-7 response to a RingPHP response
85+ return new CompletedFutureArray ([
86+ 'status ' => $ response ->getStatusCode (),
87+ 'headers ' => $ response ->getHeaders (),
88+ 'body ' => $ response ->getBody ()->detach (),
89+ 'transfer_stats ' => ['total_time ' => 0 ],
90+ 'effective_url ' => (string )$ psr7Request ->getUri (),
91+ ]);
92+ };
93+
94+ return $ handler ;
95+ }
96+
2997}
0 commit comments