Skip to content

Commit

Permalink
refactor(client)!: AuthClient::role_revoke_permission
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <lxl66566@gmail.com>

fix: add test

Signed-off-by: lxl66566 <lxl66566@gmail.com>

fix(client): fix doc test

Signed-off-by: lxl66566 <lxl66566@gmail.com>
  • Loading branch information
lxl66566 authored and bsbds committed Aug 26, 2024
1 parent d4eeafc commit 617e341
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 104 deletions.
13 changes: 3 additions & 10 deletions crates/xline-client/examples/auth_role.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand Down Expand Up @@ -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?;
Expand Down
33 changes: 23 additions & 10 deletions crates/xline-client/src/clients/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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]
Expand All @@ -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(())
Expand All @@ -742,9 +743,21 @@ impl AuthClient {
#[inline]
pub async fn role_revoke_permission(
&self,
request: AuthRoleRevokePermissionRequest,
name: impl Into<String>,
key: impl Into<Vec<u8>>,
range_option: Option<RangeOption>,
) -> Result<AuthRoleRevokePermissionResponse> {
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
Expand Down
65 changes: 0 additions & 65 deletions crates/xline-client/src/types/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use xlineapi::command::KeyRange;
pub use xlineapi::{
AuthDisableResponse, AuthEnableResponse, AuthRoleAddResponse, AuthRoleDeleteResponse,
AuthRoleGetResponse, AuthRoleGrantPermissionResponse, AuthRoleListResponse,
Expand All @@ -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<String>, key: impl Into<Vec<u8>>) -> 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<Vec<u8>>) -> Self {
self.inner.range_end = range_end.into();
self
}
}

impl From<AuthRoleRevokePermissionRequest> for xlineapi::AuthRoleRevokePermissionRequest {
#[inline]
fn from(req: AuthRoleRevokePermissionRequest) -> Self {
req.inner
}
}

/// Role access permission.
#[derive(Debug, Clone)]
pub struct Permission {
Expand Down
18 changes: 6 additions & 12 deletions crates/xline-client/tests/it/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use xline_client::{
error::Result,
types::{
auth::{AuthRoleRevokePermissionRequest, Permission, PermissionType},
auth::{Permission, PermissionType},
range_end::RangeOption,
},
};
Expand Down Expand Up @@ -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?;
Expand Down
25 changes: 18 additions & 7 deletions crates/xlinectl/src/command/role/revoke_perm.rs
Original file line number Diff line number Diff line change
@@ -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<u8>, Option<RangeOption>);

/// Definition of `revoke_perm` command
pub(super) fn command() -> Command {
Command::new("revoke_perm")
Expand All @@ -18,19 +21,23 @@ pub(super) fn build_request(matches: &ArgMatches) -> AuthRoleRevokePermissionReq
let key = matches.get_one::<String>("key").expect("required");
let range_end = matches.get_one::<String>("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(())
Expand All @@ -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)),
),
];

Expand Down

0 comments on commit 617e341

Please sign in to comment.