Skip to content

Commit

Permalink
Support for setting client certificate and key from bytes (#2646)
Browse files Browse the repository at this point in the history
* Support for setting client certificate and key from bytes

* Rename ssh_client_*_from_bytes to ssl_client_*_from_pem

* doc: clarify client_*_from_pem docs and add examples

* doc: apply missed suggestions from previous commit

* fix: run `cargo fmt`

---------

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
Co-authored-by: Austin Bonander <austin@launchbadge.com>
  • Loading branch information
3 people authored Sep 26, 2023
1 parent 6bec857 commit 3b9a274
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
50 changes: 50 additions & 0 deletions sqlx-mysql/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,31 @@ impl MySqlConnectOptions {
self
}

/// Sets the SSL client certificate as a PEM-encoded byte slice.
///
/// This should be an ASCII-encoded blob that starts with `-----BEGIN CERTIFICATE-----`.
///
/// # Example
/// Note: embedding SSL certificates and keys in the binary is not advised.
/// This is for illustration purposes only.
///
/// ```rust
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
///
/// const CERT: &[u8] = b"\
/// -----BEGIN CERTIFICATE-----
/// <Certificate data here.>
/// -----END CERTIFICATE-----";
///
/// let options = MySqlConnectOptions::new()
/// .ssl_mode(MySqlSslMode::VerifyCa)
/// .ssl_client_cert_from_pem(CERT);
/// ```
pub fn ssl_client_cert_from_pem(mut self, cert: impl AsRef<[u8]>) -> Self {
self.ssl_client_cert = Some(CertificateInput::Inline(cert.as_ref().to_vec()));
self
}

/// Sets the name of a file containing SSL client key.
///
/// # Example
Expand All @@ -230,6 +255,31 @@ impl MySqlConnectOptions {
self
}

/// Sets the SSL client key as a PEM-encoded byte slice.
///
/// This should be an ASCII-encoded blob that starts with `-----BEGIN PRIVATE KEY-----`.
///
/// # Example
/// Note: embedding SSL certificates and keys in the binary is not advised.
/// This is for illustration purposes only.
///
/// ```rust
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
///
/// const KEY: &[u8] = b"\
/// -----BEGIN PRIVATE KEY-----
/// <Private key data here.>
/// -----END PRIVATE KEY-----";
///
/// let options = MySqlConnectOptions::new()
/// .ssl_mode(MySqlSslMode::VerifyCa)
/// .ssl_client_key_from_pem(KEY);
/// ```
pub fn ssl_client_key_from_pem(mut self, key: impl AsRef<[u8]>) -> Self {
self.ssl_client_key = Some(CertificateInput::Inline(key.as_ref().to_vec()));
self
}

/// Sets the capacity of the connection's statement cache in a number of stored
/// distinct statements. Caching is handled using LRU, meaning when the
/// amount of queries hits the defined limit, the oldest statement will get
Expand Down
52 changes: 52 additions & 0 deletions sqlx-postgres/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,32 @@ impl PgConnectOptions {
self
}

/// Sets the SSL client certificate as a PEM-encoded byte slice.
///
/// This should be an ASCII-encoded blob that starts with `-----BEGIN CERTIFICATE-----`.
///
/// # Example
/// Note: embedding SSL certificates and keys in the binary is not advised.
/// This is for illustration purposes only.
///
/// ```rust
/// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions};
///
/// const CERT: &[u8] = b"\
/// -----BEGIN CERTIFICATE-----
/// <Certificate data here.>
/// -----END CERTIFICATE-----";
///
/// let options = PgConnectOptions::new()
/// // Providing a CA certificate with less than VerifyCa is pointless
/// .ssl_mode(PgSslMode::VerifyCa)
/// .ssl_client_cert_from_pem(CERT);
/// ```
pub fn ssl_client_cert_from_pem(mut self, cert: impl AsRef<[u8]>) -> Self {
self.ssl_client_cert = Some(CertificateInput::Inline(cert.as_ref().to_vec()));
self
}

/// Sets the name of a file containing SSL client key.
///
/// # Example
Expand All @@ -360,6 +386,32 @@ impl PgConnectOptions {
self
}

/// Sets the SSL client key as a PEM-encoded byte slice.
///
/// This should be an ASCII-encoded blob that starts with `-----BEGIN PRIVATE KEY-----`.
///
/// # Example
/// Note: embedding SSL certificates and keys in the binary is not advised.
/// This is for illustration purposes only.
///
/// ```rust
/// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions};
///
/// const KEY: &[u8] = b"\
/// -----BEGIN PRIVATE KEY-----
/// <Private key data here.>
/// -----END PRIVATE KEY-----";
///
/// let options = PgConnectOptions::new()
/// // Providing a CA certificate with less than VerifyCa is pointless
/// .ssl_mode(PgSslMode::VerifyCa)
/// .ssl_client_key_from_pem(KEY);
/// ```
pub fn ssl_client_key_from_pem(mut self, key: impl AsRef<[u8]>) -> Self {
self.ssl_client_key = Some(CertificateInput::Inline(key.as_ref().to_vec()));
self
}

/// Sets PEM encoded trusted SSL Certificate Authorities (CA).
///
/// # Example
Expand Down

0 comments on commit 3b9a274

Please sign in to comment.