diff --git a/CHANGELOG.md b/CHANGELOG.md index 2585f31e3..0794efe59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Yii Framework 2 redis extension Change Log - Bug #182: Better handle `cache/flush-all` command when cache component is using shared database (rob006) - Enh #195: Use `Instance::ensure()` to initialize `Session::$redis` (rob006) - Enh #199: Increase frequency of lock tries when `$timeout` is used in `Mutex::acquire()` (rob006) - +- Enh #174: Add ability to set up SSL connection (kulavvy) 2.0.11 November 05, 2019 ------------------------ diff --git a/README.md b/README.md index cbc5b040e..b88db156d 100644 --- a/README.md +++ b/README.md @@ -62,3 +62,19 @@ return [ ] ]; ``` + +**SSL configuration** example: +```php +return [ + //.... + 'components' => [ + 'redis' => [ + 'class' => 'yii\redis\Connection', + 'hostname' => 'localhost', + 'port' => 6380, + 'database' => 0, + 'useSSL' => true, + ], + ], +]; +``` diff --git a/composer.json b/composer.json index 87827d014..c4ebce564 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ } ], "require": { - "yiisoft/yii2": "~2.0.16" + "yiisoft/yii2": "~2.0.16", + "ext-openssl": "*" }, "require-dev": { "phpunit/phpunit": "<7", diff --git a/src/Connection.php b/src/Connection.php index e557dc66f..20889d4fc 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -281,6 +281,11 @@ class Connection extends Component * @var float timeout to use for redis socket when reading and writing data. If not set the php default value will be used. */ public $dataTimeout; + /** + * @var boolean Send sockets over SSL protocol. Default state is false. + * @since 2.0.12 + */ + public $useSSL = false; /** * @var integer Bitmask field which may be set to any combination of connection flags passed to [stream_socket_client()](https://www.php.net/manual/en/function.stream-socket-client.php). * Currently the select of connection flags is limited to `STREAM_CLIENT_CONNECT` (default), `STREAM_CLIENT_ASYNC_CONNECT` and `STREAM_CLIENT_PERSISTENT`. @@ -596,6 +601,9 @@ public function open() if ($this->dataTimeout !== null) { stream_set_timeout($socket, $timeout = (int) $this->dataTimeout, (int) (($this->dataTimeout - $timeout) * 1000000)); } + if ($this->useSSL) { + stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT); + } if ($this->password !== null) { $this->executeCommand('AUTH', [$this->password]); }