forked from redis/redis-py
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for password-encrypted SSL private keys (redis#1782)
Adding support for SSL private keys with a password. This PR also adds support for future SSL tests.
- Loading branch information
Showing
17 changed files
with
215 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ coverage.xml | |
.venv | ||
*.xml | ||
.coverage* | ||
docker/stunnel/keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# produces redisfab/redis-py:6.2.6 | ||
FROM redis:6.2.6-buster | ||
|
||
CMD ["redis-server", "/redis.conf"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
# produces redisfab/redis-py-cluster:6.2.6 | ||
FROM redis:6.2.6-buster | ||
|
||
COPY create_cluster.sh /create_cluster.sh | ||
RUN chmod +x /create_cluster.sh | ||
|
||
EXPOSE 16379 16380 16381 16382 16383 16384 | ||
|
||
CMD [ "/create_cluster.sh"] | ||
CMD [ "/create_cluster.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# produces redisfab/redis-py-sentinel:6.2.6 | ||
FROM redis:6.2.6-buster | ||
|
||
CMD ["redis-sentinel", "/sentinel.conf"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# produces redisfab/stunnel:latest | ||
FROM ubuntu:18.04 | ||
|
||
RUN apt-get update -qq --fix-missing | ||
RUN apt-get upgrade -qqy | ||
RUN apt install -qqy stunnel | ||
RUN mkdir -p /etc/stunnel/conf.d | ||
RUN echo "foreground = yes\ninclude = /etc/stunnel/conf.d" > /etc/stunnel/stunnel.conf | ||
RUN chown -R root:root /etc/stunnel/ | ||
|
||
CMD ["/usr/bin/stunnel"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[redis] | ||
accept = 6666 | ||
connect = master:6379 | ||
cert = /etc/stunnel/keys/server-cert.pem | ||
key = /etc/stunnel/keys/server-key.pem | ||
verify = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
DESTDIR=`dirname "$0"`/keys | ||
test -d ${DESTDIR} || mkdir ${DESTDIR} | ||
cd ${DESTDIR} | ||
|
||
SSL_SUBJECT="/C=CA/ST=Winnipeg/L=Manitoba/O=Some Corp/OU=IT Department/CN=example.com" | ||
which openssl &>/dev/null | ||
if [ $? -ne 0 ]; then | ||
echo "No openssl binary present, exiting." | ||
exit 1 | ||
fi | ||
|
||
openssl genrsa -out ca-key.pem 2048 &>/dev/null | ||
|
||
openssl req -new -x509 -nodes -days 365000 \ | ||
-key ca-key.pem \ | ||
-out ca-cert.pem \ | ||
-subj "${SSL_SUBJECT}" &>/dev/null | ||
|
||
openssl req -newkey rsa:2048 -nodes -days 365000 \ | ||
-keyout server-key.pem \ | ||
-out server-req.pem \ | ||
-subj "${SSL_SUBJECT}" &>/dev/null | ||
|
||
openssl x509 -req -days 365000 -set_serial 01 \ | ||
-in server-req.pem \ | ||
-out server-cert.pem \ | ||
-CA ca-cert.pem \ | ||
-CAkey ca-key.pem &>/dev/null | ||
|
||
openssl req -newkey rsa:2048 -nodes -days 365000 \ | ||
-keyout client-key.pem \ | ||
-out client-req.pem \ | ||
-subj "${SSL_SUBJECT}" &>/dev/null | ||
|
||
openssl x509 -req -days 365000 -set_serial 01 \ | ||
-in client-req.pem \ | ||
-out client-cert.pem \ | ||
-CA ca-cert.pem \ | ||
-CAkey ca-key.pem &>/dev/null | ||
|
||
echo "Keys generated in ${DESTDIR}:" | ||
ls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
from urllib.parse import urlparse | ||
|
||
import pytest | ||
|
||
import redis | ||
from redis.exceptions import ConnectionError | ||
|
||
|
||
@pytest.mark.ssl | ||
class TestSSL: | ||
"""Tests for SSL connections | ||
This relies on the --redis-ssl-url purely for rebuilding the client | ||
and connecting to the appropriate port. | ||
""" | ||
|
||
ROOT = os.path.join(os.path.dirname(__file__), "..") | ||
CERT_DIR = os.path.abspath(os.path.join(ROOT, "docker", "stunnel", "keys")) | ||
if not os.path.isdir(CERT_DIR): # github actions package validation case | ||
CERT_DIR = os.path.abspath( | ||
os.path.join(ROOT, "..", "docker", "stunnel", "keys") | ||
) | ||
if not os.path.isdir(CERT_DIR): | ||
raise IOError(f"No SSL certificates found. They should be in {CERT_DIR}") | ||
|
||
def test_ssl_with_invalid_cert(self, request): | ||
ssl_url = request.config.option.redis_ssl_url | ||
sslclient = redis.from_url(ssl_url) | ||
with pytest.raises(ConnectionError) as e: | ||
sslclient.ping() | ||
assert "SSL: CERTIFICATE_VERIFY_FAILED" in str(e) | ||
|
||
def test_ssl_connection(self, request): | ||
ssl_url = request.config.option.redis_ssl_url | ||
p = urlparse(ssl_url)[1].split(":") | ||
r = redis.Redis(host=p[0], port=p[1], ssl=True, ssl_cert_reqs="none") | ||
assert r.ping() | ||
|
||
def test_ssl_connection_without_ssl(self, request): | ||
ssl_url = request.config.option.redis_ssl_url | ||
p = urlparse(ssl_url)[1].split(":") | ||
r = redis.Redis(host=p[0], port=p[1], ssl=False) | ||
|
||
with pytest.raises(ConnectionError) as e: | ||
r.ping() | ||
assert "Connection closed by server" in str(e) | ||
|
||
def test_validating_self_signed_certificate(self, request): | ||
ssl_url = request.config.option.redis_ssl_url | ||
p = urlparse(ssl_url)[1].split(":") | ||
r = redis.Redis( | ||
host=p[0], | ||
port=p[1], | ||
ssl=True, | ||
ssl_certfile=os.path.join(self.CERT_DIR, "server-cert.pem"), | ||
ssl_keyfile=os.path.join(self.CERT_DIR, "server-key.pem"), | ||
ssl_cert_reqs="required", | ||
ssl_ca_certs=os.path.join(self.CERT_DIR, "server-cert.pem"), | ||
) | ||
assert r.ping() |
Oops, something went wrong.