Skip to content

Commit f777f74

Browse files
authored
Merge pull request #1059 from ISibboI/master
`tokio-postgres`: Set user to executing process' user by default.
2 parents e7eb24a + 7a5b19a commit f777f74

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
- run: docker compose up -d
8383
- uses: sfackler/actions/rustup@master
8484
with:
85-
version: 1.65.0
85+
version: 1.67.0
8686
- run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT
8787
id: rust-version
8888
- uses: actions/cache@v3

postgres/src/config.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use tokio_postgres::{Error, Socket};
2929
///
3030
/// ## Keys
3131
///
32-
/// * `user` - The username to authenticate with. Required.
32+
/// * `user` - The username to authenticate with. Defaults to the user executing this process.
3333
/// * `password` - The password to authenticate with.
3434
/// * `dbname` - The name of the database to connect to. Defaults to the username.
3535
/// * `options` - Command line options used to configure the server.
@@ -143,15 +143,16 @@ impl Config {
143143

144144
/// Sets the user to authenticate with.
145145
///
146-
/// Required.
146+
/// If the user is not set, then this defaults to the user executing this process.
147147
pub fn user(&mut self, user: &str) -> &mut Config {
148148
self.config.user(user);
149149
self
150150
}
151151

152-
/// Gets the user to authenticate with, if one has been configured with
153-
/// the `user` method.
154-
pub fn get_user(&self) -> Option<&str> {
152+
/// Gets the user to authenticate with.
153+
/// If no user has been configured with the [`user`](Config::user) method,
154+
/// then this defaults to the user executing this process.
155+
pub fn get_user(&self) -> &str {
155156
self.config.get_user()
156157
}
157158

tokio-postgres/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ postgres-types = { version = "0.2.4", path = "../postgres-types" }
5959
tokio = { version = "1.27", features = ["io-util"] }
6060
tokio-util = { version = "0.7", features = ["codec"] }
6161
rand = "0.8.5"
62+
whoami = "1.4.1"
6263

6364
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
6465
socket2 = { version = "0.5", features = ["all"] }

tokio-postgres/src/config.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub enum Host {
9393
///
9494
/// ## Keys
9595
///
96-
/// * `user` - The username to authenticate with. Required.
96+
/// * `user` - The username to authenticate with. Defaults to the user executing this process.
9797
/// * `password` - The password to authenticate with.
9898
/// * `dbname` - The name of the database to connect to. Defaults to the username.
9999
/// * `options` - Command line options used to configure the server.
@@ -190,7 +190,7 @@ pub enum Host {
190190
/// ```
191191
#[derive(Clone, PartialEq, Eq)]
192192
pub struct Config {
193-
pub(crate) user: Option<String>,
193+
user: String,
194194
pub(crate) password: Option<Vec<u8>>,
195195
pub(crate) dbname: Option<String>,
196196
pub(crate) options: Option<String>,
@@ -219,7 +219,7 @@ impl Config {
219219
/// Creates a new configuration.
220220
pub fn new() -> Config {
221221
Config {
222-
user: None,
222+
user: whoami::username(),
223223
password: None,
224224
dbname: None,
225225
options: None,
@@ -245,16 +245,17 @@ impl Config {
245245

246246
/// Sets the user to authenticate with.
247247
///
248-
/// Required.
248+
/// If the user is not set, then this defaults to the user executing this process.
249249
pub fn user(&mut self, user: &str) -> &mut Config {
250-
self.user = Some(user.to_string());
250+
self.user = user.to_string();
251251
self
252252
}
253253

254-
/// Gets the user to authenticate with, if one has been configured with
255-
/// the `user` method.
256-
pub fn get_user(&self) -> Option<&str> {
257-
self.user.as_deref()
254+
/// Gets the user to authenticate with.
255+
/// If no user has been configured with the [`user`](Config::user) method,
256+
/// then this defaults to the user executing this process.
257+
pub fn get_user(&self) -> &str {
258+
&self.user
258259
}
259260

260261
/// Sets the password to authenticate with.
@@ -1124,7 +1125,7 @@ mod tests {
11241125
fn test_simple_parsing() {
11251126
let s = "user=pass_user dbname=postgres host=host1,host2 hostaddr=127.0.0.1,127.0.0.2 port=26257";
11261127
let config = s.parse::<Config>().unwrap();
1127-
assert_eq!(Some("pass_user"), config.get_user());
1128+
assert_eq!("pass_user", config.get_user());
11281129
assert_eq!(Some("postgres"), config.get_dbname());
11291130
assert_eq!(
11301131
[

tokio-postgres/src/connect_raw.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ where
113113
T: AsyncRead + AsyncWrite + Unpin,
114114
{
115115
let mut params = vec![("client_encoding", "UTF8")];
116-
if let Some(user) = &config.user {
117-
params.push(("user", &**user));
118-
}
116+
params.push(("user", config.get_user()));
119117
if let Some(dbname) = &config.dbname {
120118
params.push(("database", &**dbname));
121119
}
@@ -158,10 +156,7 @@ where
158156
Some(Message::AuthenticationMd5Password(body)) => {
159157
can_skip_channel_binding(config)?;
160158

161-
let user = config
162-
.user
163-
.as_ref()
164-
.ok_or_else(|| Error::config("user missing".into()))?;
159+
let user = config.get_user();
165160
let pass = config
166161
.password
167162
.as_ref()

0 commit comments

Comments
 (0)