In this article we will talk about how to secure your ClickHouse network and will enrich article with examples.
To boost your ClickHouse network, you have to monitor your network traffic and user who is connecting your database.
Boosting ClickHouse network is follow the steps below;
- Enable TLS
- Encrypt Cluster Communications
ClickHouse allows for both encrypted and unencrypted network communications. To harden network communications, unencrypted ports should be disabled and TLS enabled.
TLS encryption required a Certificate, and whether to use a public or private Certificate Authority (CA) is based on your needs.
- Public CA: Recommended for external services or connections where you can not control where they will be connecting from.
- Private CA: Best used when the ClickHouse services are internal only and you can control where hosts are connecting from.
- Self-signed certificate: Only recommended for testing environments.
Whichever method is used, the following files will be required to enable TLS with CLickHouse:
- Server X509 Certificate: Default name
- Private Key: Default name
- Diffie-Hellman parameters: Default name
The instructions below require the use of
openssl, and was tested against version
Generate the private key
openssl genrsa -aes256 -out server.key 2048
dhparam.pem to create a 4096 encrypted file
openssl dhparam -out dhparam.pem 4096
Create the Certificate Signing Request (CSR) from the generated private key
openssl req -new -key server.key -out server.csr
Store the files
dhparam.pem in a secure location, typically
Create a Private CA
Create the Certificate Private Key
openssl genrsa -aes256 -out internalCA.key 2048
Create the self-signed root certificate from the certificate key
openssl req -new -x509 -days 3650 -key internalCA.key \ -sha256 -extensions v3_ca -out internalCA.crt
Store the Certificate Private Key and the self-signed root certificate in a secure location.
server.csr file with the self-signed root certificate:
openssl x509 -sha256 -req -in server.csr -CA internalCA.crt \ -CAkey internalCA.key -CAcreateserial -out server.crt -days 365
Store the file
Self Signed Certificate
server.key file from previous steps, create the self-signed certificate. Replace
my.host.name with the actual host name used:
openssl req -subj "/CN=my.host.name" -new -key server.key -out server.crt
Store the file
clickhouse-client user that connects to the server with the self-signed certificate will have to allow
invalidCertificateHandler by updating their
clickhouse-client configuration files at
<config> <openSSL> <client> <invalidCertificateHandler> <name>AcceptCertificateHandler</name> </invalidCertificateHandler> </client> </openSSL>
Enable TLS in ClickHouse
To enable TLS and disable unencrypted ports:
/etc/clickhouse-server/config.d files. Comment out unencrypted ports, including
<!-- <http_port>8123</http_port> --> <!-- <tcp_port>9000</tcp_port> -->
Enable encrypted ports. To more details please visit ClickHouse documentation.
Specify the certificate files to use:
<openSSL> <server> <!-- Used for https server AND secure tcp port --> <certificateFile>/etc/clickhouse-server/server.crt</certificateFile> <privateKeyFile>/etc/clickhouse-server/server.key</privateKeyFile> <dhParamsFile>/etc/clickhouse-server/dhparams.pem</dhParamsFile> ... </server> ... </openSSL>
Encrypt Cluster Communications
If your organization runs ClickHouse as a cluster, then cluster-to-cluster communications should be encrypted. This includes distributed queries and inter service replication. To harden cluster communications:
Create a user for distributed queries. For example, if the cluster is contained in a subdomain named
test2, etc. This internal user be set with or without a password:
CREATE USER IF NOT EXISTS internal ON CLUSTER 'my_cluster' IDENTIFIED WITH NO_PASSWORD HOST REGEXP 'test!'
Enable TLS for interservice replication and comment out the unencrypted interserver port by updating the
<!-- <interserver_http_port>9009</interserver_http_port> --> <interserver_https_port>9010</interserver_https_port> -->
Set an the
interserver_http_credentials in the
/etc/clickhouse-server/config.d files, and include the internal username and password:
<interserver_http_credentials> <user>internal</user> <password></password> </interserver_http_credentials>
Enable TLS for distributed queries by editing the file
Clickhouse version 20 and later You have to do it for each shard
<remote_servers> <my_cluster> <shard> <secret>shared secret text</secret> <!-- Update here --> <internal_replication>true</internal_replication> <replica> <host>test1</host> <!-- Update here --> <port>9440</port> <!-- Secure Port --> <secure>1</secure> <!-- Update here, sets port to secure --> </replica> </shard>