-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API key requirement to Ingest methods
Add API key management methods Update UI
- Loading branch information
Showing
20 changed files
with
773 additions
and
296 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
notifico-app/migration/src/m20250203_000001_create_apikey_table.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use sea_orm_migration::{prelude::*, schema::*}; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(ApiKey::Table) | ||
.if_not_exists() | ||
.col(pk_uuid(ApiKey::Id)) | ||
.col(uuid_uniq(ApiKey::Key)) | ||
.col(uuid(ApiKey::ProjectId)) | ||
.col(string(ApiKey::Description).default("")) | ||
.col(date_time(ApiKey::CreatedAt).default(Expr::current_timestamp())) | ||
.foreign_key( | ||
ForeignKey::create() | ||
.from(ApiKey::Table, ApiKey::ProjectId) | ||
.to(Project::Table, Project::Id) | ||
.on_delete(ForeignKeyAction::Cascade) | ||
.on_update(ForeignKeyAction::Restrict), | ||
) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum ApiKey { | ||
Table, | ||
Id, | ||
Key, | ||
ProjectId, | ||
Description, | ||
CreatedAt, | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Project { | ||
Table, | ||
Id, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
use crate::crud_table::{ | ||
AdminCrudError, AdminCrudTable, ItemWithId, ListQueryParams, ListableTrait, PaginatedResult, | ||
}; | ||
use crate::entity; | ||
use futures::FutureExt; | ||
use metrics::{counter, gauge, Counter, Gauge}; | ||
use moka::future::Cache; | ||
use moka::notification::ListenerFuture; | ||
use sea_orm::ActiveValue::Unchanged; | ||
use sea_orm::{ | ||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, NotSet, PaginatorTrait, | ||
QueryFilter, Set, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::time::Duration; | ||
use utoipa::ToSchema; | ||
use uuid::Uuid; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] | ||
pub struct ApiKey { | ||
pub key: Uuid, | ||
pub description: String, | ||
pub project_id: Uuid, | ||
pub created_at: Option<chrono::NaiveDateTime>, | ||
} | ||
|
||
pub struct ApiKeyController { | ||
db: DatabaseConnection, | ||
authorization_cache: Cache<Uuid, Uuid>, | ||
authorization_cache_gauge: Gauge, | ||
authorization_cache_hit: Counter, | ||
authorization_cache_miss: Counter, | ||
authorization_invalid_key: Counter, | ||
} | ||
|
||
impl ApiKeyController { | ||
pub fn new(db: DatabaseConnection) -> Self { | ||
let authorization_cache_capacity = 100; | ||
gauge!("ingest_api_key_cache_capacity").set(authorization_cache_capacity as f64); | ||
|
||
let authorization_cache_gauge = gauge!("ingest_api_key_cache_total"); | ||
let authorization_cache_gauge_for_fut = authorization_cache_gauge.clone(); | ||
|
||
let authorization_cache = Cache::builder() | ||
.max_capacity(authorization_cache_capacity) | ||
.time_to_live(Duration::from_secs(1)) | ||
.async_eviction_listener(move |_, _, _| -> ListenerFuture { | ||
authorization_cache_gauge_for_fut.decrement(1); | ||
async {}.boxed() | ||
}) | ||
.build(); | ||
|
||
Self { | ||
db, | ||
authorization_cache, | ||
authorization_cache_gauge, | ||
authorization_cache_hit: counter!("ingest_api_key_cache_hit"), | ||
authorization_cache_miss: counter!("ingest_api_key_cache_miss"), | ||
authorization_invalid_key: counter!("ingest_api_key_invalid"), | ||
} | ||
} | ||
} | ||
|
||
impl From<entity::api_key::Model> for ApiKey { | ||
fn from(value: entity::api_key::Model) -> Self { | ||
ApiKey { | ||
key: value.key, | ||
description: value.description, | ||
project_id: value.project_id, | ||
created_at: Some(value.created_at), | ||
} | ||
} | ||
} | ||
|
||
impl AdminCrudTable for ApiKeyController { | ||
type Item = ApiKey; | ||
|
||
async fn get_by_id(&self, id: Uuid) -> Result<Option<Self::Item>, AdminCrudError> { | ||
let query = entity::api_key::Entity::find_by_id(id) | ||
.one(&self.db) | ||
.await?; | ||
Ok(query.map(ApiKey::from)) | ||
} | ||
|
||
async fn list( | ||
&self, | ||
params: ListQueryParams, | ||
) -> Result<PaginatedResult<ItemWithId<Self::Item>>, AdminCrudError> { | ||
let params = params.try_into()?; | ||
let query = entity::api_key::Entity::find() | ||
.apply_params(¶ms) | ||
.unwrap() | ||
.all(&self.db) | ||
.await?; | ||
|
||
Ok(PaginatedResult { | ||
items: query | ||
.into_iter() | ||
.map(|m| ItemWithId { | ||
id: m.id, | ||
item: ApiKey::from(m), | ||
}) | ||
.collect(), | ||
total: entity::api_key::Entity::find() | ||
.apply_filter(¶ms)? | ||
.count(&self.db) | ||
.await?, | ||
}) | ||
} | ||
|
||
async fn create(&self, item: Self::Item) -> Result<ItemWithId<Self::Item>, AdminCrudError> { | ||
let id = Uuid::now_v7(); | ||
let key = Uuid::new_v4(); | ||
|
||
entity::api_key::ActiveModel { | ||
id: Set(id), | ||
key: Set(key), | ||
project_id: Set(item.project_id), | ||
description: Set(item.description.to_string()), | ||
created_at: NotSet, | ||
} | ||
.insert(&self.db) | ||
.await?; | ||
|
||
Ok(ItemWithId { | ||
id, | ||
item: ApiKey { | ||
key, | ||
description: item.description.to_string(), | ||
project_id: item.project_id, | ||
created_at: Some(chrono::Utc::now().naive_utc()), | ||
}, | ||
}) | ||
} | ||
|
||
async fn update( | ||
&self, | ||
id: Uuid, | ||
item: Self::Item, | ||
) -> Result<ItemWithId<Self::Item>, AdminCrudError> { | ||
entity::api_key::ActiveModel { | ||
id: Unchanged(id), | ||
key: NotSet, | ||
project_id: NotSet, | ||
description: Set(item.description.to_string()), | ||
created_at: NotSet, | ||
} | ||
.update(&self.db) | ||
.await?; | ||
Ok(ItemWithId { id, item }) | ||
} | ||
|
||
async fn delete(&self, id: Uuid) -> Result<(), AdminCrudError> { | ||
entity::api_key::Entity::delete_by_id(id) | ||
.exec(&self.db) | ||
.await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub enum ApiKeyError { | ||
InvalidApiKey, | ||
InternalError, | ||
} | ||
|
||
impl ApiKeyController { | ||
pub async fn authorize_api_key(&self, key: &str) -> Result<Uuid, ApiKeyError> { | ||
let Ok(key_uuid) = Uuid::try_parse(key) else { | ||
self.authorization_invalid_key.increment(1); | ||
return Err(ApiKeyError::InvalidApiKey); | ||
}; | ||
|
||
let cached_project_id = self.authorization_cache.get(&key_uuid).await; | ||
|
||
if let Some(project_id) = cached_project_id { | ||
// Cache Hit | ||
self.authorization_cache_hit.increment(1); | ||
Ok(project_id) | ||
} else { | ||
// Cache Miss | ||
self.authorization_cache_miss.increment(1); | ||
|
||
let Some(api_key_entry) = entity::api_key::Entity::find() | ||
.filter(entity::api_key::Column::Key.eq(key_uuid)) | ||
.one(&self.db) | ||
.await | ||
.map_err(|_| ApiKeyError::InternalError)? | ||
else { | ||
self.authorization_invalid_key.increment(1); | ||
return Err(ApiKeyError::InvalidApiKey); | ||
}; | ||
|
||
let project_id = api_key_entry.project_id; | ||
|
||
self.authorization_cache.insert(key_uuid, project_id).await; | ||
self.authorization_cache_gauge.increment(1); | ||
|
||
Ok(project_id) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod api_key; | ||
pub mod event; | ||
pub mod group; | ||
pub mod pipeline; | ||
|
Oops, something went wrong.