|
1 | 1 | //! Asynchronous client & synchronous client.
|
2 | 2 |
|
3 | 3 | use crate::error::{Error, Result};
|
| 4 | +use crate::rpc::auth::Permission; |
4 | 5 | use crate::rpc::auth::{AuthClient, AuthDisableResponse, AuthEnableResponse};
|
| 6 | +use crate::rpc::auth::{ |
| 7 | + RoleAddResponse, RoleDeleteResponse, RoleGetResponse, RoleGrantPermissionResponse, |
| 8 | + RoleListResponse, RoleRevokePermissionOptions, RoleRevokePermissionResponse, |
| 9 | +}; |
5 | 10 | use crate::rpc::kv::{
|
6 | 11 | CompactionOptions, CompactionResponse, DeleteOptions, DeleteResponse, GetOptions, GetResponse,
|
7 | 12 | KvClient, PutOptions, PutResponse, Txn, TxnResponse,
|
@@ -233,6 +238,51 @@ impl Client {
|
233 | 238 | pub async fn auth_disable(&mut self) -> Result<AuthDisableResponse> {
|
234 | 239 | self.auth.auth_disable().await
|
235 | 240 | }
|
| 241 | + |
| 242 | + /// Adds role. |
| 243 | + #[inline] |
| 244 | + pub async fn role_add(&mut self, name: impl Into<String>) -> Result<RoleAddResponse> { |
| 245 | + self.auth.role_add(name).await |
| 246 | + } |
| 247 | + |
| 248 | + /// Deletes role. |
| 249 | + #[inline] |
| 250 | + pub async fn role_delete(&mut self, name: impl Into<String>) -> Result<RoleDeleteResponse> { |
| 251 | + self.auth.role_delete(name).await |
| 252 | + } |
| 253 | + |
| 254 | + /// Gets role. |
| 255 | + #[inline] |
| 256 | + pub async fn role_get(&mut self, name: impl Into<String>) -> Result<RoleGetResponse> { |
| 257 | + self.auth.role_get(name).await |
| 258 | + } |
| 259 | + |
| 260 | + /// Lists role. |
| 261 | + #[inline] |
| 262 | + pub async fn role_list(&mut self) -> Result<RoleListResponse> { |
| 263 | + self.auth.role_list().await |
| 264 | + } |
| 265 | + |
| 266 | + /// Grants role permission. |
| 267 | + #[inline] |
| 268 | + pub async fn role_grant_permission( |
| 269 | + &mut self, |
| 270 | + name: impl Into<String>, |
| 271 | + perm: Permission, |
| 272 | + ) -> Result<RoleGrantPermissionResponse> { |
| 273 | + self.auth.role_grant_permission(name, perm).await |
| 274 | + } |
| 275 | + |
| 276 | + /// Revokes role permission. |
| 277 | + #[inline] |
| 278 | + pub async fn role_revoke_permission( |
| 279 | + &mut self, |
| 280 | + name: impl Into<String>, |
| 281 | + key: impl Into<Vec<u8>>, |
| 282 | + options: Option<RoleRevokePermissionOptions>, |
| 283 | + ) -> Result<RoleRevokePermissionResponse> { |
| 284 | + self.auth.role_revoke_permission(name, key, options).await |
| 285 | + } |
236 | 286 | }
|
237 | 287 |
|
238 | 288 | /// Options for `Connect` operation.
|
@@ -260,7 +310,7 @@ impl ConnectOptions {
|
260 | 310 | #[cfg(test)]
|
261 | 311 | mod tests {
|
262 | 312 | use super::*;
|
263 |
| - use crate::{Compare, CompareOp, EventType, TxnOp, TxnOpResponse}; |
| 313 | + use crate::{Compare, CompareOp, EventType, PermissionType, TxnOp, TxnOpResponse}; |
264 | 314 |
|
265 | 315 | /// Get client for testing.
|
266 | 316 | async fn get_client() -> Result<Client> {
|
@@ -637,4 +687,120 @@ mod tests {
|
637 | 687 |
|
638 | 688 | Ok(())
|
639 | 689 | }
|
| 690 | + |
| 691 | + #[tokio::test] |
| 692 | + async fn test_role() -> Result<()> { |
| 693 | + let mut client = get_client().await?; |
| 694 | + |
| 695 | + let role1 = "role1"; |
| 696 | + let role2 = "role2"; |
| 697 | + |
| 698 | + let _resp = client.role_delete(role1).await; |
| 699 | + let _resp = client.role_delete(role2).await; |
| 700 | + client.role_add(role1).await?; |
| 701 | + |
| 702 | + let resp = client.role_get(role1).await; |
| 703 | + if let Err(_) = resp { |
| 704 | + assert!(false); |
| 705 | + } |
| 706 | + |
| 707 | + client.role_delete(role1).await?; |
| 708 | + let resp = client.role_get(role1).await; |
| 709 | + if let Ok(_) = resp { |
| 710 | + assert!(false); |
| 711 | + } |
| 712 | + |
| 713 | + client.role_add(role2).await?; |
| 714 | + let resp = client.role_get(role2).await; |
| 715 | + if let Err(_) = resp { |
| 716 | + assert!(false); |
| 717 | + } |
| 718 | + |
| 719 | + let resp = client.role_list().await; |
| 720 | + if let Err(_) = resp { |
| 721 | + assert!(false); |
| 722 | + } |
| 723 | + |
| 724 | + if let Ok(l) = resp { |
| 725 | + assert!(l.roles().contains(&role2.to_string())); |
| 726 | + } |
| 727 | + |
| 728 | + client |
| 729 | + .role_grant_permission(role2, Permission::read("123")) |
| 730 | + .await?; |
| 731 | + client |
| 732 | + .role_grant_permission(role2, Permission::write("abc").with_from_key()) |
| 733 | + .await?; |
| 734 | + client |
| 735 | + .role_grant_permission(role2, Permission::read_write("hi").with_range_end("hjj")) |
| 736 | + .await?; |
| 737 | + client |
| 738 | + .role_grant_permission( |
| 739 | + role2, |
| 740 | + Permission::new(PermissionType::Write, "pp").with_prefix(), |
| 741 | + ) |
| 742 | + .await?; |
| 743 | + client |
| 744 | + .role_grant_permission( |
| 745 | + role2, |
| 746 | + Permission::new(PermissionType::Read, "xyz").with_all_keys(), |
| 747 | + ) |
| 748 | + .await?; |
| 749 | + |
| 750 | + let resp = client.role_get(role2).await; |
| 751 | + if let Err(_) = resp { |
| 752 | + assert!(false); |
| 753 | + } |
| 754 | + if let Ok(r) = resp { |
| 755 | + let permissions = r.permissions(); |
| 756 | + assert!(permissions.contains(&Permission::read("123"))); |
| 757 | + assert!(permissions.contains(&Permission::write("abc").with_from_key())); |
| 758 | + assert!(permissions.contains(&Permission::read_write("hi").with_range_end("hjj"))); |
| 759 | + assert!(permissions.contains(&Permission::write("pp").with_prefix())); |
| 760 | + assert!(permissions.contains(&Permission::read("xyz").with_all_keys())); |
| 761 | + } |
| 762 | + |
| 763 | + //revoke all permission |
| 764 | + client.role_revoke_permission(role2, "123", None).await?; |
| 765 | + client |
| 766 | + .role_revoke_permission( |
| 767 | + role2, |
| 768 | + "abc", |
| 769 | + Some(RoleRevokePermissionOptions::new().with_from_key()), |
| 770 | + ) |
| 771 | + .await?; |
| 772 | + client |
| 773 | + .role_revoke_permission( |
| 774 | + role2, |
| 775 | + "hi", |
| 776 | + Some(RoleRevokePermissionOptions::new().with_range_end("hjj")), |
| 777 | + ) |
| 778 | + .await?; |
| 779 | + client |
| 780 | + .role_revoke_permission( |
| 781 | + role2, |
| 782 | + "pp", |
| 783 | + Some(RoleRevokePermissionOptions::new().with_prefix()), |
| 784 | + ) |
| 785 | + .await?; |
| 786 | + client |
| 787 | + .role_revoke_permission( |
| 788 | + role2, |
| 789 | + "xyz", |
| 790 | + Some(RoleRevokePermissionOptions::new().with_all_keys()), |
| 791 | + ) |
| 792 | + .await?; |
| 793 | + |
| 794 | + let resp = client.role_get(role2).await; |
| 795 | + if let Err(_) = resp { |
| 796 | + assert!(false); |
| 797 | + } |
| 798 | + if let Ok(r) = resp { |
| 799 | + assert!(r.permissions().is_empty()); |
| 800 | + } |
| 801 | + |
| 802 | + client.role_delete(role2).await?; |
| 803 | + |
| 804 | + Ok(()) |
| 805 | + } |
640 | 806 | }
|
0 commit comments