use std::{
future::{Future, IntoFuture},
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use crate::{
Client, ResponseHeader,
client::ClientInner,
pb::{authpb, etcdserverpb},
range::{AsRange, TargetRange},
};
impl Client {
pub fn role_add(&self, name: &str) -> RoleAdd<Self> {
RoleAdd::new(name).with_client(self.clone())
}
pub fn role_get(&self, name: &str) -> RoleGet<Self> {
RoleGet::new(name).with_client(self.clone())
}
pub fn role_list(&self) -> RoleList<Self> {
RoleList::new().with_client(self.clone())
}
pub fn role_delete(&self, name: &str) -> RoleDelete<Self> {
RoleDelete::new(name).with_client(self.clone())
}
pub fn role_grant_permission(&self, name: &str, permission: Permission) -> RoleGrantPermission<Self> {
RoleGrantPermission::new(name, permission).with_client(self.clone())
}
pub fn role_revoke_permission(&self, name: &str, range: impl AsRange) -> RoleRevokePermission<Self> {
RoleRevokePermission::new(name, range).with_client(self.clone())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PermissionType {
Read,
Write,
ReadWrite,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Permission {
perm_type: PermissionType,
key: Bytes,
range_end: Bytes,
}
impl Permission {
pub fn read(range: impl AsRange) -> Self {
Self::new(PermissionType::Read, range)
}
pub fn write(range: impl AsRange) -> Self {
Self::new(PermissionType::Write, range)
}
pub fn read_write(range: impl AsRange) -> Self {
Self::new(PermissionType::ReadWrite, range)
}
fn new(perm_type: PermissionType, range: impl AsRange) -> Self {
let (key, range_end) = range.as_boundaries();
Self {
perm_type,
key,
range_end,
}
}
pub fn permission_type(&self) -> PermissionType {
self.perm_type
}
pub fn target_range(&self) -> TargetRange<'_> {
TargetRange::from_wire(&self.key, &self.range_end)
}
pub(crate) fn from_pb(pb: authpb::Permission) -> Self {
let perm_type = match authpb::permission::Type::try_from(pb.perm_type)
.expect("etcd should return a known permission type")
{
authpb::permission::Type::Read => PermissionType::Read,
authpb::permission::Type::Write => PermissionType::Write,
authpb::permission::Type::Readwrite => PermissionType::ReadWrite,
};
Self {
perm_type,
key: pb.key,
range_end: pb.range_end,
}
}
pub(crate) fn into_pb(self) -> authpb::Permission {
let perm_type = match self.perm_type {
PermissionType::Read => authpb::permission::Type::Read,
PermissionType::Write => authpb::permission::Type::Write,
PermissionType::ReadWrite => authpb::permission::Type::Readwrite,
};
authpb::Permission {
perm_type: perm_type as i32,
key: self.key,
range_end: self.range_end,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct RoleAddResponse {
header: ResponseHeader,
}
impl RoleAddResponse {
pub(crate) fn new(header: ResponseHeader) -> Self {
Self { header }
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
}
#[derive(Clone)]
#[must_use = "RoleAdd does nothing unless you `await` it"]
pub struct RoleAdd<C> {
client: C,
pub(crate) request: etcdserverpb::AuthRoleAddRequest,
}
impl RoleAdd<()> {
pub fn new(name: &str) -> Self {
Self {
client: (),
request: etcdserverpb::AuthRoleAddRequest { name: name.into() },
}
}
}
impl<C> RoleAdd<C> {
pub fn with_client<C2>(self, client: C2) -> RoleAdd<C2> {
RoleAdd {
client,
request: self.request,
}
}
pub fn name(&self) -> &str {
&self.request.name
}
pub(crate) fn into_parts(self) -> (C, RoleAdd<()>) {
(
self.client,
RoleAdd {
client: (),
request: self.request,
},
)
}
}
pub struct RoleAddFuture(Pin<Box<dyn Future<Output = Result<RoleAddResponse, RoleError>> + Send>>);
impl RoleAddFuture {
pub(crate) fn new(future: impl Future<Output = Result<RoleAddResponse, RoleError>> + Send + 'static) -> Self {
Self(Box::pin(future))
}
}
impl Future for RoleAddFuture {
type Output = Result<RoleAddResponse, RoleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}
impl<C: crate::driver::AuthDriver> IntoFuture for RoleAdd<C> {
type Output = Result<RoleAddResponse, RoleError>;
type IntoFuture = C::RoleAddFuture;
fn into_future(self) -> Self::IntoFuture {
let (client, detached) = self.into_parts();
client.execute_role_add(detached)
}
}
#[derive(Clone)]
#[must_use = "RoleGet does nothing unless you `await` it"]
pub struct RoleGet<C> {
client: C,
pub(crate) request: etcdserverpb::AuthRoleGetRequest,
}
impl RoleGet<()> {
pub fn new(name: &str) -> Self {
Self {
client: (),
request: etcdserverpb::AuthRoleGetRequest { role: name.into() },
}
}
}
impl<C> RoleGet<C> {
pub fn with_client<C2>(self, client: C2) -> RoleGet<C2> {
RoleGet {
client,
request: self.request,
}
}
pub fn name(&self) -> &str {
&self.request.role
}
pub(crate) fn into_parts(self) -> (C, RoleGet<()>) {
(
self.client,
RoleGet {
client: (),
request: self.request,
},
)
}
}
#[derive(Clone, Debug)]
pub struct RoleGetResponse {
header: ResponseHeader,
permissions: Vec<Permission>,
}
impl RoleGetResponse {
pub(crate) fn new(header: ResponseHeader, permissions: Vec<Permission>) -> Self {
Self { header, permissions }
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
pub fn permissions(&self) -> &[Permission] {
&self.permissions
}
pub fn into_permissions(self) -> Vec<Permission> {
self.permissions
}
}
pub struct RoleGetFuture(Pin<Box<dyn Future<Output = Result<RoleGetResponse, RoleError>> + Send>>);
impl RoleGetFuture {
pub(crate) fn new(future: impl Future<Output = Result<RoleGetResponse, RoleError>> + Send + 'static) -> Self {
Self(Box::pin(future))
}
}
impl Future for RoleGetFuture {
type Output = Result<RoleGetResponse, RoleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}
impl<C: crate::driver::AuthDriver> IntoFuture for RoleGet<C> {
type Output = Result<RoleGetResponse, RoleError>;
type IntoFuture = C::RoleGetFuture;
fn into_future(self) -> Self::IntoFuture {
let (client, detached) = self.into_parts();
client.execute_role_get(detached)
}
}
#[derive(Clone)]
#[must_use = "RoleList does nothing unless you `await` it"]
pub struct RoleList<C> {
client: C,
}
impl RoleList<()> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self { client: () }
}
}
impl<C> RoleList<C> {
pub fn with_client<C2>(self, client: C2) -> RoleList<C2> {
RoleList { client }
}
pub(crate) fn into_parts(self) -> (C, RoleList<()>) {
(self.client, RoleList { client: () })
}
}
#[derive(Clone, Debug)]
pub struct RoleListResponse {
header: ResponseHeader,
roles: Vec<String>,
}
impl RoleListResponse {
pub(crate) fn new(header: ResponseHeader, roles: Vec<String>) -> Self {
Self { header, roles }
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
pub fn roles(&self) -> &[String] {
&self.roles
}
pub fn into_roles(self) -> Vec<String> {
self.roles
}
}
pub struct RoleListFuture(Pin<Box<dyn Future<Output = Result<RoleListResponse, RoleError>> + Send>>);
impl RoleListFuture {
pub(crate) fn new(future: impl Future<Output = Result<RoleListResponse, RoleError>> + Send + 'static) -> Self {
Self(Box::pin(future))
}
}
impl Future for RoleListFuture {
type Output = Result<RoleListResponse, RoleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}
impl<C: crate::driver::AuthDriver> IntoFuture for RoleList<C> {
type Output = Result<RoleListResponse, RoleError>;
type IntoFuture = C::RoleListFuture;
fn into_future(self) -> Self::IntoFuture {
let (client, detached) = self.into_parts();
client.execute_role_list(detached)
}
}
#[derive(Clone)]
#[must_use = "RoleDelete does nothing unless you `await` it"]
pub struct RoleDelete<C> {
client: C,
pub(crate) request: etcdserverpb::AuthRoleDeleteRequest,
}
impl RoleDelete<()> {
pub fn new(name: &str) -> Self {
Self {
client: (),
request: etcdserverpb::AuthRoleDeleteRequest { role: name.into() },
}
}
}
impl<C> RoleDelete<C> {
pub fn with_client<C2>(self, client: C2) -> RoleDelete<C2> {
RoleDelete {
client,
request: self.request,
}
}
pub fn name(&self) -> &str {
&self.request.role
}
pub(crate) fn into_parts(self) -> (C, RoleDelete<()>) {
(
self.client,
RoleDelete {
client: (),
request: self.request,
},
)
}
}
#[derive(Clone, Copy, Debug)]
pub struct RoleDeleteResponse {
header: ResponseHeader,
}
impl RoleDeleteResponse {
pub(crate) fn new(header: ResponseHeader) -> Self {
Self { header }
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
}
pub struct RoleDeleteFuture(Pin<Box<dyn Future<Output = Result<RoleDeleteResponse, RoleError>> + Send>>);
impl RoleDeleteFuture {
pub(crate) fn new(future: impl Future<Output = Result<RoleDeleteResponse, RoleError>> + Send + 'static) -> Self {
Self(Box::pin(future))
}
}
impl Future for RoleDeleteFuture {
type Output = Result<RoleDeleteResponse, RoleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}
impl<C: crate::driver::AuthDriver> IntoFuture for RoleDelete<C> {
type Output = Result<RoleDeleteResponse, RoleError>;
type IntoFuture = C::RoleDeleteFuture;
fn into_future(self) -> Self::IntoFuture {
let (client, detached) = self.into_parts();
client.execute_role_delete(detached)
}
}
#[derive(Clone)]
#[must_use = "RoleGrantPermission does nothing unless you `await` it"]
pub struct RoleGrantPermission<C> {
client: C,
pub(crate) name: String,
pub(crate) permission: Permission,
}
impl RoleGrantPermission<()> {
pub fn new(name: &str, permission: Permission) -> Self {
Self {
client: (),
name: name.into(),
permission,
}
}
}
impl<C> RoleGrantPermission<C> {
pub fn with_client<C2>(self, client: C2) -> RoleGrantPermission<C2> {
RoleGrantPermission {
client,
name: self.name,
permission: self.permission,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn permission(&self) -> &Permission {
&self.permission
}
pub(crate) fn into_parts(self) -> (C, RoleGrantPermission<()>) {
(
self.client,
RoleGrantPermission {
client: (),
name: self.name,
permission: self.permission,
},
)
}
}
#[derive(Clone, Copy, Debug)]
pub struct RoleGrantPermissionResponse {
header: ResponseHeader,
}
impl RoleGrantPermissionResponse {
pub(crate) fn new(header: ResponseHeader) -> Self {
Self { header }
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
}
pub struct RoleGrantPermissionFuture(
Pin<Box<dyn Future<Output = Result<RoleGrantPermissionResponse, RoleError>> + Send>>,
);
impl RoleGrantPermissionFuture {
pub(crate) fn new(
future: impl Future<Output = Result<RoleGrantPermissionResponse, RoleError>> + Send + 'static,
) -> Self {
Self(Box::pin(future))
}
}
impl Future for RoleGrantPermissionFuture {
type Output = Result<RoleGrantPermissionResponse, RoleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}
impl<C: crate::driver::AuthDriver> IntoFuture for RoleGrantPermission<C> {
type Output = Result<RoleGrantPermissionResponse, RoleError>;
type IntoFuture = C::RoleGrantPermissionFuture;
fn into_future(self) -> Self::IntoFuture {
let (client, detached) = self.into_parts();
client.execute_role_grant_permission(detached)
}
}
#[derive(Clone)]
#[must_use = "RoleRevokePermission does nothing unless you `await` it"]
pub struct RoleRevokePermission<C> {
client: C,
pub(crate) request: etcdserverpb::AuthRoleRevokePermissionRequest,
}
impl RoleRevokePermission<()> {
pub fn new(name: &str, range: impl AsRange) -> Self {
let (key, range_end) = range.as_boundaries();
Self {
client: (),
request: etcdserverpb::AuthRoleRevokePermissionRequest {
role: name.into(),
key,
range_end,
},
}
}
}
impl<C> RoleRevokePermission<C> {
pub fn with_client<C2>(self, client: C2) -> RoleRevokePermission<C2> {
RoleRevokePermission {
client,
request: self.request,
}
}
pub fn name(&self) -> &str {
&self.request.role
}
pub fn target_range(&self) -> TargetRange<'_> {
TargetRange::from_wire(&self.request.key, &self.request.range_end)
}
pub(crate) fn into_parts(self) -> (C, RoleRevokePermission<()>) {
(
self.client,
RoleRevokePermission {
client: (),
request: self.request,
},
)
}
}
#[derive(Clone, Copy, Debug)]
pub struct RoleRevokePermissionResponse {
header: ResponseHeader,
}
impl RoleRevokePermissionResponse {
pub(crate) fn new(header: ResponseHeader) -> Self {
Self { header }
}
pub fn header(&self) -> &ResponseHeader {
&self.header
}
}
pub struct RoleRevokePermissionFuture(
Pin<Box<dyn Future<Output = Result<RoleRevokePermissionResponse, RoleError>> + Send>>,
);
impl RoleRevokePermissionFuture {
pub(crate) fn new(
future: impl Future<Output = Result<RoleRevokePermissionResponse, RoleError>> + Send + 'static,
) -> Self {
Self(Box::pin(future))
}
}
impl Future for RoleRevokePermissionFuture {
type Output = Result<RoleRevokePermissionResponse, RoleError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}
impl<C: crate::driver::AuthDriver> IntoFuture for RoleRevokePermission<C> {
type Output = Result<RoleRevokePermissionResponse, RoleError>;
type IntoFuture = C::RoleRevokePermissionFuture;
fn into_future(self) -> Self::IntoFuture {
let (client, detached) = self.into_parts();
client.execute_role_revoke_permission(detached)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RoleErrorKind {
RoleAlreadyExists,
RoleNotFound,
PermissionNotGranted,
InvalidAuthManagement,
AuthNotEnabled,
Authentication,
Exhausted,
Unavailable,
Timeout,
Unknown,
}
define_op_error! {
pub struct RoleError(RoleErrorKind);
}
impl RoleError {
pub(crate) fn from_status(status: tonic::Status) -> Self {
let kind = match status.code() {
tonic::Code::FailedPrecondition => {
let msg = status.message();
if msg.contains("already exists") {
RoleErrorKind::RoleAlreadyExists
} else if msg.contains("role name not found") {
RoleErrorKind::RoleNotFound
} else if msg.contains("not granted") {
RoleErrorKind::PermissionNotGranted
} else if msg.contains("not enabled") {
RoleErrorKind::AuthNotEnabled
} else {
RoleErrorKind::Unknown
}
}
tonic::Code::Unauthenticated => RoleErrorKind::Authentication,
tonic::Code::PermissionDenied => RoleErrorKind::Authentication,
tonic::Code::InvalidArgument => {
if status.message().contains("invalid auth management") {
RoleErrorKind::InvalidAuthManagement
} else if ClientInner::is_stale_token_error(&status) {
RoleErrorKind::Authentication
} else {
RoleErrorKind::Unknown
}
}
tonic::Code::ResourceExhausted => RoleErrorKind::Exhausted,
tonic::Code::Unavailable => RoleErrorKind::Unavailable,
tonic::Code::Cancelled | tonic::Code::DeadlineExceeded => RoleErrorKind::Timeout,
_ => RoleErrorKind::Unknown,
};
Self::new(kind, "", Some(status))
}
}
const _: () = {
fn _assert_send<T: Send>() {}
fn _check() {
_assert_send::<RoleAddFuture>();
_assert_send::<RoleGetFuture>();
_assert_send::<RoleListFuture>();
_assert_send::<RoleDeleteFuture>();
_assert_send::<RoleGrantPermissionFuture>();
_assert_send::<RoleRevokePermissionFuture>();
}
};
#[cfg(test)]
mod test {
use super::*;
#[test]
fn invalid_argument_is_authentication_only_for_token_messages() {
use RoleErrorKind::*;
for (message, expected) in [
("etcdserver: user name is empty", Authentication),
("etcdserver: revision of auth store is old", Authentication),
("etcdserver: invalid auth management", InvalidAuthManagement),
("etcdserver: role name is empty", Unknown),
("etcdserver: request is too large", Unknown),
] {
let kind = RoleError::from_status(tonic::Status::invalid_argument(message)).kind();
assert_eq!(kind, expected, "{message:?}");
}
}
}