Skip to content

Commit 435ff99

Browse files
committed
Merge branch 'mz-0.7.2'
2 parents 0f57ded + 5ae9ff0 commit 435ff99

File tree

13 files changed

+1699
-810
lines changed

13 files changed

+1699
-810
lines changed

README.md

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
1-
# Rust-Postgres
1+
# Materialize fork of Rust-Postgres
22

3-
PostgreSQL support for Rust.
3+
This repo serves as a staging area in order to develop and use features of the
4+
rust postgtres client before they are accepted upstream.
45

5-
## postgres [![Latest Version](https://img.shields.io/crates/v/postgres.svg)](https://crates.io/crates/postgres)
6+
Since development on this repo and the upstream one can happen in parallel this
7+
repo adops a branching strategy that keeps both in sync and keeps a tidy
8+
history. Importantly, the release branches are **never** forced-pushed so that
9+
older versions of materialize are always buildable.
610

7-
[Documentation](https://docs.rs/postgres)
11+
## Branching strategy
812

9-
A native, synchronous PostgreSQL client.
13+
For every upstream release a local `mz-{version}` branch is created. The latest
14+
such branch should be made the default branch of this repo in the Github
15+
settings.
1016

11-
## tokio-postgres [![Latest Version](https://img.shields.io/crates/v/tokio-postgres.svg)](https://crates.io/crates/tokio-postgres)
17+
Whenever a PR is opened it should targed the current release branch (it should
18+
be picked automatically if its set as default on Github).
1219

13-
[Documentation](https://docs.rs/tokio-postgres)
14-
15-
A native, asynchronous PostgreSQL client.
16-
17-
## postgres-types [![Latest Version](https://img.shields.io/crates/v/postgres-types.svg)](https://crates.io/crates/postgres-types)
18-
19-
[Documentation](https://docs.rs/postgres-types)
20-
21-
Conversions between Rust and Postgres types.
22-
23-
## postgres-native-tls [![Latest Version](https://img.shields.io/crates/v/postgres-native-tls.svg)](https://crates.io/crates/postgres-native-tls)
24-
25-
[Documentation](https://docs.rs/postgres-native-tls)
26-
27-
TLS support for postgres and tokio-postgres via native-tls.
28-
29-
## postgres-openssl [![Latest Version](https://img.shields.io/crates/v/postgres-openssl.svg)](https://crates.io/crates/postgres-openssl)
30-
31-
[Documentation](https://docs.rs/postgres-openssl)
32-
33-
TLS support for postgres and tokio-postgres via openssl.
34-
35-
# Running test suite
36-
37-
The test suite requires postgres to be running in the correct configuration. The easiest way to do this is with docker:
38-
39-
1. Install `docker` and `docker-compose`.
40-
1. On ubuntu: `sudo apt install docker.io docker-compose`.
41-
1. Make sure your user has permissions for docker.
42-
1. On ubuntu: ``sudo usermod -aG docker $USER``
43-
1. Change to top-level directory of `rust-postgres` repo.
44-
1. Run `docker-compose up -d`.
45-
1. Run `cargo test`.
46-
1. Run `docker-compose stop`.
20+
Whenever a new version is created upstream a new `mz-{version}` branch on this
21+
repo is created, initially pointing at the release commit of the upstream repo.
22+
Then, all the fork-specific work is rebased on top of it. This process gives
23+
the opportunity to prune PRs that have successfully made it to the upstream
24+
repo and keep a clean per-version history.

codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = ["Steven Fackler <sfackler@gmail.com>"]
55

66
[dependencies]
7-
phf_codegen = "0.8"
7+
phf_codegen = "0.10"
88
regex = "1.0"
99
marksman_escape = "0.1"
1010
linked-hash-map = "0.5"

codegen/src/sqlstate.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use linked_hash_map::LinkedHashMap;
2-
use phf_codegen;
32
use std::fs::File;
43
use std::io::{BufWriter, Write};
54

@@ -11,7 +10,9 @@ pub fn build() {
1110
let codes = parse_codes();
1211

1312
make_type(&mut file);
13+
make_code(&codes, &mut file);
1414
make_consts(&codes, &mut file);
15+
make_inner(&codes, &mut file);
1516
make_map(&codes, &mut file);
1617
}
1718

@@ -38,26 +39,51 @@ fn make_type(file: &mut BufWriter<File>) {
3839
write!(
3940
file,
4041
"// Autogenerated file - DO NOT EDIT
41-
use std::borrow::Cow;
4242
4343
/// A SQLSTATE error code
4444
#[derive(PartialEq, Eq, Clone, Debug)]
45-
pub struct SqlState(Cow<'static, str>);
45+
pub struct SqlState(Inner);
4646
4747
impl SqlState {{
4848
/// Creates a `SqlState` from its error code.
4949
pub fn from_code(s: &str) -> SqlState {{
5050
match SQLSTATE_MAP.get(s) {{
5151
Some(state) => state.clone(),
52-
None => SqlState(Cow::Owned(s.to_string())),
52+
None => SqlState(Inner::Other(s.into())),
5353
}}
5454
}}
55+
"
56+
)
57+
.unwrap();
58+
}
5559

60+
fn make_code(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
61+
write!(
62+
file,
63+
r#"
5664
/// Returns the error code corresponding to the `SqlState`.
5765
pub fn code(&self) -> &str {{
58-
&self.0
66+
match &self.0 {{"#,
67+
)
68+
.unwrap();
69+
70+
for code in codes.keys() {
71+
write!(
72+
file,
73+
r#"
74+
Inner::E{code} => "{code}","#,
75+
code = code,
76+
)
77+
.unwrap();
78+
}
79+
80+
write!(
81+
file,
82+
r#"
83+
Inner::Other(code) => code,
84+
}}
5985
}}
60-
"
86+
"#
6187
)
6288
.unwrap();
6389
}
@@ -69,7 +95,7 @@ fn make_consts(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<
6995
file,
7096
r#"
7197
/// {code}
72-
pub const {name}: SqlState = SqlState(Cow::Borrowed("{code}"));
98+
pub const {name}: SqlState = SqlState(Inner::E{code});
7399
"#,
74100
name = name,
75101
code = code,
@@ -81,6 +107,35 @@ fn make_consts(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<
81107
write!(file, "}}").unwrap();
82108
}
83109

110+
fn make_inner(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
111+
write!(
112+
file,
113+
r#"
114+
115+
#[derive(PartialEq, Eq, Clone, Debug)]
116+
#[allow(clippy::upper_case_acronyms)]
117+
enum Inner {{"#,
118+
)
119+
.unwrap();
120+
for code in codes.keys() {
121+
write!(
122+
file,
123+
r#"
124+
E{},"#,
125+
code,
126+
)
127+
.unwrap();
128+
}
129+
write!(
130+
file,
131+
r#"
132+
Other(Box<str>),
133+
}}
134+
"#,
135+
)
136+
.unwrap();
137+
}
138+
84139
fn make_map(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
85140
let mut builder = phf_codegen::Map::new();
86141
for (code, names) in codes {

postgres-native-tls/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use std::future::Future;
5151
use std::io;
5252
use std::pin::Pin;
5353
use std::task::{Context, Poll};
54-
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
54+
use tokio::io::{AsyncRead, AsyncWrite, BufReader, ReadBuf};
5555
use tokio_postgres::tls;
5656
#[cfg(feature = "runtime")]
5757
use tokio_postgres::tls::MakeTlsConnect;
@@ -115,6 +115,7 @@ where
115115
type Future = Pin<Box<dyn Future<Output = Result<TlsStream<S>, native_tls::Error>> + Send>>;
116116

117117
fn connect(self, stream: S) -> Self::Future {
118+
let stream = BufReader::with_capacity(8192, stream);
118119
let future = async move {
119120
let stream = self.connector.connect(&self.domain, stream).await?;
120121

@@ -126,7 +127,7 @@ where
126127
}
127128

128129
/// The stream returned by `TlsConnector`.
129-
pub struct TlsStream<S>(tokio_native_tls::TlsStream<S>);
130+
pub struct TlsStream<S>(tokio_native_tls::TlsStream<BufReader<S>>);
130131

131132
impl<S> AsyncRead for TlsStream<S>
132133
where

postgres-openssl/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use std::pin::Pin;
5757
#[cfg(feature = "runtime")]
5858
use std::sync::Arc;
5959
use std::task::{Context, Poll};
60-
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
60+
use tokio::io::{AsyncRead, AsyncWrite, BufReader, ReadBuf};
6161
use tokio_openssl::SslStream;
6262
use tokio_postgres::tls;
6363
#[cfg(feature = "runtime")]
@@ -140,6 +140,7 @@ where
140140
type Future = Pin<Box<dyn Future<Output = Result<TlsStream<S>, Self::Error>> + Send>>;
141141

142142
fn connect(self, stream: S) -> Self::Future {
143+
let stream = BufReader::with_capacity(8192, stream);
143144
let future = async move {
144145
let ssl = self.ssl.into_ssl(&self.domain)?;
145146
let mut stream = SslStream::new(ssl, stream)?;
@@ -182,7 +183,7 @@ impl Error for ConnectError {
182183
}
183184

184185
/// The stream returned by `TlsConnector`.
185-
pub struct TlsStream<S>(SslStream<S>);
186+
pub struct TlsStream<S>(SslStream<BufReader<S>>);
186187

187188
impl<S> AsyncRead for TlsStream<S>
188189
where

postgres-protocol/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ base64 = "0.13"
1313
byteorder = "1.0"
1414
bytes = "1.0"
1515
fallible-iterator = "0.2"
16-
hmac = "0.10"
17-
md-5 = "0.9"
16+
hmac = "0.12"
17+
md-5 = "0.10.0"
1818
memchr = "2.0"
1919
rand = "0.8"
20-
sha2 = "0.9"
20+
sha2 = "0.10.0"
2121
stringprep = "0.1"

postgres-protocol/src/authentication/sasl.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! SASL-based authentication support.
22
3-
use hmac::{Hmac, Mac, NewMac};
3+
use hmac::{Hmac, Mac};
44
use rand::{self, Rng};
55
use sha2::digest::FixedOutput;
66
use sha2::{Digest, Sha256};
@@ -33,15 +33,16 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
3333
}
3434

3535
pub(crate) fn hi(str: &[u8], salt: &[u8], i: u32) -> [u8; 32] {
36-
let mut hmac = Hmac::<Sha256>::new_varkey(str).expect("HMAC is able to accept all key sizes");
36+
let mut hmac =
37+
Hmac::<Sha256>::new_from_slice(str).expect("HMAC is able to accept all key sizes");
3738
hmac.update(salt);
3839
hmac.update(&[0, 0, 0, 1]);
3940
let mut prev = hmac.finalize().into_bytes();
4041

4142
let mut hi = prev;
4243

4344
for _ in 1..i {
44-
let mut hmac = Hmac::<Sha256>::new_varkey(str).expect("already checked above");
45+
let mut hmac = Hmac::<Sha256>::new_from_slice(str).expect("already checked above");
4546
hmac.update(&prev);
4647
prev = hmac.finalize().into_bytes();
4748

@@ -195,7 +196,7 @@ impl ScramSha256 {
195196

196197
let salted_password = hi(&password, &salt, parsed.iteration_count);
197198

198-
let mut hmac = Hmac::<Sha256>::new_varkey(&salted_password)
199+
let mut hmac = Hmac::<Sha256>::new_from_slice(&salted_password)
199200
.expect("HMAC is able to accept all key sizes");
200201
hmac.update(b"Client Key");
201202
let client_key = hmac.finalize().into_bytes();
@@ -214,8 +215,8 @@ impl ScramSha256 {
214215

215216
let auth_message = format!("n=,r={},{},{}", client_nonce, message, self.message);
216217

217-
let mut hmac =
218-
Hmac::<Sha256>::new_varkey(&stored_key).expect("HMAC is able to accept all key sizes");
218+
let mut hmac = Hmac::<Sha256>::new_from_slice(&stored_key)
219+
.expect("HMAC is able to accept all key sizes");
219220
hmac.update(auth_message.as_bytes());
220221
let client_signature = hmac.finalize().into_bytes();
221222

@@ -266,15 +267,15 @@ impl ScramSha256 {
266267
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
267268
};
268269

269-
let mut hmac = Hmac::<Sha256>::new_varkey(&salted_password)
270+
let mut hmac = Hmac::<Sha256>::new_from_slice(&salted_password)
270271
.expect("HMAC is able to accept all key sizes");
271272
hmac.update(b"Server Key");
272273
let server_key = hmac.finalize().into_bytes();
273274

274-
let mut hmac =
275-
Hmac::<Sha256>::new_varkey(&server_key).expect("HMAC is able to accept all key sizes");
275+
let mut hmac = Hmac::<Sha256>::new_from_slice(&server_key)
276+
.expect("HMAC is able to accept all key sizes");
276277
hmac.update(auth_message.as_bytes());
277-
hmac.verify(&verifier)
278+
hmac.verify(verifier.as_slice().into())
278279
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "SCRAM verification error"))
279280
}
280281
}

postgres-protocol/src/password/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! end up in logs pg_stat displays, etc.
88
99
use crate::authentication::sasl;
10-
use hmac::{Hmac, Mac, NewMac};
10+
use hmac::{Hmac, Mac};
1111
use md5::Md5;
1212
use rand::RngCore;
1313
use sha2::digest::FixedOutput;
@@ -61,8 +61,8 @@ pub(crate) fn scram_sha_256_salt(password: &[u8], salt: [u8; SCRAM_DEFAULT_SALT_
6161
let salted_password = sasl::hi(&prepared, &salt, SCRAM_DEFAULT_ITERATIONS);
6262

6363
// client key
64-
let mut hmac =
65-
Hmac::<Sha256>::new_varkey(&salted_password).expect("HMAC is able to accept all key sizes");
64+
let mut hmac = Hmac::<Sha256>::new_from_slice(&salted_password)
65+
.expect("HMAC is able to accept all key sizes");
6666
hmac.update(b"Client Key");
6767
let client_key = hmac.finalize().into_bytes();
6868

@@ -72,8 +72,8 @@ pub(crate) fn scram_sha_256_salt(password: &[u8], salt: [u8; SCRAM_DEFAULT_SALT_
7272
let stored_key = hash.finalize_fixed();
7373

7474
// server key
75-
let mut hmac =
76-
Hmac::<Sha256>::new_varkey(&salted_password).expect("HMAC is able to accept all key sizes");
75+
let mut hmac = Hmac::<Sha256>::new_from_slice(&salted_password)
76+
.expect("HMAC is able to accept all key sizes");
7777
hmac.update(b"Server Key");
7878
let server_key = hmac.finalize().into_bytes();
7979

0 commit comments

Comments
 (0)