Files
windmill/backend/windmill-object-store/src/fork_store.rs
T
9375805fdf feat: isolate fork object storage under a per-fork prefix (#11829)
* feat: route the python SDK's S3 client settings through the S3 proxy

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: drop the boto3 config key and expose the proxied bucket to polars

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: proxy S3 settings only for server-signed tokens and keep tls verify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: isolate fork object storage under a per-fork prefix with read-through

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: guard fork storage isolation against traversal, shared lakes and orphaning

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: serialize fork purges with same-id creation and guard shared middle forks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: drop fork storage mappings on every replica and lock fork deletion against child creation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref to 455681e55eb7cdfcfdb7cd79cbae75b333e07943

This commit updates the EE repository reference after PR #893 was merged in windmill-ee-private.

Previous ee-repo-ref: 93f26e3cf816e4eb97b7d4dbbb5fc97fdf9821af

New ee-repo-ref: 455681e55eb7cdfcfdb7cd79cbae75b333e07943

Automated by sync-ee-ref workflow.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
2026-10-16 22:21:38 +02:00

261 lines
9.4 KiB
Rust

//! An [`ObjectStore`] over one workspace storage that applies the workspace's
//! [`ForkStorageKeys`]: callers keep using logical keys.
use std::sync::Arc;
use futures::{stream::BoxStream, StreamExt, TryStreamExt};
use object_store::path::Path;
use object_store::{
GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, ObjectStore, PutMultipartOpts,
PutOptions, PutPayload, PutResult, Result,
};
use windmill_common::fork_storage::{fork_storage_keys, ForkStorageKeys};
use crate::{build_object_store_client, ObjectStoreResource};
/// The client for `w_id`'s storage `storage` (None = default). Every server-side access to a
/// workspace storage by logical key goes through this, so fork isolation holds on each path.
///
/// It checks no authorization: the caller must already have checked that the requester may
/// use `w_id`'s storage, and its permission rules for each key it reads or writes.
pub async fn workspace_object_store(
db: &windmill_common::DB,
w_id: &str,
storage: Option<&str>,
resource: &ObjectStoreResource,
) -> windmill_common::error::Result<Arc<dyn ObjectStore>> {
Ok(workspace_fork_store(db, w_id, storage, resource).await?)
}
/// [`workspace_object_store`] as its concrete type, under the same authorization contract.
pub async fn workspace_fork_store(
db: &windmill_common::DB,
w_id: &str,
storage: Option<&str>,
resource: &ObjectStoreResource,
) -> windmill_common::error::Result<Arc<ForkStore>> {
let inner = build_object_store_client(resource).await?;
let keys = fork_storage_keys(db, w_id, storage).await?;
Ok(Arc::new(ForkStore { inner, keys }))
}
#[derive(Debug)]
pub struct ForkStore {
inner: Arc<dyn ObjectStore>,
keys: Arc<ForkStorageKeys>,
}
impl ForkStore {
pub fn new(inner: Arc<dyn ObjectStore>, keys: Arc<ForkStorageKeys>) -> Self {
Self { inner, keys }
}
pub fn keys(&self) -> &ForkStorageKeys {
&self.keys
}
/// This store with reads confined to the workspace's own copies.
pub fn own_only(&self) -> Arc<dyn ObjectStore> {
Arc::new(ForkStore { inner: self.inner.clone(), keys: Arc::new(self.keys.own_only()) })
}
}
impl std::fmt::Display for ForkStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ForkStore({})", self.inner)
}
}
fn denied(location: &Path, e: windmill_common::error::Error) -> object_store::Error {
object_store::Error::PermissionDenied { path: location.to_string(), source: Box::new(e) }
}
fn parse(key: String) -> Result<Path> {
Path::parse(key).map_err(|source| object_store::Error::InvalidPath { source })
}
impl ForkStore {
fn write_path(&self, location: &Path) -> Result<Path> {
parse(
self.keys
.write_key(location.as_ref())
.map_err(|e| denied(location, e))?,
)
}
fn read_paths(&self, location: &Path) -> Result<Vec<Path>> {
self.keys
.read_keys(location.as_ref())
.map_err(|e| denied(location, e))?
.into_iter()
.map(parse)
.collect()
}
/// The physical path of the first of `location`'s read paths that exists (the last one
/// when none does).
pub async fn resolve_read(&self, location: &Path) -> Result<Path> {
parse(self.resolve_read_key(location.as_ref()).await?)
}
/// [`Self::resolve_read`] on a raw key, for callers that forward the key themselves.
pub async fn resolve_read_key(&self, key: &str) -> Result<String> {
let location = Path::from(key);
let mut keys = self.keys.read_keys(key).map_err(|e| denied(&location, e))?;
let last = keys.pop().expect("read_keys returns at least one key");
for candidate in keys {
match self.inner.head(&parse(candidate.clone())?).await {
Ok(_) => return Ok(candidate),
Err(object_store::Error::NotFound { .. }) => continue,
Err(e) => return Err(e),
}
}
Ok(last)
}
}
#[async_trait::async_trait]
impl ObjectStore for ForkStore {
async fn put_opts(
&self,
location: &Path,
payload: PutPayload,
opts: PutOptions,
) -> Result<PutResult> {
self.inner
.put_opts(&self.write_path(location)?, payload, opts)
.await
}
async fn put_multipart_opts(
&self,
location: &Path,
opts: PutMultipartOpts,
) -> Result<Box<dyn MultipartUpload>> {
self.inner
.put_multipart_opts(&self.write_path(location)?, opts)
.await
}
async fn get_opts(&self, location: &Path, options: GetOptions) -> Result<GetResult> {
let paths = self.read_paths(location)?;
let last = paths.len() - 1;
for (i, path) in paths.into_iter().enumerate() {
match self.inner.get_opts(&path, options.clone()).await {
Err(object_store::Error::NotFound { .. }) if i < last => continue,
Ok(mut result) => {
result.meta.location = location.clone();
return Ok(result);
}
Err(e) => return Err(e),
}
}
unreachable!("read_keys returns at least one key")
}
async fn head(&self, location: &Path) -> Result<ObjectMeta> {
let paths = self.read_paths(location)?;
let last = paths.len() - 1;
for (i, path) in paths.into_iter().enumerate() {
match self.inner.head(&path).await {
Err(object_store::Error::NotFound { .. }) if i < last => continue,
Ok(meta) => return Ok(ObjectMeta { location: location.clone(), ..meta }),
Err(e) => return Err(e),
}
}
unreachable!("read_keys returns at least one key")
}
/// Removes this workspace's copy only: an ancestor's object stays readable through it.
async fn delete(&self, location: &Path) -> Result<()> {
self.inner.delete(&self.write_path(location)?).await
}
fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, Result<ObjectMeta>> {
let prefix = prefix.map(|p| p.as_ref().to_string()).unwrap_or_default();
let mapping = match self.keys.list(&prefix) {
Ok(m) => m,
Err(e) => {
let err = denied(&Path::from(prefix), e);
return futures::stream::once(async move { Err(err) }).boxed();
}
};
let physical = Path::from(mapping.physical_prefix.clone());
self.inner
.list(Some(&physical))
.try_filter_map(move |meta| {
let logical = mapping.logical_key(meta.location.as_ref());
futures::future::ready(Ok(logical
.and_then(|l| Path::parse(l).ok())
.map(|location| ObjectMeta { location, ..meta })))
})
.boxed()
}
fn list_with_offset(
&self,
prefix: Option<&Path>,
offset: &Path,
) -> BoxStream<'static, Result<ObjectMeta>> {
let prefix = prefix.map(|p| p.as_ref().to_string()).unwrap_or_default();
let mapping = match self.keys.list(&prefix) {
Ok(m) => m,
Err(e) => {
let err = denied(&Path::from(prefix), e);
return futures::stream::once(async move { Err(err) }).boxed();
}
};
let physical = Path::from(mapping.physical_prefix.clone());
let physical_offset = match parse(mapping.physical_key(offset.as_ref())) {
Ok(p) => p,
Err(e) => return futures::stream::once(async move { Err(e) }).boxed(),
};
self.inner
.list_with_offset(Some(&physical), &physical_offset)
.try_filter_map(move |meta| {
let logical = mapping.logical_key(meta.location.as_ref());
futures::future::ready(Ok(logical
.and_then(|l| Path::parse(l).ok())
.map(|location| ObjectMeta { location, ..meta })))
})
.boxed()
}
async fn list_with_delimiter(&self, prefix: Option<&Path>) -> Result<ListResult> {
let prefix_str = prefix.map(|p| p.as_ref().to_string()).unwrap_or_default();
let mapping = self
.keys
.list(&prefix_str)
.map_err(|e| denied(&Path::from(prefix_str.clone()), e))?;
let physical = Path::from(mapping.physical_prefix.clone());
let listed = self.inner.list_with_delimiter(Some(&physical)).await?;
let logical = |p: &Path| {
mapping
.logical_key(p.as_ref())
.and_then(|l| Path::parse(l).ok())
};
Ok(ListResult {
common_prefixes: listed.common_prefixes.iter().filter_map(logical).collect(),
objects: listed
.objects
.into_iter()
.filter_map(|meta| {
logical(&meta.location).map(|location| ObjectMeta { location, ..meta })
})
.collect(),
})
}
async fn copy(&self, from: &Path, to: &Path) -> Result<()> {
let from = self.resolve_read(from).await?;
self.inner.copy(&from, &self.write_path(to)?).await
}
async fn copy_if_not_exists(&self, from: &Path, to: &Path) -> Result<()> {
let from = self.resolve_read(from).await?;
self.inner
.copy_if_not_exists(&from, &self.write_path(to)?)
.await
}
}