Skip to content

Commit

Permalink
Fixed bug in ClientBuilder where basic authentication is overridden b…
Browse files Browse the repository at this point in the history
…y connection params (#160)

* feat: test basic authentication is not overridden

Signed-off-by: Salih Candir <salih@live.at>
  • Loading branch information
CSalih authored Jul 12, 2023
1 parent 8b6cdbc commit 5c5453c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Updated backport helper version and add fix to backport workflow when tag is applied before closing PR ([#131](https://github.com/opensearch-project/opensearch-php/pull/131))
- Fixed host urls with trailing slash in the url ([#130](https://github.com/opensearch-project/opensearch-php/pull/140))
- Fixed point-in-time APIs ([#142](https://github.com/opensearch-project/opensearch-php/pull/142))
- Fixed bug in ClientBuilder where basic authentication is overridden by connection params ([#160](https://github.com/opensearch-project/opensearch-php/pull/160))

### Security

Expand Down
25 changes: 17 additions & 8 deletions src/OpenSearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class ClientBuilder
*/
private $includePortInHostHeader = false;

/**
* @var string|null
*/
private $basicAuthentication = null;

/**
* Create an instance of ClientBuilder
*/
Expand Down Expand Up @@ -427,14 +432,7 @@ public function setHosts(array $hosts): ClientBuilder
*/
public function setBasicAuthentication(string $username, string $password): ClientBuilder
{
if (isset($this->connectionParams['client']['curl']) === false) {
$this->connectionParams['client']['curl'] = [];
}

$this->connectionParams['client']['curl'] += [
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username.':'.$password
];
$this->basicAuthentication = $username.':'.$password;

return $this;
}
Expand Down Expand Up @@ -634,6 +632,17 @@ public function build(): Client

$this->connectionParams['client']['port_in_header'] = $this->includePortInHostHeader;

if (! is_null($this->basicAuthentication)) {
if (isset($this->connectionParams['client']['curl']) === false) {
$this->connectionParams['client']['curl'] = [];
}

$this->connectionParams['client']['curl'] += [
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->basicAuthentication
];
}

if (is_null($this->connectionFactory)) {
// Make sure we are setting Content-Type and Accept (unless the user has explicitly
// overridden it
Expand Down
11 changes: 11 additions & 0 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ public function testFromConfigQuiteFalseWithUnknownKey()
);
}

public function testFromConfigUsingBasicAuthentication()
{
$config = [
'basicAuthentication' => ["foo", "bar"],
'connectionParams' => [],
];
$client = ClientBuilder::fromConfig($config);

$this->assertEquals('foo:bar', $client->transport->getConnection()->getUserPass());
}

public function testCompatibilityHeaderDefaultIsOff()
{
$client = ClientBuilder::create()
Expand Down

0 comments on commit 5c5453c

Please sign in to comment.