udb 0.3.1

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! `ApiKeyService` handler over the UDB-owned API-key primitives.

use std::sync::Arc;

use tonic::{Request, Response, Status};
use uuid::Uuid;

use crate::proto::udb::core::apikey::entity::v1 as apikey_entity_pb;
use crate::proto::udb::core::apikey::services::v1 as apikey_pb;
use apikey_pb::api_key_service_server::ApiKeyService;

use crate::runtime::authn::{self, ApiKeyRecord, ApiKeyStore, AuthnConfig, UnavailableApiKeyStore};

use super::events::{self, AuthEvent, AuthEventSink, topics};
use super::mappings::{bounded_page_response, bounded_page_window, timestamp_from_unix};
use super::now_unix;

pub struct ApiKeyServiceImpl {
    api_keys: Arc<dyn ApiKeyStore>,
    config: AuthnConfig,
    event_sink: Arc<dyn AuthEventSink>,
    /// Direct pool for read-only usage aggregation over `api_key_usages`
    /// (the trait-based store does not expose analytic queries).
    pg_pool: Option<sqlx::PgPool>,
}

impl ApiKeyServiceImpl {
    pub fn new(config: AuthnConfig) -> Self {
        Self {
            api_keys: Arc::new(UnavailableApiKeyStore),
            config,
            event_sink: events::noop_sink(),
            pg_pool: None,
        }
    }

    pub fn with_store(config: AuthnConfig, api_keys: Arc<dyn ApiKeyStore>) -> Self {
        Self {
            api_keys,
            config,
            event_sink: events::noop_sink(),
            pg_pool: None,
        }
    }

    /// Attach the Postgres pool used for usage-stat aggregation.
    pub(crate) fn with_postgres(mut self, pool: Option<sqlx::PgPool>) -> Self {
        self.pg_pool = pool;
        self
    }

    /// Attach the domain-event sink (outbox → Kafka). Defaults to a no-op.
    pub(crate) fn with_event_sink(mut self, sink: Arc<dyn AuthEventSink>) -> Self {
        self.event_sink = sink;
        self
    }

    fn hash_key(&self) -> Vec<u8> {
        self.config.api_key_hash_secret().as_bytes().to_vec()
    }

    async fn emit_event(&self, event: AuthEvent) {
        let topic = event.topic;
        if let Err(err) = self.event_sink.emit(event).await {
            tracing::warn!(topic, error = %err, "failed to publish apikey event");
        }
    }
}

fn expires_at_unix(ts: Option<prost_types::Timestamp>) -> u64 {
    ts.map(|ts| ts.seconds.max(0) as u64).unwrap_or(0)
}

fn status_for(rec: &ApiKeyRecord, now_unix: u64) -> apikey_entity_pb::ApiKeyStatus {
    if rec.is_revoked() {
        apikey_entity_pb::ApiKeyStatus::Revoked
    } else if rec.is_expired(now_unix) {
        apikey_entity_pb::ApiKeyStatus::Expired
    } else {
        apikey_entity_pb::ApiKeyStatus::Active
    }
}

fn api_key_to_pb(rec: &ApiKeyRecord, now_unix: u64) -> apikey_entity_pb::ApiKey {
    apikey_entity_pb::ApiKey {
        key_id: rec.key_prefix.clone(),
        key_prefix: rec.key_prefix.clone(),
        // Never return the stored key hash over the read API — the digest should
        // not leave the storage layer (defense-in-depth if the hash secret leaks).
        key_hash: String::new(),
        name: rec.key_prefix.clone(),
        description: String::new(),
        owner_type: apikey_entity_pb::ApiKeyOwnerType::ServiceAccount as i32,
        owner_id: rec.principal_id.clone(),
        scopes_json: serde_json::to_string(&rec.scopes).unwrap_or_else(|_| "[]".to_string()),
        status: status_for(rec, now_unix) as i32,
        ip_allowlist_json: "[]".to_string(),
        rate_limit_per_minute: 60,
        rate_limit_per_day: 10_000,
        created_by: String::new(),
        revoked_by: String::new(),
        revoke_reason: String::new(),
        expires_at: timestamp_from_unix(rec.expires_at_unix),
        last_used_at: timestamp_from_unix(rec.last_used_at_unix),
        created_at: timestamp_from_unix(rec.created_at_unix),
        updated_at: timestamp_from_unix(rec.created_at_unix),
        deleted_at: timestamp_from_unix(rec.revoked_at_unix),
        deleted_by: String::new(),
        tenant_id: rec.tenant_id.clone(),
        project_id: rec.project_id.clone(),
        allowed_resources_json: "[]".to_string(),
        metadata_json: serde_json::json!({
            "service_identity": rec.service_identity.clone(),
            "native_model": "udb.core.apikey.entity.v1.ApiKey"
        })
        .to_string(),
    }
}

