Organize device inventory by customer and restrict token access to its audience
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
//! Fresh-device bootstrap. Public scripts contain no credentials. Downloads use
|
||||
//! the tenant's one-time enrollment token until the final enrollment consumes it.
|
||||
use super::*;
|
||||
mod starter;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -22,7 +23,7 @@ struct TenantPins { current: Pin, previous: Vec<Previous> }
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(super) struct UpdaterQuery { platform: Platform, sha256: Option<String> }
|
||||
|
||||
fn config(platform: Platform, tenant: Uuid, wanted: Option<&str>) -> Result<Config, Error> {
|
||||
async fn config(s: &crate::State, platform: Platform, tenant: Uuid, wanted: Option<&str>) -> Result<Config, Error> {
|
||||
let slot = match platform {
|
||||
Platform::LinuxX64 => "RMM_AGENT_BOOTSTRAP_LINUX_FILE",
|
||||
Platform::WindowsX64 => "RMM_AGENT_BOOTSTRAP_WINDOWS_FILE",
|
||||
@@ -42,6 +43,15 @@ fn config(platform: Platform, tenant: Uuid, wanted: Option<&str>) -> Result<Conf
|
||||
};
|
||||
config.updater_sha256 = selected.sha256;
|
||||
config.updater_bytes = selected.bytes;
|
||||
} else {
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
||||
let pin = sqlx::query("SELECT sha256,size_bytes FROM customer_starter_workers WHERE platform=$1")
|
||||
.bind(platform_name(platform)).fetch_optional(&mut *tx).await?;
|
||||
tx.commit().await?;
|
||||
if let Some(pin) = pin {
|
||||
config.updater_sha256 = pin.get("sha256");
|
||||
config.updater_bytes = pin.get::<i64, _>("size_bytes") as u64;
|
||||
}
|
||||
}
|
||||
if wanted.is_some_and(|digest| digest != config.updater_sha256) { return Err(missing()); }
|
||||
let url = url::Url::parse(&config.gateway_origin).map_err(|_| invalid())?;
|
||||
@@ -98,6 +108,7 @@ pub(super) async fn issue(
|
||||
let mut bytes = [0; 32];
|
||||
openssl::rand::rand_bytes(&mut bytes).map_err(|_| fail("TOKEN_GENERATION_FAILED"))?;
|
||||
let token = hex::encode(bytes);
|
||||
starter::publish_missing(&mut tx, t).await?;
|
||||
let release_platforms: Vec<String> =
|
||||
sqlx::query_scalar("SELECT DISTINCT platform FROM agent_update_releases ORDER BY platform")
|
||||
.fetch_all(&mut *tx)
|
||||
@@ -170,7 +181,7 @@ pub(super) async fn current(
|
||||
Query(q): Query<Current>,
|
||||
) -> Result<Response, Error> {
|
||||
let t = token_tenant(&s, &headers).await?;
|
||||
let c = config(q.platform, t, None)?;
|
||||
let c = config(&s, q.platform, t, None).await?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
||||
let release: Uuid = sqlx::query_scalar("SELECT release_id FROM agent_update_releases WHERE platform=$1 ORDER BY release_sequence DESC LIMIT 1")
|
||||
.bind(platform_name(q.platform)).fetch_optional(&mut *tx).await?.ok_or_else(missing)?;
|
||||
@@ -217,6 +228,6 @@ pub(super) async fn updater(
|
||||
Query(q): Query<UpdaterQuery>,
|
||||
) -> Result<Response, Error> {
|
||||
let tenant = token_tenant(&s, &headers).await?;
|
||||
let c = config(q.platform, tenant, q.sha256.as_deref())?;
|
||||
let c = config(&s, q.platform, tenant, q.sha256.as_deref()).await?;
|
||||
verified_binary(tenant, c.updater_sha256, c.updater_bytes).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user