Skip to content

Commit

Permalink
support for basic auth and api key in combi with cloud id
Browse files Browse the repository at this point in the history
  • Loading branch information
philkra committed Jul 29, 2019
1 parent edbe7bd commit cb8559d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 229 deletions.
26 changes: 7 additions & 19 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,22 @@ If you want to connect to Elastic Cloud, you can use your Cloud ID and use basic

[source,php]
----
use Elasticsearch\Helper\ElasticCloudHelper;
$cloud = new ElasticCloudHelper('elastic-cloud-id'); <1>
$cloud->setBasicAuthentication('username', 'secure-password'); <2>
$client = ClientBuilder::create()
->setHosts($cloud->getHost()) <3>
->setElasticCloudConnectionParams() <4>
->build();
->setElasticCloudId('<elastic-cloud-id>', '<username>', '<secure-password>') <1>, <2>
->build();
----
<1> Your Cloud ID provided by the Elastic Cloud platform
<2> Your basic authentication credentials
<3> Let the `ElasticCloudHelper` unwrap the Cloud ID to a hostname
<3> Activate the best practises for communicating with Elastic Cloud

[source,php]
----
use Elasticsearch\Helper\ElasticCloudHelper;
$cloud = new ElasticCloudHelper('elastic-cloud-id');
$client = ClientBuilder::create()
->setHosts($cloud->getHost())
->setElasticCloudConnectionParams()
->setApiKeyPairAuthentication('id', 'api_key') <1>
->build();
->setElasticCloudId('<elastic-cloud-id>') <1>
->setApiKeyPairAuthentication('<id>', '<api_key>') <2>
->build();
----
<1> Your ApiKey pair as described https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/security.html[here]
<1> Your Cloud ID provided by the Elastic Cloud platform
<2> Your ApiKey pair as described https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/security.html[here]

=== Authorization and Encryption

Expand Down
45 changes: 33 additions & 12 deletions src/Elasticsearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\ConnectionPool\Selectors;
use Elasticsearch\Serializers\SmartSerializer;
use Elasticsearch\Helper\ElasticCloudIdParser;
use GuzzleHttp\Ring\Client\CurlHandler;
use GuzzleHttp\Ring\Client\CurlMultiHandler;
use GuzzleHttp\Ring\Client\Middleware;
Expand Down Expand Up @@ -372,23 +373,43 @@ public function setConnectionParams(array $params): ClientBuilder
}

/**
* Set Elastic Cloud's best Practices Connection Parameters
* Set Elastic Cloud ID to connect to Elastic Cloud
*
* <i>Add custom/additional Params as parameter</i>
* <b>No authentication is provided</b>
*
* @param array $additional
* - set Hostname
* - set best practices for the connection
*
* @param string $cloudId
* @param string $username, optional if using Basic Authentication
* @param string $password, optional if using Basic Authentication
*
* @throws Elasticsearch\Common\Exceptions\ElasticCloudIdParseException
*/
public function setElasticCloudConnectionParams(array $additional = [])
public function setElasticCloudId(string $cloudId, ?string $username = null, ?string $password = null)
{
$this->setConnectionParams(
$cloud = new ElasticCloudIdParser($cloudId);
$hosts = [
[
'client' => [
'curl' => [
CURLOPT_ENCODING => 1,
],
]
] + $additional
);
'host' => $cloud->getClusterDns(),
'port' => '',
'scheme' => 'https',
'user' => $username,
'pass' => $password,
]
];

// Register the Hosts array
$this->setHosts($hosts);

// Merge best practices for the connection
$this->setConnectionParams([
'client' => [
'curl' => [
CURLOPT_ENCODING => 1,

This comment has been minimized.

Copy link
@ezimuel

ezimuel Jan 10, 2020

Contributor

I didn't find any information about CURLOPT_ENCODING = 1, what value is it, e.g. gzip?

This comment has been minimized.

Copy link
@philkra

philkra Jan 10, 2020

Author Contributor

That is clearly not correct, I made mistake on that one as the docs state: https://www.php.net/manual/en/function.curl-setopt.php

This comment has been minimized.

Copy link
@ezimuel

ezimuel Jan 10, 2020

Contributor

Ok, no problem. It was my fault since I did the review. I'll fix it.

],
]
]);

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException;

/**
* Class ElasticCloudHelper
* Class ElasticCloudIdParser
*
* @category Elasticsearch
* @package Elasticsearch\Helper
* @author Philip Krauss <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elastic.co
*/
class ElasticCloudHelper
class ElasticCloudIdParser
{

/**
Expand All @@ -35,11 +35,6 @@ class ElasticCloudHelper
*/
private $clusterDns;

/**
* @var array
*/
private $basicAuth = [];

/**
* @param string $cloudId
*/
Expand Down Expand Up @@ -79,40 +74,6 @@ public function getClusterDns(): string
return $this->clusterDns;
}

/**
* Get Elastic Cloud Host Array
*
* @link https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html#_extended_host_configuration
*
* @return array
*/
public function getHost(): array
{
return [
[
'host' => $this->clusterDns,
'port' => '',
'scheme' => 'https',
] + $this->basicAuth
];
}

/**
* Set the Basic Authentication Credentials
*
* @param string $username
* @param string $password
*
* @return void
*/
public function setBasicAuthentication(string $username, string $password): void
{
$this->basicAuth = [
'user' => $username,
'pass' => $password,
];
}

/**
* Parse the Elastic Cloud Params from the CloudId
*
Expand Down
157 changes: 0 additions & 157 deletions tests/Elasticsearch/Tests/Helper/ElasticCloudHelperTest.php

This file was deleted.

Loading

0 comments on commit cb8559d

Please sign in to comment.