Skip to content

Commit c7b2ae9

Browse files
committed
Add Database CLI options for TLS encryption for databases
Closes keycloak#46603 Signed-off-by: Pedro Ruivo <1492066+pruivo@users.noreply.github.com>
1 parent 08ae2be commit c7b2ae9

23 files changed

+1449
-19
lines changed

docs/guides/server/db.adoc

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,260 @@ The `transaction-default-timeout` option takes precedence over the unsupported `
439439
If you are using the Quarkus property, migrate to the supported `transaction-default-timeout` option and remove the Quarkus property from your configuration.
440440
====
441441

442+
== Secure the database connection
443+
444+
Encrypting the traffic between {project_name} and the database is recommended for increased security, as it prevents third parties from examining the network traffic.
445+
446+
It is recommended to go a step further and enable certificate verification to prevent more complex attacks such as DNS poisoning and address hijacking, whereby {project_name} could be directed to a different server than intended.
447+
To perform the certificate validation, the database certificate, or the Certificate Authority (CA) certificate, must be added to the {project_name} truststore.
448+
449+
This section provides guidance on how to enable these settings in {project_name} and configure the JDBC driver properly.
450+
Configuring the database server with the private keys and certificates is outside the scope of this section.
451+
Consult your vendor documentation on how to do it.
452+
453+
=== Using {project_name} CLI options
454+
455+
{project_name} provides unified CLI options to configure database TLS settings across different database vendors.
456+
These options simplify the configuration by abstracting vendor-specific JDBC properties and providing a consistent interface.
457+
458+
The following options are available:
459+
460+
`db-tls-mode`::
461+
Sets the TLS mode for the database connection.
462+
Valid values are `disabled` and `verify-server`.
463+
When set to `verify-server`, it enables encryption and server identity verification.
464+
Default: `disabled`
465+
466+
`db-tls-trust-store-file`::
467+
The path to the truststore file containing the database server certificates or Certificate Authority (CA) certificates used to verify the database server's identity.
468+
469+
`db-tls-trust-store-password`::
470+
The password to access the truststore file (if required and supported by the JDBC driver).
471+
472+
`db-tls-trust-store-type`::
473+
The type of the truststore file.
474+
Common values include `JKS` (Java KeyStore) and `PKCS12`.
475+
If not specified, the driver's default truststore type is used.
476+
477+
NOTE: These unified CLI options are the recommended approach for configuring database TLS.
478+
{project_name} automatically translates these options to the appropriate vendor-specific JDBC properties.
479+
480+
The following example demonstrates how to configure database TLS using these options for a PostgreSQL database.
481+
482+
<@kc.start parameters="--db=postgres --db-tls-mode=verify-server --db-tls-trust-store-file=/path/to/certificate.pem"/>
483+
484+
Alternatively, you can use `--truststore-paths` instead of the `db-tls-trust-store-*` options to add your certificate to the Java truststore.
485+
486+
<@kc.start parameters="--db=postgres --db-tls-mode=verify-server --truststore-paths=/path/to/certificate.pem"/>
487+
488+
=== Using the Java truststore
489+
490+
{project_name} allows adding certificates to the Java truststore using the CLI option `--truststore-paths`, which is supported by most drivers.
491+
The recommended settings for each database are provided below.
492+
493+
==== PostgreSQL
494+
// https://jdbc.postgresql.org/documentation/ssl/
495+
PostgreSQL requires the following JDBC properties to be configured.
496+
497+
[%autowidth]
498+
|===
499+
|JDBC Property |Value | Description
500+
501+
m|sslmode
502+
m|verify-full
503+
|Encrypts the network traffic and validates the server identity.
504+
505+
m|sslfactory
506+
m|org.postgresql.ssl.DefaultJavaSSLFactory
507+
|By default, the driver uses `LibPQFactory` which does not support the Java truststore.
508+
509+
|===
510+
511+
<@kc.start parameters="--truststore-paths=/path/to/database_cert.pem --db-url-properties=?sslmode=verify-full&sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory"/>
512+
513+
==== MySQL
514+
// https://dev.mysql.com/doc/connector-j/en/connector-j-reference-using-ssl.html
515+
MySQL requires the following JDBC property to be configured.
516+
517+
[%autowidth]
518+
|===
519+
|JDBC Property |Value | Description
520+
521+
m|sslMode
522+
m|VERIFY_IDENTITY
523+
|Encrypts the network traffic and validates the server identity.
524+
525+
|===
526+
527+
<@kc.start parameters="--truststore-paths=/path/to/database_cert.pem --db-url-properties=?sslMode=VERIFY_IDENTITY"/>
528+
529+
[%autowidth]
530+
==== MariaDB
531+
// https://mariadb.com/docs/connectors/mariadb-connector-j/using-tls-ssl-with-mariadb-java-connector
532+
MariaDB requires the following JDBC property to be configured.
533+
534+
|===
535+
|JDBC Property |Value | Description
536+
537+
m|sslMode
538+
m|verify-full
539+
|Encrypts the network traffic and validates the server identity.
540+
541+
|===
542+
543+
<@kc.start parameters="--truststore-paths=/path/to/database_cert.pem --db-url-properties=?sslMode=verify-full"/>
544+
545+
[%autowidth]
546+
==== Microsoft SQL Server
547+
// https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-with-ssl-encryption?view=sql-server-ver17#configuring-the-connection
548+
Microsoft SQL Server requires the following JDBC properties to be configured.
549+
550+
|===
551+
|JDBC Property |Value | Description
552+
553+
m|encrypt
554+
m|true
555+
|Encrypts the network traffic.
556+
557+
m|trustServerCertificate
558+
m|false
559+
|Validates the server identity.
560+
561+
|===
562+
563+
<@kc.start parameters="--truststore-paths=/path/to/database_cert.pem --db-url-properties=;encrypt=true;trustServerCertificate=false"/>
564+
565+
[%autowidth]
566+
==== Oracle
567+
// https://docs.oracle.com/en/database/oracle/oracle-database/21/jajdb/oracle/jdbc/OracleDriver.html
568+
Oracle by default verifies the server identity when the protocol `tcps` is used.
569+
570+
<@kc.start parameters="--truststore-paths=/path/to/database_cert.pem --db-url=jdbc:oracle:thin:@tcps://host:port/database"/>
571+
572+
=== Using database-specific certificate paths
573+
574+
Alternatively, instead of using the Java truststore with `--truststore-paths`, you can configure each database driver to use its own certificate file or truststore directly through JDBC properties.
575+
This approach is useful when you want to keep database certificates separate from the Java truststore.
576+
577+
==== PostgreSQL
578+
579+
PostgreSQL supports specifying the certificate file directly using the `sslrootcert` property.
580+
581+
[%autowidth]
582+
|===
583+
|JDBC Property |Value | Description
584+
585+
m|sslmode
586+
m|verify-full
587+
|Encrypts the network traffic and validates the server identity.
588+
589+
m|sslrootcert
590+
m|/path/to/cert.pem
591+
|The path to the server's certificate file on the client machine.
592+
593+
|===
594+
595+
<@kc.start parameters="--db-url-properties=?sslmode=verify-full&sslrootcert=/path/to/cert.pem"/>
596+
597+
==== MySQL
598+
599+
MySQL supports specifying the certificate file or truststore through dedicated SSL properties.
600+
601+
[%autowidth]
602+
|===
603+
|JDBC Property |Value | Description
604+
605+
m|sslMode
606+
m|VERIFY_IDENTITY
607+
|Encrypts the network traffic and validates the server identity.
608+
609+
m|trustCertificateKeyStoreUrl
610+
m|file:/path/to/truststore.jks
611+
|The URL to a custom truststore file containing the server certificate.
612+
613+
m|trustCertificateKeyStorePassword
614+
m|password
615+
|The password for the custom truststore (if required).
616+
617+
|===
618+
619+
<@kc.start parameters="--db-url-properties=?sslMode=VERIFY_IDENTITY&trustCertificateKeyStoreUrl=file:/path/to/truststore.jks&trustCertificateKeyStorePassword=password"/>
620+
621+
==== MariaDB
622+
623+
MariaDB supports specifying the certificate file directly using the `serverSslCert` property.
624+
625+
[%autowidth]
626+
|===
627+
|JDBC Property |Value | Description
628+
629+
m|sslMode
630+
m|verify-full
631+
|Encrypts the network traffic and validates the server identity.
632+
633+
m|serverSslCert
634+
m|/path/to/cert.pem
635+
|The path to the server's certificate file on the client machine.
636+
637+
|===
638+
639+
<@kc.start parameters="--db-url-properties=?sslMode=verify-full&serverSslCert=/path/to/cert.pem"/>
640+
641+
==== Microsoft SQL Server
642+
643+
Microsoft SQL Server supports specifying a custom truststore using the `trustStore` and `trustStorePassword` properties.
644+
645+
[%autowidth]
646+
|===
647+
|JDBC Property |Value | Description
648+
649+
m|encrypt
650+
m|true
651+
|Encrypts the network traffic.
652+
653+
m|trustServerCertificate
654+
m|false
655+
|Validates the server identity.
656+
657+
m|trustStore
658+
m|/path/to/truststore.jks
659+
|The path to a custom truststore file containing the server certificate.
660+
661+
m|trustStorePassword
662+
m|password
663+
|The password for the custom truststore.
664+
665+
|===
666+
667+
<@kc.start parameters="--db-url-properties=;encrypt=true;trustServerCertificate=false;trustStore=/path/to/truststore.jks;trustStorePassword=password"/>
668+
669+
==== Oracle
670+
671+
Oracle supports using Oracle Wallet or Java keystores for certificate management.
672+
When using `tcps` protocol, you can specify a custom keystore location.
673+
674+
[%autowidth]
675+
|===
676+
|JDBC Property |Value | Description
677+
678+
m|javax.net.ssl.trustStore
679+
m|/path/to/truststore.jks
680+
|The path to a custom truststore file containing the server certificate.
681+
682+
m|javax.net.ssl.trustStorePassword
683+
m|password
684+
|The password for the custom truststore.
685+
686+
m|javax.net.ssl.trustStoreType
687+
m|JKS
688+
|The type of truststore (JKS, PKCS12, etc.).
689+
690+
|===
691+
692+
<@kc.start parameters="--db-url=jdbc:oracle:thin:@tcps://host:port/database --db-url-properties=?javax.net.ssl.trustStore=/path/to/truststore.jks&javax.net.ssl.trustStorePassword=password&javax.net.ssl.trustStoreType=JKS"/>
693+
694+
Alternatively, you can use Oracle Wallet by configuring the wallet location in the JDBC URL or through Oracle-specific properties.
695+
442696
== Setting JPA provider configuration option for migrationStrategy
443697

444698
To setup the JPA migrationStrategy (manual/update/validate) you should setup JPA provider as follows:

0 commit comments

Comments
 (0)