Adapt code to new flags structure

This commit is contained in:
F. Levi 2024-10-20 10:18:09 +03:00
parent e9668eff79
commit 04d86a4d9e
3 changed files with 23 additions and 17 deletions

View File

@ -293,18 +293,24 @@ impl HeedAuthStore {
/// optionally on a specific index, for a given key. /// optionally on a specific index, for a given key.
pub struct KeyIdActionCodec; pub struct KeyIdActionCodec;
impl KeyIdActionCodec {
fn action_parts_to_repr([p1, p2, p3, p4]: &[u8; 4]) -> u32 {
((p1 << 24) | (p2 << 16) | (p3 << 8) | p4) as u32
}
}
impl<'a> milli::heed::BytesDecode<'a> for KeyIdActionCodec { impl<'a> milli::heed::BytesDecode<'a> for KeyIdActionCodec {
type DItem = (KeyId, Action, Option<&'a [u8]>); type DItem = (KeyId, Action, Option<&'a [u8]>);
fn bytes_decode(bytes: &'a [u8]) -> StdResult<Self::DItem, BoxedError> { fn bytes_decode(bytes: &'a [u8]) -> StdResult<Self::DItem, BoxedError> {
let (key_id_bytes, action_bytes) = try_split_array_at(bytes).ok_or(SliceTooShortError)?; let (key_id_bytes, action_bytes) = try_split_array_at(bytes).ok_or(SliceTooShortError)?;
let (&action_byte, index) = let (action_repr, index) =
match try_split_array_at(action_bytes).ok_or(SliceTooShortError)? { match try_split_array_at::<u8, 4>(action_bytes).ok_or(SliceTooShortError)? {
([action], []) => (action, None), (action_parts, []) => (Self::action_parts_to_repr(action_parts), None),
([action], index) => (action, Some(index)), (action_parts, index) => (Self::action_parts_to_repr(action_parts), Some(index)),
}; };
let key_id = Uuid::from_bytes(*key_id_bytes); let key_id = Uuid::from_bytes(*key_id_bytes);
let action = Action::from_repr(action_byte).ok_or(InvalidActionError { action_byte })?; let action = Action::from_repr(action_repr).ok_or(InvalidActionError { action_repr })?;
Ok((key_id, action, index)) Ok((key_id, action, index))
} }
@ -317,7 +323,7 @@ impl<'a> milli::heed::BytesEncode<'a> for KeyIdActionCodec {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
bytes.extend_from_slice(key_id.as_bytes()); bytes.extend_from_slice(key_id.as_bytes());
let action_bytes = u8::to_be_bytes(action.repr()); let action_bytes = u32::to_be_bytes(action.repr());
bytes.extend_from_slice(&action_bytes); bytes.extend_from_slice(&action_bytes);
if let Some(index) = index { if let Some(index) = index {
bytes.extend_from_slice(index); bytes.extend_from_slice(index);
@ -332,9 +338,9 @@ impl<'a> milli::heed::BytesEncode<'a> for KeyIdActionCodec {
pub struct SliceTooShortError; pub struct SliceTooShortError;
#[derive(Error, Debug)] #[derive(Error, Debug)]
#[error("cannot construct a valid Action from {action_byte}")] #[error("cannot construct a valid Action from {action_repr}")]
pub struct InvalidActionError { pub struct InvalidActionError {
pub action_byte: u8, pub action_repr: u32,
} }
pub fn generate_key_as_hexa(uid: Uuid, master_key: &[u8]) -> String { pub fn generate_key_as_hexa(uid: Uuid, master_key: &[u8]) -> String {

View File

@ -314,14 +314,14 @@ impl Action {
SETTINGS_UPDATE => Some(Self::SettingsUpdate), SETTINGS_UPDATE => Some(Self::SettingsUpdate),
SETTINGS_ALL => Some(Self::SettingsAll), SETTINGS_ALL => Some(Self::SettingsAll),
STATS_GET => Some(Self::StatsGet), STATS_GET => Some(Self::StatsGet),
// @TODO: Issue: Since stats has only one element, all is the same as the one single element // TODO: Issue: Since stats has only one element, all is the same as the one single element
// so this will never match all, because it matches that one and only element first // so this will never match all, because it matches that one and only element first
STATS_ALL => Some(Self::StatsAll), STATS_ALL => Some(Self::StatsAll),
METRICS_GET => Some(Self::MetricsGet), METRICS_GET => Some(Self::MetricsGet),
// @TODO: Same // TODO: Same
METRICS_ALL => Some(Self::MetricsAll), METRICS_ALL => Some(Self::MetricsAll),
DUMPS_CREATE => Some(Self::DumpsCreate), DUMPS_CREATE => Some(Self::DumpsCreate),
// @TODO: Same // TODO: Same
DUMPS_ALL => Some(Self::DumpsAll), DUMPS_ALL => Some(Self::DumpsAll),
SNAPSHOTS_CREATE => Some(Self::SnapshotsCreate), SNAPSHOTS_CREATE => Some(Self::SnapshotsCreate),
VERSION => Some(Self::Version), VERSION => Some(Self::Version),
@ -332,7 +332,7 @@ impl Action {
EXPERIMENTAL_FEATURES_GET => Some(Self::ExperimentalFeaturesGet), EXPERIMENTAL_FEATURES_GET => Some(Self::ExperimentalFeaturesGet),
EXPERIMENTAL_FEATURES_UPDATE => Some(Self::ExperimentalFeaturesUpdate), EXPERIMENTAL_FEATURES_UPDATE => Some(Self::ExperimentalFeaturesUpdate),
ALL => Some(Self::All), ALL => Some(Self::All),
_otherwise => None, _ => None,
} }
} }

View File

@ -171,7 +171,7 @@ pub mod policies {
#[error("Could not decode tenant token, {0}.")] #[error("Could not decode tenant token, {0}.")]
CouldNotDecodeTenantToken(jsonwebtoken::errors::Error), CouldNotDecodeTenantToken(jsonwebtoken::errors::Error),
#[error("Invalid action `{0}`.")] #[error("Invalid action `{0}`.")]
InternalInvalidAction(u8), InternalInvalidAction(u32),
} }
impl From<jsonwebtoken::errors::Error> for AuthError { impl From<jsonwebtoken::errors::Error> for AuthError {
@ -214,14 +214,14 @@ pub mod policies {
Ok(api_key_uid) Ok(api_key_uid)
} }
fn is_keys_action(action: u8) -> bool { fn is_keys_action(action: u32) -> bool {
use actions::*; use actions::*;
matches!(action, KEYS_GET | KEYS_CREATE | KEYS_UPDATE | KEYS_DELETE) matches!(action, KEYS_GET | KEYS_CREATE | KEYS_UPDATE | KEYS_DELETE)
} }
pub struct ActionPolicy<const A: u8>; pub struct ActionPolicy<const A: u32>;
impl<const A: u8> Policy for ActionPolicy<A> { impl<const A: u32> Policy for ActionPolicy<A> {
/// Attempts to grant authentication from a bearer token (that can be a tenant token or an API key), the requested Action, /// Attempts to grant authentication from a bearer token (that can be a tenant token or an API key), the requested Action,
/// and a list of requested indexes. /// and a list of requested indexes.
/// ///
@ -294,7 +294,7 @@ pub mod policies {
} }
} }
impl<const A: u8> ActionPolicy<A> { impl<const A: u32> ActionPolicy<A> {
fn authenticate_tenant_token( fn authenticate_tenant_token(
auth: &AuthController, auth: &AuthController,
token: &str, token: &str,