Skip to content

SSL/TLS with certification file #1600

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

Closed
bluemmb opened this issue Jul 11, 2019 · 18 comments
Closed

SSL/TLS with certification file #1600

bluemmb opened this issue Jul 11, 2019 · 18 comments
Assignees
Milestone

Comments

@bluemmb
Copy link

bluemmb commented Jul 11, 2019

Expected behaviour

Connect to encrypted connection with certification file

Actual behaviour

--

I'm seeing this behaviour on

  • OS: *
  • Redis: *
  • PHP: *
  • phpredis: *

Steps to reproduce, backtrace or example script

--

I can see that connecting to encrypted connections is discussed and supported here #1314 and #1561, but still i don't know how to set the certification file path to this plugin ? am I missing something ?

@yatsukhnenko
Copy link
Member

@bluemmb unfortunately there is no way to do this right now.

@bluemmb
Copy link
Author

bluemmb commented Jul 12, 2019

@yatsukhnenko Thanks for the answer and the great work you have done on this repository.

Is there a default path for the file ? or can I read the file and pass it as a parameter or something to the plugin ? ( because there should be a key for the encryption anyway )

I have written an app and put it on the ibm cloud for my client. Because this plugin lacks this feature I had to go with predis. It is also very good library but latency of cloud network (~ 0.9ms) + overhead of the plain php code causes an undesirable latency for using Redis ( as I am testing things since the last week, there is not much of a difference between using redis or mysql with these conditions ) + I should mention that I am not sure that if C code's speed can help on this ?

Also I really like performance and I think it is not good that php lacks this important thing in the toolset. So do you have any suggestion ? Or is there any plan on doing this ? If neither, where can I start to work on helping to add this feature to this repo ?

@yatsukhnenko
Copy link
Member

Is there a default path for the file ? or can I read the file and pass it as a parameter or something to the plugin ?

We use PHP stream wrapper so you may try to find default parameters somewhere in sources of PHP-engine.

Or is there any plan on doing this ? If neither, where can I start to work on helping to add this feature to this repo ?

It would be great to add ability to manage parameters of ssl/tls connections. We need to path them into redis_sock_connect in some way (as arguments or via INI variables) and handle whule creating connection.
I thought that we need to rewrite connect to handle array of parameters but @michael-grunder was against to make such big change because it will add more complexity to code and may break applications for users.

@Tommuh
Copy link

Tommuh commented Jan 28, 2020

You could use Spiped to make the connection secure. I have done it with master/slave and sentinels.
I think Redis shouldn't implement this because it add an extra layer that can go wrong.
Keep doing what you're good at and let others do what they're good at.

https://www.tarsnap.com/spiped.html
https://redis.io/topics/encryption

@gabfl
Copy link

gabfl commented May 19, 2020

SSL/TLS is supported by Redis starting with version 6 as seen on https://redis.io/topics/encryption

Would it be possible to implement this feature in phpredis now that it is supported by Redis itself?

@yatsukhnenko yatsukhnenko self-assigned this May 20, 2020
@yatsukhnenko yatsukhnenko added this to the 5.3.0 milestone May 20, 2020
yatsukhnenko added a commit that referenced this issue May 27, 2020
Ssl context options in Redis::connect
@yatsukhnenko
Copy link
Member

@bluemmb @gabfl could you test issue-1600 branch? You can specify ssl context options as last argument in Redis::connect, for example:

$r = new Redis();
$r->connect('tls://github.com', 443, 0, null, 0, 0, [
  'local_cert' => '/path/to/cert.pem',
]);

@gabfl
Copy link

gabfl commented May 27, 2020

@yatsukhnenko Thank you very much for taking the time to write a patch, it is very appreciated

I staged your branch:

# Stage test branch
git clone https://github.com/phpredis/phpredis.git && cd phpredis
git fetch origin issue-1600
git checkout issue-1600

And compiled phpredis from it:

phpize
./configure
make && make install
echo "extension=redis.so" >> /etc/php/7.2/cli/conf.d/redis.ini

I used this php snippet:

<?php

$r = new Redis();
$r->connect('tls://127.0.0.1', 6379, 0, null, 0, 0, [
  'local_cert' => '/root/redis-6.0.3/tests/tls/redis.crt',
  'local_pk' => '/root/redis-6.0.3/tests/tls/redis.key',
  'cafile' => '/root/redis-6.0.3/tests/tls/ca.crt',
  'verify_peer_name' => false
]);

var_dump($r->set('test_tls', 'it works'));
var_dump($r->get('test_tls'));

and it works:

root@ubuntu-s-4vcpu-8gb-nyc1-01:~/phpredis# php phpredis.php
bool(true)
string(8) "it works"
root@ubuntu-s-4vcpu-8gb-nyc1-01:~/phpredis# 

🎉

@ywarnier
Copy link

Would there be any way to use TLS and a CA certificate in php.ini (session.save_path)?

