TLS Configuration
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Connections between Dgraph database and its clients can be secured using TLS. In
addition, Dgraph can now secure gRPC communications between Dgraph Alpha and
Dgraph Zero server nodes using mutual TLS (mTLS). Dgraph can now also secure
communications over the Dgraph Zero gRPC-external-private
port used by
Dgraph’s Live Loader and Bulk Loader clients. To learn more about the HTTP and
gRPC ports used by Dgraph Alpha and Dgraph Zero, see
Ports Usage. Password-protected private keys are not
supported.
To further improve TLS security, only TLS v1.2 cypher suites that use 128-bit or greater RSA or AES encryption are supported.
If you’re generating encrypted private keys with openssl
, be sure to specify
the encryption algorithm explicitly (like -aes256
). This forces openssl
to
include DEK-Info
header in private key, which is required to decrypt the key
by Dgraph. When default encryption is used, openssl
doesn’t write that
header and key can’t be decrypted.
Dgraph certificate management tool
This section refers to the dgraph cert
command which was introduced in
v1.0.9. For previous releases, see the previous TLS configuration
documentation.
The dgraph cert
program creates and manages CA-signed certificates and private
keys using a generated Dgraph Root CA. There are three types of certificate/key
pairs:
- Root CA certificate/key pair: This is used to sign and verify node and client certificates. If the root CA certificate is changed then you must regenerate all certificates, and this certificate must be accessible to the Alpha nodes.
- Node certificate/key pair: This is shared by the Dgraph Alpha nodes and used for accepting TLS connections.
- Client certificate/key pair: This is used by the clients (such as Live Loader or Ratel) to communicate with Dgraph Alpha server nodes where client authentication with mTLS is required.
The default location where the cert command stores certificates (and keys) is
tls
under the Dgraph working directory. The default directory path can be
overridden using the --dir
option. For example:
File naming conventions
The following file naming conventions are used by Dgraph for proper TLS setup.
File name | Description | Use |
---|---|---|
ca.crt | Dgraph Root CA certificate | Verify all certificates |
ca.key | Dgraph CA private key | Validate CA certificate |
node.crt | Dgraph node certificate | Shared by all nodes for accepting TLS connections |
node.key | Dgraph node private key | Validate node certificate |
client.name.crt | Dgraph client certificate | Authenticate a client name |
client.name.key | Dgraph client private key | Validate name client certificate |
For client authentication, each client must have their own certificate and key. These are then used to connect to the Dgraph server nodes.
The node certificate node.crt
can support multiple node names using multiple
host names and/or IP address. Just separate the names with commas when
generating the certificate.
You must delete the old node cert and key before you can generate a new pair.
When using host names for node certificates, including localhost
, your
clients must connect to the matching host name — such as localhost
not
127.0.0.1
. If you need to use IP addresses, then add them to the node
certificate.
Certificate inspection
The command dgraph cert ls
lists all certificates and keys in the --dir
directory (default dgraph-tls
), along with details to inspect and validate
cert/key pairs.
Example of command output:
Important points:
- The cert/key pairs should always have matching SHA-256 digests. Otherwise, the
cert must be regenerated. If the Root CA pair differ, all cert/key must be
regenerated; the flag
--force
can help. - All certificates must pass Dgraph CA verification.
- All key files should have the least access permissions, especially the
ca.key
, but be readable. - Key files won’t be overwritten if they have limited access, even with
--force
. - Node certificates are only valid for the hosts listed.
- Client certificates are only valid for the named client/user.
TLS options
Starting in release v21.03, pre-existing TLS configuration options have been
replaced by the --tls
superflag and its
options. The following --tls
configuration options are available for Dgraph
Alpha and Dgraph Zero nodes:
ca-cert <path>
- Path and filename of the Dgraph Root CA (for example,ca.crt
)server-cert <path>
- Path and filename of the node certificate (for example,node.crt
)server-key <path>
- Path and filename of the node certificate private key (for example,node.key
)use-system-ca
- Include System CA with Dgraph Root CA.client-auth-type <string>
- TLS client authentication used to validate client connections from external ports. To learn more, see Client Authentication Options.
Dgraph now allows you to specify the path and filename of the CA root
certificate, the node certificate, and the node certificate private key. So,
these files don’t need to have specific filenames or exist in the same
directory, as in previous Dgraph versions that used the --tls_dir
flag.
You can configure Dgraph Live Loader with the following --tls
options:
ca-cert <path>
- Dgraph root CA, such as./tls/ca.crt
use-system-ca
- Include System CA with Dgraph Root CA.client-cert
- User cert file provided by the client to Alphaclient-key
- User private key file provided by the client to Alphaserver-name <string>
- Server name, used for validating the server’s TLS host name.
Using TLS with only external ports encrypted
To encrypt communication between Dgraph server nodes and clients over external ports, you can configure certificates and run Dgraph Alpha and Dgraph Zero using the following commands:
Dgraph Alpha:
Dgraph Zero:
You can then run Dgraph Live Loader on a Dgraph Alpha node using the following command:
Using TLS with internal and external ports encrypted
If you require client authentication (mutual TLS, or mTLS), you can configure certificates and run Dgraph Alpha and Dgraph Zero with settings that encrypt both internal ports (those used within the cluster) as well as external ports (those used by clients that connect to the cluster, including Bulk Loader and Live Loader).
The following example shows how to encrypt both internal and external ports:
Dgraph Alpha:
Dgraph Zero:
You can then run Dgraph Live Loader using the following:
Client authentication options
The server always requests client authentication. There are four different
values for the client-auth-type
option that change the security policy of the
client certificate.
Value | Client Cert/Key | Client Certificate Verified |
---|---|---|
REQUEST | optional | Client certificate isn’t VERIFIED if provided. (least secure) |
REQUIREANY | required | Client certificate is never VERIFIED |
VERIFYIFGIVEN | optional | Client certificate is VERIFIED if provided (default) |
REQUIREANDVERIFY | required | Client certificate is always VERIFIED (most secure) |
REQUIREANDVERIFY
is the most secure but also the most difficult to configure
for clients. When using this value, the value of server-name
is matched
against the certificate SANs values and the connection host.
If mTLS is enabled using internal-port=true
, internal ports (by default 5080
and 7080) use the REQUIREANDVERIFY
setting. Unless otherwise configured,
external ports (by default 9080, 8080, and 6080) use the VERIFYIFGIVEN
setting. Changing the client-auth-type
option to another setting only
affects client authentication on external ports.
Using Ratel with client authentication
Ratel UI (and any other JavaScript clients built on top of dgraph-js-http
)
connect to Dgraph servers via HTTP, when TLS is enabled servers begin to expect
HTTPS requests only.
If you haven’t already created the CA certificate and the node certificate for Alpha servers from the earlier instructions (see Dgraph Certificate Management Tool), the first step would be to generate these certificates, it can be done by the following command:
If Dgraph Alpha’s client-auth-type
option is set to REQUEST
or
VERIFYIFGIVEN
(default), then client certificate isn’t mandatory. The steps
after generating CA/node certificate are as follows:
Step 1: Install Dgraph root CA into system CA
Linux
Step 2: Install Dgraph root CA into web browsers trusted CA list
Firefox
- Choose Preferences -> Privacy & Security -> View Certificates -> Authorities
- Click on Import and import the
ca.crt
Chrome
- Choose Settings -> Privacy and Security -> Security -> Manage Certificates -> Authorities
- Click on Import and import the
ca.crt
Step 3. Point Ratel to the https://
endpoint of Alpha server
- Change the Dgraph Alpha server address to
https://
instead ofhttp://
, for examplehttps://localhost:8080
.
For REQUIREANY
and REQUIREANDVERIFY
as client-auth-type
option, you need
to follow the preceding steps and install client certificate on your browser:
-
Generate a client certificate:
dgraph cert -c laptopuser
. -
Convert it to a
.p12
file:Use any password you like for export, it’s used to encrypt the p12 file.
-
Import the client certificate to your browser. It can be done in Chrome as follows:
- Choose Settings -> Privacy and Security -> Security -> Manage Certificates -> Your Certificates
- Click on Import and import the
laptopuser.p12
.
Mutual TLS may not work in Firefox because Firefox is unable to send privately signed client certificates, this issue is filed here.
Next time you use Ratel to connect to an Alpha with Client authentication enabled the browser prompts you for a client certificate to use. Select the client’s certificate you’ve imported in the preceding step and queries/mutations should succeed.
Using Curl with Client authentication
When TLS is enabled, curl
requests to Dgraph need some specific options to
work. For instance (for changing draining mode):
If you are using curl
with
Client Authentication set to REQUIREANY
or
REQUIREANDVERIFY
, you need to provide the client certificate and private key.
For instance (for an export request):
Refer to the curl
documentation for further information on its TLS options.
Access data using a client
Some examples of connecting via a Client when TLS is in use can be found below:
Troubleshooting Ratel’s Client authentication
If you are getting errors in Ratel when TLS is enabled, try opening your Dgraph Alpha URL as a web page.
Assuming you are running Dgraph on your local machine, opening
https://localhost:8080/
in the browser should produce a message
Dgraph browser is available for running separately using the dgraph-ratel binary
.
In case you are getting a connection error, try not passing the
client-auth-type
flag when starting an Alpha. If you are still getting an
error, check that your host name is correct and the port is open. Then, make
sure that “Dgraph Root CA” certificate is installed and trusted correctly.
After that, if things work without passing client-auth-type
but stop working
when REQUIREANY
and REQUIREANDVERIFY
are set, make sure the .p12
file is
installed correctly.
Was this page helpful?