Skip to content

Commit c1a17ca

Browse files
committed
Add client cert and key to MySQL connector
1 parent 73678ca commit c1a17ca

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

sqlx-core/src/mysql/connection/tls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ async fn upgrade(stream: &mut MySqlStream, options: &MySqlConnectOptions) -> Res
5353
accept_invalid_certs,
5454
accept_invalid_host_names,
5555
options.ssl_ca.as_ref(),
56-
None,
57-
None,
56+
options.ssl_client_cert.as_ref(),
57+
options.ssl_client_key.as_ref(),
5858
)
5959
.await?;
6060

sqlx-core/src/mysql/options/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ pub struct MySqlConnectOptions {
6161
pub(crate) database: Option<String>,
6262
pub(crate) ssl_mode: MySqlSslMode,
6363
pub(crate) ssl_ca: Option<CertificateInput>,
64+
pub(crate) ssl_client_cert: Option<CertificateInput>,
65+
pub(crate) ssl_client_key: Option<CertificateInput>,
6466
pub(crate) statement_cache_capacity: usize,
6567
pub(crate) charset: String,
6668
pub(crate) collation: Option<String>,
@@ -87,6 +89,8 @@ impl MySqlConnectOptions {
8789
collation: None,
8890
ssl_mode: MySqlSslMode::Preferred,
8991
ssl_ca: None,
92+
ssl_client_cert: None,
93+
ssl_client_key: None,
9094
statement_cache_capacity: 100,
9195
log_settings: Default::default(),
9296
}
@@ -184,6 +188,36 @@ impl MySqlConnectOptions {
184188
self
185189
}
186190

191+
/// Sets the name of a file containing SSL client certificate.
192+
///
193+
/// # Example
194+
///
195+
/// ```rust
196+
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
197+
/// let options = MySqlConnectOptions::new()
198+
/// .ssl_mode(MySqlSslMode::VerifyCa)
199+
/// .ssl_client_cert("path/to/client.crt");
200+
/// ```
201+
pub fn ssl_client_cert(mut self, cert: impl AsRef<Path>) -> Self {
202+
self.ssl_client_cert = Some(CertificateInput::File(cert.as_ref().to_path_buf()));
203+
self
204+
}
205+
206+
/// Sets the name of a file containing SSL client key.
207+
///
208+
/// # Example
209+
///
210+
/// ```rust
211+
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
212+
/// let options = MySqlConnectOptions::new()
213+
/// .ssl_mode(MySqlSslMode::VerifyCa)
214+
/// .ssl_client_key("path/to/client.key");
215+
/// ```
216+
pub fn ssl_client_key(mut self, key: impl AsRef<Path>) -> Self {
217+
self.ssl_client_key = Some(CertificateInput::File(key.as_ref().to_path_buf()));
218+
self
219+
}
220+
187221
/// Sets the capacity of the connection's statement cache in a number of stored
188222
/// distinct statements. Caching is handled using LRU, meaning when the
189223
/// amount of queries hits the defined limit, the oldest statement will get

sqlx-core/src/mysql/options/parse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ impl FromStr for MySqlConnectOptions {
5959
options = options.collation(&*value);
6060
}
6161

62+
"sslcert" => options = options.ssl_client_cert(&*value),
63+
64+
"sslkey" => options = options.ssl_client_key(&*value),
65+
6266
"statement-cache-capacity" => {
6367
options =
6468
options.statement_cache_capacity(value.parse().map_err(Error::config)?);

sqlx-core/src/postgres/options/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ impl PgConnectOptions {
331331
/// .ssl_mode(PgSslMode::VerifyCa)
332332
/// .ssl_client_key("./client.key");
333333
/// ```
334-
pub fn ssl_client_key(mut self, cert: impl AsRef<Path>) -> Self {
335-
self.ssl_client_key = Some(CertificateInput::File(cert.as_ref().to_path_buf()));
334+
pub fn ssl_client_key(mut self, key: impl AsRef<Path>) -> Self {
335+
self.ssl_client_key = Some(CertificateInput::File(key.as_ref().to_path_buf()));
336336
self
337337
}
338338

0 commit comments

Comments
 (0)