7
7
use Elasticsearch \Common \Exceptions \InvalidArgumentException ;
8
8
use Elasticsearch \Common \Exceptions \RuntimeException ;
9
9
use Elasticsearch \Common \Exceptions \ElasticCloudIdParseException ;
10
+ use Elasticsearch \Common \Exceptions \AuthenticationConfigException ;
10
11
use Elasticsearch \ConnectionPool \AbstractConnectionPool ;
11
12
use Elasticsearch \ConnectionPool \Selectors \RoundRobinSelector ;
12
13
use Elasticsearch \ConnectionPool \Selectors \SelectorInterface ;
@@ -334,23 +335,23 @@ public function setHosts(array $hosts): ClientBuilder
334
335
/**
335
336
* Set the APIKey Pair, consiting of the API Id and the ApiKey of the Response from /_security/api_key
336
337
*
337
- * <i>APIKey will have precedence over Basic Authentication</i>
338
- *
339
338
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
340
339
*
341
340
* @param string $id
342
341
* @param string $apiKey
342
+ *
343
+ * @throws Elasticsearch\Common\Exceptions\AuthenticationConfigException
343
344
*/
344
345
public function setApiKey (string $ id , string $ apiKey )
345
346
{
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
+
346
351
$ this ->connectionParams ['client ' ]['headers ' ]['Authorization ' ] = [
347
352
'ApiKey ' . base64_encode ($ id . ': ' . $ apiKey )
348
353
];
349
354
350
- // Remove Basic Auth Credentials if set
351
- unset($ this ->connectionParams ['client ' ]['curl ' ][CURLOPT_HTTPAUTH ]);
352
- unset($ this ->connectionParams ['client ' ]['curl ' ][CURLOPT_USERPWD ]);
353
-
354
355
return $ this ;
355
356
}
356
357
@@ -359,9 +360,15 @@ public function setApiKey(string $id, string $apiKey)
359
360
*
360
361
* @param string $username
361
362
* @param string $password
363
+ *
364
+ * @throws Elasticsearch\Common\Exceptions\AuthenticationConfigException
362
365
*/
363
366
public function setBasicAuthentication (string $ username , string $ password )
364
367
{
368
+ if (isset ($ this ->connectionParams ['client ' ]['headers ' ]['Authorization ' ]) === true ) {
369
+ throw new AuthenticationConfigException ("You can't use APIKey - and Basic Authenication together. " );
370
+ }
371
+
365
372
if (isset ($ this ->connectionParams ['client ' ]['curl ' ]) === false ) {
366
373
$ this ->connectionParams ['client ' ]['curl ' ] = [];
367
374
}
0 commit comments