Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Cache] Fix Couchbase password parsing #52758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Cache] Add support for URL encoded characters in Couchbase DSN
  • Loading branch information
alexandre-daubois committed Dec 29, 2023
commit 4e6f0520ea9bc2ed1d5e0d8622c64ada25251aed
8 changes: 4 additions & 4 deletions 8 .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ jobs:

- name: Configure Couchbase
run: |
curl -s -u 'username=Administrator&password=111111' -X POST http://localhost:8091/node/controller/setupServices -d 'services=kv%2Cn1ql%2Cindex%2Cfts'
curl -s -X POST http://localhost:8091/settings/web -d 'username=Administrator&password=111111&port=SAME'
curl -s -u Administrator:111111 -X POST http://localhost:8091/pools/default/buckets -d 'ramQuotaMB=100&bucketType=ephemeral&name=cache'
curl -s -u Administrator:111111 -X POST http://localhost:8091/pools/default -d 'memoryQuota=256'
curl -s -u 'username=Administrator&password=111111@' -X POST http://localhost:8091/node/controller/setupServices -d 'services=kv%2Cn1ql%2Cindex%2Cfts'
curl -s -X POST http://localhost:8091/settings/web -d 'username=Administrator&password=111111%40&port=SAME'
curl -s -u Administrator:111111@ -X POST http://localhost:8091/pools/default/buckets -d 'ramQuotaMB=100&bucketType=ephemeral&name=cache'
curl -s -u Administrator:111111@ -X POST http://localhost:8091/pools/default -d 'memoryQuota=256'

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion 2 phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<env name="ZOOKEEPER_HOST" value="localhost" />
<env name="COUCHBASE_HOST" value="localhost" />
<env name="COUCHBASE_USER" value="Administrator" />
<env name="COUCHBASE_PASS" value="111111" />
<env name="COUCHBASE_PASS" value="111111%40" />
</php>

<testsuites>
Expand Down
22 changes: 10 additions & 12 deletions 22 src/Symfony/Component/Cache/Adapter/CouchbaseCollectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn

set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));

$dsnPattern = '/^(?<protocol>couchbase(?:s)?)\:\/\/(?:(?<username>[^\:]+)\:(?<password>[^\@]{6,})@)?'
.'(?<host>[^\:]+(?:\:\d+)?)(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))'
.'(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?(?:\?(?<options>.*))?$/i';

$pathPattern = '/^(?:\/(?<bucketName>[^\/\?]+))(?:(?:\/(?<scopeName>[^\/]+))(?:\/(?<collectionName>[^\/\?]+)))?(?:\/)?$/';
$newServers = [];
$protocol = 'couchbase';
try {
Expand All @@ -74,31 +71,32 @@ public static function createConnection(#[\SensitiveParameter] array|string $dsn
throw new InvalidArgumentException('Invalid Couchbase DSN: it does not start with "couchbase:".');
}

preg_match($dsnPattern, $server, $matches);
$params = parse_url($server);

$username = $matches['username'] ?: $username;
$password = $matches['password'] ?: $password;
$protocol = $matches['protocol'] ?: $protocol;
$username = isset($params['user']) ? rawurldecode($params['user']) : $username;
$password = isset($params['pass']) ? rawurldecode($params['pass']) : $password;
$protocol = $params['scheme'] ?? $protocol;

if (isset($matches['options'])) {
$optionsInDsn = self::getOptions($matches['options']);
if (isset($params['query'])) {
$optionsInDsn = self::getOptions($params['query']);

foreach ($optionsInDsn as $parameter => $value) {
$options[$parameter] = $value;
}
}

$newServers[] = $matches['host'];
$newServers[] = $params['host'];
}

$option = isset($matches['options']) ? '?'.$matches['options'] : '';
$option = isset($params['query']) ? '?'.$params['query'] : '';
$connectionString = $protocol.'://'.implode(',', $newServers).$option;

$clusterOptions = new ClusterOptions();
$clusterOptions->credentials($username, $password);

$client = new Cluster($connectionString, $clusterOptions);

preg_match($pathPattern, $params['path'] ?? '', $matches);
$bucket = $client->bucket($matches['bucketName']);
$collection = $bucket->defaultCollection();
if (!empty($matches['scopeName'])) {
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add option `sentinel_master` as an alias for `redis_sentinel`
* Deprecate `CouchbaseBucketAdapter`, use `CouchbaseCollectionAdapter`
* Add support for URL encoded characters in Couchbase DSN

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,24 @@ public function createCachePool($defaultLifetime = 0): CacheItemPoolInterface

return new CouchbaseCollectionAdapter($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}

/**
* Couchbase consider expiration time greater than 30 days as an absolute timestamp.
* This test case overrides parent to avoid this behavior for the "k2" item.
*/
public function testExpiration()
{
$cache = $this->createCachePool();
$cache->save($cache->getItem('k1')->set('v1')->expiresAfter(2));
$cache->save($cache->getItem('k2')->set('v2')->expiresAfter(86400));

sleep(3);
$item = $cache->getItem('k1');
$this->assertFalse($item->isHit());
$this->assertNull($item->get(), "Item's value must be null when isHit() is false.");

$item = $cache->getItem('k2');
$this->assertTrue($item->isHit());
$this->assertSame('v2', $item->get());
}
}
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Cache/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<env name="MEMCACHED_HOST" value="localhost" />
<env name="COUCHBASE_HOST" value="localhost" />
<env name="COUCHBASE_USER" value="Administrator" />
<env name="COUCHBASE_PASS" value="111111" />
<env name="COUCHBASE_PASS" value="111111%40" />
</php>

<testsuites>
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.