From 7f51141ed015daa8f7f7ec27f4b4dc6865371048 Mon Sep 17 00:00:00 2001 From: shuiyisong <113876041+shuiyisong@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:51:26 +0800 Subject: [PATCH] refactor: `auth` crate (#2148) * chore: move user_info to auth crate * chore: temp commit before resolving tests compile error * chore: fix compile issue * chore: minor fix * chore: tmp save * chore: change user_info to trait * chore: minor change & use auth result user info in pg session setup * chore: add as_any to user_info * chore: rename user_info * chore: remove ice file * chore: add permission checker * chore: add grpc permission check * chore: add session spawn user_info to query_ctx * chore: minor update * chore: add permission checker to sql handler & temp save * chore: add permission checker to prometheus handler * chore: add permission checker to opentsdb handler * chore: add permission checker to other handlers * chore: add test * chore: add user_info setting on http entrance * chore: fix toml * chore: remove box in permission req * chore: cr issue * chore: cr issue --- Cargo.lock | 22 ++++++ Cargo.toml | 2 + src/auth/Cargo.toml | 26 +++++++ src/auth/src/common.rs | 68 ++++++++++++++++ .../src/auth.rs => auth/src/error.rs} | 78 +------------------ src/auth/src/lib.rs | 33 ++++++++ src/auth/src/permission.rs | 60 ++++++++++++++ .../tests/auth.rs => auth/src/tests.rs} | 42 +++++----- src/auth/src/user_info.rs | 47 +++++++++++ src/auth/src/user_provider.rs | 46 +++++++++++ .../user_provider/static_user_provider.rs} | 41 ++++++---- src/auth/tests/mod.rs | 61 +++++++++++++++ src/cmd/Cargo.toml | 1 + src/cmd/src/error.rs | 2 +- src/cmd/src/frontend.rs | 6 +- src/cmd/src/standalone.rs | 2 +- src/frontend/Cargo.toml | 1 + src/frontend/src/error.rs | 8 ++ src/frontend/src/instance.rs | 37 ++++++++- src/frontend/src/instance/grpc.rs | 11 ++- src/frontend/src/instance/influxdb.rs | 8 ++ src/frontend/src/instance/opentsdb.rs | 8 ++ src/frontend/src/instance/otlp.rs | 8 +- src/frontend/src/instance/prom_store.rs | 14 +++- src/frontend/src/server.rs | 2 +- src/query/Cargo.toml | 44 +++++------ src/servers/Cargo.toml | 5 +- src/servers/src/error.rs | 6 +- src/servers/src/grpc.rs | 2 +- src/servers/src/grpc/handler.rs | 17 ++-- src/servers/src/http.rs | 2 +- src/servers/src/http/authorize.rs | 32 ++++---- src/servers/src/http/handler.rs | 8 +- src/servers/src/http/influxdb.rs | 10 ++- src/servers/src/http/opentsdb.rs | 5 +- src/servers/src/http/otlp.rs | 5 +- src/servers/src/lib.rs | 1 - src/servers/src/mysql/handler.rs | 4 +- src/servers/src/mysql/server.rs | 2 +- src/servers/src/postgres.rs | 2 +- src/servers/src/postgres/auth_handler.rs | 52 +++++++------ src/servers/src/postgres/server.rs | 2 +- src/servers/src/prometheus.rs | 2 +- src/servers/tests/grpc/mod.rs | 4 +- src/servers/tests/http/authorize.rs | 18 ++--- src/servers/tests/http/http_handler_test.rs | 7 +- src/servers/tests/http/influxdb_test.rs | 3 +- src/servers/tests/mod.rs | 1 - src/servers/tests/mysql/mysql_server_test.rs | 2 +- src/servers/tests/postgres/mod.rs | 4 +- src/session/Cargo.toml | 1 + src/session/src/context.rs | 46 ++++------- src/session/src/lib.rs | 16 ++-- tests-integration/Cargo.toml | 1 + tests-integration/src/test_util.rs | 2 +- tests-integration/tests/grpc.rs | 17 ++-- tests-integration/tests/http.rs | 10 ++- tests-integration/tests/sql.rs | 24 +++--- 58 files changed, 690 insertions(+), 301 deletions(-) create mode 100644 src/auth/Cargo.toml create mode 100644 src/auth/src/common.rs rename src/{servers/src/auth.rs => auth/src/error.rs} (53%) create mode 100644 src/auth/src/lib.rs create mode 100644 src/auth/src/permission.rs rename src/{servers/tests/auth.rs => auth/src/tests.rs} (84%) create mode 100644 src/auth/src/user_info.rs create mode 100644 src/auth/src/user_provider.rs rename src/{servers/src/auth/user_provider.rs => auth/src/user_provider/static_user_provider.rs} (89%) create mode 100644 src/auth/tests/mod.rs diff --git a/Cargo.lock b/Cargo.lock index a5d1a50343..15a87e7630 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -672,6 +672,23 @@ dependencies = [ "winapi", ] +[[package]] +name = "auth" +version = "0.3.2" +dependencies = [ + "api", + "async-trait", + "common-error", + "common-test-util", + "digest", + "hex", + "secrecy", + "sha1", + "snafu", + "sql", + "tokio", +] + [[package]] name = "auto_ops" version = "0.3.0" @@ -1559,6 +1576,7 @@ version = "0.3.2" dependencies = [ "anymap", "async-trait", + "auth", "catalog", "chrono", "clap 3.2.25", @@ -3242,6 +3260,7 @@ dependencies = [ "async-compat", "async-stream", "async-trait", + "auth", "catalog", "chrono", "client", @@ -8824,6 +8843,7 @@ dependencies = [ "api", "arrow-flight", "async-trait", + "auth", "axum", "axum-macros", "axum-test-helper", @@ -8912,6 +8932,7 @@ name = "session" version = "0.3.2" dependencies = [ "arc-swap", + "auth", "common-catalog", "common-telemetry", "common-time", @@ -9933,6 +9954,7 @@ version = "0.3.2" dependencies = [ "api", "async-trait", + "auth", "axum", "axum-test-helper", "catalog", diff --git a/Cargo.toml b/Cargo.toml index e322f66182..916ed8fd80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "benchmarks", "src/api", + "src/auth", "src/catalog", "src/client", "src/cmd", @@ -102,6 +103,7 @@ metrics = "0.20" meter-core = { git = "https://github.com/GreptimeTeam/greptime-meter.git", rev = "abbd357c1e193cd270ea65ee7652334a150b628f" } ## workspaces members api = { path = "src/api" } +auth = { path = "src/auth" } catalog = { path = "src/catalog" } client = { path = "src/client" } cmd = { path = "src/cmd" } diff --git a/src/auth/Cargo.toml b/src/auth/Cargo.toml new file mode 100644 index 0000000000..ae1c4a49ff --- /dev/null +++ b/src/auth/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "auth" +version.workspace = true +edition.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] +default = [] +testing = [] + +[dependencies] +api.workspace = true +async-trait.workspace = true +common-error.workspace = true +digest = "0.10" +hex = { version = "0.4" } +secrecy = { version = "0.8", features = ["serde", "alloc"] } +sha1 = "0.10" +snafu.workspace = true +sql.workspace = true +tokio.workspace = true + +[dev-dependencies] +common-test-util.workspace = true diff --git a/src/auth/src/common.rs b/src/auth/src/common.rs new file mode 100644 index 0000000000..8278ae53dc --- /dev/null +++ b/src/auth/src/common.rs @@ -0,0 +1,68 @@ +// Copyright 2023 Greptime Team +// +// 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 std::sync::Arc; + +use secrecy::SecretString; +use snafu::OptionExt; + +use crate::error::{InvalidConfigSnafu, Result}; +use crate::user_info::DefaultUserInfo; +use crate::user_provider::static_user_provider::{StaticUserProvider, STATIC_USER_PROVIDER}; +use crate::{UserInfoRef, UserProviderRef}; + +pub(crate) const DEFAULT_USERNAME: &str = "greptime"; + +/// construct a [`UserInfo`] impl with name +/// use default username `greptime` if None is provided +pub fn userinfo_by_name(username: Option) -> UserInfoRef { + DefaultUserInfo::with_name(username.unwrap_or_else(|| DEFAULT_USERNAME.to_string())) +} + +pub fn user_provider_from_option(opt: &String) -> Result { + let (name, content) = opt.split_once(':').context(InvalidConfigSnafu { + value: opt.to_string(), + msg: "UserProviderOption must be in format `