From 93ec3daf4745a0c2cd586b83edcdabfb1eef0513 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Wed, 11 Mar 2020 11:31:51 -0700 Subject: [PATCH] Add TLS support for ZooKeeper This adds a script to generate TLS certs for zookeeper. It also adds new config file options for specifying certs for a TLS connection, adds a howto document to advise admins on how to configure ZK for TLS. It also removes the 'required' flag for the SASL auth parameters, since they are not actually required. Include the default openssl.cnf file since some distros modify it to specify paths that are incompatbile with the zk-ca.sh script. Change-Id: Icd976cc32dfd9f75f8cfb1c9ad11e08af31723d6 --- doc/source/discussion/components.rst | 13 +- doc/source/howtos/admin.rst | 1 + doc/source/howtos/zookeeper.rst | 118 +++++++++ tests/base.py | 1 + tools/openssl.cnf | 352 +++++++++++++++++++++++++++ tools/zk-ca.sh | 103 ++++++++ zuul/cmd/scheduler.py | 11 +- zuul/cmd/web.py | 6 + zuul/web/__init__.py | 23 +- zuul/zk.py | 19 +- 10 files changed, 629 insertions(+), 18 deletions(-) create mode 100644 doc/source/howtos/zookeeper.rst create mode 100644 tools/openssl.cnf create mode 100755 tools/zk-ca.sh diff --git a/doc/source/discussion/components.rst b/doc/source/discussion/components.rst index 5d93bd9327..ba36c89c68 100644 --- a/doc/source/discussion/components.rst +++ b/doc/source/discussion/components.rst @@ -64,7 +64,7 @@ TCP port 4730 by default. The Zuul scheduler communicates with Nodepool via the ZooKeeper protocol. Nodepool requires an external ZooKeeper cluster, and the Zuul scheduler needs to be able to connect to the hosts in that -cluster on TCP port 2181. +cluster on TCP port 2181 or 2281. Both the Nodepool launchers and Zuul executors need to be able to communicate with the hosts which nodepool provides. If these are on @@ -193,6 +193,17 @@ The following sections of ``zuul.conf`` are used by all Zuul components: The ZooKeeper session timeout, in seconds. + .. attr:: tls_cert + + If using TLS, the path to the PEM encoded certificate file. + + .. attr:: tls_key + + If using TLS, the path to the PEM encoded key file. + + .. attr:: tls_ca + + If using TLS, the path to the PEM encoded CA certificate file. .. _scheduler: diff --git a/doc/source/howtos/admin.rst b/doc/source/howtos/admin.rst index a829d25285..9fd4933f27 100644 --- a/doc/source/howtos/admin.rst +++ b/doc/source/howtos/admin.rst @@ -7,3 +7,4 @@ Admin How-to Guides installation zuul-from-scratch troubleshooting + zookeeper diff --git a/doc/source/howtos/zookeeper.rst b/doc/source/howtos/zookeeper.rst new file mode 100644 index 0000000000..ef127a5995 --- /dev/null +++ b/doc/source/howtos/zookeeper.rst @@ -0,0 +1,118 @@ +ZooKeeper Administration +======================== + +This section will cover some basic tasks and recommendations when +setting up ZooKeeper for use with Zuul. A complete tutorial for +ZooKeeper is out of scope for this documentation. + +Configuration +------------- + +The following general configuration setting in +``/etc/zookeeper/zoo.cfg`` is recommended: + +.. code-block:: + + autopurge.purgeInterval=6 + +This instructs ZooKeeper to purge old snapshots every 6 hours. This +will avoid filling the disk. + +Encrypted Connections +--------------------- + +ZooKeeper version 3.5.1 or greater is required for TLS support. +ZooKeeper performs hostname validation for all ZooKeeper servers +("quorum members"), therefore each member of the ZooKeeper cluster +should have its own certificate. This does not apply to clients which +may share a certificate. + +ZooKeeper performs certificate validation on all connections (server +and client). If you use a private Certificate Authority (CA) (which +is generally recommended and discussed below), then these TLS +certificates not only serve to encrypt traffic, but also to +authenticate and authorize clients to the cluster. Only clients with +certificates authorized by a CA explicitly trusted by your ZooKeeper +installation will be able to connect. + +.. note:: The instructions below direct you to sign certificates with + a CA that you create specifically for Zuul's ZooKeeper + cluster. If you use a CA you share with other users in your + organization, any certificate signed by that CA will be able + to connect to your ZooKeeper cluster. In this case, you may + need to take additional steps such as network isolation to + protect your ZooKeeper cluster. These are beyond the scope + of this document. + +The ``tools/zk-ca.sh`` script in the Zuul source code repository can +be used to quickly and easily generate self-signed certificates for +all ZooKeeper cluster members and clients. + +Make a directory for it to store the certificates and CA data, and run +it once for each client: + +.. code-block:: + + mkdir /etc/zookeeper/ca + tools/zk-ca.sh /etc/zookeeper/ca zookeeper1.example.com + tools/zk-ca.sh /etc/zookeeper/ca zookeeper2.example.com + tools/zk-ca.sh /etc/zookeeper/ca zookeeper3.example.com + +Add the following to ``/etc/zookeeper/zoo.cfg``: + +.. code-block:: + + # Necessary for TLS support + serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory + + # Client TLS configuration + secureClientPort=2281 + ssl.keyStore.location=/etc/zookeeper/ca/keystores/zookeeper1.example.com.jks + ssl.keyStore.password=keystorepassword + ssl.trustStore.location=/etc/zookeeper/ca/certs/cacert.pem + + # Server TLS configuration + sslQuorum=true + ssl.quorum.keyStore.location=/etc/zookeeper/ca/keystores/zookeeper1.example.com.jks + ssl.quorum.keyStore.password=keystorepassword + ssl.quorum.trustStore.location=/etc/zookeeper/ca/certs/cacert.pem + +Change the name of the certificate filenames as appropriate for the +host (e.g., ``zookeeper1.example.com.jks``). Note that the keystore +password ``keystorepassword``, which is set by the ``zk-ca.sh`` +script, does not need to be changed as long as file permissions +provide sufficient protection. The password is present because many +Java utilities misbehave when interacting with keystores with empty or +missing passwords. + +In order to disable plaintext connections, ensure that the +``clientPort`` option does not appear in ``zoo.cfg``. Use the new +method of specifying Zookeeper quorum servers, which looks like this: + +.. code-block:: + + server.1=zookeeper1.example.com:2888:3888 + server.2=zookeeper2.example.com:2888:3888 + server.3=zookeeper3.example.com:2888:3888 + +This format normally includes ``;2181`` at the end of each line, +signifying that the server should listen on port 2181 for plaintext +client connections (this is equivalent to the ``clientPort`` option). +Omit it to disable plaintext connections. The earlier addition of +``secureClientPort`` to the config file instructs ZooKeeper to listen +for encrypted connections on port 2281. + +Be sure to specify port 2281 rather than the standard 2181 in the +:attr:`zookeeper.hosts` setting in ``zuul.conf``. + +Finally, add the :attr:`zookeeper.tls_cert`, +:attr:`zookeeper.tls_key`, and :attr:`zookeeper.tls_ca` options. Your +``zuul.conf`` file should look like: + +.. code-block:: + + [zookeeper] + hosts=zookeeper1.example.com:2281,zookeeper2.example.com:2281,zookeeper3.example.com:2281 + tls_cert=/etc/zookeeper/ca/certs/client.pem + tls_key=/etc/zookeeper/ca/keys/clientkey.pem + tls_ca=/etc/zookeeper/ca/certs/cacert.pem diff --git a/tests/base.py b/tests/base.py index aa43ab2e7e..effed1c54b 100644 --- a/tests/base.py +++ b/tests/base.py @@ -3193,6 +3193,7 @@ class ZuulWebFixture(fixtures.Fixture): info=self.info, connections=self.connections, zk_hosts=self.zk_hosts, + zk_timeout=10, command_socket=os.path.join(self.test_root, 'web.socket'), authenticators=self.authenticators) self.web.start() diff --git a/tools/openssl.cnf b/tools/openssl.cnf new file mode 100644 index 0000000000..7d1a8bb6e7 --- /dev/null +++ b/tools/openssl.cnf @@ -0,0 +1,352 @@ +# +# OpenSSL example configuration file. +# This is mostly being used for generation of certificate requests. +# + +# Note that you can include other files from the main configuration +# file using the .include directive. +#.include filename + +# This definition stops the following lines choking if HOME isn't +# defined. +HOME = . +RANDFILE = $ENV::HOME/.rnd + +# Extra OBJECT IDENTIFIER info: +#oid_file = $ENV::HOME/.oid +oid_section = new_oids + +# To use this configuration file with the "-extfile" option of the +# "openssl x509" utility, name here the section containing the +# X.509v3 extensions to use: +# extensions = +# (Alternatively, use a configuration file that has only +# X.509v3 extensions in its main [= default] section.) + +[ new_oids ] + +# We can add new OIDs in here for use by 'ca', 'req' and 'ts'. +# Add a simple OID like this: +# testoid1=1.2.3.4 +# Or use config file substitution like this: +# testoid2=${testoid1}.5.6 + +# Policies used by the TSA examples. +tsa_policy1 = 1.2.3.4.1 +tsa_policy2 = 1.2.3.4.5.6 +tsa_policy3 = 1.2.3.4.5.7 + +#################################################################### +[ ca ] +default_ca = CA_default # The default ca section + +#################################################################### +[ CA_default ] + +dir = ./demoCA # Where everything is kept +certs = $dir/certs # Where the issued certs are kept +crl_dir = $dir/crl # Where the issued crl are kept +database = $dir/index.txt # database index file. +#unique_subject = no # Set to 'no' to allow creation of + # several certs with same subject. +new_certs_dir = $dir/newcerts # default place for new certs. + +certificate = $dir/cacert.pem # The CA certificate +serial = $dir/serial # The current serial number +crlnumber = $dir/crlnumber # the current crl number + # must be commented out to leave a V1 CRL +crl = $dir/crl.pem # The current CRL +private_key = $dir/private/cakey.pem# The private key +RANDFILE = $dir/private/.rand # private random number file + +x509_extensions = usr_cert # The extensions to add to the cert + +# Comment out the following two lines for the "traditional" +# (and highly broken) format. +name_opt = ca_default # Subject Name options +cert_opt = ca_default # Certificate field options + +# Extension copying option: use with caution. +# copy_extensions = copy + +# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs +# so this is commented out by default to leave a V1 CRL. +# crlnumber must also be commented out to leave a V1 CRL. +# crl_extensions = crl_ext + +default_days = 365 # how long to certify for +default_crl_days= 30 # how long before next CRL +default_md = default # use public key default MD +preserve = no # keep passed DN ordering + +# A few difference way of specifying how similar the request should look +# For type CA, the listed attributes must be the same, and the optional +# and supplied fields are just that :-) +policy = policy_match + +# For the CA policy +[ policy_match ] +countryName = match +stateOrProvinceName = match +organizationName = match +organizationalUnitName = optional +commonName = supplied +emailAddress = optional + +# For the 'anything' policy +# At this point in time, you must list all acceptable 'object' +# types. +[ policy_anything ] +countryName = optional +stateOrProvinceName = optional +localityName = optional +organizationName = optional +organizationalUnitName = optional +commonName = supplied +emailAddress = optional + +#################################################################### +[ req ] +default_bits = 2048 +default_keyfile = privkey.pem +distinguished_name = req_distinguished_name +attributes = req_attributes +x509_extensions = v3_ca # The extensions to add to the self signed cert + +# Passwords for private keys if not present they will be prompted for +# input_password = secret +# output_password = secret + +# This sets a mask for permitted string types. There are several options. +# default: PrintableString, T61String, BMPString. +# pkix : PrintableString, BMPString (PKIX recommendation before 2004) +# utf8only: only UTF8Strings (PKIX recommendation after 2004). +# nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings). +# MASK:XXXX a literal mask value. +# WARNING: ancient versions of Netscape crash on BMPStrings or UTF8Strings. +string_mask = utf8only + +# req_extensions = v3_req # The extensions to add to a certificate request + +[ req_distinguished_name ] +countryName = Country Name (2 letter code) +countryName_default = AU +countryName_min = 2 +countryName_max = 2 + +stateOrProvinceName = State or Province Name (full name) +stateOrProvinceName_default = Some-State + +localityName = Locality Name (eg, city) + +0.organizationName = Organization Name (eg, company) +0.organizationName_default = Internet Widgits Pty Ltd + +# we can do this but it is not needed normally :-) +#1.organizationName = Second Organization Name (eg, company) +#1.organizationName_default = World Wide Web Pty Ltd + +organizationalUnitName = Organizational Unit Name (eg, section) +#organizationalUnitName_default = + +commonName = Common Name (e.g. server FQDN or YOUR name) +commonName_max = 64 + +emailAddress = Email Address +emailAddress_max = 64 + +# SET-ex3 = SET extension number 3 + +[ req_attributes ] +challengePassword = A challenge password +challengePassword_min = 4 +challengePassword_max = 20 + +unstructuredName = An optional company name + +[ usr_cert ] + +# These extensions are added when 'ca' signs a request. + +# This goes against PKIX guidelines but some CAs do it and some software +# requires this to avoid interpreting an end user certificate as a CA. + +basicConstraints=CA:FALSE + +# Here are some examples of the usage of nsCertType. If it is omitted +# the certificate can be used for anything *except* object signing. + +# This is OK for an SSL server. +# nsCertType = server + +# For an object signing certificate this would be used. +# nsCertType = objsign + +# For normal client use this is typical +# nsCertType = client, email + +# and for everything including object signing: +# nsCertType = client, email, objsign + +# This is typical in keyUsage for a client certificate. +# keyUsage = nonRepudiation, digitalSignature, keyEncipherment + +# This will be displayed in Netscape's comment listbox. +nsComment = "OpenSSL Generated Certificate" + +# PKIX recommendations harmless if included in all certificates. +subjectKeyIdentifier=hash +authorityKeyIdentifier=keyid,issuer + +# This stuff is for subjectAltName and issuerAltname. +# Import the email address. +# subjectAltName=email:copy +# An alternative to produce certificates that aren't +# deprecated according to PKIX. +# subjectAltName=email:move + +# Copy subject details +# issuerAltName=issuer:copy + +#nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem +#nsBaseUrl +#nsRevocationUrl +#nsRenewalUrl +#nsCaPolicyUrl +#nsSslServerName + +# This is required for TSA certificates. +# extendedKeyUsage = critical,timeStamping + +[ v3_req ] + +# Extensions to add to a certificate request + +basicConstraints = CA:FALSE +keyUsage = nonRepudiation, digitalSignature, keyEncipherment + +[ v3_ca ] + + +# Extensions for a typical CA + + +# PKIX recommendation. + +subjectKeyIdentifier=hash + +authorityKeyIdentifier=keyid:always,issuer + +basicConstraints = critical,CA:true + +# Key usage: this is typical for a CA certificate. However since it will +# prevent it being used as an test self-signed certificate it is best +# left out by default. +# keyUsage = cRLSign, keyCertSign + +# Some might want this also +# nsCertType = sslCA, emailCA + +# Include email address in subject alt name: another PKIX recommendation +# subjectAltName=email:copy +# Copy issuer details +# issuerAltName=issuer:copy + +# DER hex encoding of an extension: beware experts only! +# obj=DER:02:03 +# Where 'obj' is a standard or added object +# You can even override a supported extension: +# basicConstraints= critical, DER:30:03:01:01:FF + +[ crl_ext ] + +# CRL extensions. +# Only issuerAltName and authorityKeyIdentifier make any sense in a CRL. + +# issuerAltName=issuer:copy +authorityKeyIdentifier=keyid:always + +[ proxy_cert_ext ] +# These extensions should be added when creating a proxy certificate + +# This goes against PKIX guidelines but some CAs do it and some software +# requires this to avoid interpreting an end user certificate as a CA. + +basicConstraints=CA:FALSE + +# Here are some examples of the usage of nsCertType. If it is omitted +# the certificate can be used for anything *except* object signing. + +# This is OK for an SSL server. +# nsCertType = server + +# For an object signing certificate this would be used. +# nsCertType = objsign + +# For normal client use this is typical +# nsCertType = client, email + +# and for everything including object signing: +# nsCertType = client, email, objsign + +# This is typical in keyUsage for a client certificate. +# keyUsage = nonRepudiation, digitalSignature, keyEncipherment + +# This will be displayed in Netscape's comment listbox. +nsComment = "OpenSSL Generated Certificate" + +# PKIX recommendations harmless if included in all certificates. +subjectKeyIdentifier=hash +authorityKeyIdentifier=keyid,issuer + +# This stuff is for subjectAltName and issuerAltname. +# Import the email address. +# subjectAltName=email:copy +# An alternative to produce certificates that aren't +# deprecated according to PKIX. +# subjectAltName=email:move + +# Copy subject details +# issuerAltName=issuer:copy + +#nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem +#nsBaseUrl +#nsRevocationUrl +#nsRenewalUrl +#nsCaPolicyUrl +#nsSslServerName + +# This really needs to be in place for it to be a proxy certificate. +proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo + +#################################################################### +[ tsa ] + +default_tsa = tsa_config1 # the default TSA section + +[ tsa_config1 ] + +# These are used by the TSA reply generation only. +dir = ./demoCA # TSA root directory +serial = $dir/tsaserial # The current serial number (mandatory) +crypto_device = builtin # OpenSSL engine to use for signing +signer_cert = $dir/tsacert.pem # The TSA signing certificate + # (optional) +certs = $dir/cacert.pem # Certificate chain to include in reply + # (optional) +signer_key = $dir/private/tsakey.pem # The TSA private key (optional) +signer_digest = sha256 # Signing digest to use. (Optional) +default_policy = tsa_policy1 # Policy if request did not specify it + # (optional) +other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional) +digests = sha1, sha256, sha384, sha512 # Acceptable message digests (mandatory) +accuracy = secs:1, millisecs:500, microsecs:100 # (optional) +clock_precision_digits = 0 # number of digits after dot. (optional) +ordering = yes # Is ordering defined for timestamps? + # (optional, default: no) +tsa_name = yes # Must the TSA name be included in the reply? + # (optional, default: no) +ess_cert_id_chain = no # Must the ESS cert id chain be included? + # (optional, default: no) +ess_cert_id_alg = sha1 # algorithm to compute certificate + # identifier (optional, default: sha1) diff --git a/tools/zk-ca.sh b/tools/zk-ca.sh new file mode 100755 index 0000000000..78b4e52650 --- /dev/null +++ b/tools/zk-ca.sh @@ -0,0 +1,103 @@ +#!/bin/sh -e + +# Copyright 2020 Red Hat, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# Manage a CA for Zookeeper + +CAROOT=$1 +SERVER=$2 + +SUBJECT='/C=US/ST=California/L=Oakland/O=Company Name/OU=Org' +TOOLSDIR=$(dirname $0) +CONFIG="-config $TOOLSDIR/openssl.cnf" + +make_ca() { + mkdir $CAROOT/demoCA + mkdir $CAROOT/demoCA/reqs + mkdir $CAROOT/demoCA/newcerts + mkdir $CAROOT/demoCA/crl + mkdir $CAROOT/demoCA/private + chmod 700 $CAROOT/demoCA/private + touch $CAROOT/demoCA/index.txt + touch $CAROOT/demoCA/index.txt.attr + mkdir $CAROOT/certs + mkdir $CAROOT/keys + mkdir $CAROOT/keystores + chmod 700 $CAROOT/keys + chmod 700 $CAROOT/keystores + + openssl req $CONFIG -new -nodes -subj "$SUBJECT/CN=caroot" \ + -keyout $CAROOT/demoCA/private/cakey.pem \ + -out $CAROOT/demoCA/reqs/careq.pem + openssl ca $CONFIG -create_serial -days 3560 -batch -selfsign -extensions v3_ca \ + -out $CAROOT/demoCA/cacert.pem \ + -keyfile $CAROOT/demoCA/private/cakey.pem \ + -infiles $CAROOT/demoCA/reqs/careq.pem + cp $CAROOT/demoCA/cacert.pem $CAROOT/certs +} + +make_client() { + openssl req $CONFIG -new -nodes -subj "$SUBJECT/CN=client" \ + -keyout $CAROOT/keys/clientkey.pem \ + -out $CAROOT/demoCA/reqs/clientreq.pem + openssl ca $CONFIG -batch -policy policy_anything -days 3560 \ + -out $CAROOT/certs/client.pem \ + -infiles $CAROOT/demoCA/reqs/clientreq.pem +} + +make_server() { + openssl req $CONFIG -new -nodes -subj "$SUBJECT/CN=$SERVER" \ + -keyout $CAROOT/keys/${SERVER}key.pem \ + -out $CAROOT/demoCA/reqs/${SERVER}req.pem + openssl ca $CONFIG -batch -policy policy_anything -days 3560 \ + -out $CAROOT/certs/$SERVER.pem \ + -infiles $CAROOT/demoCA/reqs/${SERVER}req.pem + cat $CAROOT/certs/$SERVER.pem $CAROOT/keys/${SERVER}key.pem \ + > $CAROOT/keystores/$SERVER.pem +} + +help() { + echo "$0 CAROOT [SERVER]" + echo + echo " CAROOT is the path to a directory in which to store the CA" + echo " and certificates." + echo " SERVER is the FQDN of a server for which a certificate should" + echo " be generated" +} + +if [ ! -d "$CAROOT" ]; then + echo "CAROOT must be a directory" + help + exit 1 +fi + +cd $CAROOT +CAROOT=`pwd` + +if [ ! -d "$CAROOT/demoCA" ]; then + echo 'Generate CA' + make_ca + echo 'Generate client certificate' + make_client +fi + +if [ -f "$CAROOT/certs/$SERVER.pem" ]; then + echo "Certificate for $SERVER already exists" + exit 0 +fi + +if [ "$SERVER" != "" ]; then + make_server +fi diff --git a/zuul/cmd/scheduler.py b/zuul/cmd/scheduler.py index 82a589091a..1cb693ce4b 100755 --- a/zuul/cmd/scheduler.py +++ b/zuul/cmd/scheduler.py @@ -149,10 +149,17 @@ class Scheduler(zuul.cmd.ZuulDaemonApp): zookeeper_hosts = get_default(self.config, 'zookeeper', 'hosts', None) if not zookeeper_hosts: raise Exception("The zookeeper hosts config value is required") + zookeeper_tls_key = get_default(self.config, 'zookeeper', 'tls_key') + zookeeper_tls_cert = get_default(self.config, 'zookeeper', 'tls_cert') + zookeeper_tls_ca = get_default(self.config, 'zookeeper', 'tls_ca') zookeeper_timeout = float(get_default(self.config, 'zookeeper', 'session_timeout', 10.0)) - - zookeeper.connect(zookeeper_hosts, timeout=zookeeper_timeout) + zookeeper.connect( + zookeeper_hosts, + timeout=zookeeper_timeout, + tls_cert=zookeeper_tls_cert, + tls_key=zookeeper_tls_key, + tls_ca=zookeeper_tls_ca) self.configure_connections() self.sched.setExecutor(gearman) diff --git a/zuul/cmd/web.py b/zuul/cmd/web.py index b992e621cd..a29a98b4f6 100755 --- a/zuul/cmd/web.py +++ b/zuul/cmd/web.py @@ -86,6 +86,12 @@ class WebServer(zuul.cmd.ZuulDaemonApp): self.config, 'zookeeper', 'hosts', None) if not params["zk_hosts"]: raise Exception("The zookeeper hosts config value is required") + params["zk_tls_key"] = get_default(self.config, 'zookeeper', 'tls_key') + params["zk_tls_cert"] = get_default(self.config, + 'zookeeper', 'tls_cert') + params["zk_tls_ca"] = get_default(self.config, 'zookeeper', 'tls_ca') + params["zk_timeout"] = float(get_default(self.config, 'zookeeper', + 'session_timeout', 10.0)) try: self.web = zuul.web.ZuulWeb(**params) diff --git a/zuul/web/__init__.py b/zuul/web/__init__.py index 6c3b6a9339..b2eb570385 100755 --- a/zuul/web/__init__.py +++ b/zuul/web/__init__.py @@ -1069,17 +1069,13 @@ class StreamManager(object): class ZuulWeb(object): log = logging.getLogger("zuul.web.ZuulWeb") - def __init__(self, listen_address, listen_port, - gear_server, gear_port, - ssl_key=None, ssl_cert=None, ssl_ca=None, - static_cache_expiry=3600, - connections=None, - info=None, - static_path=None, - zk_hosts=None, - authenticators=None, - command_socket=None, - ): + def __init__(self, listen_address, listen_port, gear_server, + gear_port, ssl_key=None, ssl_cert=None, ssl_ca=None, + static_cache_expiry=3600, connections=None, + info=None, static_path=None, zk_hosts=None, + zk_timeout=None, zk_tls_cert=None, zk_tls_key=None, + zk_tls_ca=None, authenticators=None, + command_socket=None): self.start_time = time.time() self.listen_address = listen_address self.listen_port = listen_port @@ -1095,7 +1091,10 @@ class ZuulWeb(object): client_id='Zuul Web Server') self.zk = zuul.zk.ZooKeeper(enable_cache=True) if zk_hosts: - self.zk.connect(hosts=zk_hosts, read_only=True) + self.zk.connect(hosts=zk_hosts, read_only=True, + timeout=zk_timeout, tls_cert=zk_tls_cert, + tls_key=zk_tls_key, tls_ca=zk_tls_ca) + self.connections = connections self.authenticators = authenticators self.stream_manager = StreamManager() diff --git a/zuul/zk.py b/zuul/zk.py index 371a365dd5..980249526f 100644 --- a/zuul/zk.py +++ b/zuul/zk.py @@ -115,7 +115,8 @@ class ZooKeeper(object): self.log.warning("Retrying zookeeper connection") self._last_retry_log = now - def connect(self, hosts, read_only=False, timeout=10.0): + def connect(self, hosts, read_only=False, timeout=10.0, + tls_cert=None, tls_key=None, tls_ca=None): ''' Establish a connection with ZooKeeper cluster. @@ -127,10 +128,22 @@ class ZooKeeper(object): :param bool read_only: If True, establishes a read-only connection. :param float timeout: The ZooKeeper session timeout, in seconds (default: 10.0). + :param str tls_key: Path to TLS key + :param str tls_cert: Path to TLS cert + :param str tls_ca: Path to TLS CA cert ''' + if self.client is None: - self.client = KazooClient(hosts=hosts, read_only=read_only, - timeout=timeout) + args = dict(hosts=hosts, + read_only=read_only, + timeout=timeout, + ) + if tls_key: + args['use_ssl'] = True + args['keyfile'] = tls_key + args['certfile'] = tls_cert + args['ca'] = tls_ca + self.client = KazooClient(**args) self.client.add_listener(self._connection_listener) # Manually retry initial connection attempt while True: