Skip to content

Commit 71ab3e1

Browse files
committed
add basic auth method
1 parent 5162667 commit 71ab3e1

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ public function setHosts(array $hosts): ClientBuilder
339339
/**
340340
* Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
341341
*
342+
* <i>APIKey will have precedence over Basic Authentication</i>
343+
*
342344
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
343345
*
344346
* @param string $id
@@ -350,12 +352,29 @@ public function setApiKey(string $id, string $apiKey)
350352
'ApiKey ' . base64_encode($id . ':' . $apiKey)
351353
];
352354

355+
// Remove Basic Auth Credentials if set
356+
unset($this->connectionParams['client']['curl'][CURLOPT_HTTPAUTH]);
357+
unset($this->connectionParams['client']['curl'][CURLOPT_USERPWD]);
358+
353359
return $this;
354360
}
355361

356-
public function setConnectionParams(array $params): ClientBuilder
362+
/**
363+
* Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
364+
*
365+
* @param string $username
366+
* @param string $password
367+
*/
368+
public function setBasicAuthentication(string $username, string $password)
357369
{
358-
$this->connectionParams = $params;
370+
if(isset($this->connectionParams['client']['curl']) === false) {
371+
$this->connectionParams['client']['curl'] = [];
372+
}
373+
374+
$this->connectionParams['client']['curl'] += [
375+
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
376+
CURLOPT_USERPWD => $username.':'.$password
377+
];
359378

360379
return $this;
361380
}
@@ -390,6 +409,13 @@ public function setElasticCloudId(string $cloudId)
390409
return $this;
391410
}
392411

412+
public function setConnectionParams(array $params): ClientBuilder
413+
{
414+
$this->connectionParams = $params;
415+
416+
return $this;
417+
}
418+
393419
public function setRetries(int $retries): ClientBuilder
394420
{
395421
$this->retries = $retries;

src/Elasticsearch/Connections/Connection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ public function __construct(
129129
$this->transportSchema = $hostDetails['scheme'];
130130
}
131131

132-
if (isset($hostDetails['user']) && isset($hostDetails['pass'])) {
132+
// Only Set the Basic if API Key is not set and setBasicAuthentication was not called prior
133+
if (isset($connectionParams['client']['headers']['Authorization']) === false
134+
&& isset($connectionParams['client']['curl'][CURLOPT_HTTPAUTH]) === false
135+
&& isset($hostDetails['user'])
136+
&& isset($hostDetails['pass'])){
133137
$connectionParams['client']['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
134138
$connectionParams['client']['curl'][CURLOPT_USERPWD] = $hostDetails['user'].':'.$hostDetails['pass'];
135139
}

0 commit comments

Comments
 (0)