From 95ce388228008313e170de331bbd7b38ec8d61b7 Mon Sep 17 00:00:00 2001 From: Florian Semm Date: Fri, 16 Feb 2018 10:20:31 +0100 Subject: [PATCH] checks if ssl param is set (#1457) * checks if ssl param is set * add test-cases * update changelog * remove dev-requirement --- CHANGELOG.md | 1 + lib/Elastica/Transport/AwsAuthV4.php | 14 +++++-- test/Elastica/Transport/AwsAuthV4Test.php | 51 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d8c39a777..0e8db0c600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the - Characters "<" and ">" will be removed when a query term is passed to [`Util::escapeTerm`](https://github.com/ruflin/Elastica/pull/1415/files). Since v5.1 the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_reserved_characters) states that these symbols cannot be escaped ever. - Remove [`each()`](http://www.php.net/each) usage to fix PHP 7.2 compatibility - Fix [#1435](https://github.com/ruflin/Elastica/issues/1435) forcing `doc_as_upsert` to be boolean, acording [Elastic doc-update documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_literal_doc_as_upsert_literal) +- Fix [#1456](https://github.com/ruflin/Elastica/issues/1456) set SSL as connection scheme if it is required ### Added diff --git a/lib/Elastica/Transport/AwsAuthV4.php b/lib/Elastica/Transport/AwsAuthV4.php index a3d960c6a3..5da180030a 100644 --- a/lib/Elastica/Transport/AwsAuthV4.php +++ b/lib/Elastica/Transport/AwsAuthV4.php @@ -74,7 +74,7 @@ private function getCredentialProvider() private function initializePortAndScheme() { $connection = $this->getConnection(); - if (true === $this->getConfig($connection, 'ssl')) { + if (true === $this->isSslRequired($connection)) { $this->_scheme = 'https'; $connection->setPort(443); } else { @@ -83,10 +83,16 @@ private function initializePortAndScheme() } } - private function getConfig(Connection $conn, $key, $default = null) + /** + * @param Connection $conn + * @param bool $default + * + * @return bool + */ + private function isSslRequired(Connection $conn, $default = false) { - return $conn->hasConfig($key) - ? $conn->getConfig($key) + return $conn->hasParam('ssl') + ? (bool) $conn->getParam('ssl') : $default; } } diff --git a/test/Elastica/Transport/AwsAuthV4Test.php b/test/Elastica/Transport/AwsAuthV4Test.php index e13189409f..c9a9899408 100644 --- a/test/Elastica/Transport/AwsAuthV4Test.php +++ b/test/Elastica/Transport/AwsAuthV4Test.php @@ -28,6 +28,7 @@ public function testSignsWithProvidedCredentials() ]; $client = $this->_getClient($config); + try { $client->request('_status', 'GET'); } catch (GuzzleException $e) { @@ -50,6 +51,56 @@ public function testSignsWithProvidedCredentials() } } + public function testUseHttpAsDefaultProtocol() + { + $config = [ + 'persistent' => false, + 'transport' => 'AwsAuthV4', + 'aws_access_key_id' => 'foo', + 'aws_secret_access_key' => 'bar', + 'aws_session_token' => 'baz', + 'aws_region' => 'us-east-1', + ]; + $client = $this->_getClient($config); + + try { + $client->request('_status', 'GET'); + + $this->assertEquals(80, $client->getLastRequest()->toArray()['port']); + } catch (GuzzleException $e) { + $guzzleException = $e->getGuzzleException(); + if ($guzzleException instanceof RequestException) { + $request = $guzzleException->getRequest(); + } + } + } + + public function testSetHttpsIfItIsRequired() + { + $config = [ + 'persistent' => false, + 'transport' => 'AwsAuthV4', + 'aws_access_key_id' => 'foo', + 'aws_secret_access_key' => 'bar', + 'aws_session_token' => 'baz', + 'aws_region' => 'us-east-1', + 'ssl' => true + ]; + $client = $this->_getClient($config); + + try { + $client->request('_status', 'GET'); + + } catch (GuzzleException $e) { + $guzzleException = $e->getGuzzleException(); + if ($guzzleException instanceof RequestException) { + $request = $guzzleException->getRequest(); + + $this->assertEquals('https', $request->getUri()->getScheme()); + } + } + } + public function testSignsWithEnvironmentalCredentials() { $config = [