Skip to content

Commit 3b9a274

Browse files
wyhayaabonander
andauthored
Support for setting client certificate and key from bytes (#2646)
* 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 <[email protected]> Co-authored-by: Austin Bonander <[email protected]>
1 parent 6bec857 commit 3b9a274

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

sqlx-mysql/src/options/mod.rs

+50
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,31 @@ impl MySqlConnectOptions {
215215
self
216216
}
217217

218+
/// Sets the SSL client certificate as a PEM-encoded byte slice.
219+
///
220+
/// This should be an ASCII-encoded blob that starts with `-----BEGIN CERTIFICATE-----`.
221+
///
222+
/// # Example
223+
/// Note: embedding SSL certificates and keys in the binary is not advised.
224+
/// This is for illustration purposes only.
225+
///
226+
/// ```rust
227+
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
228+
///
229+
/// const CERT: &[u8] = b"\
230+
/// -----BEGIN CERTIFICATE-----
231+
/// <Certificate data here.>
232+
/// -----END CERTIFICATE-----";
233+
///
234+
/// let options = MySqlConnectOptions::new()
235+
/// .ssl_mode(MySqlSslMode::VerifyCa)
236+
/// .ssl_client_cert_from_pem(CERT);
237+
/// ```
238+
pub fn ssl_client_cert_from_pem(mut self, cert: impl AsRef<[u8]>) -> Self {
239+
self.ssl_client_cert = Some(CertificateInput::Inline(cert.as_ref().to_vec()));
240+
self
241+
}
242+
218243
/// Sets the name of a file containing SSL client key.
219244
///
220245
/// # Example
@@ -230,6 +255,31 @@ impl MySqlConnectOptions {
230255
self
231256
}
232257

258+
/// Sets the SSL client key as a PEM-encoded byte slice.
259+
///
260+
/// This should be an ASCII-encoded blob that starts with `-----BEGIN PRIVATE KEY-----`.
261+
///
262+
/// # Example
263+
/// Note: embedding SSL certificates and keys in the binary is not advised.
264+
/// This is for illustration purposes only.
265+
///
266+
/// ```rust
267+
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
268+
///
269+
/// const KEY: &[u8] = b"\
270+
/// -----BEGIN PRIVATE KEY-----
271+
/// <Private key data here.>
272+
/// -----END PRIVATE KEY-----";
273+
///
274+
/// let options = MySqlConnectOptions::new()
275+
/// .ssl_mode(MySqlSslMode::VerifyCa)
276+
/// .ssl_client_key_from_pem(KEY);
277+
/// ```
278+
pub fn ssl_client_key_from_pem(mut self, key: impl AsRef<[u8]>) -> Self {
279+
self.ssl_client_key = Some(CertificateInput::Inline(key.as_ref().to_vec()));
280+
self
281+
}
282+
233283
/// Sets the capacity of the connection's statement cache in a number of stored
234284
/// distinct statements. Caching is handled using LRU, meaning when the
235285
/// amount of queries hits the defined limit, the oldest statement will get

sqlx-postgres/src/options/mod.rs

+52
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,32 @@ impl PgConnectOptions {
344344
self
345345
}
346346

347+
/// Sets the SSL client certificate as a PEM-encoded byte slice.
348+
///
349+
/// This should be an ASCII-encoded blob that starts with `-----BEGIN CERTIFICATE-----`.
350+
///
351+
/// # Example
352+
/// Note: embedding SSL certificates and keys in the binary is not advised.
353+
/// This is for illustration purposes only.
354+
///
355+
/// ```rust
356+
/// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions};
357+
///
358+
/// const CERT: &[u8] = b"\
359+
/// -----BEGIN CERTIFICATE-----
360+
/// <Certificate data here.>
361+
/// -----END CERTIFICATE-----";
362+
///
363+
/// let options = PgConnectOptions::new()
364+
/// // Providing a CA certificate with less than VerifyCa is pointless
365+
/// .ssl_mode(PgSslMode::VerifyCa)
366+
/// .ssl_client_cert_from_pem(CERT);
367+
/// ```
368+
pub fn ssl_client_cert_from_pem(mut self, cert: impl AsRef<[u8]>) -> Self {
369+
self.ssl_client_cert = Some(CertificateInput::Inline(cert.as_ref().to_vec()));
370+
self
371+
}
372+
347373
/// Sets the name of a file containing SSL client key.
348374
///
349375
/// # Example
@@ -360,6 +386,32 @@ impl PgConnectOptions {
360386
self
361387
}
362388

389+
/// Sets the SSL client key as a PEM-encoded byte slice.
390+
///
391+
/// This should be an ASCII-encoded blob that starts with `-----BEGIN PRIVATE KEY-----`.
392+
///
393+
/// # Example
394+
/// Note: embedding SSL certificates and keys in the binary is not advised.
395+
/// This is for illustration purposes only.
396+
///
397+
/// ```rust
398+
/// # use sqlx_core::postgres::{PgSslMode, PgConnectOptions};
399+
///
400+
/// const KEY: &[u8] = b"\
401+
/// -----BEGIN PRIVATE KEY-----
402+
/// <Private key data here.>
403+
/// -----END PRIVATE KEY-----";
404+
///
405+
/// let options = PgConnectOptions::new()
406+
/// // Providing a CA certificate with less than VerifyCa is pointless
407+
/// .ssl_mode(PgSslMode::VerifyCa)
408+
/// .ssl_client_key_from_pem(KEY);
409+
/// ```
410+
pub fn ssl_client_key_from_pem(mut self, key: impl AsRef<[u8]>) -> Self {
411+
self.ssl_client_key = Some(CertificateInput::Inline(key.as_ref().to_vec()));
412+
self
413+
}
414+
363415
/// Sets PEM encoded trusted SSL Certificate Authorities (CA).
364416
///
365417
/// # Example

0 commit comments

Comments
 (0)