From 30e72d2d528552c72cd1aa48a54e1c04e528b39c Mon Sep 17 00:00:00 2001 From: Yureka Date: Sat, 15 Jun 2024 18:37:01 +0200 Subject: refactor(castore/directory/objectstore): use new order validation logic Use the new helper introduced in CL 11708 instead of rolling our own. Change-Id: I292a9bc8baf73a6c75efe784031bcda1835bb645 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11709 Tested-by: BuildkiteCI Autosubmit: yuka Reviewed-by: flokli --- tvix/castore/src/directoryservice/object_store.rs | 24 ++++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'tvix/castore') diff --git a/tvix/castore/src/directoryservice/object_store.rs b/tvix/castore/src/directoryservice/object_store.rs index 90e53f9286a0..feaaaa39cd50 100644 --- a/tvix/castore/src/directoryservice/object_store.rs +++ b/tvix/castore/src/directoryservice/object_store.rs @@ -1,4 +1,3 @@ -use std::collections::HashSet; use std::sync::Arc; use data_encoding::HEXLOWER; @@ -16,7 +15,9 @@ use tonic::async_trait; use tracing::{instrument, trace, warn, Level}; use url::Url; -use super::{DirectoryGraph, DirectoryPutter, DirectoryService, LeavesToRootValidator}; +use super::{ + DirectoryGraph, DirectoryPutter, DirectoryService, LeavesToRootValidator, RootToLeavesValidator, +}; use crate::{proto, B3Digest, Error}; /// Stores directory closures in an object store. @@ -97,9 +98,10 @@ impl DirectoryService for ObjectStoreDirectoryService { &self, root_directory_digest: &B3Digest, ) -> BoxStream<'static, Result> { - // The Directory digests we're expecting to receive. - let mut expected_directory_digests: HashSet = - HashSet::from([root_directory_digest.clone()]); + // Check that we are not passing on bogus from the object store to the client, and that the + // trust chain from the root digest to the leaves is intact + let mut order_validator = + RootToLeavesValidator::new_with_root_digest(root_directory_digest.clone()); let dir_path = derive_dirs_path(&self.base_path, root_directory_digest); let object_store = self.object_store.clone(); @@ -130,8 +132,7 @@ impl DirectoryService for ObjectStoreDirectoryService { let digest: B3Digest = hasher.update(&buf).finalize().as_bytes().into(); // Ensure to only decode the directory objects whose digests we trust - let was_expected = expected_directory_digests.remove(&digest); - if !was_expected { + if !order_validator.digest_allowed(&digest) { return Err(crate::Error::StorageError(format!( "received unexpected directory {}", digest @@ -143,13 +144,8 @@ impl DirectoryService for ObjectStoreDirectoryService { Error::StorageError(e.to_string()) })?; - for directory in &directory.directories { - // Allow the children to appear next - expected_directory_digests.insert( - B3Digest::try_from(directory.digest.clone()) - .map_err(|e| Error::StorageError(e.to_string()))?, - ); - } + // Allow the children to appear next + order_validator.add_directory_unchecked(&directory); Ok(directory) })()) -- cgit 1.4.1