Skip to content

Commit

Permalink
Set user to executing processes' user by default.
Browse files Browse the repository at this point in the history
This mimics the behaviour of libpq and some other libraries (see sfackler#1024).
This commit uses the `whoami` crate, and thus goes as far as defaulting the user to the executing process' user name on all operating systems.
  • Loading branch information
ISibboI committed Aug 19, 2023
1 parent e7eb24a commit 98814b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
1 change: 1 addition & 0 deletions tokio-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ postgres-types = { version = "0.2.4", path = "../postgres-types" }
tokio = { version = "1.27", features = ["io-util"] }
tokio-util = { version = "0.7", features = ["codec"] }
rand = "0.8.5"
whoami = "1.4.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
socket2 = { version = "0.5", features = ["all"] }
Expand Down
21 changes: 11 additions & 10 deletions tokio-postgres/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub enum Host {
///
/// ## Keys
///
/// * `user` - The username to authenticate with. Required.
/// * `user` - The username to authenticate with. Defaults to the user executing this process.
/// * `password` - The password to authenticate with.
/// * `dbname` - The name of the database to connect to. Defaults to the username.
/// * `options` - Command line options used to configure the server.
Expand Down Expand Up @@ -190,7 +190,7 @@ pub enum Host {
/// ```
#[derive(Clone, PartialEq, Eq)]
pub struct Config {
pub(crate) user: Option<String>,
user: String,
pub(crate) password: Option<Vec<u8>>,
pub(crate) dbname: Option<String>,
pub(crate) options: Option<String>,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl Config {
/// Creates a new configuration.
pub fn new() -> Config {
Config {
user: None,
user: whoami::username(),
password: None,
dbname: None,
options: None,
Expand All @@ -245,16 +245,17 @@ impl Config {

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

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

/// Sets the password to authenticate with.
Expand Down Expand Up @@ -1124,7 +1125,7 @@ mod tests {
fn test_simple_parsing() {
let s = "user=pass_user dbname=postgres host=host1,host2 hostaddr=127.0.0.1,127.0.0.2 port=26257";
let config = s.parse::<Config>().unwrap();
assert_eq!(Some("pass_user"), config.get_user());
assert_eq!("pass_user", config.get_user());
assert_eq!(Some("postgres"), config.get_dbname());
assert_eq!(
[
Expand Down
9 changes: 2 additions & 7 deletions tokio-postgres/src/connect_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ where
T: AsyncRead + AsyncWrite + Unpin,
{
let mut params = vec![("client_encoding", "UTF8")];
if let Some(user) = &config.user {
params.push(("user", &**user));
}
params.push(("user", config.get_user()));
if let Some(dbname) = &config.dbname {
params.push(("database", &**dbname));
}
Expand Down Expand Up @@ -158,10 +156,7 @@ where
Some(Message::AuthenticationMd5Password(body)) => {
can_skip_channel_binding(config)?;

let user = config
.user
.as_ref()
.ok_or_else(|| Error::config("user missing".into()))?;
let user = config.get_user();
let pass = config
.password
.as_ref()
Expand Down

0 comments on commit 98814b8

Please sign in to comment.