Skip to content

Commit a4bd72f

Browse files
authored
Merge pull request #2646 from BohuTANG/dev-create-user-2630
USER-2630: create user
2 parents 7442d7b + de68816 commit a4bd72f

20 files changed

+167
-22
lines changed

common/management/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod user;
1818

1919
pub use namespace::NamespaceApi;
2020
pub use namespace::NamespaceMgr;
21-
pub use user::user_api::AuthType;
2221
pub use user::user_api::UserInfo;
2322
pub use user::user_api::UserMgrApi;
2423
pub use user::user_mgr::UserMgr;

common/management/src/user/user_api.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@ use std::convert::TryFrom;
1717

1818
use common_exception::ErrorCode;
1919
use common_exception::Result;
20+
use common_meta_types::AuthType;
2021
use common_meta_types::SeqV;
2122

22-
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
23-
pub enum AuthType {
24-
None = 0,
25-
PlainText = 1,
26-
DoubleSha1 = 2,
27-
Sha256 = 3,
28-
}
29-
3023
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
3124
pub struct UserInfo {
3225
pub name: String,

common/management/src/user/user_mgr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ use common_exception::Result;
2020
use common_exception::ToErrorCode;
2121
use common_meta_api::KVApi;
2222
use common_meta_types::AddResult;
23+
use common_meta_types::AuthType;
2324
use common_meta_types::IntoSeqV;
2425
use common_meta_types::MatchSeq;
2526
use common_meta_types::MatchSeqExt;
2627
use common_meta_types::SeqV;
2728

28-
use super::user_api::AuthType;
2929
use crate::user::user_api::UserInfo;
3030
use crate::user::user_api::UserMgrApi;
3131

common/management/src/user/user_mgr_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use common_meta_types::UpsertKVActionReply;
2929
use mockall::predicate::*;
3030
use mockall::*;
3131

32-
use crate::user::user_api::AuthType;
3332
use crate::user::user_api::UserInfo;
3433
use crate::user::user_api::UserMgrApi;
3534
use crate::UserMgr;
@@ -66,6 +65,7 @@ mock! {
6665
}
6766

6867
mod add {
68+
use common_meta_types::AuthType;
6969

7070
use super::*;
7171

@@ -177,6 +177,7 @@ mod add {
177177
}
178178

179179
mod get {
180+
use common_meta_types::AuthType;
180181

181182
use super::*;
182183

@@ -309,6 +310,7 @@ mod get {
309310
}
310311

311312
mod get_users {
313+
use common_meta_types::AuthType;
312314

313315
use super::*;
314316

@@ -438,6 +440,7 @@ mod drop {
438440
}
439441

440442
mod update {
443+
use common_meta_types::AuthType;
441444

442445
use super::*;
443446

common/meta/types/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
//! This crate defines data types used in meta data storage service.
1616
17+
#[cfg(test)]
18+
mod cluster_test;
19+
#[cfg(test)]
20+
mod match_seq_test;
21+
1722
pub use change::AddResult;
1823
pub use change::Change;
1924
pub use cluster::Node;
@@ -47,6 +52,7 @@ pub use table_info::TableIdent;
4752
pub use table_info::TableInfo;
4853
pub use table_info::TableMeta;
4954
pub use table_reply::CreateTableReply;
55+
pub use user::AuthType;
5056

5157
mod change;
5258
mod cluster;
@@ -65,8 +71,4 @@ mod seq_num;
6571
mod seq_value;
6672
mod table_info;
6773
mod table_reply;
68-
69-
#[cfg(test)]
70-
mod cluster_test;
71-
#[cfg(test)]
72-
mod match_seq_test;
74+
mod user;

common/meta/types/src/user.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
16+
pub enum AuthType {
17+
None = 0,
18+
PlainText = 1,
19+
DoubleSha1 = 2,
20+
Sha256 = 3,
21+
}

common/planners/src/plan_user_create.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ use std::sync::Arc;
1616

1717
use common_datavalues::DataSchema;
1818
use common_datavalues::DataSchemaRef;
19+
use common_meta_types::AuthType;
1920

2021
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq)]
2122
pub struct CreateUserPlan {
2223
pub name: String,
24+
pub password: Vec<u8>,
25+
pub host_name: String,
26+
pub auth_type: AuthType,
2327
}
2428

2529
impl CreateUserPlan {

query/src/interpreters/interpreter_factory.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use common_exception::Result;
1919
use common_planners::PlanNode;
2020

2121
use crate::interpreters::interpreter_kill::KillInterpreter;
22+
use crate::interpreters::CreatUserInterpreter;
2223
use crate::interpreters::CreateDatabaseInterpreter;
2324
use crate::interpreters::CreateTableInterpreter;
2425
use crate::interpreters::DescribeTableInterpreter;
@@ -52,6 +53,7 @@ impl InterpreterFactory {
5253
PlanNode::InsertInto(v) => InsertIntoInterpreter::try_create(ctx, v),
5354
PlanNode::ShowCreateTable(v) => ShowCreateTableInterpreter::try_create(ctx, v),
5455
PlanNode::Kill(v) => KillInterpreter::try_create(ctx, v),
56+
PlanNode::CreateUser(v) => CreatUserInterpreter::try_create(ctx, v),
5557
_ => Result::Err(ErrorCode::UnknownTypeOfQuery(format!(
5658
"Can't get the interpreter by plan:{}",
5759
plan.name()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2020 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::sync::Arc;
16+
17+
use common_exception::Result;
18+
use common_management::UserInfo;
19+
use common_planners::CreateUserPlan;
20+
use common_streams::DataBlockStream;
21+
use common_streams::SendableDataBlockStream;
22+
use common_tracing::tracing;
23+
24+
use crate::interpreters::Interpreter;
25+
use crate::interpreters::InterpreterPtr;
26+
use crate::sessions::DatabendQueryContextRef;
27+
28+
#[derive(Debug)]
29+
pub struct CreatUserInterpreter {
30+
ctx: DatabendQueryContextRef,
31+
plan: CreateUserPlan,
32+
}
33+
34+
impl CreatUserInterpreter {
35+
pub fn try_create(
36+
ctx: DatabendQueryContextRef,
37+
plan: CreateUserPlan,
38+
) -> Result<InterpreterPtr> {
39+
Ok(Arc::new(CreatUserInterpreter { ctx, plan }))
40+
}
41+
}
42+
43+
#[async_trait::async_trait]
44+
impl Interpreter for CreatUserInterpreter {
45+
fn name(&self) -> &str {
46+
"CreateUserInterpreter"
47+
}
48+
49+
#[tracing::instrument(level = "info", skip(self), fields(ctx.id = self.ctx.get_id().as_str()))]
50+
async fn execute(&self) -> Result<SendableDataBlockStream> {
51+
let plan = self.plan.clone();
52+
let user_mgr = self.ctx.get_sessions_manager().get_user_manager();
53+
let user_info = UserInfo {
54+
name: plan.name,
55+
password: plan.password,
56+
auth_type: plan.auth_type,
57+
};
58+
user_mgr.add_user(user_info).await?;
59+
60+
Ok(Box::pin(DataBlockStream::create(
61+
self.plan.schema(),
62+
None,
63+
vec![],
64+
)))
65+
}
66+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use common_base::tokio;
16+
use common_exception::Result;
17+
use common_planners::*;
18+
use futures::stream::StreamExt;
19+
use pretty_assertions::assert_eq;
20+
21+
use crate::interpreters::*;
22+
use crate::sql::*;
23+
24+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
25+
async fn test_create_user_interpreter() -> Result<()> {
26+
common_tracing::init_default_ut_tracing();
27+
28+
let ctx = crate::tests::try_create_context()?;
29+
30+
if let PlanNode::CreateUser(plan) = PlanParser::create(ctx.clone())
31+
.build_from_sql("CREATE USER 'test'@'localhost' IDENTIFIED BY 'password'")?
32+
{
33+
let executor = CreatUserInterpreter::try_create(ctx, plan.clone())?;
34+
assert_eq!(executor.name(), "CreateUserInterpreter");
35+
let mut stream = executor.execute().await?;
36+
while let Some(_block) = stream.next().await {}
37+
} else {
38+
panic!()
39+
}
40+
41+
Ok(())
42+
}

query/src/interpreters/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ mod interpreter_truncate_table_test;
3535
#[cfg(test)]
3636
mod interpreter_use_database_test;
3737
#[cfg(test)]
38+
mod interpreter_user_create_test;
39+
#[cfg(test)]
3840
mod plan_scheduler_test;
3941

4042
mod interpreter;
@@ -52,6 +54,7 @@ mod interpreter_table_create;
5254
mod interpreter_table_drop;
5355
mod interpreter_truncate_table;
5456
mod interpreter_use_database;
57+
mod interpreter_user_create;
5558
#[allow(clippy::needless_range_loop)]
5659
mod plan_scheduler;
5760

@@ -70,3 +73,4 @@ pub use interpreter_table_create::CreateTableInterpreter;
7073
pub use interpreter_table_drop::DropTableInterpreter;
7174
pub use interpreter_truncate_table::TruncateTableInterpreter;
7275
pub use interpreter_use_database::UseDatabaseInterpreter;
76+
pub use interpreter_user_create::CreatUserInterpreter;

query/src/sql/plan_parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ impl PlanParser {
338338
pub fn sql_create_user_to_plan(&self, create: &DfCreateUser) -> Result<PlanNode> {
339339
Ok(PlanNode::CreateUser(CreateUserPlan {
340340
name: create.name.clone(),
341+
password: Vec::from(create.password.clone()),
342+
host_name: create.host_name.clone(),
343+
auth_type: create.auth_type.clone(),
341344
}))
342345
}
343346

query/src/sql/sql_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use std::time::Instant;
1919

2020
use common_exception::ErrorCode;
21-
use common_management::AuthType;
21+
use common_meta_types::AuthType;
2222
use common_planners::ExplainType;
2323
use metrics::histogram;
2424
use sqlparser::ast::BinaryOperator;

query/src/sql/sql_parser_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
use common_exception::Result;
16-
use common_management::AuthType;
16+
use common_meta_types::AuthType;
1717
use sqlparser::ast::*;
1818

1919
use crate::sql::sql_statement::DfDropDatabase;

query/src/sql/sql_statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use common_management::AuthType;
15+
use common_meta_types::AuthType;
1616
use common_planners::ExplainType;
1717
use nom::bytes::complete::tag;
1818
use nom::bytes::complete::take_till1;

query/src/users/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414
//
1515

16-
use common_management::AuthType;
1716
use common_management::UserInfo;
17+
use common_meta_types::AuthType;
1818

1919
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2020
pub struct User {

query/src/users/user_mgr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use std::sync::Arc;
1616

1717
use common_exception::Result;
18-
use common_management::AuthType;
1918
use common_management::UserInfo;
2019
use common_management::UserMgr;
2120
use common_management::UserMgrApi;
2221
use common_meta_api::KVApi;
22+
use common_meta_types::AuthType;
2323
use sha2::Digest;
2424

2525
use crate::common::MetaClientProvider;

query/src/users/user_mgr_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use common_base::tokio;
1616
use common_exception::Result;
17-
use common_management::AuthType;
17+
use common_meta_types::AuthType;
1818
use pretty_assertions::assert_eq;
1919

2020
use crate::configs::Config;

tests/suites/0_stateless/05_0004_ddl_create_user.result

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE USER 'test'@'localhost' IDENTIFIED BY 'password';
2+
CREATE USER 'test'@'localhost' IDENTIFIED BY 'password'; -- {ErrorCode 3001}
3+
CREATE USER 'test-a'@'localhost' IDENTIFIED WITH plaintext_password BY 'password';
4+
CREATE USER 'test-b'@'localhost' IDENTIFIED WITH sha256_password BY 'password';
5+
CREATE USER 'test-c'@'localhost' IDENTIFIED WITH double_sha1_password BY 'password';
6+
CREATE USER 'test-d@localhost' IDENTIFIED WITH sha256_password BY 'password';

0 commit comments

Comments
 (0)