diff --git a/crates/xline-client/examples/auth_role.rs b/crates/xline-client/examples/auth_role.rs index a23e686a8..fe09d34ac 100644 --- a/crates/xline-client/examples/auth_role.rs +++ b/crates/xline-client/examples/auth_role.rs @@ -1,8 +1,5 @@ use anyhow::Result; -use xline_client::{ - types::auth::{AuthRoleRevokePermissionRequest, PermissionType}, - Client, ClientOptions, -}; +use xline_client::{types::auth::PermissionType, Client, ClientOptions}; #[tokio::main] async fn main() -> Result<()> { @@ -38,12 +35,8 @@ async fn main() -> Result<()> { } // revoke permissions from roles - client - .role_revoke_permission(AuthRoleRevokePermissionRequest::new("role1", "key1")) - .await?; - client - .role_revoke_permission(AuthRoleRevokePermissionRequest::new("role2", "key2")) - .await?; + client.role_revoke_permission("role1", "key1", None).await?; + client.role_revoke_permission("role2", "key2", None).await?; // delete roles client.role_delete("role1").await?; diff --git a/crates/xline-client/src/clients/auth.rs b/crates/xline-client/src/clients/auth.rs index 44038cc28..e786f4cd6 100644 --- a/crates/xline-client/src/clients/auth.rs +++ b/crates/xline-client/src/clients/auth.rs @@ -9,14 +9,12 @@ use xlineapi::{ AuthUserAddResponse, AuthUserChangePasswordResponse, AuthUserDeleteResponse, AuthUserGetResponse, AuthUserGrantRoleResponse, AuthUserListResponse, AuthUserRevokeRoleResponse, AuthenticateResponse, RequestWrapper, ResponseWrapper, + Type as PermissionType, }; use crate::{ error::{Result, XlineClientError}, - types::{ - auth::{AuthRoleRevokePermissionRequest, Permission, PermissionType}, - range_end::RangeOption, - }, + types::{auth::Permission, range_end::RangeOption}, AuthService, CurpClient, }; @@ -717,9 +715,7 @@ impl AuthClient { /// # Examples /// /// ```no_run - /// use xline_client::{ - /// types::auth::AuthRoleRevokePermissionRequest, Client, ClientOptions, - /// }; + /// use xline_client::{Client, ClientOptions, types::range_end::RangeOption}; /// use anyhow::Result; /// /// #[tokio::main] @@ -732,8 +728,13 @@ impl AuthClient { /// /// // grant the role /// + /// client.role_revoke_permission("role", "key", None).await?; /// client - /// .role_revoke_permission(AuthRoleRevokePermissionRequest::new("role", "key")) + /// .role_revoke_permission( + /// "role2", + /// "hi", + /// Some(RangeOption::RangeEnd("hjj".into())), + /// ) /// .await?; /// /// Ok(()) @@ -742,9 +743,21 @@ impl AuthClient { #[inline] pub async fn role_revoke_permission( &self, - request: AuthRoleRevokePermissionRequest, + name: impl Into, + key: impl Into>, + range_option: Option, ) -> Result { - self.handle_req(request.inner, false).await + let mut key = key.into(); + let range_end = range_option.unwrap_or_default().get_range_end(&mut key); + self.handle_req( + xlineapi::AuthRoleRevokePermissionRequest { + role: name.into(), + key, + range_end, + }, + false, + ) + .await } /// Send request using fast path diff --git a/crates/xline-client/src/types/auth.rs b/crates/xline-client/src/types/auth.rs index 87291ee57..a025d7323 100644 --- a/crates/xline-client/src/types/auth.rs +++ b/crates/xline-client/src/types/auth.rs @@ -1,4 +1,3 @@ -use xlineapi::command::KeyRange; pub use xlineapi::{ AuthDisableResponse, AuthEnableResponse, AuthRoleAddResponse, AuthRoleDeleteResponse, AuthRoleGetResponse, AuthRoleGrantPermissionResponse, AuthRoleListResponse, @@ -10,70 +9,6 @@ pub use xlineapi::{ use super::range_end::RangeOption; -/// Request for `AuthRoleRevokePermission` -#[derive(Debug, PartialEq)] -pub struct AuthRoleRevokePermissionRequest { - /// Inner request - pub(crate) inner: xlineapi::AuthRoleRevokePermissionRequest, -} - -impl AuthRoleRevokePermissionRequest { - /// Creates a new `RoleRevokePermissionOption` from pb role revoke permission. - /// - /// `role` is the name of the role to revoke permission, - /// `key` is the key to revoke from the role. - #[inline] - pub fn new(role: impl Into, key: impl Into>) -> Self { - Self { - inner: xlineapi::AuthRoleRevokePermissionRequest { - role: role.into(), - key: key.into(), - ..Default::default() - }, - } - } - - /// If set, Xline will return all keys with the matching prefix - #[inline] - #[must_use] - pub fn with_prefix(mut self) -> Self { - if self.inner.key.is_empty() { - self.inner.key = vec![0]; - self.inner.range_end = vec![0]; - } else { - self.inner.range_end = KeyRange::get_prefix(&self.inner.key); - } - self - } - - /// If set, Xline will return all keys that are equal or greater than the given key - #[inline] - #[must_use] - pub fn with_from_key(mut self) -> Self { - if self.inner.key.is_empty() { - self.inner.key = vec![0]; - } - self.inner.range_end = vec![0]; - self - } - - /// `range_end` is the upper bound on the requested range \[key,` range_en`d). - /// If `range_end` is '\0', the range is all keys >= key. - #[inline] - #[must_use] - pub fn with_range_end(mut self, range_end: impl Into>) -> Self { - self.inner.range_end = range_end.into(); - self - } -} - -impl From for xlineapi::AuthRoleRevokePermissionRequest { - #[inline] - fn from(req: AuthRoleRevokePermissionRequest) -> Self { - req.inner - } -} - /// Role access permission. #[derive(Debug, Clone)] pub struct Permission { diff --git a/crates/xline-client/tests/it/auth.rs b/crates/xline-client/tests/it/auth.rs index ecd77e5b0..da32304c2 100644 --- a/crates/xline-client/tests/it/auth.rs +++ b/crates/xline-client/tests/it/auth.rs @@ -2,7 +2,7 @@ use xline_client::{ error::Result, types::{ - auth::{AuthRoleRevokePermissionRequest, Permission, PermissionType}, + auth::{Permission, PermissionType}, range_end::RangeOption, }, }; @@ -79,24 +79,18 @@ async fn permission_operations_should_success_in_normal_path() -> Result<()> { } // revoke all permission + client.role_revoke_permission(role1, "123", None).await?; client - .role_revoke_permission(AuthRoleRevokePermissionRequest::new(role1, "123")) + .role_revoke_permission(role1, "abc", Some(RangeOption::FromKey)) .await?; client - .role_revoke_permission(AuthRoleRevokePermissionRequest::new(role1, "abc").with_from_key()) + .role_revoke_permission(role1, "hi", Some(RangeOption::RangeEnd("hjj".into()))) .await?; client - .role_revoke_permission( - AuthRoleRevokePermissionRequest::new(role1, "hi").with_range_end("hjj"), - ) + .role_revoke_permission(role1, "pp", Some(RangeOption::Prefix)) .await?; client - .role_revoke_permission(AuthRoleRevokePermissionRequest::new(role1, "pp").with_prefix()) - .await?; - client - .role_revoke_permission( - AuthRoleRevokePermissionRequest::new(role1, vec![0]).with_from_key(), - ) + .role_revoke_permission(role1, vec![0], Some(RangeOption::FromKey)) .await?; let role_get_resp = client.role_get(role1).await?; diff --git a/crates/xlinectl/src/command/role/revoke_perm.rs b/crates/xlinectl/src/command/role/revoke_perm.rs index 8ba5c2071..8973c605b 100644 --- a/crates/xlinectl/src/command/role/revoke_perm.rs +++ b/crates/xlinectl/src/command/role/revoke_perm.rs @@ -1,8 +1,11 @@ use clap::{arg, ArgMatches, Command}; -use xline_client::{error::Result, types::auth::AuthRoleRevokePermissionRequest, Client}; +use xline_client::{error::Result, types::range_end::RangeOption, Client}; use crate::utils::printer::Printer; +/// Temp request type for `revoke_perm` command +type AuthRoleRevokePermissionRequest = (String, Vec, Option); + /// Definition of `revoke_perm` command pub(super) fn command() -> Command { Command::new("revoke_perm") @@ -18,19 +21,23 @@ pub(super) fn build_request(matches: &ArgMatches) -> AuthRoleRevokePermissionReq let key = matches.get_one::("key").expect("required"); let range_end = matches.get_one::("range_end"); - let mut request = AuthRoleRevokePermissionRequest::new(name, key.as_bytes()); + let key = key.as_bytes().to_vec(); + let mut option = None; if let Some(range_end) = range_end { - request = request.with_range_end(range_end.as_bytes()); + option = Some(RangeOption::RangeEnd(range_end.as_bytes().to_vec())); }; - request + (name.into(), key, option) } /// Execute the command pub(super) async fn execute(client: &mut Client, matches: &ArgMatches) -> Result<()> { let req = build_request(matches); - let resp = client.auth_client().role_revoke_permission(req).await?; + let resp = client + .auth_client() + .role_revoke_permission(req.0, req.1, req.2) + .await?; resp.print(); Ok(()) @@ -48,11 +55,15 @@ mod tests { let test_cases = vec![ TestCase::new( vec!["revoke_perm", "Admin", "key1", "key2"], - Some(AuthRoleRevokePermissionRequest::new("Admin", "key1").with_range_end("key2")), + Some(( + "Admin".into(), + "key1".into(), + Some(RangeOption::RangeEnd("key2".into())), + )), ), TestCase::new( vec!["revoke_perm", "Admin", "key3"], - Some(AuthRoleRevokePermissionRequest::new("Admin", "key3")), + Some(("Admin".into(), "key3".into(), None)), ), ];