Files
predis/tests/Predis/Connection/PhpiredisSocketConnectionTest.php
T
Daniele Alessandri ebb72377bb Implement TLS/SSL-encrypted connections.
This is handy for accessing remote Redis instances over a secure SSL connection
which is currently a popular option or even requirement with many cloud hosting
environments.

In order to configure the client to use an SSL-encrypted connection the scheme
in the connection parameters must be either "tsl" or "rediss" and a set of SSL
options (see http://php.net/manual/en/context.ssl.php) must be provided via the
"ssl" parameter as a named array.

The following example (which does not necessarily represent an example of good
practices!) illustrates how to set the "ssl" parameter using a named array and
the equivalent URI string:

  // Parameters as named array
  $parameters = [
    'scheme' => 'tls',
    'host'   => '127.0.0.1',
    'ssl'    => [
        'cafile'            => '/home/adaniele/redis.pem',
        'verify_peer_name'  => false,
    ],
  ];

  // Parameters as URI string
  $parameters = 'tls://127.0.0.1?ssl[cafile]=redis.pem&ssl[verify_peer_name]=1';

Support for SSL is currently limited to the Predis\Connection\StreamConnection
backend but we intend to investigate if it is possible to extend this feature
to Predis\Connection\PhpiredisStreamConnection in the future.

Be aware that using encrypted connections may lead to a performance degradation
especially in the connect() operation due to the overhead of the TLS handshake.
Unfortunately there is no real way to reuse SSL sessions from userland, aside
from enabling persistent connections, but this will work only on PHP >= 7.0.0
because previous versions of PHP do not provide enough info about a stream from
get_stream_meta_data().

NOTE: Redis does not have built-in support for SSL-encrypted connections, but if
you want to expose it to public networks you may want to rely on "stunnel".
2015-07-27 19:16:20 +02:00

77 lines
2.3 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Connection;
/**
* @group ext-phpiredis
* @requires extension phpiredis
*/
class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
{
const CONNECTION_CLASS = 'Predis\Connection\PhpiredisSocketConnection';
/**
* @group disconnected
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid scheme: 'tls'.
*/
public function testSupportsSchemeTls()
{
$connection = $this->createConnectionWithParams(array('scheme' => 'tls'));
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
}
/**
* @group disconnected
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid scheme: 'rediss'.
*/
public function testSupportsSchemeRediss()
{
$connection = $this->createConnectionWithParams(array('scheme' => 'rediss'));
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
}
// ******************************************************************** //
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //
/**
* @group connected
* @expectedException \Predis\Connection\ConnectionException
* @expectedExceptionMessage Cannot resolve the address of 'bogus.tld'.
*/
public function testThrowsExceptionOnUnresolvableHostname()
{
$connection = $this->createConnectionWithParams(array('host' => 'bogus.tld'));
$connection->connect();
}
/**
* @medium
* @group connected
* @expectedException \Predis\Protocol\ProtocolException
*/
public function testThrowsExceptionOnProtocolDesynchronizationErrors()
{
$connection = $this->createConnection();
$socket = $connection->getResource();
$connection->writeRequest($this->getCurrentProfile()->createCommand('ping'));
socket_read($socket, 1);
$connection->read();
}
}