2021-12-06 22:45:41 +08:00
|
|
|
mod dump;
|
2021-11-09 01:31:27 +08:00
|
|
|
pub mod error;
|
|
|
|
mod store;
|
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-11-09 01:31:27 +08:00
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
use error::{AuthControllerError, Result};
|
2023-01-26 00:22:32 +08:00
|
|
|
use maplit::hashset;
|
2023-01-25 23:12:40 +08:00
|
|
|
use meilisearch_types::index_uid_pattern::IndexUidPattern;
|
2023-01-11 19:33:56 +08:00
|
|
|
use meilisearch_types::keys::{Action, CreateApiKey, Key, PatchApiKey};
|
2023-01-19 02:07:26 +08:00
|
|
|
use meilisearch_types::milli::update::Setting;
|
2022-01-12 22:35:33 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-21 00:00:07 +08:00
|
|
|
pub use store::open_auth_store_env;
|
|
|
|
use store::{generate_key_as_hexa, HeedAuthStore};
|
2022-02-14 22:32:41 +08:00
|
|
|
use time::OffsetDateTime;
|
2022-06-02 00:06:20 +08:00
|
|
|
use uuid::Uuid;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AuthController {
|
|
|
|
store: Arc<HeedAuthStore>,
|
|
|
|
master_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AuthController {
|
|
|
|
pub fn new(db_path: impl AsRef<Path>, master_key: &Option<String>) -> Result<Self> {
|
|
|
|
let store = HeedAuthStore::new(db_path)?;
|
|
|
|
|
|
|
|
if store.is_empty()? {
|
|
|
|
generate_default_keys(&store)?;
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:00:07 +08:00
|
|
|
Ok(Self { store: Arc::new(store), master_key: master_key.clone() })
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
2023-01-24 23:17:23 +08:00
|
|
|
/// Return the size of the `AuthController` database in bytes.
|
|
|
|
pub fn size(&self) -> Result<u64> {
|
|
|
|
self.store.size()
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:31:34 +08:00
|
|
|
pub fn create_key(&self, create_key: CreateApiKey) -> Result<Key> {
|
2023-01-11 19:33:56 +08:00
|
|
|
match self.store.get_api_key(create_key.uid)? {
|
|
|
|
Some(_) => Err(AuthControllerError::ApiKeyAlreadyExists(create_key.uid.to_string())),
|
|
|
|
None => self.store.put_api_key(create_key.to_key()),
|
2022-05-25 16:32:47 +08:00
|
|
|
}
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
2023-01-11 19:33:56 +08:00
|
|
|
pub fn update_key(&self, uid: Uuid, patch: PatchApiKey) -> Result<Key> {
|
2022-05-25 16:32:47 +08:00
|
|
|
let mut key = self.get_key(uid)?;
|
2023-01-19 02:07:26 +08:00
|
|
|
match patch.description {
|
|
|
|
Setting::NotSet => (),
|
|
|
|
description => key.description = description.set(),
|
|
|
|
};
|
|
|
|
match patch.name {
|
|
|
|
Setting::NotSet => (),
|
|
|
|
name => key.name = name.set(),
|
|
|
|
};
|
2023-01-11 19:33:56 +08:00
|
|
|
key.updated_at = OffsetDateTime::now_utc();
|
2021-11-09 01:31:27 +08:00
|
|
|
self.store.put_api_key(key)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:32:47 +08:00
|
|
|
pub fn get_key(&self, uid: Uuid) -> Result<Key> {
|
2021-11-09 01:31:27 +08:00
|
|
|
self.store
|
2022-05-25 16:32:47 +08:00
|
|
|
.get_api_key(uid)?
|
|
|
|
.ok_or_else(|| AuthControllerError::ApiKeyNotFound(uid.to_string()))
|
|
|
|
}
|
|
|
|
|
2022-06-02 00:06:20 +08:00
|
|
|
pub fn get_optional_uid_from_encoded_key(&self, encoded_key: &[u8]) -> Result<Option<Uuid>> {
|
2022-05-25 16:32:47 +08:00
|
|
|
match &self.master_key {
|
2022-10-21 00:00:07 +08:00
|
|
|
Some(master_key) => {
|
|
|
|
self.store.get_uid_from_encoded_key(encoded_key, master_key.as_bytes())
|
|
|
|
}
|
2022-05-25 16:32:47 +08:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 00:06:20 +08:00
|
|
|
pub fn get_uid_from_encoded_key(&self, encoded_key: &str) -> Result<Uuid> {
|
|
|
|
self.get_optional_uid_from_encoded_key(encoded_key.as_bytes())?
|
|
|
|
.ok_or_else(|| AuthControllerError::ApiKeyNotFound(encoded_key.to_string()))
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
pub fn get_key_filters(
|
|
|
|
&self,
|
2022-05-25 16:32:47 +08:00
|
|
|
uid: Uuid,
|
2022-01-12 22:35:33 +08:00
|
|
|
search_rules: Option<SearchRules>,
|
|
|
|
) -> Result<AuthFilter> {
|
2021-11-09 01:31:27 +08:00
|
|
|
let mut filters = AuthFilter::default();
|
2023-01-26 00:22:32 +08:00
|
|
|
let key = self.get_key(uid)?;
|
|
|
|
|
2023-02-20 16:25:29 +08:00
|
|
|
filters.key_authorized_indexes = SearchRules::Set(key.indexes.into_iter().collect());
|
|
|
|
|
2023-01-26 00:22:32 +08:00
|
|
|
filters.search_rules = match search_rules {
|
|
|
|
Some(search_rules) => search_rules,
|
2023-02-20 16:25:29 +08:00
|
|
|
None => filters.key_authorized_indexes.clone(),
|
2023-01-26 00:22:32 +08:00
|
|
|
};
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2022-09-06 21:13:09 +08:00
|
|
|
filters.allow_index_creation = self.is_key_authorized(uid, Action::IndexesAdd, None)?;
|
2022-05-25 16:32:47 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
Ok(filters)
|
|
|
|
}
|
|
|
|
|
2022-03-03 01:22:34 +08:00
|
|
|
pub fn list_keys(&self) -> Result<Vec<Key>> {
|
2021-11-09 01:31:27 +08:00
|
|
|
self.store.list_api_keys()
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:32:47 +08:00
|
|
|
pub fn delete_key(&self, uid: Uuid) -> Result<()> {
|
|
|
|
if self.store.delete_api_key(uid)? {
|
2021-11-09 01:31:27 +08:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2022-05-25 16:32:47 +08:00
|
|
|
Err(AuthControllerError::ApiKeyNotFound(uid.to_string()))
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_master_key(&self) -> Option<&String> {
|
|
|
|
self.master_key.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
/// Generate a valid key from a key id using the current master key.
|
|
|
|
/// Returns None if no master key has been set.
|
2022-05-25 16:32:47 +08:00
|
|
|
pub fn generate_key(&self, uid: Uuid) -> Option<String> {
|
2022-10-21 00:00:07 +08:00
|
|
|
self.master_key.as_ref().map(|master_key| generate_key_as_hexa(uid, master_key.as_bytes()))
|
2022-01-12 22:35:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the provided key is authorized to make a specific action
|
|
|
|
/// without checking if the key is valid.
|
|
|
|
pub fn is_key_authorized(
|
|
|
|
&self,
|
2022-05-25 16:32:47 +08:00
|
|
|
uid: Uuid,
|
2022-01-12 22:35:33 +08:00
|
|
|
action: Action,
|
|
|
|
index: Option<&str>,
|
|
|
|
) -> Result<bool> {
|
|
|
|
match self
|
|
|
|
.store
|
|
|
|
// check if the key has access to all indexes.
|
2022-05-25 16:32:47 +08:00
|
|
|
.get_expiration_date(uid, action, None)?
|
2022-01-12 22:35:33 +08:00
|
|
|
.or(match index {
|
|
|
|
// else check if the key has access to the requested index.
|
2023-01-25 23:12:40 +08:00
|
|
|
Some(index) => self.store.get_expiration_date(uid, action, Some(index))?,
|
2022-01-12 22:35:33 +08:00
|
|
|
// or to any index if no index has been requested.
|
2022-05-25 16:32:47 +08:00
|
|
|
None => self.store.prefix_first_expiration_date(uid, action)?,
|
2022-01-12 22:35:33 +08:00
|
|
|
}) {
|
|
|
|
// check expiration date.
|
2022-02-14 22:32:41 +08:00
|
|
|
Some(Some(exp)) => Ok(OffsetDateTime::now_utc() < exp),
|
2022-01-12 22:35:33 +08:00
|
|
|
// no expiration date.
|
|
|
|
Some(None) => Ok(true),
|
|
|
|
// action or index forbidden.
|
|
|
|
None => Ok(false),
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 07:39:01 +08:00
|
|
|
|
|
|
|
/// Delete all the keys in the DB.
|
|
|
|
pub fn raw_delete_all_keys(&mut self) -> Result<()> {
|
|
|
|
self.store.delete_all_keys()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete all the keys in the DB.
|
|
|
|
pub fn raw_insert_key(&mut self, key: Key) -> Result<()> {
|
|
|
|
self.store.put_api_key(key)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AuthFilter {
|
2023-02-20 16:25:29 +08:00
|
|
|
search_rules: SearchRules,
|
|
|
|
key_authorized_indexes: SearchRules,
|
2021-12-15 21:52:33 +08:00
|
|
|
pub allow_index_creation: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AuthFilter {
|
|
|
|
fn default() -> Self {
|
2023-02-20 16:25:29 +08:00
|
|
|
Self {
|
|
|
|
search_rules: SearchRules::default(),
|
|
|
|
key_authorized_indexes: SearchRules::default(),
|
|
|
|
allow_index_creation: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AuthFilter {
|
|
|
|
pub fn is_index_authorized(&self, index: &str) -> bool {
|
|
|
|
self.key_authorized_indexes.is_index_authorized(index)
|
|
|
|
&& self.search_rules.is_index_authorized(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_index_search_rules(&self, index: &str) -> Option<IndexSearchRules> {
|
|
|
|
if !self.is_index_authorized(index) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.search_rules.get_index_search_rules(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the list of indexes such that `self.is_index_authorized(index) == true`,
|
|
|
|
/// or `None` if all indexes satisfy this condition.
|
|
|
|
///
|
|
|
|
/// FIXME: this works only when there are no tenant tokens, otherwise it ignores the rules of the API key.
|
|
|
|
///
|
|
|
|
/// It is better to use `is_index_authorized` when possible.
|
|
|
|
pub fn authorized_indexes(&self) -> Option<Vec<IndexUidPattern>> {
|
|
|
|
self.search_rules.authorized_indexes()
|
2021-12-15 21:52:33 +08:00
|
|
|
}
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
2022-01-12 22:35:33 +08:00
|
|
|
/// Transparent wrapper around a list of allowed indexes with the search rules to apply for each.
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum SearchRules {
|
2023-01-26 00:22:32 +08:00
|
|
|
Set(HashSet<IndexUidPattern>),
|
|
|
|
Map(HashMap<IndexUidPattern, Option<IndexSearchRules>>),
|
2022-01-12 22:35:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SearchRules {
|
|
|
|
fn default() -> Self {
|
2023-01-26 00:22:32 +08:00
|
|
|
Self::Set(hashset! { IndexUidPattern::all() })
|
2022-01-12 22:35:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SearchRules {
|
2023-02-20 16:25:29 +08:00
|
|
|
fn is_index_authorized(&self, index: &str) -> bool {
|
2022-01-12 22:35:33 +08:00
|
|
|
match self {
|
2023-01-25 23:12:40 +08:00
|
|
|
Self::Set(set) => {
|
|
|
|
set.contains("*")
|
|
|
|
|| set.contains(index)
|
2023-01-26 00:22:32 +08:00
|
|
|
|| set.iter().any(|pattern| pattern.matches_str(index))
|
2023-01-25 23:12:40 +08:00
|
|
|
}
|
|
|
|
Self::Map(map) => {
|
|
|
|
map.contains_key("*")
|
|
|
|
|| map.contains_key(index)
|
2023-01-26 00:22:32 +08:00
|
|
|
|| map.keys().any(|pattern| pattern.matches_str(index))
|
2023-01-25 23:12:40 +08:00
|
|
|
}
|
2022-01-12 22:35:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:25:29 +08:00
|
|
|
fn get_index_search_rules(&self, index: &str) -> Option<IndexSearchRules> {
|
2022-01-12 22:35:33 +08:00
|
|
|
match self {
|
2023-02-02 01:21:45 +08:00
|
|
|
Self::Set(_) => {
|
2023-01-26 00:22:32 +08:00
|
|
|
if self.is_index_authorized(index) {
|
2022-01-12 22:35:33 +08:00
|
|
|
Some(IndexSearchRules::default())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 00:00:07 +08:00
|
|
|
Self::Map(map) => {
|
2023-01-26 00:22:32 +08:00
|
|
|
// We must take the most retrictive rule of this index uid patterns set of rules.
|
|
|
|
map.iter()
|
|
|
|
.filter(|(pattern, _)| pattern.matches_str(index))
|
|
|
|
.max_by_key(|(pattern, _)| (pattern.is_exact(), pattern.len()))
|
2023-02-09 20:21:20 +08:00
|
|
|
.and_then(|(_, rule)| rule.clone())
|
2022-10-21 00:00:07 +08:00
|
|
|
}
|
2022-01-12 22:35:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-27 17:17:50 +08:00
|
|
|
|
|
|
|
/// Return the list of indexes such that `self.is_index_authorized(index) == true`,
|
|
|
|
/// or `None` if all indexes satisfy this condition.
|
2023-02-20 16:25:29 +08:00
|
|
|
fn authorized_indexes(&self) -> Option<Vec<IndexUidPattern>> {
|
2022-10-27 17:17:50 +08:00
|
|
|
match self {
|
|
|
|
SearchRules::Set(set) => {
|
|
|
|
if set.contains("*") {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(set.iter().cloned().collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SearchRules::Map(map) => {
|
|
|
|
if map.contains_key("*") {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(map.keys().cloned().collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 22:35:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for SearchRules {
|
2023-01-26 00:22:32 +08:00
|
|
|
type Item = (IndexUidPattern, IndexSearchRules);
|
2022-01-12 22:35:33 +08:00
|
|
|
type IntoIter = Box<dyn Iterator<Item = Self::Item>>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
match self {
|
|
|
|
Self::Set(array) => {
|
|
|
|
Box::new(array.into_iter().map(|i| (i, IndexSearchRules::default())))
|
|
|
|
}
|
|
|
|
Self::Map(map) => {
|
|
|
|
Box::new(map.into_iter().map(|(i, isr)| (i, isr.unwrap_or_default())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Contains the rules to apply on the top of the search query for a specific index.
|
|
|
|
///
|
|
|
|
/// filter: search filter to apply in addition to query filters.
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
|
|
|
pub struct IndexSearchRules {
|
|
|
|
pub filter: Option<serde_json::Value>,
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
fn generate_default_keys(store: &HeedAuthStore) -> Result<()> {
|
|
|
|
store.put_api_key(Key::default_admin())?;
|
|
|
|
store.put_api_key(Key::default_search())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-12-22 18:55:27 +08:00
|
|
|
|
|
|
|
pub const MASTER_KEY_MIN_SIZE: usize = 16;
|
|
|
|
const MASTER_KEY_GEN_SIZE: usize = 32;
|
|
|
|
|
|
|
|
pub fn generate_master_key() -> String {
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
use rand::RngCore;
|
|
|
|
|
2023-01-02 23:33:02 +08:00
|
|
|
// We need to use a cryptographically-secure source of randomness. That's why we're using the OsRng; https://crates.io/crates/getrandom
|
2022-12-22 18:55:27 +08:00
|
|
|
let mut csprng = OsRng;
|
|
|
|
let mut buf = vec![0; MASTER_KEY_GEN_SIZE];
|
|
|
|
csprng.fill_bytes(&mut buf);
|
|
|
|
|
|
|
|
// let's encode the random bytes to base64 to make them human-readable and not too long.
|
|
|
|
// We're using the URL_SAFE alphabet that will produce keys without =, / or other unusual characters.
|
|
|
|
base64::encode_config(buf, base64::URL_SAFE_NO_PAD)
|
|
|
|
}
|