mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
Merge feat/transactional-package-install: install and update packages as immutable revisions activated atomically (#25)
This commit is contained in:
commit
74135a462a
9 changed files with 1564 additions and 126 deletions
|
|
@ -14,7 +14,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web
|
|||
|
||||
**Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities.
|
||||
|
||||
**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall.
|
||||
**Package management** — `weft-pack` handles check, sign, verify, install and update, rollback, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall.
|
||||
|
||||
**File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking.
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,9 @@ pub(crate) struct LaunchPackage {
|
|||
/// The image the package root is mounted from, held for as long as the
|
||||
/// session runs.
|
||||
pub(crate) image: Option<Mount>,
|
||||
/// The lock that keeps an installed revision from being removed while
|
||||
/// the session runs from it.
|
||||
pub(crate) pin: Option<weft_ipc_types::store::Pin>,
|
||||
}
|
||||
|
||||
/// Resolves `app_id` in the package stores.
|
||||
|
|
@ -68,16 +71,47 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result<LaunchPackage, Refusal
|
|||
let root = image.root().to_path_buf();
|
||||
return from_root(app_id, root, Some(image));
|
||||
}
|
||||
// A store whose entry for the app cannot be used does not hide the app
|
||||
// in a later store, as it does not in the list of installed apps; the
|
||||
// first such problem is reported when no store has the app.
|
||||
let mut unusable = None;
|
||||
for store in stores {
|
||||
let dir = store.join(app_id);
|
||||
if std::fs::symlink_metadata(dir.join(MANIFEST_FILE)).is_ok() {
|
||||
return from_root(app_id, dir, None);
|
||||
let active = match weft_ipc_types::store::active(store, app_id) {
|
||||
Ok(Some(active)) => active,
|
||||
Ok(None) => continue,
|
||||
Err(e) => {
|
||||
tracing::warn!(app_id, error = %e, "package entry skipped");
|
||||
unusable.get_or_insert(match e {
|
||||
weft_ipc_types::store::StoreError::NotAPackage(_) => {
|
||||
Refusal::new(403, e.to_string())
|
||||
}
|
||||
weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let dir = active.dir().to_path_buf();
|
||||
// A directory left with only app data by an earlier layout is not a
|
||||
// package.
|
||||
if std::fs::symlink_metadata(dir.join(MANIFEST_FILE)).is_err() {
|
||||
continue;
|
||||
}
|
||||
// The session runs from the revision itself, not the link, so an
|
||||
// update or rollback that switches the link while it runs leaves it
|
||||
// on the bytes it started with. From here on a refusal is final: a
|
||||
// package that is being changed, or that fails its checks, is not
|
||||
// silently replaced by another store's copy.
|
||||
let pin = weft_ipc_types::store::pin(&dir).map_err(|e| {
|
||||
Refusal::new(
|
||||
500,
|
||||
format!("cannot hold {} while it runs: {e}", dir.display()),
|
||||
)
|
||||
})?;
|
||||
let mut package = from_root(app_id, dir, None)?;
|
||||
package.pin = Some(pin);
|
||||
return Ok(package);
|
||||
}
|
||||
Err(Refusal::new(
|
||||
404,
|
||||
format!("package {app_id} is not installed"),
|
||||
))
|
||||
Err(unusable.unwrap_or_else(|| Refusal::new(404, format!("package {app_id} is not installed"))))
|
||||
}
|
||||
|
||||
fn from_root(app_id: &str, root: PathBuf, image: Option<Mount>) -> Result<LaunchPackage, Refusal> {
|
||||
|
|
@ -129,6 +163,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option<Mount>) -> Result<Launch
|
|||
ui_entry,
|
||||
root,
|
||||
image,
|
||||
pin: None,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -249,6 +284,7 @@ impl LaunchPackage {
|
|||
ui_entry: PathBuf::from("/nonexistent/ui/index.html"),
|
||||
capabilities: Vec::new(),
|
||||
image: None,
|
||||
pin: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -382,14 +418,69 @@ mod tests {
|
|||
std::os::unix::fs::symlink(store.join("manifest.toml"), &manifest).unwrap();
|
||||
let manifest_link = refusal(resolve(ID));
|
||||
finish(&store);
|
||||
for refused in [root_link, manifest_link] {
|
||||
assert_eq!(refused.code, 403, "{}", refused.message);
|
||||
assert!(
|
||||
refused.message.contains("not a link"),
|
||||
"{}",
|
||||
refused.message
|
||||
);
|
||||
}
|
||||
// Only a link to one of the app's own revisions is followed.
|
||||
assert_eq!(root_link.code, 403, "{}", root_link.message);
|
||||
assert!(
|
||||
root_link
|
||||
.message
|
||||
.contains("nor a link to one of its revisions"),
|
||||
"{}",
|
||||
root_link.message
|
||||
);
|
||||
assert_eq!(manifest_link.code, 403, "{}", manifest_link.message);
|
||||
assert!(
|
||||
manifest_link.message.contains("not a link"),
|
||||
"{}",
|
||||
manifest_link.message
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn an_unusable_store_entry_does_not_hide_a_later_store() {
|
||||
let _env = crate::tests::env_lock().blocking_lock();
|
||||
let user = store("unusable_user", ID, "app.wasm");
|
||||
let system = std::env::temp_dir().join(format!(
|
||||
"weft_appd_launch_unusable_system_{}",
|
||||
std::process::id()
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(&system);
|
||||
std::fs::create_dir_all(&system).unwrap();
|
||||
std::fs::rename(user.join(ID), system.join(ID)).unwrap();
|
||||
// A link to something other than one of the app's revisions.
|
||||
std::os::unix::fs::symlink("/tmp", user.join(ID)).unwrap();
|
||||
let found = resolve_in(ID, &[user.clone(), system.clone()]).map(|p| p.root);
|
||||
let alone = refusal(resolve_in(ID, std::slice::from_ref(&user)));
|
||||
finish(&user);
|
||||
let _ = std::fs::remove_dir_all(&system);
|
||||
assert_eq!(found.ok(), Some(system.join(ID)));
|
||||
// With no other store, its problem is reported, not "not installed".
|
||||
assert_eq!(alone.code, 403, "{}", alone.message);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_session_runs_from_its_revision_and_holds_it() {
|
||||
use weft_ipc_types::store::{link_target, revisions_of, try_claim};
|
||||
let _env = crate::tests::env_lock().blocking_lock();
|
||||
let store = store("revision", ID, "app.wasm");
|
||||
let revision = "ab".repeat(32);
|
||||
let dir = revisions_of(&store, ID).join(&revision);
|
||||
std::fs::create_dir_all(dir.parent().unwrap()).unwrap();
|
||||
std::fs::rename(store.join(ID), &dir).unwrap();
|
||||
std::os::unix::fs::symlink(link_target(ID, &revision), store.join(ID)).unwrap();
|
||||
let package = resolve(ID).unwrap();
|
||||
let held = try_claim(&dir).unwrap().is_none();
|
||||
let root = package.root.clone();
|
||||
let module = package.module.clone();
|
||||
drop(package);
|
||||
let released = try_claim(&dir).unwrap().is_some();
|
||||
finish(&store);
|
||||
// The children get the revision's own paths, not the link's.
|
||||
assert_eq!(root, dir);
|
||||
assert_eq!(module, dir.join("app.wasm"));
|
||||
assert!(held, "the revision is not held while the session runs");
|
||||
assert!(released, "the revision is still held after the session");
|
||||
}
|
||||
|
||||
const DEMO: &str = "org.weft.demo.counter";
|
||||
|
|
|
|||
|
|
@ -631,12 +631,22 @@ fn scan_installed_apps() -> Vec<AppInfo> {
|
|||
continue;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else {
|
||||
// Only an app's active package, named after the ID its manifest
|
||||
// declares, is installed; staging copies, revisions and stray
|
||||
// directories or links are not.
|
||||
let Some(name) = entry.file_name().to_str().map(str::to_owned) else {
|
||||
continue;
|
||||
};
|
||||
// Only a directory named after the ID its manifest declares is an
|
||||
// installed package; staging copies and stray directories are not.
|
||||
if entry.file_name().to_str() != Some(m.package.id.as_str()) {
|
||||
if !weft_ipc_types::package::is_valid_app_id(&name) {
|
||||
continue;
|
||||
}
|
||||
let Ok(Some(found)) = weft_ipc_types::store::active(&root, &name) else {
|
||||
continue;
|
||||
};
|
||||
let Ok(m) = weft_ipc_types::manifest::Manifest::read(found.dir()) else {
|
||||
continue;
|
||||
};
|
||||
if m.package.id != name {
|
||||
continue;
|
||||
}
|
||||
if seen.insert(m.package.id.clone()) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
pub mod capability;
|
||||
pub mod manifest;
|
||||
pub mod package;
|
||||
pub mod store;
|
||||
pub mod trust;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
|
|||
238
crates/weft-ipc-types/src/store.rs
Normal file
238
crates/weft-ipc-types/src/store.rs
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
//! The layout of a package store and the revisions it holds.
|
||||
//!
|
||||
//! Each installed revision of a package is an immutable directory named by
|
||||
//! its content digest, `<store>/.revisions/<app_id>/<digest>`. The active
|
||||
//! revision is chosen by `<store>/<app_id>`, a symbolic link to it that one
|
||||
//! rename replaces, so an update or rollback switches every new launch at
|
||||
//! once and an interrupted one leaves the previous choice in place. Stores
|
||||
//! written before revisions existed hold the package directly in
|
||||
//! `<store>/<app_id>`; that layout is still read.
|
||||
//!
|
||||
//! A session pins the revision it was launched from with a shared lock on
|
||||
//! the revision directory, held for the session's lifetime. Package
|
||||
//! operations remove a revision only after taking that lock exclusively, so
|
||||
//! a live session keeps running on the bytes it started with.
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// The directory of a store that holds package revisions.
|
||||
pub const REVISIONS_DIR: &str = ".revisions";
|
||||
|
||||
/// The directory holding the revisions of `app_id`.
|
||||
pub fn revisions_of(store_root: &Path, app_id: &str) -> PathBuf {
|
||||
store_root.join(REVISIONS_DIR).join(app_id)
|
||||
}
|
||||
|
||||
/// The link target that makes `revision` the active revision of `app_id`,
|
||||
/// relative to the store root.
|
||||
pub fn link_target(app_id: &str, revision: &str) -> PathBuf {
|
||||
Path::new(REVISIONS_DIR).join(app_id).join(revision)
|
||||
}
|
||||
|
||||
/// Whether `name` is a revision name: the hex content digest of a package.
|
||||
pub fn is_revision_name(name: &str) -> bool {
|
||||
name.len() == 64 && name.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
|
||||
}
|
||||
|
||||
/// Where the active package of `app_id` is in a store.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Active {
|
||||
/// A revision, by name, in `.revisions/<app_id>/`.
|
||||
Revision { name: String, dir: PathBuf },
|
||||
/// A package directory installed before revisions existed.
|
||||
Directory(PathBuf),
|
||||
}
|
||||
|
||||
impl Active {
|
||||
/// The package root.
|
||||
pub fn dir(&self) -> &Path {
|
||||
match self {
|
||||
Self::Revision { dir, .. } | Self::Directory(dir) => dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why the active package of a store cannot be determined.
|
||||
#[derive(Debug)]
|
||||
pub enum StoreError {
|
||||
Io(PathBuf, std::io::Error),
|
||||
/// `<store>/<app_id>` is neither a package directory nor a link to one
|
||||
/// of its revisions.
|
||||
NotAPackage(PathBuf),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for StoreError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Io(path, e) => write!(f, "cannot read {}: {e}", path.display()),
|
||||
Self::NotAPackage(path) => write!(
|
||||
f,
|
||||
"{} is neither a package directory nor a link to one of its revisions",
|
||||
path.display()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for StoreError {}
|
||||
|
||||
/// The active package of `app_id` in `store_root`, if any. A link is
|
||||
/// followed only when it names one of the app's own revisions, exactly as
|
||||
/// package operations write it, and that revision is a directory.
|
||||
pub fn active(store_root: &Path, app_id: &str) -> Result<Option<Active>, StoreError> {
|
||||
let entry = store_root.join(app_id);
|
||||
let metadata = match std::fs::symlink_metadata(&entry) {
|
||||
Ok(m) => m,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(e) => return Err(StoreError::Io(entry, e)),
|
||||
};
|
||||
if metadata.is_dir() {
|
||||
return Ok(Some(Active::Directory(entry)));
|
||||
}
|
||||
if !metadata.file_type().is_symlink() {
|
||||
return Err(StoreError::NotAPackage(entry));
|
||||
}
|
||||
let target = std::fs::read_link(&entry).map_err(|e| StoreError::Io(entry.clone(), e))?;
|
||||
let name = target
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.filter(|n| is_revision_name(n))
|
||||
.map(str::to_owned);
|
||||
let Some(name) = name.filter(|n| target == link_target(app_id, n)) else {
|
||||
return Err(StoreError::NotAPackage(entry));
|
||||
};
|
||||
let dir = revisions_of(store_root, app_id).join(&name);
|
||||
match std::fs::symlink_metadata(&dir) {
|
||||
Ok(m) if m.is_dir() => Ok(Some(Active::Revision { name, dir })),
|
||||
Ok(_) => Err(StoreError::NotAPackage(entry)),
|
||||
Err(e) => Err(StoreError::Io(dir, e)),
|
||||
}
|
||||
}
|
||||
|
||||
/// A shared lock on a package directory, held while a session runs from it.
|
||||
#[derive(Debug)]
|
||||
pub struct Pin {
|
||||
_dir: File,
|
||||
}
|
||||
|
||||
/// Pins the package directory `dir`. The lock is taken on the directory
|
||||
/// that is opened, and the path must still name that directory afterwards:
|
||||
/// a revision removed or replaced in between is not pinned. A directory a
|
||||
/// package operation holds is not waited for, so nobody able to lock a
|
||||
/// package directory can stall launches; the launch is refused instead.
|
||||
pub fn pin(dir: &Path) -> std::io::Result<Pin> {
|
||||
let file = File::open(dir)?;
|
||||
match file.try_lock_shared() {
|
||||
Ok(()) => {}
|
||||
Err(std::fs::TryLockError::WouldBlock) => {
|
||||
return Err(std::io::Error::other(format!(
|
||||
"{} is being changed; try again",
|
||||
dir.display()
|
||||
)));
|
||||
}
|
||||
Err(std::fs::TryLockError::Error(e)) => return Err(e),
|
||||
}
|
||||
if !same_file(&file, dir)? {
|
||||
return Err(std::io::Error::other(format!(
|
||||
"{} changed while it was being pinned",
|
||||
dir.display()
|
||||
)));
|
||||
}
|
||||
Ok(Pin { _dir: file })
|
||||
}
|
||||
|
||||
/// Takes the exclusive lock on a package directory that no session pins,
|
||||
/// or returns `None` while one does.
|
||||
pub fn try_claim(dir: &Path) -> std::io::Result<Option<File>> {
|
||||
let file = File::open(dir)?;
|
||||
match file.try_lock() {
|
||||
Ok(()) => Ok(Some(file)),
|
||||
Err(std::fs::TryLockError::WouldBlock) => Ok(None),
|
||||
Err(std::fs::TryLockError::Error(e)) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn same_file(file: &File, path: &Path) -> std::io::Result<bool> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let opened = file.metadata()?;
|
||||
let named = std::fs::symlink_metadata(path)?;
|
||||
Ok(opened.dev() == named.dev() && opened.ino() == named.ino())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn same_file(_file: &File, _path: &Path) -> std::io::Result<bool> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn store(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("weft_store_{name}_{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
const REV: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn only_links_to_the_apps_own_revisions_are_followed() {
|
||||
use std::os::unix::fs::symlink;
|
||||
let root = store("links");
|
||||
std::fs::create_dir_all(revisions_of(&root, "org.a").join(REV)).unwrap();
|
||||
std::fs::create_dir_all(revisions_of(&root, "org.b").join(REV)).unwrap();
|
||||
assert!(active(&root, "org.a").unwrap().is_none());
|
||||
|
||||
symlink(link_target("org.a", REV), root.join("org.a")).unwrap();
|
||||
assert_eq!(
|
||||
active(&root, "org.a").unwrap(),
|
||||
Some(Active::Revision {
|
||||
name: REV.to_owned(),
|
||||
dir: revisions_of(&root, "org.a").join(REV),
|
||||
})
|
||||
);
|
||||
// Another app's revision, an absolute target, a non-revision name.
|
||||
for target in [
|
||||
link_target("org.b", REV),
|
||||
revisions_of(&root, "org.c").join(REV),
|
||||
PathBuf::from(".revisions/org.c/latest"),
|
||||
] {
|
||||
let _ = std::fs::remove_file(root.join("org.c"));
|
||||
std::fs::create_dir_all(revisions_of(&root, "org.c").join(REV)).unwrap();
|
||||
symlink(&target, root.join("org.c")).unwrap();
|
||||
assert!(
|
||||
matches!(active(&root, "org.c"), Err(StoreError::NotAPackage(_))),
|
||||
"{}",
|
||||
target.display()
|
||||
);
|
||||
}
|
||||
// A package installed before revisions existed.
|
||||
std::fs::create_dir(root.join("org.d")).unwrap();
|
||||
assert_eq!(
|
||||
active(&root, "org.d").unwrap(),
|
||||
Some(Active::Directory(root.join("org.d")))
|
||||
);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pinned_directory_cannot_be_claimed() {
|
||||
let root = store("pin");
|
||||
let dir = root.join("rev");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let pinned = pin(&dir).unwrap();
|
||||
assert!(try_claim(&dir).unwrap().is_none());
|
||||
// Other sessions can pin it too.
|
||||
let again = pin(&dir).unwrap();
|
||||
drop((pinned, again));
|
||||
let claimed = try_claim(&dir).unwrap();
|
||||
assert!(claimed.is_some());
|
||||
drop(claimed);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -28,7 +28,7 @@ WASI Preview 2 + Component Model execution host (Wasmtime 30). Runs the componen
|
|||
|
||||
### weft-pack
|
||||
|
||||
Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner), `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`.
|
||||
Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner; installs or updates, see [Installing and updating](#installing-and-updating)), `uninstall`, `rollback`, `list`, `build-image` (EROFS dm-verity), `info`.
|
||||
|
||||
### weft-file-portal
|
||||
|
||||
|
|
@ -115,11 +115,19 @@ Package store roots (in priority order):
|
|||
`weft-appd` resolves a launch once. A verified image in any store root takes precedence over a directory install in any root, so a user's directory cannot shadow a system image; otherwise the first root with a directory install is used:
|
||||
|
||||
- **Verified image:** `<root>/<id>.app.img` with its dm-verity hash tree `<id>.app.hash` and root hash `<id>.app.roothash`, the names `weft-pack build-image` and `build-verity` produce. When any of the three exists, the image must mount through `weft-mount-helper`; a missing companion, a malformed root hash, a missing helper or a failed mount refuses the launch, and appd never falls back to a directory install. The root hash is read from the store next to the image and is not yet authenticated, so dm-verity detects corruption but not a store whose image, hash tree and root hash were replaced together. The mount belongs to the session and is released when the session stops.
|
||||
- **Directory install:** `<root>/<id>/wapp.toml`.
|
||||
- **Directory install:** `<root>/<id>`, a link to the active revision `.revisions/<id>/<digest>` written by `weft-pack`, or, for a store written before revisions, a package directory. A link is followed only in exactly that form, to one of the app's own revisions; any other link is refused (403). The session runs from the revision directory itself and holds a shared lock on it until it stops, so it keeps its bytes while an update or rollback switches the link.
|
||||
|
||||
The package root must be a directory and its manifest a regular file, neither a symbolic link, and the manifest must declare the requested ID. `[runtime].module` and `[ui].entry` must be relative paths of plain components, without symbolic links, naming regular files inside that root. The component, the UI document and the capabilities all come from that one manifest. A refused launch reports 400 (invalid ID), 404 (not installed), 403 (invalid package, image or image metadata) or 500 (the host cannot read the package or mount the image).
|
||||
|
||||
A directory install can still be changed by its owner while a session runs, and nothing verifies its content at launch.
|
||||
A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch, with the limits security.md describes.
|
||||
|
||||
### Installing and updating
|
||||
|
||||
`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `<root>/.revisions/<id>/<digest>`; installing the same content again reuses it. One rename of the link `<root>/<id>` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall <id>` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. Such a replacement, for the same content with another signature file, is visible under the revision's name at once, even if the installation then fails. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused.
|
||||
|
||||
The revision active before an update is kept for `weft-pack rollback <id>`, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data.
|
||||
|
||||
A launch is refused (500, try again) while a package operation holds the revision it would run from, rather than waiting for it. Only sessions started by this weft-appd hold their revisions: a session left from an earlier version, or processes that outlive weft-appd, do not, and a package directory from before revisions such sessions run from can be replaced under them. Package operations on one app are serialised by the owner lock in the data home of the user running them; the store itself is not locked against users with other data homes.
|
||||
|
||||
## App data
|
||||
|
||||
|
|
|
|||
|
|
@ -88,6 +88,7 @@ python3 tests/frame/check_counter.py # Counter round trip through the session
|
|||
python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves
|
||||
python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept
|
||||
python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge
|
||||
python3 tests/frame/check_update.py # updates leave running sessions on their revision; failed updates, rollback and uninstall
|
||||
```
|
||||
|
||||
## Demo apps (wasm32-wasip2)
|
||||
|
|
|
|||
219
tests/frame/check_update.py
Executable file
219
tests/frame/check_update.py
Executable file
|
|
@ -0,0 +1,219 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Check that package updates are transactional and leave running sessions alone.
|
||||
|
||||
The harness installs a test application with weft-pack into a private store
|
||||
and runs weft-appd with the real runtime and app shell on a nested desktop
|
||||
(see session.py). Each revision of the application's page shows its own
|
||||
colour. In the presented pixels and the store:
|
||||
|
||||
1. A session started from revision one keeps showing it after the package is
|
||||
updated to revision two, and its app shell runs from revision one's own
|
||||
directory, which the update keeps while the session runs.
|
||||
2. An update that fails (an invalid component) changes nothing: revision two
|
||||
stays active.
|
||||
3. A session launched after the update shows revision two.
|
||||
4. Once the first session has ended, the next update removes revision one,
|
||||
and keeps revision two for rollback; `weft-pack rollback` makes revision
|
||||
two active again.
|
||||
5. Uninstalling while a session runs stops new launches (404) but not the
|
||||
session; the next uninstall, after it has ended, removes the revisions.
|
||||
|
||||
The application is unsigned development content using the Counter demo's
|
||||
component.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from check_counter import AppdClient, free_port, read_endpoint # noqa: E402
|
||||
from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402
|
||||
|
||||
APP_ID = "org.weft.test.update"
|
||||
COLOURS = {"one": (200, 0, 0), "two": (0, 0, 200), "three": (0, 160, 0)}
|
||||
VERSIONS = {"one": 1, "two": 2, "bad": 3, "three": 4}
|
||||
STARTUP_TIMEOUT = 120.0
|
||||
|
||||
|
||||
def write_revision(src, name, wasm=None):
|
||||
shutil.rmtree(src, ignore_errors=True)
|
||||
(src / "ui").mkdir(parents=True)
|
||||
(src / "wapp.toml").write_text(
|
||||
f'[package]\nid = "{APP_ID}"\nname = "Update"\nversion = "0.0.{VERSIONS[name]}"\n'
|
||||
'[runtime]\nmodule = "app.wasm"\n[ui]\nentry = "ui/index.html"\n')
|
||||
if wasm is None:
|
||||
shutil.copy(ROOT / "examples/org.weft.demo.counter/app.wasm", src / "app.wasm")
|
||||
else:
|
||||
(src / "app.wasm").write_bytes(wasm)
|
||||
colour = COLOURS.get(name, (0, 0, 0))
|
||||
(src / "ui/index.html").write_text(
|
||||
'<!DOCTYPE html><html><body style="margin:0;width:100vw;height:100vh;'
|
||||
f'background:rgb{colour}"></body></html>')
|
||||
|
||||
|
||||
def shows(desktop, colour, timeout=20):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
width, height, pixels = parse_ppm(desktop.capture())
|
||||
if bounding_box(width, height, pixels, colour) is not None:
|
||||
return True
|
||||
time.sleep(0.3)
|
||||
return False
|
||||
|
||||
|
||||
def run(args, desktop, store, home):
|
||||
t = Path(args.target)
|
||||
env = dict(os.environ, WEFT_APP_STORE=str(store), HOME=str(home),
|
||||
XDG_DATA_HOME=str(home / "share"))
|
||||
|
||||
def pack(*arguments, ok=True):
|
||||
done = subprocess.run([t / "weft-pack", *arguments], env=env,
|
||||
capture_output=True, text=True, timeout=60)
|
||||
if ok and done.returncode != 0:
|
||||
raise AssertionError(f"weft-pack {' '.join(arguments)} failed: {done.stderr}")
|
||||
if not ok and done.returncode == 0:
|
||||
raise AssertionError(f"weft-pack {' '.join(arguments)} succeeded: {done.stdout}")
|
||||
return done.stdout + done.stderr
|
||||
|
||||
revisions = store / ".revisions" / APP_ID
|
||||
|
||||
def active():
|
||||
return os.readlink(store / APP_ID)
|
||||
|
||||
src = home / "src"
|
||||
write_revision(src, "one")
|
||||
pack("install", str(src), "--dev")
|
||||
one = store / active()
|
||||
|
||||
desktop.launch_client("appd", [t / "weft-appd"], {
|
||||
"WEFT_RUNTIME_BIN": str(t / "weft-runtime"),
|
||||
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
|
||||
"WEFT_APP_STORE": str(store),
|
||||
"WEFT_DISABLE_CGROUP": "1",
|
||||
"WEFT_APPD_WS_PORT": str(free_port()),
|
||||
"HOME": str(home),
|
||||
"XDG_DATA_HOME": str(home / "share"),
|
||||
})
|
||||
port, token = read_endpoint(desktop.runtime, 30)
|
||||
appd = AppdClient(port, token)
|
||||
|
||||
def launch():
|
||||
appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0})
|
||||
reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), STARTUP_TIMEOUT)
|
||||
if reply.get("type") != "APP_READY":
|
||||
raise AssertionError(f"the app did not start: {reply}")
|
||||
return reply["session_id"]
|
||||
|
||||
def stop(session_id):
|
||||
appd.send({"type": "TERMINATE_APP", "session_id": session_id, "force": True})
|
||||
appd.wait_for(lambda m: m.get("type") == "APP_STATE" and m.get("state") == "stopped"
|
||||
and m.get("session_id") == session_id, 20)
|
||||
|
||||
first = launch()
|
||||
if not shows(desktop, COLOURS["one"]):
|
||||
raise AssertionError("revision one is not shown")
|
||||
shells = subprocess.run(["pgrep", "-af", "weft-app-shell"], capture_output=True,
|
||||
text=True).stdout
|
||||
if str(one / "ui/index.html") not in shells:
|
||||
raise AssertionError(f"the app shell does not run from revision one: {shells}")
|
||||
|
||||
# 1. An update leaves the running session on its revision.
|
||||
write_revision(src, "two")
|
||||
pack("install", str(src), "--dev")
|
||||
two = store / active()
|
||||
if two == one:
|
||||
raise AssertionError("the update did not activate a new revision")
|
||||
time.sleep(2)
|
||||
if not shows(desktop, COLOURS["one"], 5) or shows(desktop, COLOURS["two"], 1):
|
||||
raise AssertionError("the update changed what the running session shows")
|
||||
if (one / "ui/index.html").read_text().find("200, 0, 0") < 0:
|
||||
raise AssertionError("the running session's revision changed")
|
||||
|
||||
# 2. A failed update changes nothing.
|
||||
write_revision(src, "bad", wasm=b"not a component")
|
||||
pack("install", str(src), "--dev", ok=False)
|
||||
if store / active() != two:
|
||||
raise AssertionError("a failed update changed the active revision")
|
||||
|
||||
# 3. A new launch gets the update.
|
||||
second = launch()
|
||||
if not shows(desktop, COLOURS["two"]):
|
||||
raise AssertionError("a launch after the update does not show revision two")
|
||||
stop(second)
|
||||
|
||||
# 4. With the first session ended, the next update collects revision one.
|
||||
if not one.is_dir():
|
||||
raise AssertionError("revision one was removed while its session ran")
|
||||
stop(first)
|
||||
write_revision(src, "three")
|
||||
pack("install", str(src), "--dev")
|
||||
if one.exists():
|
||||
raise AssertionError("revision one was kept after its session ended")
|
||||
if not two.is_dir():
|
||||
raise AssertionError("the previous revision was not kept for rollback")
|
||||
pack("rollback", APP_ID)
|
||||
if store / active() != two:
|
||||
raise AssertionError("rollback did not make revision two active")
|
||||
third = launch()
|
||||
if not shows(desktop, COLOURS["two"]):
|
||||
raise AssertionError("a launch after rollback does not show revision two")
|
||||
|
||||
# 5. Uninstall stops new launches, not the running session.
|
||||
pack("uninstall", APP_ID)
|
||||
appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0})
|
||||
refused = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 30)
|
||||
if refused.get("code") != 404:
|
||||
raise AssertionError(f"a launch after uninstall was not refused: {refused}")
|
||||
if not two.is_dir() or not shows(desktop, COLOURS["two"], 5):
|
||||
raise AssertionError("uninstall disturbed the running session")
|
||||
stop(third)
|
||||
pack("uninstall", APP_ID, ok=False)
|
||||
if revisions.exists():
|
||||
raise AssertionError(f"revisions left after uninstall: {list(revisions.iterdir())}")
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor")
|
||||
parser.add_argument("--target", type=Path, default=ROOT / "target/debug",
|
||||
help="directory with weft-appd, weft-pack, weft-runtime and weft-app-shell")
|
||||
parser.add_argument("--output", type=Path, default=ROOT / "target/update-check")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
missing = missing_tools() + [str(p) for p in (args.compositor, args.target / "weft-appd",
|
||||
args.target / "weft-pack",
|
||||
args.target / "weft-runtime",
|
||||
args.target / "weft-app-shell")
|
||||
if not p.is_file()]
|
||||
if missing:
|
||||
print("missing: " + ", ".join(missing), file=sys.stderr)
|
||||
return 2
|
||||
store = Path(tempfile.mkdtemp(prefix="weft-store-"))
|
||||
home = Path(tempfile.mkdtemp(prefix="weft-home-"))
|
||||
try:
|
||||
with Desktop(args.compositor, args.output, outputs=["appd.log", "update.ppm"]) as desktop:
|
||||
try:
|
||||
run(args, desktop, store, home)
|
||||
except AssertionError:
|
||||
(args.output / "update.ppm").write_bytes(desktop.capture())
|
||||
raise
|
||||
except (AssertionError, SessionError, TimeoutError) as failure:
|
||||
print(f"update check failed: {failure}", file=sys.stderr)
|
||||
print(f"logs and capture in {args.output}", file=sys.stderr)
|
||||
return 1
|
||||
finally:
|
||||
shutil.rmtree(store, ignore_errors=True)
|
||||
shutil.rmtree(home, ignore_errors=True)
|
||||
print("update check passed")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Loading…
Reference in a new issue