Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98814b8

Browse files
committed
Set user to executing processes' user by default.
This mimics the behaviour of libpq and some other libraries (see#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.
1 parente7eb24a commit98814b8

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

‎tokio-postgres/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 11 additions & 10 deletions
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
pubstructConfig{
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
pubfnnew() ->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
pubfnuser(&mutself,user:&str) ->&mutConfig{
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-
pubfnget_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+
pubfnget_user(&self) ->&str{
258+
&self.user
258259
}
259260

260261
/// Sets the password to authenticate with.
@@ -1124,7 +1125,7 @@ mod tests {
11241125
fntest_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‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ where
113113
T:AsyncRead +AsyncWrite +Unpin,
114114
{
115115
letmut params =vec![("client_encoding","UTF8")];
116-
ifletSome(user) =&config.user{
117-
params.push(("user",&**user));
118-
}
116+
params.push(("user", config.get_user()));
119117
ifletSome(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)

[8]ページ先頭

©2009-2025 Movatter.jp