Skip to content

Commit 47a203b

Browse files
move provider and project api keys logic to next (#1005)
* move provider and project api keys logic to next * Update frontend/lib/cache.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * fix cache key entry * tiny fix on returning --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
1 parent e2b5ddf commit 47a203b

File tree

25 files changed

+406
-471
lines changed

25 files changed

+406
-471
lines changed

app-server/src/api/utils.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ use sqlx::PgPool;
44

55
use crate::project_api_keys::hash_api_key;
66
use crate::{
7-
cache::{keys::PROJECT_API_KEY_CACHE_KEY, Cache, CacheTrait},
8-
db::{self, project_api_keys::DBProjectApiKey},
7+
cache::{Cache, CacheTrait, keys::PROJECT_API_KEY_CACHE_KEY},
8+
db::{self, project_api_keys::ProjectApiKey},
99
};
1010

1111
pub async fn get_api_key_from_raw_value(
1212
pool: &PgPool,
1313
cache: Arc<Cache>,
1414
raw_api_key: String,
15-
) -> anyhow::Result<DBProjectApiKey> {
15+
) -> anyhow::Result<ProjectApiKey> {
1616
let api_key_hash = hash_api_key(&raw_api_key);
1717
let cache_key = format!("{PROJECT_API_KEY_CACHE_KEY}:{api_key_hash}");
18-
let cache_res = cache.get::<DBProjectApiKey>(&cache_key).await;
18+
let cache_res = cache.get::<ProjectApiKey>(&cache_key).await;
1919
match cache_res {
2020
Ok(Some(api_key)) => Ok(api_key),
2121
Ok(None) | Err(_) => {
2222
let api_key = db::project_api_keys::get_api_key(pool, &api_key_hash).await?;
2323
let _ = cache
24-
.insert::<DBProjectApiKey>(&cache_key, api_key.clone())
24+
.insert::<ProjectApiKey>(&cache_key, api_key.clone())
2525
.await;
2626

2727
Ok(api_key)

app-server/src/auth/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ pub async fn project_validator(
4848

4949
match get_api_key_from_raw_value(&db.pool, cache, credentials.token().to_string()).await {
5050
Ok(api_key) => {
51-
req.extensions_mut()
52-
.insert(api_key.into_with_raw(credentials.token().to_string()));
51+
req.extensions_mut().insert(api_key);
5352
Ok(req)
5453
}
5554
Err(e) => {

app-server/src/cache/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub trait CacheTrait {
3030
async fn insert<T>(&self, key: &str, value: T) -> Result<(), CacheError>
3131
where
3232
T: Serialize + Send;
33+
#[allow(dead_code)]
3334
async fn remove(&self, key: &str) -> Result<(), CacheError>;
3435
async fn set_ttl(&self, key: &str, seconds: u64) -> Result<(), CacheError>;
3536
async fn insert_with_ttl<T>(&self, key: &str, value: T, seconds: u64) -> Result<(), CacheError>

app-server/src/db/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub mod events;
1111
pub mod prices;
1212
pub mod project_api_keys;
1313
pub mod projects;
14-
pub mod provider_api_keys;
1514
pub mod slack_channel_to_events;
1615
pub mod slack_integrations;
1716
pub mod spans;

app-server/src/db/project_api_keys.rs

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,16 @@ use sqlx::{FromRow, PgPool};
44
use uuid::Uuid;
55

66
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
7-
pub struct DBProjectApiKey {
8-
pub project_id: Uuid,
9-
pub name: Option<String>,
10-
pub hash: String,
11-
pub shorthand: String,
12-
}
13-
14-
impl DBProjectApiKey {
15-
pub fn into_with_raw(self, raw: String) -> ProjectApiKey {
16-
ProjectApiKey {
17-
project_id: self.project_id,
18-
name: self.name,
19-
hash: self.hash,
20-
shorthand: self.shorthand,
21-
raw,
22-
}
23-
}
24-
}
25-
26-
// TODO: Remove this and use DBProjectApiKey directly
27-
#[derive(Clone)]
7+
#[serde(rename_all = "camelCase")]
288
pub struct ProjectApiKey {
299
pub project_id: Uuid,
3010
pub name: Option<String>,
3111
pub hash: String,
3212
pub shorthand: String,
33-
pub raw: String,
34-
}
35-
36-
impl Into<DBProjectApiKey> for ProjectApiKey {
37-
fn into(self) -> DBProjectApiKey {
38-
DBProjectApiKey {
39-
project_id: self.project_id,
40-
name: self.name,
41-
hash: self.hash,
42-
shorthand: self.shorthand,
43-
}
44-
}
45-
}
46-
47-
#[derive(Serialize, FromRow)]
48-
pub struct ProjectApiKeyResponse {
49-
pub id: Uuid,
50-
pub project_id: Uuid,
51-
pub name: Option<String>,
52-
pub shorthand: String,
53-
}
54-
55-
pub async fn create_project_api_key(
56-
pool: &PgPool,
57-
project_id: &Uuid,
58-
name: &Option<String>,
59-
hash: &String,
60-
shorthand: &String,
61-
) -> Result<DBProjectApiKey> {
62-
let key_info = sqlx::query_as::<_, DBProjectApiKey>(
63-
"INSERT
64-
INTO project_api_keys (shorthand, project_id, name, hash)
65-
VALUES ($1, $2, $3, $4)
66-
RETURNING id, project_id, name, hash, shorthand",
67-
)
68-
.bind(&shorthand)
69-
.bind(&project_id)
70-
.bind(&name)
71-
.bind(&hash)
72-
.fetch_one(pool)
73-
.await?;
74-
75-
Ok(key_info)
7613
}
7714

78-
pub async fn get_api_keys_for_project(
79-
db: &PgPool,
80-
project_id: &Uuid,
81-
) -> Result<Vec<ProjectApiKeyResponse>> {
82-
let api_keys = sqlx::query_as::<_, ProjectApiKeyResponse>(
83-
"SELECT
84-
project_api_keys.project_id,
85-
project_api_keys.name,
86-
project_api_keys.id,
87-
project_api_keys.shorthand
88-
FROM
89-
project_api_keys
90-
WHERE
91-
project_api_keys.project_id = $1",
92-
)
93-
.bind(project_id)
94-
.fetch_all(db)
95-
.await?;
96-
97-
Ok(api_keys)
98-
}
99-
100-
pub async fn get_api_key(pool: &PgPool, hash: &String) -> Result<DBProjectApiKey> {
101-
let api_key = match sqlx::query_as::<_, DBProjectApiKey>(
15+
pub async fn get_api_key(pool: &PgPool, hash: &String) -> Result<ProjectApiKey> {
16+
let api_key = match sqlx::query_as::<_, ProjectApiKey>(
10217
"SELECT
10318
project_api_keys.hash,
10419
project_api_keys.project_id,
@@ -121,23 +36,3 @@ pub async fn get_api_key(pool: &PgPool, hash: &String) -> Result<DBProjectApiKey
12136

12237
Ok(api_key)
12338
}
124-
125-
#[derive(FromRow)]
126-
struct ProjectApiKeyHash {
127-
hash: String,
128-
}
129-
130-
pub async fn delete_api_key(pool: &PgPool, id: &Uuid, project_id: &Uuid) -> Result<String> {
131-
let row = sqlx::query_as::<_, ProjectApiKeyHash>(
132-
"DELETE
133-
FROM project_api_keys
134-
WHERE id = $1 AND project_id = $2
135-
RETURNING hash",
136-
)
137-
.bind(id)
138-
.bind(project_id)
139-
.fetch_one(pool)
140-
.await?;
141-
142-
Ok(row.hash)
143-
}

app-server/src/db/provider_api_keys.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

app-server/src/db/utils.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
use rand::distr::{Alphanumeric, SampleString};
21
use uuid::Uuid;
32

4-
pub fn generate_random_key() -> String {
5-
Alphanumeric.sample_string(&mut rand::rng(), 64)
6-
}
7-
83
pub fn span_id_to_uuid(span_id: &[u8]) -> Uuid {
94
let mut padded_vec = vec![0; 8];
105
padded_vec.extend_from_slice(&span_id.to_vec());

app-server/src/main.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ mod names;
7070
mod notifications;
7171
mod opentelemetry_proto;
7272
mod project_api_keys;
73-
mod provider_api_keys;
7473
mod query_engine;
7574
mod realtime;
7675
mod routes;
@@ -743,7 +742,7 @@ fn main() -> anyhow::Result<()> {
743742
// == Name generator ==
744743
let name_generator = Arc::new(NameGenerator::new());
745744

746-
log::info!("Enabling producer mode, spinning up full HTTP server");
745+
log::info!("Spinning up full HTTP server");
747746
HttpServer::new(move || {
748747
let project_auth = HttpAuthentication::bearer(auth::project_validator);
749748

@@ -796,12 +795,8 @@ fn main() -> anyhow::Result<()> {
796795
.service(
797796
// auth on path projects/{project_id} is handled by middleware on Next.js
798797
web::scope("/api/v1/projects/{project_id}")
799-
.service(routes::api_keys::create_project_api_key)
800-
.service(routes::api_keys::get_api_keys_for_project)
801-
.service(routes::api_keys::revoke_project_api_key)
802798
.service(routes::evaluations::get_evaluation_score_stats)
803799
.service(routes::evaluations::get_evaluation_score_distribution)
804-
.service(routes::provider_api_keys::save_api_key)
805800
.service(routes::spans::create_span)
806801
.service(routes::sql::execute_sql_query)
807802
.service(routes::sql::validate_sql_query),
@@ -821,7 +816,7 @@ fn main() -> anyhow::Result<()> {
821816
.name("grpc".to_string())
822817
.spawn(move || {
823818
runtime_handle.block_on(async {
824-
log::info!("Enabling producer mode, spinning up gRPC server");
819+
log::info!("Spinning up gRPC server");
825820

826821
let process_traces_service = ProcessTracesService::new(
827822
db.clone(),

app-server/src/notifications/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,6 @@ async fn process_single_notification(
194194

195195
Ok(())
196196
}
197-
_ => {
198-
log::warn!("Unknown notification type: {:?}", message.notification_type);
199-
Ok(())
200-
}
201197
};
202198

203199
match result {

app-server/src/project_api_keys/mod.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
1-
use crate::db::utils::generate_random_key;
21
use sha3::{Digest, Sha3_256};
32

4-
pub struct ProjectApiKeyVals {
5-
pub value: String,
6-
pub hash: String,
7-
pub shorthand: String,
8-
}
9-
10-
impl ProjectApiKeyVals {
11-
pub fn new() -> Self {
12-
let value = generate_random_key();
13-
let hash = hash_api_key(&value);
14-
let shorthand = format!("{}...{}", &value[..4], &value[value.len() - 4..]);
15-
Self {
16-
value,
17-
hash,
18-
shorthand,
19-
}
20-
}
21-
}
22-
233
pub fn hash_api_key(api_key: &str) -> String {
244
let mut hasher = Sha3_256::new();
255
hasher.update(api_key.as_bytes());

0 commit comments

Comments
 (0)