Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USER-2678: Add user privileges [PATCH-1] #2703

Merged
merged 3 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion common/management/src/user/user_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use common_exception::ErrorCode;
use common_exception::Result;
use common_meta_types::AuthType;
use common_meta_types::SeqV;
use common_meta_types::UserPrivilege;

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct UserInfo {
pub name: String,
pub hostname: String,
pub password: Vec<u8>,
pub auth_type: AuthType,
pub privileges: UserPrivilege,
}

impl UserInfo {
Expand All @@ -35,11 +37,14 @@ impl UserInfo {
password: Vec<u8>,
auth_type: AuthType,
) -> Self {
// Default is no privileges.
let privileges = UserPrivilege::empty();
UserInfo {
name,
hostname,
password,
auth_type,
privileges,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion common/meta/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ common-exception = {path = "../../exception"}

async-raft = { git = "https://github.com/datafuse-extras/async-raft", tag = "v0.6.2-alpha.14" }
derive_more = "0.99.16"
enumflags2 = {version = "0.7.1", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"


[dev-dependencies]
pretty_assertions = "1.0"
9 changes: 7 additions & 2 deletions common/meta/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
mod cluster_test;
#[cfg(test)]
mod match_seq_test;
#[cfg(test)]
mod user_privilege_test;

pub use change::AddResult;
pub use change::Change;
Expand Down Expand Up @@ -52,7 +54,9 @@ pub use table_info::TableIdent;
pub use table_info::TableInfo;
pub use table_info::TableMeta;
pub use table_reply::CreateTableReply;
pub use user::AuthType;
pub use user_auth::AuthType;
pub use user_privilege::UserPrivilege;
pub use user_privilege::UserPrivilegeType;

mod change;
mod cluster;
Expand All @@ -71,4 +75,5 @@ mod seq_num;
mod seq_value;
mod table_info;
mod table_reply;
mod user;
mod user_auth;
mod user_privilege;
File renamed without changes.
65 changes: 65 additions & 0 deletions common/meta/types/src/user_privilege.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use enumflags2::bitflags;
use enumflags2::make_bitflags;
use enumflags2::BitFlags;

#[bitflags]
#[repr(u64)]
#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum UserPrivilegeType {
// UsagePrivilege is a synonym for “no privileges”
Usage = 1 << 0,
// Privilege to create databases and tables.
Create = 1 << 1,
// Privilege to select rows from tables in a database.
Select = 1 << 2,
// Privilege to insert into tables in a database.
Insert = 1 << 3,
// Privilege to SET variables.
Set = 1 << 4,
}

const ALL_PRIVILEGES: BitFlags<UserPrivilegeType> = make_bitflags!(
UserPrivilegeType::{Create
| Select
| Insert
| Set}
);

#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub struct UserPrivilege {
privileges: BitFlags<UserPrivilegeType>,
}

impl UserPrivilege {
pub fn empty() -> Self {
UserPrivilege {
privileges: BitFlags::empty(),
}
}

pub fn set_privilege(&mut self, privilege: UserPrivilegeType) {
self.privileges |= privilege;
}

pub fn has_privilege(&mut self, privilege: UserPrivilegeType) -> bool {
self.privileges.contains(privilege)
}

pub fn set_all_privileges(&mut self) {
self.privileges |= ALL_PRIVILEGES;
}
}
35 changes: 35 additions & 0 deletions common/meta/types/src/user_privilege_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use common_exception::exception::Result;

use crate::UserPrivilege;
use crate::UserPrivilegeType;

#[test]
fn test_user_privilege() -> Result<()> {
let mut privileges = UserPrivilege::empty();
let r = privileges.has_privilege(UserPrivilegeType::Set);
assert!(!r);

privileges.set_privilege(UserPrivilegeType::Set);
let r = privileges.has_privilege(UserPrivilegeType::Set);
assert!(r);

privileges.set_all_privileges();
let r = privileges.has_privilege(UserPrivilegeType::Create);
assert!(r);

Ok(())
}
3 changes: 3 additions & 0 deletions query/src/datasources/database/system/users_table_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use common_base::tokio;
use common_exception::Result;
use common_management::UserInfo;
use common_meta_types::AuthType;
use common_meta_types::UserPrivilege;
use futures::TryStreamExt;
use pretty_assertions::assert_eq;

Expand All @@ -36,6 +37,7 @@ async fn test_users_table() -> Result<()> {
hostname: "localhost".to_string(),
password: Vec::from(""),
auth_type: AuthType::None,
privileges: UserPrivilege::empty(),
})
.await?;
ctx.get_sessions_manager()
Expand All @@ -45,6 +47,7 @@ async fn test_users_table() -> Result<()> {
hostname: "%".to_string(),
password: Vec::from("123456789"),
auth_type: AuthType::PlainText,
privileges: UserPrivilege::empty(),
})
.await?;

Expand Down
2 changes: 2 additions & 0 deletions query/src/interpreters/interpreter_user_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;

use common_exception::Result;
use common_management::UserInfo;
use common_meta_types::UserPrivilege;
use common_planners::CreateUserPlan;
use common_streams::DataBlockStream;
use common_streams::SendableDataBlockStream;
Expand Down Expand Up @@ -55,6 +56,7 @@ impl Interpreter for CreatUserInterpreter {
hostname: plan.hostname,
password: plan.password,
auth_type: plan.auth_type,
privileges: UserPrivilege::empty(),
};
user_mgr.add_user(user_info).await?;

Expand Down
3 changes: 3 additions & 0 deletions query/src/users/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use common_management::UserInfo;
use common_meta_types::AuthType;
use common_meta_types::UserPrivilege;

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct User {
Expand Down Expand Up @@ -42,11 +43,13 @@ impl User {

impl From<&User> for UserInfo {
fn from(user: &User) -> Self {
let privileges = UserPrivilege::empty();
UserInfo {
name: user.name.clone(),
hostname: user.hostname.clone(),
password: Vec::from(user.password.clone()),
auth_type: user.auth_type.clone(),
privileges,
}
}
}
Expand Down