Skip to content

Commit d140440

Browse files
committed
ensure auth config conflicts throw an exception
1 parent 7fda608 commit d140440

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
88
use Elasticsearch\Common\Exceptions\RuntimeException;
99
use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException;
10+
use Elasticsearch\Common\Exceptions\AuthenticationConfigException;
1011
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
1112
use Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector;
1213
use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
@@ -334,23 +335,23 @@ public function setHosts(array $hosts): ClientBuilder
334335
/**
335336
* Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
336337
*
337-
* <i>APIKey will have precedence over Basic Authentication</i>
338-
*
339338
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
340339
*
341340
* @param string $id
342341
* @param string $apiKey
342+
*
343+
* @throws Elasticsearch\Common\Exceptions\AuthenticationConfigException
343344
*/
344345
public function setApiKey(string $id, string $apiKey)
345346
{
347+
if (isset($this->connectionParams['client']['curl'][CURLOPT_HTTPAUTH]) === true) {
348+
throw new AuthenticationConfigException("You can't use APIKey - and Basic Authenication together.");
349+
}
350+
346351
$this->connectionParams['client']['headers']['Authorization'] = [
347352
'ApiKey ' . base64_encode($id . ':' . $apiKey)
348353
];
349354

350-
// Remove Basic Auth Credentials if set
351-
unset($this->connectionParams['client']['curl'][CURLOPT_HTTPAUTH]);
352-
unset($this->connectionParams['client']['curl'][CURLOPT_USERPWD]);
353-
354355
return $this;
355356
}
356357

@@ -359,9 +360,15 @@ public function setApiKey(string $id, string $apiKey)
359360
*
360361
* @param string $username
361362
* @param string $password
363+
*
364+
* @throws Elasticsearch\Common\Exceptions\AuthenticationConfigException
362365
*/
363366
public function setBasicAuthentication(string $username, string $password)
364367
{
368+
if (isset($this->connectionParams['client']['headers']['Authorization']) === true) {
369+
throw new AuthenticationConfigException("You can't use APIKey - and Basic Authenication together.");
370+
}
371+
365372
if (isset($this->connectionParams['client']['curl']) === false) {
366373
$this->connectionParams['client']['curl'] = [];
367374
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
// Licensed to Elasticsearch B.V under one or more agreements.
4+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
5+
// See the LICENSE file in the project root for more information
6+
7+
namespace Elasticsearch\Common\Exceptions;
8+
9+
/**
10+
* AuthenticationConfigException
11+
*
12+
* @category Elasticsearch
13+
* @package Elasticsearch\Common\Exceptions
14+
* @author Philip Krauss <[email protected]>
15+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
16+
* @link http://elastic.co
17+
*/
18+
class AuthenticationConfigException extends \RuntimeException implements ElasticsearchException
19+
{
20+
}

0 commit comments

Comments
 (0)