#[tonic::async_trait]
impl ApiKeyService for ApiKeyServiceImpl {
    async fn create_api_key(
        &self,
        request: Request<apikey_pb::CreateApiKeyRequest>,
    ) -> Result<Response<apikey_pb::CreateApiKeyResponse>, Status> {
        if self.hash_key().is_empty() {
            return Err(Status::failed_precondition(
                "API key hashing requires UDB_SESSION_HASH_SECRET",
            ));
        }
        let req = request.into_inner();
        if req.owner_id.trim().is_empty() {
            return Err(Status::invalid_argument("owner_id is required"));
        }
        let prefix = format!(
            "udbk_{}",
            Uuid::new_v4()
                .simple()
                .to_string()
                .chars()
                .take(12)
                .collect::<String>()
        );
        let plain_key = format!("{}.{}", prefix, Uuid::new_v4().simple());
        let now = now_unix();
        let tenant_id = req
            .context
            .as_ref()
            .and_then(|ctx| ctx.tenant.as_ref())
            .map(|tenant| tenant.tenant_id.clone())
            .unwrap_or_default();
        let project_id = req
            .context
            .as_ref()
            .and_then(|ctx| ctx.tenant.as_ref())
            .map(|tenant| tenant.project_id.clone())
            .unwrap_or_default();
        let rec = ApiKeyRecord {
            key_prefix: authn::api_key_prefix(&plain_key),
            key_hash: authn::hash_secret(&plain_key, &self.hash_key()),
            principal_id: req.owner_id,
            service_identity: String::new(),
            tenant_id,
            project_id,
            scopes: req.scopes,
            created_at_unix: now,
            last_used_at_unix: 0,
            expires_at_unix: expires_at_unix(req.expires_at),
            revoked_at_unix: 0,
        };
        self.api_keys
            .put(rec.clone())
            .await
            .map_err(Status::internal)?;
        self.emit_event(AuthEvent::new(
            topics::API_KEY_CREATED,
            rec.key_prefix.clone(),
            rec.tenant_id.clone(),
            serde_json::json!({
                "key_id": rec.key_prefix.clone(),
                "key_prefix": rec.key_prefix.clone(),
                "owner_id": rec.principal_id.clone(),
                "scopes": rec.scopes.clone(),
                "tenant_id": rec.tenant_id.clone(),
                "project_id": rec.project_id.clone(),
            }),
        ))
        .await;
        Ok(Response::new(apikey_pb::CreateApiKeyResponse {
            key: Some(api_key_to_pb(&rec, now)),
            plain_key,
        }))
    }

