Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/#6495-redis-server-uri-parsing' into develop
Browse files Browse the repository at this point in the history
Forward port #6495
  • Loading branch information
Ocramius committed Aug 6, 2014
2 parents 078dde2 + 93d18d9 commit 84dcc9f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
54 changes: 50 additions & 4 deletions library/Zend/Cache/Storage/Adapter/RedisResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class RedisResourceManager
{

/**
* Registered resources
*
Expand Down Expand Up @@ -162,13 +161,15 @@ public function getServer($id)
* array('host' => <host>[, 'port' => <port>[, 'timeout' => <timeout>]])
*
* @param string|array $server
*
* @throws Exception\InvalidArgumentException
*/
protected function normalizeServer(&$server)
{
$host = null;
$port = null;
$timeout = 0;

// convert a single server into an array
if ($server instanceof Traversable) {
$server = ArrayUtils::iteratorToArray($server);
Expand All @@ -192,7 +193,7 @@ protected function normalizeServer(&$server)
} else {
// parse server from URI host{:?port}
$server = trim($server);
if (!strpos($server, '/') === 0) {
if (strpos($server, '/') !== 0) {
//non unix domain socket connection
$server = parse_url($server);
} else {
Expand All @@ -218,6 +219,37 @@ protected function normalizeServer(&$server)
);
}

/**
* Extract password to be used on connection
*
* @param mixed $resource
* @param mixed $serverUri
*
* @return string|null
*/
protected function extractPassword($resource, $serverUri)
{
if (! empty($resource['password'])) {
return $resource['password'];
}

if (! is_string($serverUri)) {
return null;
}

// parse server from URI host{:?port}
$server = trim($serverUri);

if (strpos($server, '/') === 0) {
return null;
}

//non unix domain socket connection
$server = parse_url($server);

return isset($server['pass']) ? $server['pass'] : null;
}

/**
* Connects to redis server
*
Expand Down Expand Up @@ -288,6 +320,11 @@ public function setResource($id, $resource)
// normalize and validate params
$this->normalizePersistentId($resource['persistent_id']);
$this->normalizeLibOptions($resource['lib_options']);

// #6495 note: order is important here, as `normalizeServer` applies destructive
// transformations on $resource['server']
$resource['password'] = $this->extractPassword($resource, $resource['server']);

$this->normalizeServer($resource['server']);
} else {
//there are two ways of determining if redis is already initialized
Expand Down Expand Up @@ -545,12 +582,21 @@ public function setServer($id, $server)

$this->normalizeServer($server);

$resource = & $this->resources[$id];
$resource = & $this->resources[$id];
$resource['password'] = $this->extractPassword($resource, $server);

if ($resource['resource'] instanceof RedisResource) {
$this->setResource($id, array('server' => $server));
$resourceParams = array('server' => $server);

if (! empty($resource['password'])) {
$resourceParams['password'] = $resource['password'];
}

$this->setResource($id, $resourceParams);
} else {
$resource['server'] = $server;
}

return $this;
}

Expand Down
64 changes: 64 additions & 0 deletions tests/ZendTest/Cache/Storage/Adapter/RedisResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,70 @@ public function setUp()
$this->resourceManager = new RedisResourceManager();
}

/**
* @group 6495
*/
public function testSetServerWithPasswordInUri()
{
$dummyResId = '1234567890';
$server = 'redis://dummyuser:dummypass@testhost:1234';

$this->resourceManager->setServer($dummyResId, $server);

$server = $this->resourceManager->getServer($dummyResId);

$this->assertEquals('testhost', $server['host']);
$this->assertEquals(1234, $server['port']);
$this->assertEquals('dummypass', $this->resourceManager->getPassword($dummyResId));
}

/**
* @group 6495
*/
public function testSetServerWithPasswordInParameters()
{
$server = 'redis://dummyuser:dummypass@testhost:1234';
$dummyResId2 = '12345678901';
$resource = array(
'persistent_id' => 1234,
'server' => $server,
'password' => 'abcd1234'
);

$this->resourceManager->setResource($dummyResId2, $resource);

$server = $this->resourceManager->getServer($dummyResId2);

$this->assertEquals('testhost', $server['host']);
$this->assertEquals(1234, $server['port']);
$this->assertEquals('abcd1234', $this->resourceManager->getPassword($dummyResId2));
}

/**
* @group 6495
*/
public function testSetServerWithPasswordInUriShouldNotOverridePreviousResource()
{
$server = 'redis://dummyuser:dummypass@testhost:1234';
$server2 = 'redis://dummyuser:dummypass@testhost2:1234';
$dummyResId2 = '12345678901';
$resource = array(
'persistent_id' => 1234,
'server' => $server,
'password' => 'abcd1234'
);

$this->resourceManager->setResource($dummyResId2, $resource);
$this->resourceManager->setServer($dummyResId2, $server2);

$server = $this->resourceManager->getServer($dummyResId2);

$this->assertEquals('testhost2', $server['host']);
$this->assertEquals(1234, $server['port']);
// Password should not be overridden
$this->assertEquals('abcd1234', $this->resourceManager->getPassword($dummyResId2));
}

/**
* Test with 'persistent_id'
*/
Expand Down

0 comments on commit 84dcc9f

Please # to comment.