From 121be5eedacb5a649601e975674c161980297d7b Mon Sep 17 00:00:00 2001 From: weisd Date: Sun, 12 Jan 2025 03:29:57 +0800 Subject: [PATCH] iam add_user --- iam/src/error.rs | 2 ++ iam/src/lib.rs | 5 +++++ iam/src/manager.rs | 32 +++++++++++++++++++++++++++++-- rustfs/src/admin/handlers/user.rs | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/iam/src/error.rs b/iam/src/error.rs index 6233b2266..25ae2af20 100644 --- a/iam/src/error.rs +++ b/iam/src/error.rs @@ -46,6 +46,8 @@ pub enum Error { #[error("invalid access_key")] InvalidAccessKey, + #[error("action not allowed")] + IAMActionNotAllowed, } pub type Result = std::result::Result; diff --git a/iam/src/lib.rs b/iam/src/lib.rs index a4e05f6c4..81683b9b5 100644 --- a/iam/src/lib.rs +++ b/iam/src/lib.rs @@ -104,3 +104,8 @@ pub async fn list_users() -> crate::Result> { pub async fn get_user(ak: &str) -> crate::Result<(Option, bool)> { get()?.check_key(ak).await } + +pub async fn create_user(ak: &str, sk: &str, status: &str) -> crate::Result { + get()?.add_user(ak, sk, status).await + // notify +} diff --git a/iam/src/manager.rs b/iam/src/manager.rs index e33a48b9a..0be502209 100644 --- a/iam/src/manager.rs +++ b/iam/src/manager.rs @@ -310,7 +310,7 @@ where Ok(m) } - pub async fn add_user(&self, access_key: &str, secret_key: &str, status: &str) -> crate::Result<()> { + pub async fn add_user(&self, access_key: &str, secret_key: &str, status: &str) -> crate::Result { let status = { match status { "disabled" => auth::ACCOUNT_ON, @@ -319,6 +319,34 @@ where } }; - todo!() + let users = self.cache.users.load(); + if let Some(x) = users.get(access_key) { + if x.credentials.is_temp() { + return Err(crate::Error::IAMActionNotAllowed); + } + } + + let user_entiry = UserIdentity::from(Credentials { + access_key: access_key.to_string(), + secret_key: secret_key.to_string(), + status: status.to_string(), + ..Default::default() + }); + let path = format!( + "config/iam/{}{}/identity.json", + UserType::Reg.prefix(), + user_entiry.credentials.access_key + ); + debug!("save object: {path:?}"); + self.api.save_iam_config(&user_entiry, path).await?; + + Cache::add_or_update( + &self.cache.users, + &user_entiry.credentials.access_key, + &user_entiry, + OffsetDateTime::now_utc(), + ); + + Ok(user_entiry.update_at.unwrap_or(OffsetDateTime::now_utc())) } } diff --git a/rustfs/src/admin/handlers/user.rs b/rustfs/src/admin/handlers/user.rs index a0a29e600..fe9dd8356 100644 --- a/rustfs/src/admin/handlers/user.rs +++ b/rustfs/src/admin/handlers/user.rs @@ -22,6 +22,7 @@ pub struct AddUser {} #[async_trait::async_trait] impl Operation for AddUser { async fn call(&self, req: S3Request, _params: Params<'_, '_>) -> S3Result> { + warn!("handle AddUser"); let query = { if let Some(query) = req.uri.query() { let input: AddUserQuery = @@ -61,7 +62,6 @@ impl Operation for AddUser { return Err(s3_error!(InvalidArgument, "can't create user with service account access key")); } - warn!("handle AddUser"); return Err(s3_error!(NotImplemented)); } }