    async fn get_api_key(
        &self,
        request: Request<apikey_pb::GetApiKeyRequest>,
    ) -> Result<Response<apikey_pb::GetApiKeyResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let key = self
            .api_keys
            .get_by_prefix(&req.key_id)
            .await
            .map_err(Status::internal)?
            .map(|rec| api_key_to_pb(&rec, now));
        Ok(Response::new(apikey_pb::GetApiKeyResponse { key }))
    }

    async fn list_api_keys(
        &self,
        request: Request<apikey_pb::ListApiKeysRequest>,
    ) -> Result<Response<apikey_pb::ListApiKeysResponse>, Status> {
        let req = request.into_inner();
        if req.owner_id.trim().is_empty() {
            return Err(Status::invalid_argument("owner_id is required"));
        }
        let now = now_unix();
        let status = apikey_entity_pb::ApiKeyStatus::try_from(req.status).unwrap_or_default();
        let page = req.page.as_ref();
        let (limit, offset, _) = bounded_page_window(page);
        let (records, total) = self
            .api_keys
            .list_for_principal_status_page(&req.owner_id, status, now, limit, offset)
            .await
            .map_err(Status::internal)?;
        let keys = records.iter().map(|rec| api_key_to_pb(rec, now)).collect();
        Ok(Response::new(apikey_pb::ListApiKeysResponse {
            keys,
            page: Some(bounded_page_response(total, page)),
        }))
    }

    async fn update_api_key(
        &self,
        request: Request<apikey_pb::UpdateApiKeyRequest>,
    ) -> Result<Response<apikey_pb::UpdateApiKeyResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let mut rec = self
            .api_keys
            .get_by_prefix(&req.key_id)
            .await
            .map_err(Status::internal)?
            .ok_or_else(|| Status::not_found("api key not found"))?;
        if !req.scopes.is_empty() {
            rec.scopes = req.scopes;
        }
        if req.expires_at.is_some() {
            rec.expires_at_unix = expires_at_unix(req.expires_at);
        }
        self.api_keys
            .put(rec.clone())
            .await
            .map_err(Status::internal)?;
        self.emit_event(AuthEvent::new(
            topics::API_KEY_UPDATED,
            rec.key_prefix.clone(),
            String::new(),
            serde_json::json!({
                "key_id": req.key_id.clone(),
                "key_prefix": rec.key_prefix.clone(),
                "scopes": rec.scopes.clone(),
                "expires_at_unix": rec.expires_at_unix,
            }),
        ))
        .await;
        Ok(Response::new(apikey_pb::UpdateApiKeyResponse {
            key: Some(api_key_to_pb(&rec, now)),
        }))
    }

    async fn revoke_api_key(
        &self,
        request: Request<apikey_pb::RevokeApiKeyRequest>,
    ) -> Result<Response<apikey_pb::RevokeApiKeyResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let ok = self
            .api_keys
            .revoke(&req.key_id, now)
            .await
            .map_err(Status::internal)?;
        if !ok {
            return Err(Status::not_found("api key not found"));
        }
        self.emit_event(AuthEvent::new(
            topics::API_KEY_REVOKED,
            req.key_id.clone(),
            String::new(),
            serde_json::json!({
                "key_id": req.key_id.clone(),
                "key_prefix": req.key_id.clone(),
            }),
        ))
        .await;
        Ok(Response::new(apikey_pb::RevokeApiKeyResponse {
            key_id: req.key_id,
            revoked_at: timestamp_from_unix(now),
            operation_id: Uuid::new_v4().to_string(),
        }))
    }

    async fn validate_api_key(
        &self,
        request: Request<apikey_pb::ValidateApiKeyRequest>,
    ) -> Result<Response<apikey_pb::ValidateApiKeyResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let Some(rec) = authn::validate_api_key(
            self.api_keys.as_ref(),
            &req.plain_key,
            &self.hash_key(),
            now,
        )
        .await
        .map_err(Status::internal)?
        else {
            return Ok(Response::new(apikey_pb::ValidateApiKeyResponse {
                valid: false,
                ..Default::default()
            }));
        };
        // Best-effort last_used_at stamp on a successful match. Fire-and-forget
        // so it never adds latency to (or fails) the auth hot path.
        let _ = self.api_keys.touch_last_used(&rec.key_prefix, now).await;
        // Wildcard handling must match `SecurityContext::has_scope` (`*` and
        // `udb:*`) so an API key's scope semantics are uniform with the JWT/ABAC
        // path rather than only honoring a bare `*`.
        let scope_ok = req.required_scope.trim().is_empty()
            || rec
                .scopes
                .iter()
                .any(|scope| scope == "*" || scope == "udb:*" || scope == &req.required_scope);
        Ok(Response::new(apikey_pb::ValidateApiKeyResponse {
            valid: scope_ok,
            key_id: rec.key_prefix,
            owner_id: rec.principal_id,
            owner_type: apikey_entity_pb::ApiKeyOwnerType::ServiceAccount as i32,
            scopes: rec.scopes,
            rate_limited: false,
        }))
    }

    async fn get_api_key_usage_stats(
        &self,
        request: Request<apikey_pb::GetApiKeyUsageStatsRequest>,
    ) -> Result<Response<apikey_pb::GetApiKeyUsageStatsResponse>, Status> {
        use std::collections::BTreeMap;

        use crate::runtime::native_catalog::native_model;
        use sqlx::Row;

        let req = request.into_inner();
        let key_ref = req.key_id.trim();
        if key_ref.is_empty() {
            return Err(Status::invalid_argument("key_id is required"));
        }
        let Some(pool) = self.pg_pool.as_ref() else {
            return Err(Status::failed_precondition(
                "api-key usage stats require a Postgres backend",
            ));
        };

        // Resolve every column + the relation through the proto manifest so the
        // aggregation never hardcodes table/column names (proto is the source of
        // truth, per the native-service rule).
        let m = native_model(
            "udb.core.apikey.entity.v1.ApiKeyUsage",
            &[
                "key_id",
                "http_status",
                "latency_ms",
                "rate_limited",
                "requested_at",
            ],
        );
        // The API-key surface exposes the public `key_prefix` as the caller-facing
        // id (see `api_key_to_pb`), so `key_id` here is normally a prefix, not the
        // internal UUID FK on `api_key_usages`. Accept either: a UUID is matched
        // directly; otherwise resolve the prefix → UUID via the `api_keys` table.
        let ak = native_model(
            "udb.core.apikey.entity.v1.ApiKey",
            &["key_id", "key_prefix"],
        );
        let key_pred = if Uuid::parse_str(key_ref).is_ok() {
            format!("{key_id} = $1::UUID", key_id = m.q("key_id"))
        } else {
            format!(
                "{key_id} = (SELECT {ak_id} FROM {ak_rel} WHERE {ak_prefix} = $1 LIMIT 1)",
                key_id = m.q("key_id"),
                ak_id = ak.q("key_id"),
                ak_rel = ak.relation,
                ak_prefix = ak.q("key_prefix"),
            )
        };
        let from_secs = req.from.as_ref().map(|t| t.seconds);
        let to_secs = req.to.as_ref().map(|t| t.seconds);
        let sql = format!(
            "SELECT to_char(date_trunc('day', {req_at}), 'YYYY-MM-DD') AS day, \
                    COALESCE({status}, 0)::int AS status, \
                    COUNT(*)::bigint AS cnt, \
                    COUNT(*) FILTER (WHERE {rate_limited})::bigint AS rl, \
                    COALESCE(SUM({latency}), 0)::bigint AS lat_sum \
             FROM {rel} \
             WHERE {key_pred} \
               AND ($2::bigint IS NULL OR {req_at} >= to_timestamp($2)) \
               AND ($3::bigint IS NULL OR {req_at} <= to_timestamp($3)) \
             GROUP BY day, status \
             ORDER BY day",
            req_at = m.q("requested_at"),
            status = m.q("http_status"),
            rate_limited = m.q("rate_limited"),
            latency = m.q("latency_ms"),
            rel = m.relation,
            key_pred = key_pred,
        );
        let rows = sqlx::query(&sql)
            .bind(req.key_id.trim())
            .bind(from_secs)
            .bind(to_secs)
            .fetch_all(pool)
            .await
            .map_err(|err| Status::internal(format!("usage stats query failed: {err}")))?;

        // Fold (day, status) rows into one ApiKeyDailyStat per day.
        struct DayAgg {
            total: i64,
            rate_limited: i64,
            latency_sum: i64,
            status_counts: BTreeMap<String, i64>,
        }
        let mut by_day: BTreeMap<String, DayAgg> = BTreeMap::new();
        let mut overall_total: i64 = 0;
        for row in rows {
            let day: String = row.try_get("day").unwrap_or_default();
            let status: i32 = row.try_get("status").unwrap_or_default();
            let cnt: i64 = row.try_get("cnt").unwrap_or_default();
            let rl: i64 = row.try_get("rl").unwrap_or_default();
            let lat_sum: i64 = row.try_get("lat_sum").unwrap_or_default();
            overall_total += cnt;
            let agg = by_day.entry(day).or_insert(DayAgg {
                total: 0,
                rate_limited: 0,
                latency_sum: 0,
                status_counts: BTreeMap::new(),
            });
            agg.total += cnt;
            agg.rate_limited += rl;
            agg.latency_sum += lat_sum;
            if status != 0 {
                *agg.status_counts.entry(status.to_string()).or_insert(0) += cnt;
            }
        }
        let stats = by_day
            .into_iter()
            .map(|(date, agg)| apikey_pb::ApiKeyDailyStat {
                date,
                total_requests: agg.total,
                rate_limited_count: agg.rate_limited,
                avg_latency_ms: if agg.total > 0 {
                    agg.latency_sum as f64 / agg.total as f64
                } else {
                    0.0
                },
                status_counts: agg.status_counts.into_iter().collect(),
            })
            .collect();
        Ok(Response::new(apikey_pb::GetApiKeyUsageStatsResponse {
            stats,
            total_requests: overall_total,
        }))
    }
}