Skip to content

Commit fd1df17

Browse files
authored
Merge pull request #2651 from GrapeBaBa/issue2160_plan
[Feature]Add host_name field
2 parents 886f737 + d9d0d29 commit fd1df17

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed

common/management/src/user/user_api.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ use common_meta_types::SeqV;
2323
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2424
pub struct UserInfo {
2525
pub name: String,
26+
pub host_name: String,
2627
pub password: Vec<u8>,
2728
pub auth_type: AuthType,
2829
}
2930

3031
impl UserInfo {
31-
pub(crate) fn new(name: String, password: Vec<u8>, auth_type: AuthType) -> Self {
32+
pub(crate) fn new(
33+
name: String,
34+
host_name: String,
35+
password: Vec<u8>,
36+
auth_type: AuthType,
37+
) -> Self {
3238
UserInfo {
3339
name,
40+
host_name,
3441
password,
3542
auth_type,
3643
}
@@ -48,6 +55,7 @@ pub trait UserMgrApi: Sync + Send {
4855
async fn update_user(
4956
&self,
5057
username: String,
58+
new_host_name: Option<String>,
5159
new_password: Option<Vec<u8>>,
5260
new_auth: Option<AuthType>,
5361
seq: Option<u64>,

common/management/src/user/user_mgr.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,31 @@ impl UserMgrApi for UserMgr {
100100
async fn update_user(
101101
&self,
102102
username: String,
103+
new_host_name: Option<String>,
103104
new_password: Option<Vec<u8>>,
104105
new_auth: Option<AuthType>,
105106
seq: Option<u64>,
106107
) -> Result<Option<u64>> {
107-
if new_password.is_none() && new_auth.is_none() {
108+
if new_password.is_none() && new_auth.is_none() && new_host_name.is_none() {
108109
return Ok(seq);
109110
}
110-
let partial_update = new_auth.is_none() || new_password.is_none();
111-
let user_info = if partial_update {
111+
let full_update = new_auth.is_some() && new_password.is_some() && new_host_name.is_some();
112+
let user_info = if full_update {
113+
UserInfo::new(
114+
username.clone(),
115+
new_host_name.unwrap(),
116+
new_password.unwrap(),
117+
new_auth.unwrap(),
118+
)
119+
} else {
112120
let user_val_seq = self.get_user(username.clone(), seq);
113121
let user_info = user_val_seq.await?.data;
114122
UserInfo::new(
115123
username.clone(),
124+
new_host_name.unwrap_or(user_info.host_name),
116125
new_password.map_or(user_info.password, |v| v.to_vec()),
117-
new_auth.map_or(user_info.auth_type, |v| v),
126+
new_auth.unwrap_or(user_info.auth_type),
118127
)
119-
} else {
120-
UserInfo::new(username.clone(), new_password.unwrap(), new_auth.unwrap())
121128
};
122129

123130
let key = format!("{}/{}", self.user_prefix, user_info.name);

common/management/src/user/user_mgr_test.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ mod add {
7373
async fn test_add_user() -> common_exception::Result<()> {
7474
let test_user_name = "test_user";
7575
let test_password = "test_password";
76+
let test_hostname = "localhost";
7677
let auth_type = AuthType::Sha256;
7778
let user_info = UserInfo::new(
7879
test_user_name.to_string(),
80+
test_hostname.to_string(),
7981
Vec::from(test_password),
8082
auth_type.clone(),
8183
);
@@ -131,6 +133,7 @@ mod add {
131133

132134
let user_info = UserInfo::new(
133135
test_user_name.to_string(),
136+
test_hostname.to_string(),
134137
Vec::from(test_password),
135138
auth_type.clone(),
136139
);
@@ -161,6 +164,7 @@ mod add {
161164
let user_mgr = UserMgr::new(kv, "tenant1");
162165
let user_info = UserInfo::new(
163166
test_user_name.to_string(),
167+
test_hostname.to_string(),
164168
Vec::from(test_password),
165169
auth_type,
166170
);
@@ -184,10 +188,12 @@ mod get {
184188
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
185189
async fn test_get_user_seq_match() -> common_exception::Result<()> {
186190
let test_user_name = "test";
191+
let test_host_name = "localhost";
187192
let test_key = format!("__fd_users/tenant1/{}", test_user_name);
188193

189194
let user_info = UserInfo::new(
190195
test_user_name.to_string(),
196+
test_host_name.to_string(),
191197
Vec::from("pass"),
192198
AuthType::Sha256,
193199
);
@@ -214,10 +220,12 @@ mod get {
214220
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
215221
async fn test_get_user_do_not_care_seq() -> common_exception::Result<()> {
216222
let test_user_name = "test";
223+
let test_host_name = "localhost";
217224
let test_key = format!("__fd_users/tenant1/{}", test_user_name);
218225

219226
let user_info = UserInfo::new(
220227
test_user_name.to_string(),
228+
test_host_name.to_string(),
221229
Vec::from("pass"),
222230
AuthType::Sha256,
223231
);
@@ -316,17 +324,21 @@ mod get_users {
316324

317325
type FakeKeys = Vec<(String, SeqV<Vec<u8>>)>;
318326
type UserInfos = Vec<SeqV<UserInfo>>;
327+
319328
fn prepare() -> common_exception::Result<(FakeKeys, UserInfos)> {
320329
let mut names = vec![];
330+
let mut host_names = vec![];
321331
let mut keys = vec![];
322332
let mut res = vec![];
323333
let mut user_infos = vec![];
324334
for i in 0..9 {
325335
let name = format!("test_user_{}", i);
326336
names.push(name.clone());
337+
let host_name = format!("test_host_name_{}", i);
338+
host_names.push(host_name.clone());
327339
let key = format!("{}/{}", "tenant1", name);
328340
keys.push(key);
329-
let user_info = UserInfo::new(name, Vec::from("pass"), AuthType::Sha256);
341+
let user_info = UserInfo::new(name, host_name, Vec::from("pass"), AuthType::Sha256);
330342
res.push((
331343
"fake_key".to_string(),
332344
SeqV::new(i, serde_json::to_vec(&user_info)?),
@@ -389,7 +401,6 @@ mod get_users {
389401
}
390402

391403
mod drop {
392-
393404
use super::*;
394405

395406
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
@@ -447,6 +458,7 @@ mod update {
447458
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
448459
async fn test_update_user_normal_partial_update() -> common_exception::Result<()> {
449460
let test_user_name = "name";
461+
let test_host_name = "localhost";
450462
let test_key = format!("__fd_users/tenant1/{}", test_user_name);
451463
let test_seq = None;
452464

@@ -455,6 +467,7 @@ mod update {
455467

456468
let user_info = UserInfo::new(
457469
test_user_name.to_string(),
470+
test_host_name.to_string(),
458471
Vec::from(old_pass),
459472
old_auth_type,
460473
);
@@ -479,6 +492,7 @@ mod update {
479492
let new_pass = "new pass";
480493
let new_user_info = UserInfo::new(
481494
test_user_name.to_string(),
495+
test_host_name.to_string(),
482496
Vec::from(new_pass),
483497
AuthType::DoubleSha1,
484498
);
@@ -501,6 +515,7 @@ mod update {
501515

502516
let res = user_mgr.update_user(
503517
test_user_name.to_string(),
518+
Some(new_user_info.host_name),
504519
Some(new_user_info.password),
505520
None,
506521
test_seq,
@@ -513,6 +528,7 @@ mod update {
513528
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
514529
async fn test_update_user_normal_full_update() -> common_exception::Result<()> {
515530
let test_user_name = "name";
531+
let test_host_name = "localhost";
516532
let test_key = format!("__fd_users/tenant1/{}", test_user_name);
517533
let test_seq = None;
518534

@@ -524,6 +540,7 @@ mod update {
524540

525541
let new_user_info = UserInfo::new(
526542
test_user_name.to_string(),
543+
test_host_name.to_string(),
527544
Vec::from(new_pass),
528545
new_auth_type.clone(),
529546
);
@@ -547,6 +564,7 @@ mod update {
547564

548565
let res = user_mgr.update_user(
549566
test_user_name.to_string(),
567+
Some(test_host_name.to_string()),
550568
Some(new_user_info.password),
551569
Some(new_auth_type),
552570
test_seq,
@@ -565,7 +583,7 @@ mod update {
565583
let user_mgr = UserMgr::new(kv, "tenant1");
566584

567585
let new_password: Option<Vec<u8>> = None;
568-
let res = user_mgr.update_user(test_name.to_string(), new_password, None, None);
586+
let res = user_mgr.update_user(test_name.to_string(), None, new_password, None, None);
569587
assert!(res.await.is_ok());
570588
Ok(())
571589
}
@@ -589,6 +607,7 @@ mod update {
589607

590608
let res = user_mgr.update_user(
591609
test_user_name.to_string(),
610+
None,
592611
Some(Vec::from("new_pass".as_bytes())),
593612
None,
594613
test_seq,
@@ -625,6 +644,7 @@ mod update {
625644

626645
let res = user_mgr.update_user(
627646
test_user_name.to_string(),
647+
Some(String::from("localhost")),
628648
Some(Vec::from("new_pass".as_bytes())),
629649
Some(AuthType::Sha256),
630650
test_seq,

query/src/interpreters/interpreter_user_create.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl Interpreter for CreatUserInterpreter {
5252
let user_mgr = self.ctx.get_sessions_manager().get_user_manager();
5353
let user_info = UserInfo {
5454
name: plan.name,
55+
host_name: plan.host_name,
5556
password: plan.password,
5657
auth_type: plan.auth_type,
5758
};

query/src/users/user.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ use common_meta_types::AuthType;
1919
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2020
pub struct User {
2121
name: String,
22+
host_name: String,
2223
password: String,
2324
auth_type: AuthType,
2425
}
2526

2627
impl User {
27-
pub fn new(name: impl Into<String>, password: impl Into<String>, auth_type: AuthType) -> Self {
28+
pub fn new(
29+
name: impl Into<String>,
30+
host_name: impl Into<String>,
31+
password: impl Into<String>,
32+
auth_type: AuthType,
33+
) -> Self {
2834
User {
2935
name: name.into(),
36+
host_name: host_name.into(),
3037
password: password.into(),
3138
auth_type,
3239
}
@@ -37,6 +44,7 @@ impl From<&User> for UserInfo {
3744
fn from(user: &User) -> Self {
3845
UserInfo {
3946
name: user.name.clone(),
47+
host_name: user.host_name.clone(),
4048
password: Vec::from(user.password.clone()),
4149
auth_type: user.auth_type.clone(),
4250
}

query/src/users/user_mgr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl UserManager {
5454
match user {
5555
// TODO(BohuTANG): Mock, need removed.
5656
"default" | "" | "root" => {
57-
let user = User::new(user, "", AuthType::None);
57+
let user = User::new(user, "%", "", AuthType::None);
5858
Ok(user.into())
5959
}
6060
_ => {

query/src/users/user_mgr_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ async fn test_user_manager() -> Result<()> {
2727
config.query.tenant = "tenant1".to_string();
2828

2929
let user = "test-user1";
30+
let host_name = "localhost";
3031
let pwd = "test-pwd";
3132
let user_mgr = UserManager::create_global(config).await?;
3233

3334
// add.
3435
{
35-
let user_info = User::new(user, pwd, AuthType::PlainText);
36+
let user_info = User::new(user, host_name, pwd, AuthType::PlainText);
3637
user_mgr.add_user(user_info.into()).await?;
3738
}
3839

0 commit comments

Comments
 (0)