Skip to content

Commit 402edbf

Browse files
authored
Merge pull request #947 from wangzhen11aaa/fix_confict
Suppress user and password info output via Config structure.
2 parents d444770 + ecfa8fd commit 402edbf

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

fusequery/query/src/configs/config.rs

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0.
44

5+
use std::fmt;
6+
use std::str::FromStr;
7+
58
use common_exception::ErrorCode;
69
use common_exception::Result;
710
use lazy_static::lazy_static;
@@ -137,15 +140,81 @@ pub struct Config {
137140
pub store_api_address: String,
138141

139142
#[structopt(long, env = STORE_API_USERNAME, default_value = "root")]
140-
pub store_api_username: String,
143+
pub store_api_username: User,
141144

142145
#[structopt(long, env = STORE_API_PASSWORD, default_value = "root")]
143-
pub store_api_password: String,
146+
pub store_api_password: Password,
144147

145148
#[structopt(long, short = "c", env = CONFIG_FILE, default_value = "")]
146149
pub config_file: String,
147150
}
148151

152+
#[derive(Clone, serde::Deserialize, PartialEq, StructOpt, StructOptToml)]
153+
#[serde(default)]
154+
pub struct Password {
155+
pub store_api_password: String,
156+
}
157+
158+
impl AsRef<String> for Password {
159+
fn as_ref(&self) -> &String {
160+
&self.store_api_password
161+
}
162+
}
163+
164+
impl FromStr for Password {
165+
type Err = ErrorCode;
166+
fn from_str(s: &str) -> common_exception::Result<Self> {
167+
Ok(Self {
168+
store_api_password: s.to_string(),
169+
})
170+
}
171+
}
172+
173+
impl ToString for Password {
174+
fn to_string(&self) -> String {
175+
self.store_api_password.clone()
176+
}
177+
}
178+
179+
impl fmt::Debug for Password {
180+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181+
write!(f, "******")
182+
}
183+
}
184+
185+
#[derive(Clone, serde::Deserialize, PartialEq, StructOpt, StructOptToml)]
186+
#[serde(default)]
187+
pub struct User {
188+
pub store_api_username: String,
189+
}
190+
191+
impl ToString for User {
192+
fn to_string(&self) -> String {
193+
self.store_api_username.clone()
194+
}
195+
}
196+
197+
impl fmt::Debug for User {
198+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199+
write!(f, "******")
200+
}
201+
}
202+
203+
impl AsRef<String> for User {
204+
fn as_ref(&self) -> &String {
205+
&self.store_api_username
206+
}
207+
}
208+
209+
impl FromStr for User {
210+
type Err = ErrorCode;
211+
fn from_str(s: &str) -> common_exception::Result<Self> {
212+
Ok(Self {
213+
store_api_username: s.to_string(),
214+
})
215+
}
216+
}
217+
149218
impl Config {
150219
/// Default configs.
151220
pub fn default() -> Self {
@@ -163,8 +232,12 @@ impl Config {
163232
http_api_address: "127.0.0.1:8080".to_string(),
164233
metric_api_address: "127.0.0.1:7070".to_string(),
165234
store_api_address: "127.0.0.1:9191".to_string(),
166-
store_api_username: "root".to_string(),
167-
store_api_password: "root".to_string(),
235+
store_api_username: User {
236+
store_api_username: "root".to_string(),
237+
},
238+
store_api_password: Password {
239+
store_api_password: "root".to_string(),
240+
},
168241
config_file: "".to_string(),
169242
}
170243
}
@@ -231,8 +304,8 @@ impl Config {
231304
env_helper!(mut_config, http_api_address, String, HTTP_API_ADDRESS);
232305
env_helper!(mut_config, metric_api_address, String, METRICS_API_ADDRESS);
233306
env_helper!(mut_config, store_api_address, String, STORE_API_ADDRESS);
234-
env_helper!(mut_config, store_api_username, String, STORE_API_USERNAME);
235-
env_helper!(mut_config, store_api_password, String, STORE_API_PASSWORD);
307+
env_helper!(mut_config, store_api_username, User, STORE_API_USERNAME);
308+
env_helper!(mut_config, store_api_password, Password, STORE_API_PASSWORD);
236309

237310
Ok(mut_config)
238311
}

fusequery/query/src/configs/config_test.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use common_exception::Result;
66

7+
use crate::configs::config::Password;
8+
use crate::configs::config::User;
9+
710
#[test]
811
fn test_config() -> Result<()> {
912
use pretty_assertions::assert_eq;
@@ -26,8 +29,12 @@ fn test_config() -> Result<()> {
2629
http_api_address: "127.0.0.1:8080".to_string(),
2730
metric_api_address: "127.0.0.1:7070".to_string(),
2831
store_api_address: "127.0.0.1:9191".to_string(),
29-
store_api_username: "root".to_string(),
30-
store_api_password: "root".to_string(),
32+
store_api_username: User {
33+
store_api_username: "root".to_string(),
34+
},
35+
store_api_password: Password {
36+
store_api_password: "root".to_string(),
37+
},
3138
config_file: "".to_string(),
3239
};
3340
let actual = Config::default();
@@ -95,8 +102,8 @@ fn test_config() -> Result<()> {
95102
assert_eq!("1.2.3.4:7071", configured.metric_api_address);
96103

97104
assert_eq!("1.2.3.4:1234", configured.store_api_address);
98-
assert_eq!("admin", configured.store_api_username);
99-
assert_eq!("password!", configured.store_api_password);
105+
assert_eq!("admin", configured.store_api_username.to_string());
106+
assert_eq!("password!", configured.store_api_password.to_string());
100107

101108
// clean up
102109
std::env::remove_var("FUSE_QUERY_LOG_LEVEL");

fusequery/query/src/datasources/remote/remote_factory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl TryGetStoreClient for ClientProvider {
5353
async fn try_get_client(&self) -> Result<StoreClient> {
5454
let client = StoreClient::try_create(
5555
&self.conf.store_api_address,
56-
&self.conf.store_api_username,
57-
&self.conf.store_api_password,
56+
&self.conf.store_api_username.as_ref(),
57+
&self.conf.store_api_password.as_ref(),
5858
)
5959
.await
6060
.map_err(ErrorCode::from)?;

0 commit comments

Comments
 (0)