fix import errors and run rustfmt

This commit is contained in:
Ryan Leckey 2020-01-14 10:35:50 -08:00
parent cb1dbff544
commit 0a5b527d79
9 changed files with 60 additions and 52 deletions

View File

@ -243,4 +243,3 @@ macro_rules! impl_fmt_error {
}
};
}

View File

@ -12,7 +12,7 @@ pub use self::{
buf_mut::BufMut,
buf_stream::BufStream,
byte_str::ByteStr,
tls::MaybeTlsStream
tls::MaybeTlsStream,
};
#[cfg(test)]

View File

@ -29,6 +29,7 @@ impl MaybeTlsStream {
})
}
#[allow(dead_code)]
pub fn is_tls(&self) -> bool {
match self.inner {
Inner::NotTls(_) => false,

View File

@ -1,7 +1,7 @@
use std::convert::TryInto;
use std::io;
use async_std::net::{Shutdown, TcpStream};
use async_std::net::Shutdown;
use byteorder::{ByteOrder, LittleEndian};
use futures_core::future::BoxFuture;
use sha1::Sha1;
@ -17,8 +17,6 @@ use crate::mysql::protocol::{
use crate::mysql::rsa;
use crate::mysql::util::xor_eq;
use crate::url::Url;
use std::borrow::Cow;
use std::path::Path;
// Size before a packet is split
const MAX_PACKET_SIZE: u32 = 1024;
@ -347,7 +345,7 @@ impl MySqlConnection {
clear_text.push_str(password);
clear_text.push('\0');
return Ok(clear_text.into_boxed_bytes());
return Ok(clear_text.into_bytes().into_boxed_slice());
}
// client sends a public key request

View File

@ -39,9 +39,9 @@ mod com_stmt_prepare_ok;
mod eof;
mod err;
mod handshake_response;
mod ssl_request;
mod ok;
mod row;
mod ssl_request;
pub use auth_switch::AuthSwitch;
pub use column_count::ColumnCount;
@ -51,5 +51,5 @@ pub use eof::EofPacket;
pub use err::ErrPacket;
pub use handshake_response::HandshakeResponse;
pub use ok::OkPacket;
pub use ssl_request::SslRequest;
pub use row::Row;
pub use ssl_request::SslRequest;

View File

@ -15,7 +15,10 @@ pub struct SslRequest {
impl Encode for SslRequest {
fn encode(&self, buf: &mut Vec<u8>, capabilities: Capabilities) {
// SSL must be set or else it makes no sense to ask for an upgrade
assert!(capabilities.contains(Capabilities::SSL), "SSL bit must be set for Capabilities");
assert!(
capabilities.contains(Capabilities::SSL),
"SSL bit must be set for Capabilities"
);
// client capabilities : int<4>
buf.put_u32::<LittleEndian>(capabilities.bits() as u32);

View File

@ -1,8 +1,5 @@
use std::convert::TryInto;
use std::path::Path;
use async_native_tls::Certificate;
use async_std::fs;
use async_std::net::Shutdown;
use byteorder::NetworkEndian;
use futures_core::future::BoxFuture;
@ -13,13 +10,13 @@ use sha2::{Digest, Sha256};
use crate::cache::StatementCache;
use crate::connection::Connection;
use crate::io::{Buf, BufStream, MaybeTlsStream};
use crate::postgres::PgError;
use crate::postgres::protocol::{
self, Authentication, Decode, Encode, hi, Message, SaslInitialResponse, SaslResponse,
self, hi, Authentication, Decode, Encode, Message, SaslInitialResponse, SaslResponse,
StatementId,
};
use crate::Result;
use crate::postgres::PgError;
use crate::url::Url;
use crate::Result;
/// An asynchronous connection to a [Postgres] database.
///
@ -88,9 +85,13 @@ pub struct PgConnection {
impl PgConnection {
#[cfg(feature = "tls")]
async fn try_ssl(&mut self, url: &Url, invalid_certs: bool, invalid_hostnames: bool) -> crate::Result<bool> {
use async_native_tls::{TlsConnector, Certificate};
use std::env;
async fn try_ssl(
&mut self,
url: &Url,
invalid_certs: bool,
invalid_hostnames: bool,
) -> crate::Result<bool> {
use async_native_tls::TlsConnector;
protocol::SslRequest::encode(self.stream.buffer_mut());
@ -99,8 +100,10 @@ impl PgConnection {
match self.stream.peek(1).await? {
Some(b"N") => return Ok(false),
Some(b"S") => (),
Some(other) => return Err(tls_err!("unexpected single-byte response: 0x{:02X}", other[0]).into()),
None => return Err(tls_err!("server unexpectedly closed connection").into())
Some(other) => {
return Err(tls_err!("unexpected single-byte response: 0x{:02X}", other[0]).into())
}
None => return Err(tls_err!("server unexpectedly closed connection").into()),
}
let mut connector = TlsConnector::new()
@ -112,7 +115,7 @@ impl PgConnection {
Ok(cert) => {
connector = connector.add_root_certificate(cert);
}
Err(e) => log::warn!("failed to read Postgres root certificate: {}", e)
Err(e) => log::warn!("failed to read Postgres root certificate: {}", e),
}
}
@ -162,7 +165,7 @@ impl PgConnection {
protocol::PasswordMessage::ClearText(
url.password().unwrap_or_default(),
)
.encode(self.stream.buffer_mut());
.encode(self.stream.buffer_mut());
self.stream.flush().await?;
}
@ -173,7 +176,7 @@ impl PgConnection {
user: username,
salt,
}
.encode(self.stream.buffer_mut());
.encode(self.stream.buffer_mut());
self.stream.flush().await?;
}
@ -216,7 +219,7 @@ impl PgConnection {
"requires unimplemented authentication method: {:?}",
auth
)
.into());
.into());
}
}
}
@ -338,27 +341,37 @@ impl PgConnection {
"disable" | "allow" => (),
#[cfg(feature = "tls")]
"prefer" => if !self_.try_ssl(&url, true, true).await? {
log::warn!("server does not support TLS, falling back to unsecured connection")
},
"prefer" => {
if !self_.try_ssl(&url, true, true).await? {
log::warn!("server does not support TLS, falling back to unsecured connection")
}
}
#[cfg(not(feature = "tls"))]
"prefer" => log::info!("compiled without TLS, skipping upgrade"),
#[cfg(feature = "tls")]
"require" | "verify-ca" | "verify-full" => if !self_.try_ssl(
&url,
ssl_mode == "require", // false for both verify-ca and verify-full
ssl_mode != "verify-full" // false for only verify-full
).await? {
return Err(tls_err!("Postgres server does not support TLS").into())
"require" | "verify-ca" | "verify-full" => {
if !self_
.try_ssl(
&url,
ssl_mode == "require", // false for both verify-ca and verify-full
ssl_mode != "verify-full", // false for only verify-full
)
.await?
{
return Err(tls_err!("Postgres server does not support TLS").into());
}
}
#[cfg(not(feature = "tls"))]
"require" | "verify-ca" | "verify-full" => return Err(
tls_err!("sslmode {:?} unsupported; SQLx was compiled without `tls` feature",
ssl_mode).into()
),
"require" | "verify-ca" | "verify-full" => {
return Err(tls_err!(
"sslmode {:?} unsupported; SQLx was compiled without `tls` feature",
ssl_mode
)
.into())
}
_ => return Err(tls_err!("unknown `sslmode` value: {:?}", ssl_mode).into()),
}
@ -370,9 +383,9 @@ impl PgConnection {
impl Connection for PgConnection {
fn open<T>(url: T) -> BoxFuture<'static, Result<Self>>
where
T: TryInto<Url, Error=crate::Error>,
Self: Sized,
where
T: TryInto<Url, Error = crate::Error>,
Self: Sized,
{
Box::pin(PgConnection::open(url.try_into()))
}
@ -388,7 +401,7 @@ async fn read_root_certificate(url: &Url) -> crate::Result<async_native_tls::Cer
let root_cert_path = if let Some(path) = url.get_param("sslrootcert") {
path.into()
} else if let Ok(cert_path) = env::var("PGSSLROOTCERT"){
} else if let Ok(cert_path) = env::var("PGSSLROOTCERT") {
cert_path
} else if cfg!(windows) {
let appdata = env::var("APPDATA").map_err(|_| tls_err!("APPDATA not set"))?;

View File

@ -8,9 +8,7 @@ impl SslRequest {
// packet length: 8 bytes including self
buf.put_u32::<NetworkEndian>(8);
// 1234 in high 16 bits, 5679 in low 16
buf.put_u32::<NetworkEndian>(
(1234 << 16) | 5679,
);
buf.put_u32::<NetworkEndian>((1234 << 16) | 5679);
}
}

View File

@ -1,5 +1,5 @@
use std::convert::{TryFrom, TryInto};
use std::borrow::Cow;
use std::convert::{TryFrom, TryInto};
pub struct Url(url::Url);
@ -67,12 +67,8 @@ impl Url {
}
pub fn get_param(&self, key: &str) -> Option<Cow<str>> {
self.0.query_pairs().find_map(|(key_, val)| {
if key == key_ {
Some(val)
} else {
None
}
})
self.0
.query_pairs()
.find_map(|(key_, val)| if key == key_ { Some(val) } else { None })
}
}