Skip to content

Commit fc66c8f

Browse files
committed
give examples of connection strings enabling SSL
1 parent 330b1e2 commit fc66c8f

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

sqlx-core/src/mysql/connection.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,25 @@ const COLLATE_UTF8MB4_UNICODE_CI: u8 = 224;
3535
/// rather than as program arguments.
3636
///
3737
/// The same options for `--ssl-mode` are supported as the `ssl-mode` query parameter:
38-
/// https://dev.mysql.com/doc/refman/8.0/en/connection-options.html#option_general_ssl-mode
38+
/// <https://dev.mysql.com/doc/refman/8.0/en/connection-options.html#option_general_ssl-mode>
39+
///
40+
/// ```text
41+
/// mysql://<user>[:<password>]@<host>[:<port>]/<database>[?ssl-mode=<ssl-mode>[&ssl-ca=<path>]]
42+
/// ```
43+
/// where
44+
/// ```text
45+
/// ssl-mode = DISABLED | PREFERRED | REQUIRED | VERIFY_CA | VERIFY_IDENTITY
46+
/// path = percent (URL) encoded path on the local machine
47+
/// ```
3948
///
4049
/// If the `tls` feature is not enabled, `ssl-mode=DISABLED` and `ssl-mode=PREFERRED` are no-ops and
4150
/// `ssl-mode=REQUIRED`, `ssl-mode=VERIFY_CA` and `ssl-mode=VERIFY_IDENTITY` are forbidden
4251
/// (attempting to connect with these will return an error).
4352
///
4453
/// If the `tls` feature is enabled, an upgrade to TLS is attempted on every connection by default
4554
/// (equivalent to `ssl-mode=PREFERRED`). If the server does not support TLS (because `--ssl=0` was
46-
/// passed or an invalid certificate or key was used,
47-
/// https://dev.mysql.com/doc/refman/8.0/en/using-encrypted-connections.html)
55+
/// passed to the server or an invalid certificate or key was used:
56+
/// <https://dev.mysql.com/doc/refman/8.0/en/using-encrypted-connections.html>)
4857
/// then it falls back to an unsecured connection and logs a warning.
4958
///
5059
/// Add `ssl-mode=REQUIRED` to your connection string to emit an error if the TLS upgrade fails.
@@ -56,6 +65,17 @@ const COLLATE_UTF8MB4_UNICODE_CI: u8 = 224;
5665
/// but is instead expected to be specified as a local path with the `ssl-ca` query parameter
5766
/// (percent-encoded so the URL remains valid).
5867
///
68+
/// If you're running MySQL locally it might look something like this (for `VERIFY_CA`):
69+
/// ```text
70+
/// mysql://root:password@localhost/my_database?ssl-mode=VERIFY_CA&ssl-ca=%2Fvar%2Flib%2Fmysql%2Fca.pem
71+
/// ```
72+
///
73+
/// `%2F` is the percent-encoding for forward slash (`/`). In the example we give `/var/lib/mysql/ca.pem`
74+
/// as the CA certificate path, which is generated by the MySQL server automatically if
75+
/// no certificate is manually specified. Note that the path may vary based on the default `my.cnf`
76+
/// packaged with MySQL for your Linux distribution. Also note that unlike MySQL, MariaDB does *not*
77+
/// generate certificates automatically and they must always be passed in to enable TLS.
78+
///
5979
/// If `ssl-ca` is not specified or the file cannot be read, then an error is returned.
6080
/// `ssl-ca` implies `ssl-mode=VERIFY_CA` so you only actually need to specify the former
6181
/// but you may prefer having both to be more explicit.

sqlx-core/src/postgres/connection.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,33 @@ use crate::Result;
2626
///
2727
/// ### TLS Support (requires `tls` feature)
2828
/// This connection type supports the same `sslmode` query parameter that `libpq` does in
29-
/// connection strings: https://www.postgresql.org/docs/12/libpq-ssl.html
29+
/// connection strings: <https://www.postgresql.org/docs/12/libpq-ssl.html>
30+
///
31+
/// ```text
32+
/// postgresql://<user>[:<password>]@<host>[:<port>]/<database>[?sslmode=<ssl-mode>[&sslcrootcert=<path>]]
33+
/// ```
34+
/// where
35+
/// ```text
36+
/// ssl-mode = disable | allow | prefer | require | verify-ca | verify-full
37+
/// path = percent (URL) encoded path on the local machine
38+
/// ```
3039
///
3140
/// If the `tls` feature is not enabled, `disable`, `allow` and `prefer` are no-ops and `require`,
3241
/// `verify-ca` and `verify-full` are forbidden (attempting to connect with these will return
3342
/// an error).
3443
///
3544
/// If the `tls` feature is enabled, an upgrade to TLS is attempted on every connection by default
3645
/// (equivalent to `sslmode=prefer`). If the server does not support TLS (because it was not
37-
/// started with a valid certificate and key, see https://www.postgresql.org/docs/12/ssl-tcp.html)
46+
/// started with a valid certificate and key, see <https://www.postgresql.org/docs/12/ssl-tcp.html>)
3847
/// then it falls back to an unsecured connection and logs a warning.
3948
///
4049
/// Add `sslmode=require` to your connection string to emit an error if the TLS upgrade fails.
4150
///
51+
/// If you're running Postgres locally, your connection string might look like this:
52+
/// ```text
53+
/// postgresql://root:password@localhost/my_database?sslmode=require
54+
/// ```
55+
///
4256
/// However, like with `libpq` the server certificate is **not** checked for validity by default.
4357
///
4458
/// Specifying `sslmode=verify-ca` will cause the TLS upgrade to verify the server's SSL
@@ -57,7 +71,7 @@ use crate::Result;
5771
/// * `$HOME/.postgresql/root.crt` on POSIX systems
5872
/// * `%APPDATA%\postgresql\root.crt` on Windows
5973
///
60-
/// These locations are documented here: https://www.postgresql.org/docs/12/libpq-ssl.html#LIBQ-SSL-CERTIFICATES
74+
/// These locations are documented here: <https://www.postgresql.org/docs/12/libpq-ssl.html#LIBQ-SSL-CERTIFICATES>
6175
/// If the root certificate cannot be found by any of these means then the TLS upgrade will fail.
6276
///
6377
/// If `sslmode=verify-full` is specified, in addition to checking the certificate as with

0 commit comments

Comments
 (0)