Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit f35394b

Browse files
committed
fix fmt and test fails
Signed-off-by: David Justice <david@devigned.com>
1 parent af03830 commit f35394b

15 files changed

Lines changed: 140 additions & 96 deletions

File tree

crates/protocol/src/registry.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ impl From<AnyHash> for RecordId {
246246
}
247247
}
248248

249+
impl From<RecordId> for AnyHash {
250+
fn from(id: RecordId) -> AnyHash {
251+
id.0
252+
}
253+
}
254+
249255
#[cfg(test)]
250256
mod tests {
251257
use super::*;

crates/server/src/api/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::contentstore::ContentStore;
12
use crate::{
23
policy::{content::ContentPolicy, record::RecordPolicy},
34
services::CoreService,
@@ -12,7 +13,6 @@ use tower_http::{
1213
};
1314
use tracing::{Level, Span};
1415
use url::Url;
15-
use crate::contentstore::ContentStore;
1616

1717
pub mod v1;
1818

crates/server/src/api/v1/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::contentstore::ContentStore;
12
use crate::{
23
policy::{content::ContentPolicy, record::RecordPolicy},
34
services::CoreService,
@@ -15,7 +16,6 @@ use axum::{
1516
use serde::{Serialize, Serializer};
1617
use std::{path::PathBuf, sync::Arc};
1718
use url::Url;
18-
use crate::contentstore::ContentStore;
1919

2020
pub mod fetch;
2121
pub mod package;

crates/server/src/api/v1/package.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use std::collections::HashSet;
21
use super::{Json, Path};
32
use crate::{
3+
contentstore::ContentStore,
44
datastore::{DataStoreError, RecordStatus},
55
policy::{
66
content::{ContentPolicy, ContentPolicyError},
77
record::{RecordPolicy, RecordPolicyError},
88
},
99
services::CoreService,
10-
contentstore::ContentStore,
1110
};
11+
use axum::body::StreamBody;
12+
use axum::http::header;
1213
use axum::{
1314
debug_handler,
1415
extract::{BodyStream, State},
@@ -18,14 +19,14 @@ use axum::{
1819
Router,
1920
};
2021
use futures::StreamExt;
22+
use std::collections::HashSet;
2123
use std::sync::Arc;
2224
use std::{collections::HashMap, path::PathBuf};
23-
use axum::body::StreamBody;
24-
use axum::http::header;
25+
use std::hash::Hash;
2526
use tempfile::NamedTempFile;
2627
use tokio::io::AsyncWriteExt;
27-
use url::Url;
2828
use tokio_util::io::ReaderStream;
29+
use url::Url;
2930
use warg_api::v1::package::{
3031
ContentSource, MissingContent, PackageError, PackageRecord, PackageRecordState,
3132
PublishRecordRequest, UploadEndpoint,
@@ -70,21 +71,33 @@ impl Config {
7071
Router::new()
7172
.route("/:log_id/record", post(publish_record))
7273
.route("/:log_id/record/:record_id", get(get_record))
73-
.route("/:log_id/record/:record_id/content/:digest", post(upload_content))
74-
.route("/:log_id/record/:record_id/content/:digest", get(fetch_content))
74+
.route(
75+
"/:log_id/record/:record_id/content/:digest",
76+
post(upload_content),
77+
)
78+
.route(
79+
"/:log_id/record/:record_id/content/:digest",
80+
get(fetch_content),
81+
)
7582
.with_state(self)
7683
}
7784

78-
fn content_url(&self,
79-
log_id: &LogId,
80-
record_id: &RecordId,
81-
digest: &AnyHash) -> String {
85+
fn content_url(&self, log_id: &LogId, record_id: &RecordId, digest: &AnyHash) -> String {
86+
let log_hash: AnyHash = log_id.clone().into();
87+
let record_hash: AnyHash = record_id.clone().into();
8288
format!(
83-
"{url}/{log_id}/record/{record_id}/content/{digest}",
89+
"{url}{log_id}/record/{record_id}/content/{digest}",
90+
log_id = Self::hash_fmt(&log_hash),
91+
record_id = Self::hash_fmt(&record_hash),
92+
digest = Self::hash_fmt(digest),
8493
url = self.content_base_url,
8594
)
8695
}
8796

97+
fn hash_fmt(digest: &AnyHash) -> String {
98+
digest.to_string().replace(':', "-")
99+
}
100+
88101
fn build_missing_content<'a>(
89102
&self,
90103
log_id: &LogId,
@@ -387,13 +400,17 @@ async fn upload_content(
387400
// Only persist the file if the content was successfully processed
388401
res?;
389402

390-
let version = crate::datastore::get_release_version(config.core_service.store(), &log_id, &record_id).await?;
403+
let version =
404+
crate::datastore::get_release_version(config.core_service.store(), &log_id, &record_id)
405+
.await?;
391406
let package_id = config.core_service.store().get_package_id(&log_id).await?;
392407
let mut tmp_file = tokio::fs::File::open(&tmp_path)
393408
.await
394409
.map_err(PackageApiError::internal_error)?;
395410

396-
config.content_store.store_content(&package_id, &digest, version.to_string(), &mut tmp_file)
411+
config
412+
.content_store
413+
.store_content(&package_id, &digest, version.to_string(), &mut tmp_file)
397414
.await
398415
.map_err(PackageApiError::internal_error)?;
399416

@@ -412,7 +429,10 @@ async fn upload_content(
412429

413430
Ok((
414431
StatusCode::CREATED,
415-
[(header::LOCATION, config.content_url(&log_id, &record_id, &digest))],
432+
[(
433+
header::LOCATION,
434+
config.content_url(&log_id, &record_id, &digest),
435+
)],
416436
))
417437
}
418438

@@ -468,12 +488,12 @@ async fn fetch_content(
468488
tracing::info!("fetching content for record `{record_id}` from `{log_id}`");
469489

470490
let package_id = config.core_service.store().get_package_id(&log_id).await?;
471-
let version = crate::datastore::get_release_version(
472-
config.core_service.store(),
473-
&log_id,
474-
&record_id,
475-
).await?;
476-
let file = config.content_store.fetch_content(&package_id, &digest, version.to_string())
491+
let version =
492+
crate::datastore::get_release_version(config.core_service.store(), &log_id, &record_id)
493+
.await?;
494+
let file = config
495+
.content_store
496+
.fetch_content(&package_id, &digest, version.to_string())
477497
.await
478498
.map_err(PackageApiError::not_found)?;
479499

crates/server/src/bin/warg-server.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use anyhow::{Context, Result};
22
use clap::{Parser, ValueEnum};
3+
use oci_distribution::secrets::RegistryAuth::Anonymous;
34
use secrecy::SecretString;
45
use std::{net::SocketAddr, path::PathBuf};
5-
use oci_distribution::secrets::RegistryAuth::Anonymous;
66
use tokio::signal;
77
use tracing_subscriber::filter::LevelFilter;
88
use url::Url;
@@ -127,7 +127,7 @@ async fn main() -> Result<()> {
127127
.with_context(|| format!("failed to decode authorized keys from {path:?}"))?;
128128
config = config.with_record_policy(authorized_key_policy);
129129
}
130-
130+
131131
let config = match args.content_store {
132132
ContentStoreKind::Local => {
133133
tracing::info!("using local content store");
@@ -136,7 +136,14 @@ async fn main() -> Result<()> {
136136
ContentStoreKind::OCIv1_1 => {
137137
use warg_server::contentstore::oci::ociv1_1::OCIv1_1ContentStore;
138138
tracing::info!("using OCIv1.1 content store");
139-
config.with_content_store(OCIv1_1ContentStore::new(args.oci_registry_url.unwrap(), Anonymous, &args.content_dir).await)
139+
config.with_content_store(
140+
OCIv1_1ContentStore::new(
141+
args.oci_registry_url.unwrap(),
142+
Anonymous,
143+
&args.content_dir,
144+
)
145+
.await,
146+
)
140147
}
141148
};
142149

crates/server/src/contentstore/local.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::contentstore::{ContentStore, ContentStoreError};
12
use std::path::{Path, PathBuf};
23
use tokio::fs::File;
34
use tokio::io::copy;
45
use warg_crypto::hash::AnyHash;
56
use warg_protocol::registry::PackageId;
6-
use crate::contentstore::{ContentStore, ContentStoreError};
77

88
#[derive(Clone)]
99
pub struct LocalContentStore {
@@ -46,7 +46,7 @@ impl ContentStore for LocalContentStore {
4646
_package_id: &PackageId,
4747
digest: &AnyHash,
4848
_version: String,
49-
content: &mut File
49+
content: &mut File,
5050
) -> Result<String, ContentStoreError> {
5151
let file_path = self.content_path(digest);
5252
let mut stored_file = File::create(file_path.clone())
@@ -67,6 +67,8 @@ impl ContentStore for LocalContentStore {
6767
_version: String,
6868
) -> Result<bool, ContentStoreError> {
6969
let path = self.content_path(digest);
70-
Path::new(&path).try_exists().map_err(|e| ContentStoreError::ContentStoreInternalError(e.to_string()))
70+
Path::new(&path)
71+
.try_exists()
72+
.map_err(|e| ContentStoreError::ContentStoreInternalError(e.to_string()))
7173
}
7274
}

crates/server/src/contentstore/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use thiserror::Error;
12
use tokio::fs::File;
23
use warg_crypto::hash::AnyHash;
3-
use thiserror::Error;
44
use warg_protocol::registry::PackageId;
55

66
pub mod local;
@@ -32,7 +32,7 @@ pub trait ContentStore: Send + Sync {
3232
package_id: &PackageId,
3333
digest: &AnyHash,
3434
version: String,
35-
content: &mut File
35+
content: &mut File,
3636
) -> Result<String, ContentStoreError>;
3737

3838
async fn content_present(

crates/server/src/contentstore/oci/client.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::path::{Path, PathBuf};
22
use std::sync::Arc;
33

44
use anyhow::{Context, Result};
5+
use oci_distribution::config::{Architecture, Config as DistConfig, ConfigFile, Os};
56
use oci_distribution::{
67
client,
78
client::{ClientProtocol, Config, ImageLayer},
89
manifest::OciImageManifest,
9-
Reference,
1010
secrets::RegistryAuth,
11+
Reference,
1112
};
12-
use oci_distribution::config::{Architecture, ConfigFile, Config as DistConfig, Os};
1313
use serde_json;
1414
use tokio::fs::File;
1515
use tokio::io::{AsyncReadExt, AsyncWriteExt};
@@ -20,8 +20,7 @@ use tokio::task::block_in_place;
2020
use warg_crypto::hash::AnyHash;
2121

2222
use crate::{
23-
contentstore::ContentStoreError,
24-
contentstore::ContentStoreError::ContentStoreInternalError,
23+
contentstore::ContentStoreError, contentstore::ContentStoreError::ContentStoreInternalError,
2524
};
2625

2726
const COMPONENT_ARTIFACT_TYPE: &str = "application/vnd.bytecodealliance.component.v1+wasm";
@@ -52,7 +51,10 @@ impl Client {
5251
digest: &AnyHash,
5352
) -> Result<File, ContentStoreError> {
5453
let path = self.cached_content_path(digest);
55-
if Path::new(&path).try_exists().map_err(|e| ContentStoreError::ContentStoreInternalError(e.to_string()))? {
54+
if Path::new(&path)
55+
.try_exists()
56+
.map_err(|e| ContentStoreError::ContentStoreInternalError(e.to_string()))?
57+
{
5658
let file = File::open(path)
5759
.await
5860
.map_err(|e| ContentStoreError::ContentStoreInternalError(e.to_string()))?;
@@ -69,18 +71,13 @@ impl Client {
6971
// block_on.
7072
let result = block_in_place(|| {
7173
Handle::current().block_on(async move {
72-
let mut oci = self
73-
.oci_client
74-
.write()
75-
.await;
76-
oci
77-
.pull(&reference, &self.auth, vec![WASM_LAYER_MEDIA_TYPE])
74+
let mut oci = self.oci_client.write().await;
75+
oci.pull(&reference, &self.auth, vec![WASM_LAYER_MEDIA_TYPE])
7876
.await
7977
})
8078
});
8179

82-
let image = result
83-
.map_err(|e| ContentStoreInternalError(e.to_string()))?;
80+
let image = result.map_err(|e| ContentStoreInternalError(e.to_string()))?;
8481

8582
let layer = image
8683
.layers
@@ -90,8 +87,7 @@ impl Client {
9087
let mut file = File::create(self.cached_content_path(digest))
9188
.await
9289
.map_err(|e| ContentStoreInternalError(e.to_string()))?;
93-
file
94-
.write_all(&layer.data)
90+
file.write_all(&layer.data)
9591
.await
9692
.map_err(|e| ContentStoreInternalError(e.to_string()))?;
9793
Ok(file)
@@ -121,8 +117,8 @@ impl Client {
121117
}),
122118
..Default::default()
123119
};
124-
let config_data = serde_json::to_vec(&config)
125-
.map_err(|e| ContentStoreInternalError(e.to_string()))?;
120+
let config_data =
121+
serde_json::to_vec(&config).map_err(|e| ContentStoreInternalError(e.to_string()))?;
126122
let oci_config = Config::oci_v1(config_data, None);
127123
let mut layers = Vec::new();
128124
let wasm_layer = Self::wasm_layer(file)
@@ -135,15 +131,11 @@ impl Client {
135131

136132
// TODO: fix the higher-level lifetime error that occurs when not using block_in_place and
137133
// block_on.
138-
let result= block_in_place(|| {
134+
let result = block_in_place(|| {
139135
Handle::current().block_on(async move {
140136
tracing::log::trace!("Pushing component to {:?}", reference);
141-
let mut oci = self
142-
.oci_client
143-
.write()
144-
.await;
145-
oci
146-
.push(&reference, &layers, oci_config, &self.auth, Some(manifest))
137+
let mut oci = self.oci_client.write().await;
138+
oci.push(&reference, &layers, oci_config, &self.auth, Some(manifest))
147139
.await
148140
})
149141
});
@@ -154,7 +146,10 @@ impl Client {
154146
.map_err(|e| ContentStoreInternalError(e.to_string()))
155147
}
156148

157-
pub async fn content_exists(&self, reference: impl AsRef<str>) -> Result<bool, ContentStoreError> {
149+
pub async fn content_exists(
150+
&self,
151+
reference: impl AsRef<str>,
152+
) -> Result<bool, ContentStoreError> {
158153
let reference: Reference = reference
159154
.as_ref()
160155
.parse()
@@ -163,10 +158,7 @@ impl Client {
163158
.unwrap();
164159

165160
let mut oci = self.oci_client.write().await;
166-
match oci
167-
.fetch_manifest_digest(&reference, &self.auth)
168-
.await
169-
{
161+
match oci.fetch_manifest_digest(&reference, &self.auth).await {
170162
Ok(_) => Ok(true),
171163
Err(_) => Ok(false),
172164
}
@@ -177,7 +169,9 @@ impl Client {
177169
tracing::log::trace!("Reading wasm component from {:?}", file);
178170

179171
let mut contents = vec![];
180-
file.read_to_end(&mut contents).await.context("cannot read wasm component")?;
172+
file.read_to_end(&mut contents)
173+
.await
174+
.context("cannot read wasm component")?;
181175

182176
Ok(ImageLayer::new(
183177
contents,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod ociv1_1;
21
mod client;
2+
pub mod ociv1_1;

0 commit comments

Comments
 (0)