diff --git a/meilisearch-auth/src/store.rs b/meilisearch-auth/src/store.rs index 20198e497..62f2f9180 100644 --- a/meilisearch-auth/src/store.rs +++ b/meilisearch-auth/src/store.rs @@ -293,18 +293,24 @@ impl HeedAuthStore { /// optionally on a specific index, for a given key. 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 { type DItem = (KeyId, Action, Option<&'a [u8]>); fn bytes_decode(bytes: &'a [u8]) -> StdResult { let (key_id_bytes, action_bytes) = try_split_array_at(bytes).ok_or(SliceTooShortError)?; - let (&action_byte, index) = - match try_split_array_at(action_bytes).ok_or(SliceTooShortError)? { - ([action], []) => (action, None), - ([action], index) => (action, Some(index)), + let (action_repr, index) = + match try_split_array_at::(action_bytes).ok_or(SliceTooShortError)? { + (action_parts, []) => (Self::action_parts_to_repr(action_parts), None), + (action_parts, index) => (Self::action_parts_to_repr(action_parts), Some(index)), }; 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)) } @@ -317,7 +323,7 @@ impl<'a> milli::heed::BytesEncode<'a> for KeyIdActionCodec { let mut bytes = Vec::new(); 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); if let Some(index) = index { bytes.extend_from_slice(index); @@ -332,9 +338,9 @@ impl<'a> milli::heed::BytesEncode<'a> for KeyIdActionCodec { pub struct SliceTooShortError; #[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 action_byte: u8, + pub action_repr: u32, } pub fn generate_key_as_hexa(uid: Uuid, master_key: &[u8]) -> String { diff --git a/meilisearch-types/src/keys.rs b/meilisearch-types/src/keys.rs index ec3c5246c..e93edb402 100644 --- a/meilisearch-types/src/keys.rs +++ b/meilisearch-types/src/keys.rs @@ -314,14 +314,14 @@ impl Action { SETTINGS_UPDATE => Some(Self::SettingsUpdate), SETTINGS_ALL => Some(Self::SettingsAll), 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 STATS_ALL => Some(Self::StatsAll), METRICS_GET => Some(Self::MetricsGet), - // @TODO: Same + // TODO: Same METRICS_ALL => Some(Self::MetricsAll), DUMPS_CREATE => Some(Self::DumpsCreate), - // @TODO: Same + // TODO: Same DUMPS_ALL => Some(Self::DumpsAll), SNAPSHOTS_CREATE => Some(Self::SnapshotsCreate), VERSION => Some(Self::Version), @@ -332,7 +332,7 @@ impl Action { EXPERIMENTAL_FEATURES_GET => Some(Self::ExperimentalFeaturesGet), EXPERIMENTAL_FEATURES_UPDATE => Some(Self::ExperimentalFeaturesUpdate), ALL => Some(Self::All), - _otherwise => None, + _ => None, } } diff --git a/meilisearch/src/extractors/authentication/mod.rs b/meilisearch/src/extractors/authentication/mod.rs index 28a6d770e..6917f86d8 100644 --- a/meilisearch/src/extractors/authentication/mod.rs +++ b/meilisearch/src/extractors/authentication/mod.rs @@ -171,7 +171,7 @@ pub mod policies { #[error("Could not decode tenant token, {0}.")] CouldNotDecodeTenantToken(jsonwebtoken::errors::Error), #[error("Invalid action `{0}`.")] - InternalInvalidAction(u8), + InternalInvalidAction(u32), } impl From for AuthError { @@ -214,14 +214,14 @@ pub mod policies { Ok(api_key_uid) } - fn is_keys_action(action: u8) -> bool { + fn is_keys_action(action: u32) -> bool { use actions::*; matches!(action, KEYS_GET | KEYS_CREATE | KEYS_UPDATE | KEYS_DELETE) } - pub struct ActionPolicy; + pub struct ActionPolicy; - impl Policy for ActionPolicy { + impl Policy for ActionPolicy { /// 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. /// @@ -294,7 +294,7 @@ pub mod policies { } } - impl ActionPolicy { + impl ActionPolicy { fn authenticate_tenant_token( auth: &AuthController, token: &str,