Every admin endpoint now resolves Capability for the loaded resource and calls authz::require(...) before mutating. Forbidden → 403; every handler State carries an Arc<dyn AuthzRepo>, plumbed from the new PostgresAppMembersRepository in the picloud binary. * api.rs (scripts): AppRead/AppWriteScript/AppLogRead bound to script.app_id after load. List branches on instance_role: Member → list_for_user, others → list (or ?app= filtered). * apps_api.rs: InstanceCreateApp on POST; AppRead on get/list_domains; AppAdmin on patch/delete/slug:check; AppManageDomains on create_domain/delete_domain. list_apps membership-filters for Member. * admin_users_api.rs: InstanceManageUsers on every endpoint. Mint + PATCH refuse to grant Owner unless the caller is already Owner (CannotEscalate / 422), on top of the existing last-owner guard. * route_admin.rs: AppRead on list/check/match; AppWriteRoute on create/delete bound to the route's actual app_id (added a RouteRepository::get(uuid) lookup so delete binds correctly). * AppRepository + ScriptRepository gain list_for_user(user_id) for membership-filtered listings. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
228 lines
7.3 KiB
Rust
228 lines
7.3 KiB
Rust
//! CRUD over the `routes` table.
|
|
//!
|
|
//! The orchestrator's `AppRouteTables` is repopulated from this repo
|
|
//! after every write — see the route_admin module for the binding.
|
|
|
|
use async_trait::async_trait;
|
|
use picloud_shared::{AppId, HostKind, PathKind, Route, ScriptId};
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use crate::repo::ScriptRepositoryError;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct NewRoute {
|
|
pub app_id: AppId,
|
|
pub script_id: ScriptId,
|
|
pub host_kind: HostKind,
|
|
pub host: String,
|
|
pub host_param_name: Option<String>,
|
|
pub path_kind: PathKind,
|
|
pub path: String,
|
|
pub method: Option<String>,
|
|
}
|
|
|
|
#[async_trait]
|
|
pub trait RouteRepository: Send + Sync {
|
|
async fn list_all(&self) -> Result<Vec<Route>, ScriptRepositoryError>;
|
|
/// Single-row lookup. Used by `DELETE /api/v1/admin/routes/{id}` so
|
|
/// the capability check binds to the route's actual `app_id`
|
|
/// (not a path param).
|
|
async fn get(&self, route_id: Uuid) -> Result<Option<Route>, ScriptRepositoryError>;
|
|
async fn list_for_app(&self, app_id: AppId) -> Result<Vec<Route>, ScriptRepositoryError>;
|
|
async fn list_for_script(
|
|
&self,
|
|
script_id: ScriptId,
|
|
) -> Result<Vec<Route>, ScriptRepositoryError>;
|
|
async fn create(&self, input: NewRoute) -> Result<Route, ScriptRepositoryError>;
|
|
async fn delete(&self, route_id: Uuid) -> Result<(), ScriptRepositoryError>;
|
|
/// Count routes whose host_kind/host pair matches a pattern in
|
|
/// `app_id`. Used by the domain-claim delete guard.
|
|
async fn count_for_app_host(
|
|
&self,
|
|
app_id: AppId,
|
|
host_kind: HostKind,
|
|
host: &str,
|
|
) -> Result<i64, ScriptRepositoryError>;
|
|
}
|
|
|
|
pub struct PostgresRouteRepository {
|
|
pool: PgPool,
|
|
}
|
|
|
|
impl PostgresRouteRepository {
|
|
#[must_use]
|
|
pub fn new(pool: PgPool) -> Self {
|
|
Self { pool }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl RouteRepository for PostgresRouteRepository {
|
|
async fn list_all(&self) -> Result<Vec<Route>, ScriptRepositoryError> {
|
|
let rows = sqlx::query_as::<_, RouteRow>(
|
|
"SELECT id, app_id, script_id, host_kind, host, host_param_name, \
|
|
path_kind, path, method, created_at \
|
|
FROM routes ORDER BY created_at",
|
|
)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
Ok(rows.into_iter().map(Into::into).collect())
|
|
}
|
|
|
|
async fn get(&self, route_id: Uuid) -> Result<Option<Route>, ScriptRepositoryError> {
|
|
let row = sqlx::query_as::<_, RouteRow>(
|
|
"SELECT id, app_id, script_id, host_kind, host, host_param_name, \
|
|
path_kind, path, method, created_at \
|
|
FROM routes WHERE id = $1",
|
|
)
|
|
.bind(route_id)
|
|
.fetch_optional(&self.pool)
|
|
.await?;
|
|
Ok(row.map(Into::into))
|
|
}
|
|
|
|
async fn list_for_app(&self, app_id: AppId) -> Result<Vec<Route>, ScriptRepositoryError> {
|
|
let rows = sqlx::query_as::<_, RouteRow>(
|
|
"SELECT id, app_id, script_id, host_kind, host, host_param_name, \
|
|
path_kind, path, method, created_at \
|
|
FROM routes WHERE app_id = $1 ORDER BY created_at",
|
|
)
|
|
.bind(app_id.into_inner())
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
Ok(rows.into_iter().map(Into::into).collect())
|
|
}
|
|
|
|
async fn list_for_script(
|
|
&self,
|
|
script_id: ScriptId,
|
|
) -> Result<Vec<Route>, ScriptRepositoryError> {
|
|
let rows = sqlx::query_as::<_, RouteRow>(
|
|
"SELECT id, app_id, script_id, host_kind, host, host_param_name, \
|
|
path_kind, path, method, created_at \
|
|
FROM routes WHERE script_id = $1 ORDER BY created_at",
|
|
)
|
|
.bind(script_id.into_inner())
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
Ok(rows.into_iter().map(Into::into).collect())
|
|
}
|
|
|
|
async fn create(&self, input: NewRoute) -> Result<Route, ScriptRepositoryError> {
|
|
let res = sqlx::query_as::<_, RouteRow>(
|
|
"INSERT INTO routes ( \
|
|
app_id, script_id, host_kind, host, host_param_name, \
|
|
path_kind, path, method \
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) \
|
|
RETURNING id, app_id, script_id, host_kind, host, host_param_name, \
|
|
path_kind, path, method, created_at",
|
|
)
|
|
.bind(input.app_id.into_inner())
|
|
.bind(input.script_id.into_inner())
|
|
.bind(host_kind_str(input.host_kind))
|
|
.bind(&input.host)
|
|
.bind(input.host_param_name.as_deref())
|
|
.bind(path_kind_str(input.path_kind))
|
|
.bind(&input.path)
|
|
.bind(input.method.as_deref())
|
|
.fetch_one(&self.pool)
|
|
.await;
|
|
|
|
match res {
|
|
Ok(row) => Ok(row.into()),
|
|
Err(sqlx::Error::Database(e)) if e.is_unique_violation() => Err(
|
|
ScriptRepositoryError::Conflict("a route with this binding already exists".into()),
|
|
),
|
|
Err(sqlx::Error::Database(e)) if e.is_foreign_key_violation() => {
|
|
Err(ScriptRepositoryError::NotFound(input.script_id))
|
|
}
|
|
Err(e) => Err(e.into()),
|
|
}
|
|
}
|
|
|
|
async fn delete(&self, route_id: Uuid) -> Result<(), ScriptRepositoryError> {
|
|
let res = sqlx::query("DELETE FROM routes WHERE id = $1")
|
|
.bind(route_id)
|
|
.execute(&self.pool)
|
|
.await?;
|
|
if res.rows_affected() == 0 {
|
|
return Err(ScriptRepositoryError::NotFound(ScriptId::from(route_id)));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn count_for_app_host(
|
|
&self,
|
|
app_id: AppId,
|
|
host_kind: HostKind,
|
|
host: &str,
|
|
) -> Result<i64, ScriptRepositoryError> {
|
|
let count: (i64,) = sqlx::query_as(
|
|
"SELECT COUNT(*) FROM routes \
|
|
WHERE app_id = $1 AND host_kind = $2 AND host = $3",
|
|
)
|
|
.bind(app_id.into_inner())
|
|
.bind(host_kind_str(host_kind))
|
|
.bind(host)
|
|
.fetch_one(&self.pool)
|
|
.await?;
|
|
Ok(count.0)
|
|
}
|
|
}
|
|
|
|
const fn host_kind_str(k: HostKind) -> &'static str {
|
|
match k {
|
|
HostKind::Any => "any",
|
|
HostKind::Strict => "strict",
|
|
HostKind::Wildcard => "wildcard",
|
|
}
|
|
}
|
|
|
|
const fn path_kind_str(k: PathKind) -> &'static str {
|
|
match k {
|
|
PathKind::Exact => "exact",
|
|
PathKind::Prefix => "prefix",
|
|
PathKind::Param => "param",
|
|
}
|
|
}
|
|
|
|
#[derive(sqlx::FromRow)]
|
|
struct RouteRow {
|
|
id: Uuid,
|
|
app_id: Uuid,
|
|
script_id: Uuid,
|
|
host_kind: String,
|
|
host: String,
|
|
host_param_name: Option<String>,
|
|
path_kind: String,
|
|
path: String,
|
|
method: Option<String>,
|
|
created_at: chrono::DateTime<chrono::Utc>,
|
|
}
|
|
|
|
impl From<RouteRow> for Route {
|
|
fn from(r: RouteRow) -> Self {
|
|
Self {
|
|
id: r.id,
|
|
app_id: r.app_id.into(),
|
|
script_id: r.script_id.into(),
|
|
host_kind: match r.host_kind.as_str() {
|
|
"strict" => HostKind::Strict,
|
|
"wildcard" => HostKind::Wildcard,
|
|
_ => HostKind::Any,
|
|
},
|
|
host: r.host,
|
|
host_param_name: r.host_param_name,
|
|
path_kind: match r.path_kind.as_str() {
|
|
"prefix" => PathKind::Prefix,
|
|
"param" => PathKind::Param,
|
|
_ => PathKind::Exact,
|
|
},
|
|
path: r.path,
|
|
method: r.method,
|
|
created_at: r.created_at,
|
|
}
|
|
}
|
|
}
|