8000 Add support for maven requests by jperedadnr · Pull Request #791 · pyrsia/pyrsia · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for maven requests #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pyrsia_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use network::handlers;
use pyrsia::artifact_service::storage::{ArtifactStorage, ARTIFACTS_DIR};
use pyrsia::docker::error_util::*;
use pyrsia::docker::v2::routes::make_docker_routes;
use pyrsia::java::maven2::routes::make_maven_routes;
use pyrsia::logging::*;
use pyrsia::network::client::Client;
use pyrsia::network::p2p;
Expand Down Expand Up @@ -133,12 +134,17 @@ fn setup_http(

debug!("Setup HTTP routing");
let docker_routes = make_docker_routes(
transparency_log.clone(),
p2p_client.clone(),
artifact_storage.clone(),
);
let maven_routes = make_maven_routes(
transparency_log,
p2p_client.clone(),
artifact_storage.clone(),
);
let node_api_routes = make_node_routes(p2p_client, artifact_storage);
let all_routes = docker_routes.or(node_api_routes);
let all_routes = docker_routes.or(maven_routes).or(node_api_routes);

debug!("Setup HTTP server");
let (addr, server) = warp::serve(
Expand Down
17 changes: 17 additions & 0 deletions src/java.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2021 JFrog Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pub mod maven2;
18 changes: 18 additions & 0 deletions src/java/maven2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2021 JFrog Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pub mod handlers;
pub mod routes;
17 changes: 17 additions & 0 deletions src/java/maven2/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2021 JFrog Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pub mod maven_artifacts;
173 changes: 173 additions & 0 deletions src/java/maven2/handlers/maven_artifacts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
Copyright 2021 JFrog Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use crate::artifact_service::handlers::get_artifact;
use crate::artifact_service::storage::ArtifactStorage;
use crate::docker::error_util::{RegistryError, RegistryErrorCode};
use crate::network::client::Client;
use crate::transparency_log::log::TransparencyLog;
use anyhow::bail;
use futures::lock::Mutex;
use log::debug;
use std::sync::Arc;
use warp::{http::StatusCode, Rejection, Reply};

pub async fn handle_get_maven_artifact(
transparency_log: Arc<Mutex<TransparencyLog>>,
p2p_client: Client,
artifact_storage: ArtifactStorage,
full_path: String,
) -> Result<impl Reply, Rejection> {
debug!("Requesting maven artifact: {}", full_path);
let namespace_specific_id = get_namespace_specific_id(&full_path).map_err(|err| {
debug!("Error getting namespace id for artifact: {:?}", err);
warp::reject::custom(RegistryError {
code: RegistryErrorCode::Unknown(err.to_string()),
})
})?;

// request artifact
debug!("Requesting artifact for id {}", &namespace_specific_id);
let artifact_content = get_artifact(
transparency_log,
p2p_client,
&artifact_storage,
&namespace_specific_id,
)
.await
.map_err(|err| {
debug!("Error retrieving artifact: {:?}", err);
warp::reject::custom(RegistryError {
code: RegistryErrorCode::Unknown(err.to_string()),
})
})?;

Ok(warp::http::response::Builder::new()
.header("Content-Type", "application/octet-stream")
.status(StatusCode::OK)
.body(artifact_content)
.unwrap())
}

fn get_namespace_specific_id(full_path: &str) -> Result<String, anyhow::Error> {
// maven coordinates like "com.company:test:1.0" will produce a request
// like: "GET /maven2/com/company/test/1.0/test-1.0.jar"

// split, and remove first two strings: "" and "maven2":
let mut pieces: Vec<&str> = full_path.split('/').skip(2).collect();
if pieces.len() < 4 {
bail!(format!("Error, invalid full path: {}", full_path));
}
let file_name = pieces.pop().unwrap();
let version = pieces.pop().unwrap();
let artifact_id = pieces.pop().unwrap();
let group_id = pieces.join(".");

Ok(format!(
"MAVEN2/FILE/{}/{}/{}/{}",
group_id, artifact_id, version, file_name
))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::artifact_service::service::{Hash, HashAlgorithm};
use crate::util::test_util;
use anyhow::Context;
use futures::channel::mpsc;
use hyper::header::HeaderValue;
use libp2p::identity::Keypair;
use std::fs::File;
use std::path::PathBuf;

const VALID_ARTIFACT_HASH: &str =
"e11c16ff163ccc1efe01d2696c626891560fa82123601a5ff196d97b6ab156da";
const VALID_FULL_PATH: &str = "/maven2/test/test/1.0/test-1.0.jar";
const INVALID_FULL_PATH: &str = "/maven2/test/1.0/test-1.0.jar";
const VALID_MAVEN_ID: &str = "MAVEN2/FILE/test/test/1.0/test-1.0.jar";

#[test]
fn get_namespace_specific_id_test() {
assert_eq!(
get_namespace_specific_id(VALID_FULL_PATH).unwrap(),
VALID_MAVEN_ID
);
}

#[test]
fn get_namespace_specific_id_with_invalid_path_test() {
assert!(get_namespace_specific_id(INVALID_FULL_PATH).is_err());
}

#[tokio::test]
async fn handle_get_maven_artifact_test() {
let tmp_dir = test_util::tests::setup();

let transparency_log = Arc::new(Mutex::new(TransparencyLog::new(&tmp_dir).unwrap()));
transparency_log
.lock()
.await
.add_artifact(VALID_MAVEN_ID, VALID_ARTIFACT_HASH)
.unwrap();

let (sender, _) = mpsc::channel(1);
let p2p_client = Client {
sender,
local_peer_id: Keypair::generate_ed25519().public().to_peer_id(),
};

let artifact_storage = ArtifactStorage::new(&tmp_dir).unwrap();
create_artifact(&artifact_storage).unwrap();

let result = handle_get_maven_artifact(
transparency_log,
p2p_client,
artifact_storage,
VALID_FULL_PATH.to_string(),
)
.await;

assert!(result.is_ok());

let response = result.unwrap().into_response();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("Content-Type"),
Some(&HeaderValue::from_static("application/octet-stream"))
);

test_util::tests::teardown(tmp_dir);
}

fn get_file_reader() -> Result<File, anyhow::Error> {
// test artifact file in resources/test dir
let mut curr_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
curr_dir.push("tests/resources/test-1.0.jar");

let path = String::from(curr_dir.to_string_lossy());
let reader = File::open(path.as_str()).unwrap();
Ok(reader)
}

fn create_artifact(artifact_storage: &ArtifactStorage) -> Result<(), anyhow::Error> {
let artifact_hash = hex::decode(VALID_ARTIFACT_HASH)?;
let hash = Hash::new(HashAlgorithm::SHA256, &artifact_hash)?;
artifact_storage
.push_artifact(&mut get_file_reader()?, &hash)
.context("Error while pushing artifact")
}
}
49 changes: 49 additions & 0 deletions src/java/maven2/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 JFrog Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use super::handlers::maven_artifacts::*;
use crate::artifact_service::storage::ArtifactStorage;
use crate::network::client::Client;
use crate::transparency_log::log::TransparencyLog;
use futures::lock::Mutex;
use log::debug;
use std::sync::Arc;
use warp::Filter;

pub fn make_maven_routes(
transparency_log: TransparencyLog,
p2p_client: Client,
artifact_storage: ArtifactStorage,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let transparency_log_maven_artifact = Arc::new(Mutex::new(transparency_log));
let maven2_root = warp::path("maven2")
.and(warp::path::full())
.map(|path: warp::path::FullPath| {
let full_path: String = path.as_str().to_string();
debug!("route full path: {}", full_path);
full_path
})
.and_then(move |full_path| {
handle_get_maven_artifact(
transparency_log_maven_artifact.clone(),
p2p_client.clone(),
artifact_storage.clone(),
full_path,
)
});

warp::any().and(maven2_root)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
pub mod artifact_service;
pub mod cli_commands;
pub mod docker;
pub mod java;
pub mod logging;
pub mod network;
pub mod node_api;
Expand Down
Binary file added tests/resources/test-1.0.jar
Binary file not shown.
0