Skip to content

Commit cb8559d

Browse files
committed
support for basic auth and api key in combi with cloud id
1 parent edbe7bd commit cb8559d

File tree

5 files changed

+135
-229
lines changed

5 files changed

+135
-229
lines changed

docs/configuration.asciidoc

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,34 +94,22 @@ If you want to connect to Elastic Cloud, you can use your Cloud ID and use basic
9494

9595
[source,php]
9696
----
97-
use Elasticsearch\Helper\ElasticCloudHelper;
98-
99-
$cloud = new ElasticCloudHelper('elastic-cloud-id'); <1>
100-
$cloud->setBasicAuthentication('username', 'secure-password'); <2>
101-
10297
$client = ClientBuilder::create()
103-
->setHosts($cloud->getHost()) <3>
104-
->setElasticCloudConnectionParams() <4>
105-
->build();
98+
->setElasticCloudId('<elastic-cloud-id>', '<username>', '<secure-password>') <1>, <2>
99+
->build();
106100
----
107101
<1> Your Cloud ID provided by the Elastic Cloud platform
108102
<2> Your basic authentication credentials
109-
<3> Let the `ElasticCloudHelper` unwrap the Cloud ID to a hostname
110-
<3> Activate the best practises for communicating with Elastic Cloud
111103

112104
[source,php]
113105
----
114-
use Elasticsearch\Helper\ElasticCloudHelper;
115-
116-
$cloud = new ElasticCloudHelper('elastic-cloud-id');
117-
118106
$client = ClientBuilder::create()
119-
->setHosts($cloud->getHost())
120-
->setElasticCloudConnectionParams()
121-
->setApiKeyPairAuthentication('id', 'api_key') <1>
122-
->build();
107+
->setElasticCloudId('<elastic-cloud-id>') <1>
108+
->setApiKeyPairAuthentication('<id>', '<api_key>') <2>
109+
->build();
123110
----
124-
<1> Your ApiKey pair as described https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/security.html[here]
111+
<1> Your Cloud ID provided by the Elastic Cloud platform
112+
<2> Your ApiKey pair as described https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/security.html[here]
125113

126114
=== Authorization and Encryption
127115

src/Elasticsearch/ClientBuilder.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Elasticsearch\Serializers\SerializerInterface;
1818
use Elasticsearch\ConnectionPool\Selectors;
1919
use Elasticsearch\Serializers\SmartSerializer;
20+
use Elasticsearch\Helper\ElasticCloudIdParser;
2021
use GuzzleHttp\Ring\Client\CurlHandler;
2122
use GuzzleHttp\Ring\Client\CurlMultiHandler;
2223
use GuzzleHttp\Ring\Client\Middleware;
@@ -372,23 +373,43 @@ public function setConnectionParams(array $params): ClientBuilder
372373
}
373374

374375
/**
375-
* Set Elastic Cloud's best Practices Connection Parameters
376+
* Set Elastic Cloud ID to connect to Elastic Cloud
376377
*
377-
* <i>Add custom/additional Params as parameter</i>
378+
* <b>No authentication is provided</b>
378379
*
379-
* @param array $additional
380+
* - set Hostname
381+
* - set best practices for the connection
382+
*
383+
* @param string $cloudId
384+
* @param string $username, optional if using Basic Authentication
385+
* @param string $password, optional if using Basic Authentication
386+
*
387+
* @throws Elasticsearch\Common\Exceptions\ElasticCloudIdParseException
380388
*/
381-
public function setElasticCloudConnectionParams(array $additional = [])
389+
public function setElasticCloudId(string $cloudId, ?string $username = null, ?string $password = null)
382390
{
383-
$this->setConnectionParams(
391+
$cloud = new ElasticCloudIdParser($cloudId);
392+
$hosts = [
384393
[
385-
'client' => [
386-
'curl' => [
387-
CURLOPT_ENCODING => 1,
388-
],
389-
]
390-
] + $additional
391-
);
394+
'host' => $cloud->getClusterDns(),
395+
'port' => '',
396+
'scheme' => 'https',
397+
'user' => $username,
398+
'pass' => $password,
399+
]
400+
];
401+
402+
// Register the Hosts array
403+
$this->setHosts($hosts);
404+
405+
// Merge best practices for the connection
406+
$this->setConnectionParams([
407+
'client' => [
408+
'curl' => [
409+
CURLOPT_ENCODING => 1,
410+
],
411+
]
412+
]);
392413

393414
return $this;
394415
}

src/Elasticsearch/Helper/ElasticCloudHelper.php renamed to src/Elasticsearch/Helper/ElasticCloudIdParser.php

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException;
1010

1111
/**
12-
* Class ElasticCloudHelper
12+
* Class ElasticCloudIdParser
1313
*
1414
* @category Elasticsearch
1515
* @package Elasticsearch\Helper
1616
* @author Philip Krauss <[email protected]>
1717
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
1818
* @link http://elastic.co
1919
*/
20-
class ElasticCloudHelper
20+
class ElasticCloudIdParser
2121
{
2222

2323
/**
@@ -35,11 +35,6 @@ class ElasticCloudHelper
3535
*/
3636
private $clusterDns;
3737

38-
/**
39-
* @var array
40-
*/
41-
private $basicAuth = [];
42-
4338
/**
4439
* @param string $cloudId
4540
*/
@@ -79,40 +74,6 @@ public function getClusterDns(): string
7974
return $this->clusterDns;
8075
}
8176

82-
/**
83-
* Get Elastic Cloud Host Array
84-
*
85-
* @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html#_extended_host_configuration
86-
*
87-
* @return array
88-
*/
89-
public function getHost(): array
90-
{
91-
return [
92-
[
93-
'host' => $this->clusterDns,
94-
'port' => '',
95-
'scheme' => 'https',
96-
] + $this->basicAuth
97-
];
98-
}
99-
100-
/**
101-
* Set the Basic Authentication Credentials
102-
*
103-
* @param string $username
104-
* @param string $password
105-
*
106-
* @return void
107-
*/
108-
public function setBasicAuthentication(string $username, string $password): void
109-
{
110-
$this->basicAuth = [
111-
'user' => $username,
112-
'pass' => $password,
113-
];
114-
}
115-
11677
/**
11778
* Parse the Elastic Cloud Params from the CloudId
11879
*

tests/Elasticsearch/Tests/Helper/ElasticCloudHelperTest.php

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)