356 lines
12 KiB
Rust
356 lines
12 KiB
Rust
use crate::{artifact_store::ArtifactStore, jobs::Error};
|
|
use axum::{
|
|
Json, Router,
|
|
extract::{Path, State},
|
|
http::{HeaderMap, StatusCode},
|
|
response::{IntoResponse, Response},
|
|
routing::{get, post},
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::{Value, json};
|
|
use sqlx::{Postgres, Row, Transaction};
|
|
use uuid::Uuid;
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct AptRecipe {
|
|
pub schema_version: u32,
|
|
pub provider: String,
|
|
pub package: String,
|
|
pub version: String,
|
|
pub architecture: String,
|
|
pub recipe_version: String,
|
|
pub installation_context: String,
|
|
pub reboot_policy: String,
|
|
}
|
|
pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
|
r.route(
|
|
"/api/v1/tenants/{tenant}/package-recipes",
|
|
get(list).post(import),
|
|
)
|
|
.route("/api/v1/tenants/{tenant}/artifacts", post(upload))
|
|
.route(
|
|
"/api/v1/tenants/{tenant}/artifacts/{artifact}",
|
|
get(download),
|
|
)
|
|
}
|
|
pub async fn check(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
action: &rmm_domain::jobs::Action,
|
|
) -> Result<(), Error> {
|
|
references(tx, action).await.map(|_| ())
|
|
}
|
|
pub async fn references(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
action: &rmm_domain::jobs::Action,
|
|
) -> Result<Vec<rmm_domain::jobs::RecipeReference>, Error> {
|
|
let manifest = match action {
|
|
rmm_domain::jobs::Action::EnsurePackage {
|
|
apt_manifest: Some(m),
|
|
..
|
|
}
|
|
| rmm_domain::jobs::Action::ApplyUpdates {
|
|
apt_manifest: Some(m),
|
|
..
|
|
}
|
|
| rmm_domain::jobs::Action::AptNativeAcceptance {
|
|
apt_manifest: m, ..
|
|
} => Some(m),
|
|
_ => None,
|
|
};
|
|
if let Some(m) = manifest {
|
|
if !action.validate() {
|
|
return Err(Error(StatusCode::BAD_REQUEST, "INVALID_APT_MANIFEST"));
|
|
}
|
|
return Ok(m
|
|
.resolved_apt_transaction
|
|
.packages
|
|
.iter()
|
|
.map(|p| rmm_domain::jobs::RecipeReference {
|
|
package: p.name.clone(),
|
|
version: p.new_version.clone(),
|
|
architecture: p.architecture.clone(),
|
|
recipe_version: m.resolved_apt_transaction.recipe_version.clone(),
|
|
sha256: m.transaction_sha256.clone(),
|
|
})
|
|
.collect());
|
|
}
|
|
use rmm_domain::jobs::Action;
|
|
let required = match action {
|
|
Action::EnsurePackage {
|
|
provider,
|
|
package,
|
|
version,
|
|
architecture,
|
|
recipe_version,
|
|
..
|
|
} => {
|
|
if provider != "apt" || architecture != "amd64" {
|
|
return Err(Error(StatusCode::CONFLICT, "UNSUPPORTED_CAPABILITY"));
|
|
}
|
|
vec![(
|
|
package.clone(),
|
|
version.clone(),
|
|
Some(recipe_version.clone()),
|
|
)]
|
|
}
|
|
Action::ApplyUpdates {
|
|
packages,
|
|
provider,
|
|
reboot_policy,
|
|
..
|
|
} => {
|
|
if provider != "apt" || reboot_policy != "never" {
|
|
return Err(Error(StatusCode::CONFLICT, "POLICY_DENIED"));
|
|
}
|
|
let mut required = Vec::new();
|
|
for package in packages {
|
|
let (name, version) = package
|
|
.split_once('=')
|
|
.filter(|(n, v)| !n.is_empty() && !v.is_empty())
|
|
.ok_or(Error(
|
|
StatusCode::BAD_REQUEST,
|
|
"EXACT_PACKAGE_VERSION_REQUIRED",
|
|
))?;
|
|
required.push((name.into(), version.into(), None));
|
|
}
|
|
required
|
|
}
|
|
_ => return Ok(Vec::new()),
|
|
};
|
|
let mut references = Vec::new();
|
|
for (name, version, recipe_version) in required {
|
|
let row=sqlx::query("SELECT payload,payload_hash,recipe_version FROM package_recipes WHERE package=$1 AND package_version=$2 AND architecture='amd64'").bind(&name).bind(&version).fetch_optional(&mut **tx).await?.ok_or(Error(StatusCode::CONFLICT,"RECIPE_NOT_CONFIGURED"))?;
|
|
let bytes: Vec<u8> = row.get("payload");
|
|
if rmm_domain::jobs::hash(&bytes) != row.get::<String, _>("payload_hash")
|
|
|| recipe_version.is_some_and(|v| v != row.get::<String, _>("recipe_version"))
|
|
{
|
|
return Err(Error(StatusCode::CONFLICT, "RECIPE_BINDING_MISMATCH"));
|
|
}
|
|
references.push(rmm_domain::jobs::RecipeReference {
|
|
package: name,
|
|
version,
|
|
architecture: "amd64".into(),
|
|
recipe_version: row.get("recipe_version"),
|
|
sha256: row.get("payload_hash"),
|
|
});
|
|
}
|
|
references.sort();
|
|
references.dedup();
|
|
Ok(references)
|
|
}
|
|
async fn context<'a>(
|
|
s: &'a crate::State,
|
|
h: &HeaderMap,
|
|
t: Uuid,
|
|
write: bool,
|
|
) -> Result<(crate::auth::Principal, Transaction<'a, Postgres>), Error> {
|
|
let p = s
|
|
.auth
|
|
.principal(s, h, write)
|
|
.await
|
|
.map_err(|status| Error(status, crate::auth::error_code(status)))?;
|
|
crate::jobs::scope(&p, t, write)?;
|
|
if write && p.session_hash.is_none() {
|
|
return Err(Error(StatusCode::FORBIDDEN, "INTERACTIVE_AUTH_REQUIRED"));
|
|
}
|
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
|
crate::permissions::require_principal(
|
|
&mut tx,
|
|
&p,
|
|
if write {
|
|
"catalog:write"
|
|
} else {
|
|
"operations:read"
|
|
},
|
|
)
|
|
.await?;
|
|
Ok((p, tx))
|
|
}
|
|
pub async fn import(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(t): Path<Uuid>,
|
|
Json(recipe): Json<AptRecipe>,
|
|
) -> Result<Json<Value>, Error> {
|
|
let (p, mut tx) = context(&s, &h, t, true).await?;
|
|
let action = rmm_domain::jobs::Action::EnsurePackage {
|
|
provider: recipe.provider.clone(),
|
|
package: recipe.package.clone(),
|
|
version: recipe.version.clone(),
|
|
architecture: recipe.architecture.clone(),
|
|
recipe_version: recipe.recipe_version.clone(),
|
|
installation_context: recipe.installation_context.clone(),
|
|
recipe_id: None,
|
|
apt_manifest: None,
|
|
};
|
|
if recipe.schema_version != 1
|
|
|| recipe.reboot_policy != "never"
|
|
|| !action.validate_plan_shape()
|
|
{
|
|
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
|
}
|
|
let bytes = rmm_domain::jobs::encode(&recipe)?;
|
|
let hash = rmm_domain::jobs::hash(&bytes);
|
|
let id = Uuid::new_v4();
|
|
sqlx::query("INSERT INTO package_recipes(tenant_id,recipe_id,package,package_version,architecture,recipe_version,payload,payload_hash) VALUES($1,$2,$3,$4,$5,$6,$7,$8)")
|
|
.bind(t)
|
|
.bind(id)
|
|
.bind(&recipe.package)
|
|
.bind(&recipe.version)
|
|
.bind(&recipe.architecture)
|
|
.bind(&recipe.recipe_version)
|
|
.bind(bytes)
|
|
.bind(&hash)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query("INSERT INTO audit_events VALUES($1,$2,$3,'recipe_imported',$4,now())")
|
|
.bind(t)
|
|
.bind(Uuid::new_v4())
|
|
.bind(p.name)
|
|
.bind(json!({"recipe_id":id,"sha256":hash,"testsimulation":p.testsimulation}))
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
tx.commit().await?;
|
|
Ok(Json(json!({"recipe_id":id,"sha256":hash,"recipe":recipe})))
|
|
}
|
|
pub async fn list(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(t): Path<Uuid>,
|
|
axum::extract::Query(query): axum::extract::Query<crate::pagination::PageQuery>,
|
|
) -> Result<Json<Value>, Error> {
|
|
let (p, mut tx) = context(&s, &h, t, false).await?;
|
|
let mut page = crate::pagination::Cursor::parse(&s, &p, "catalog:recipe_id:asc", query)
|
|
.map_err(|status| Error(status, "VALIDATION_ERROR"))?;
|
|
let rows = sqlx::query(
|
|
"SELECT recipe_id,payload,payload_hash FROM package_recipes WHERE recipe_id>$1 AND created_at<=$2 ORDER BY recipe_id LIMIT $3",
|
|
)
|
|
.bind(page.last).bind(page.as_of).bind(i64::from(page.limit)+1)
|
|
.fetch_all(&mut *tx)
|
|
.await?;
|
|
let truncated = rows.len() > usize::from(page.limit);
|
|
let next = if truncated {
|
|
Some(
|
|
page.next(&s, rows[usize::from(page.limit) - 1].get("recipe_id"))
|
|
.map_err(|status| Error(status, "VALIDATION_ERROR"))?,
|
|
)
|
|
} else {
|
|
None
|
|
};
|
|
let mut items = Vec::new();
|
|
for row in rows.iter().take(usize::from(page.limit)) {
|
|
let recipe: AptRecipe = serde_json::from_slice(&row.get::<Vec<u8>, _>("payload"))?;
|
|
items.push(json!({"recipe_id":row.get::<Uuid,_>("recipe_id"),"sha256":row.get::<String,_>("payload_hash"),"recipe":recipe}));
|
|
}
|
|
tx.commit().await?;
|
|
Ok(Json(
|
|
json!({"items":items,"next_cursor":next,"completeness":if truncated{"partial"}else{"complete"},"truncated":truncated,"as_of":page.as_of}),
|
|
))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Upload {
|
|
bytes: Vec<u8>,
|
|
media_type: String,
|
|
}
|
|
pub(crate) fn store() -> Result<crate::artifact_store::LocalArtifacts, Error> {
|
|
let key =
|
|
std::env::var("RMM_JOB_KEY_FILE").unwrap_or_else(|_| "devstate/jobs/ed25519.pem".into());
|
|
let root = std::path::Path::new(&key)
|
|
.parent()
|
|
.ok_or(Error(StatusCode::SERVICE_UNAVAILABLE, "NOT_CONFIGURED"))?
|
|
.join("artifacts");
|
|
Ok(crate::artifact_store::LocalArtifacts::new(
|
|
&std::env::var("RMM_PROFILE").unwrap_or_default(),
|
|
root,
|
|
)?)
|
|
}
|
|
pub async fn resolve_artifacts(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
tenant: Uuid,
|
|
ids: &[Uuid],
|
|
) -> Result<Vec<rmm_domain::jobs::ArtifactReference>, Error> {
|
|
let mut ids = ids.to_vec();
|
|
ids.sort();
|
|
ids.dedup();
|
|
if ids.len() > 16 {
|
|
return Err(Error(StatusCode::PAYLOAD_TOO_LARGE, "ARTIFACT_COUNT_LIMIT"));
|
|
}
|
|
let mut resolved = Vec::new();
|
|
for id in ids {
|
|
let row = sqlx::query("SELECT sha256,size_bytes FROM artifacts WHERE artifact_id=$1")
|
|
.bind(id)
|
|
.fetch_optional(&mut **tx)
|
|
.await?
|
|
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
|
let hash: String = row.get("sha256");
|
|
let size: i64 = row.get("size_bytes");
|
|
let bytes = store()?.get(tenant, &hash)?;
|
|
if bytes.len() as i64 != size {
|
|
return Err(Error(
|
|
StatusCode::SERVICE_UNAVAILABLE,
|
|
"ARTIFACT_INTEGRITY_FAILURE",
|
|
));
|
|
}
|
|
resolved.push(rmm_domain::jobs::ArtifactReference {
|
|
artifact_id: id,
|
|
sha256: hash,
|
|
size_bytes: size as u64,
|
|
});
|
|
}
|
|
Ok(resolved)
|
|
}
|
|
pub async fn upload(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path(t): Path<Uuid>,
|
|
Json(i): Json<Upload>,
|
|
) -> Result<Json<Value>, Error> {
|
|
let (p, mut tx) = context(&s, &h, t, true).await?;
|
|
if i.bytes.is_empty()
|
|
|| i.bytes.len() > 262144
|
|
|| !matches!(
|
|
i.media_type.as_str(),
|
|
"application/octet-stream" | "text/plain"
|
|
)
|
|
{
|
|
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
|
}
|
|
let hash = store()?.put(t, &i.bytes)?;
|
|
let id = Uuid::new_v4();
|
|
sqlx::query("INSERT INTO artifacts(tenant_id,artifact_id,sha256,size_bytes,media_type) VALUES($1,$2,$3,$4,$5)").bind(t).bind(id).bind(&hash).bind(i.bytes.len() as i64).bind(&i.media_type).execute(&mut *tx).await?;
|
|
sqlx::query("INSERT INTO audit_events VALUES($1,$2,$3,'artifact_imported',$4,now())").bind(t).bind(Uuid::new_v4()).bind(&p.name).bind(json!({"artifact_id":id,"sha256":hash,"size_bytes":i.bytes.len(),"testsimulation":p.testsimulation})).execute(&mut *tx).await?;
|
|
tx.commit().await?;
|
|
Ok(Json(
|
|
json!({"artifact_id":id,"sha256":hash,"size_bytes":i.bytes.len()}),
|
|
))
|
|
}
|
|
pub async fn download(
|
|
State(s): State<crate::State>,
|
|
h: HeaderMap,
|
|
Path((t, id)): Path<(Uuid, Uuid)>,
|
|
) -> Result<Response, Error> {
|
|
let (_, mut tx) = context(&s, &h, t, false).await?;
|
|
let row = sqlx::query("SELECT sha256,size_bytes FROM artifacts WHERE artifact_id=$1")
|
|
.bind(id)
|
|
.fetch_optional(&mut *tx)
|
|
.await?
|
|
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
|
tx.commit().await?;
|
|
let bytes = store()?.get(t, &row.get::<String, _>("sha256"))?;
|
|
if bytes.len() as i64 != row.get::<i64, _>("size_bytes") {
|
|
return Err(Error(
|
|
StatusCode::SERVICE_UNAVAILABLE,
|
|
"ARTIFACT_INTEGRITY_FAILURE",
|
|
));
|
|
}
|
|
Ok((
|
|
[
|
|
("content-type", "application/octet-stream"),
|
|
("content-disposition", "attachment"),
|
|
],
|
|
bytes,
|
|
)
|
|
.into_response())
|
|
}
|