yatsukhnenko added a commit that referenced this issue Jun 2, 2020
Ssl context options in Redis::connect
yatsukhnenko added a commit that referenced this issue Jun 3, 2020
@yatsukhnenko
Copy link
Member

@ywarnier could you create another issue for session+tls?

@yatsukhnenko
Copy link
Member

Fixed in 5.3.0.

@ywarnier
Copy link

ywarnier commented Jul 1, 2020

@yatsukhnenko Issue #1775 is actually about that already.

@dimovnike
Copy link

the example is not working, the correct way to use it is:

$r->connect('tls://127.0.0.1', 6379, 0, null, 0, 0, [
  'stream' => [
    'local_cert' => '/root/redis-6.0.3/tests/tls/redis.crt',
    'local_pk' => '/root/redis-6.0.3/tests/tls/redis.key',
    'cafile' => '/root/redis-6.0.3/tests/tls/ca.crt',
    'verify_peer_name' => false
  ]
]);

Note the key 'stream'. Could not find this in the documentation.

@yatsukhnenko
Copy link
Member

@dimovnike thanks! I made this example while Michael making his part related to acl. We will update documentation or maybe change api again 😄

@gabfl
Copy link

gabfl commented Aug 13, 2020

It worked for me as provided, it might be related to the PHP version in use

@dimovnike
Copy link

any idea why this client uses tlsv1 only? otherwise , the curl says it supports tlsv1.2.

@yatsukhnenko
Copy link
Member

@dimovnike you can specify version tlsv1.2://

@AlexAndBear
Copy link

@yatsukhnenko I can't find anything in the documentation about the tls/ssl stuff and how to pass the arguments correctly, do you have a link for me ?

I encountered a inconsistency in this case, and I wondering why.

If we use the class Redis, we need to pass the following arguments:

	['stream' => [
				'local_cert' => '/home/jan/Downloads/redis-6.0.10/tests/tls/redis.crt',
				'local_pk' => '/home/jan/Downloads/redis-6.0.10/tests/tls/redis.key',
				'cafile' => '/home/jan/Downloads/redis-6.0.10/tests/tls/ca.crt',
				'verify_peer_name' => false
			]]

But if we use the RedisCluster class we need to provide the same arguments without the stream key

 [
				'local_cert' => '/home/jan/Downloads/redis-6.0.10/tests/tls/redis.crt',
				'local_pk' => '/home/jan/Downloads/redis-6.0.10/tests/tls/redis.key',
				'cafile' => '/home/jan/Downloads/redis-6.0.10/tests/tls/ca.crt',
				'verify_peer_name' => false
			]

Could you explain, why this is the case ?
Thank you in advance

@willbrowningme
Copy link

willbrowningme commented Feb 16, 2021

@janackermann same thing here. Connecting to a cluster only works if you remove the 'stream' key. Took me quite some time to figure that out! Also has issues with connections freezing using PHP 7.4.3 and TLSv1.3, so had to limit to TLSv1.2 only to get it to work.

dbertram added a commit to dbertram/w3-total-cache that referenced this issue Sep 26, 2022
This was initially added as part of 2.2.2, but was later removed in 2.2.3 while fixing PHP warnings related to the number of args being passed to pconnect (BoldGrid#533).

After refactoring how Redis connection arguments are being calculated/passed, we can now support disabling TLS certificate verification when the installed version of phpredis supports it.

phpredis added initial support for the stream context in version 5.3.0 (phpredis/phpredis#1600) and support for RedisCluster in 5.3.2.
cssjoe pushed a commit to BoldGrid/w3-total-cache that referenced this issue Oct 26, 2022
…ned certificates (e.g., Heroku Redis) (#559)

* Cache_Redis: remove restore_error_handler call from 2.2.2-rc.1
A custom error handler was added in this section of the code in 2.2.2-rc.1 (3d50386) that was later removed in the 2.2.3 release (5335b97), but the clean up call was missed.
* Cache_Redis: DRY logic for computing Redis connection params
Introduces a private _build_connect_args helper
* Cache_Redis: improve logic for read_timeout connect param
Support for the read_timeout connection param was added to phpredis in v3.1.3:
phpredis/phpredis@b56dc49
* Cache_Redis: restore support for disabling TLS certificate verification
This was initially added as part of 2.2.2, but was later removed in 2.2.3 while fixing PHP warnings related to the number of args being passed to pconnect (#533).
After refactoring how Redis connection arguments are being calculated/passed, we can now support disabling TLS certificate verification when the installed version of phpredis supports it.
phpredis added initial support for the stream context in version 5.3.0 (phpredis/phpredis#1600) and support for RedisCluster in 5.3.2.
* ConfigCache: add ability to disable redis tls certificate verification
This is a follow-on to #494 to extend support for the "verify TLS certificates" option to the feature that enables caching W3TC's config in redis.
* Cache Plugins: update usage stats to respect verify_tls_certificates
* Minor adjustments to meet coding standards
* Full CS adjustments to all touched files
Co-authored-by: jacobd91 <jacobd@inmotionhosting.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants