Compare commits

...

2 Commits

Author SHA1 Message Date
R Tyler Croy 036ebe5372 Properly proxy the request upstream
This "works" in that it will proxy the request, but no inline modification is
happening yet
2020-09-07 14:59:54 -07:00
R Tyler Croy be715f7a55 Bringing up to the latest dependencies 2020-09-07 14:52:22 -07:00
4 changed files with 1364 additions and 957 deletions

2263
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,8 +10,8 @@ path = "src/test_registry/main.rs"
[dependencies]
# Used for serving the website
tide = "~0.5.1"
async-std = "~1.4.0"
tide = "~0.13.0"
async-std = { version = "~1.6.0", features = ["attributes"]}
# Logging
log = "~0.4.8"
@ -21,7 +21,7 @@ pretty_env_logger = "~0.3.1"
config = { version = "~0.10.1", features = ["yaml"] }
# Used for making http requests to the upstream docker registry
surf = "~1.0.3"
surf = "2.0.0-alpha.4"
serde = { version = "~1.0.104", features = ["derive"] }
serde_json = "~1.0.0"

View File

@ -46,6 +46,7 @@ fn load_settings() -> config::Config {
/**
* AppState is a simple struct to carry information into request handlers
*/
#[derive(Clone, Debug)]
struct AppState {
conf: config::Config,
upstream: String,
@ -75,23 +76,24 @@ impl AppState {
* Proxy the given response to the upstream registry and return the response
* back to the client request it.
*/
async fn proxy_upstream(req: Request<AppState>) -> Response {
let full_url = format!("{}{}", req.state().upstream, req.uri());
async fn proxy_upstream(req: Request<AppState>) -> Result<Response, tide::Error> {
let full_url = format!("{}{}", req.state().upstream, req.url().path());
info!("Proxying request upstream to {}", full_url);
/*
* We need to send the Authorization header along as well, otherwise
* the upstream repository might complain that we're not authorized
*/
let token = req.header("Authorization").unwrap_or("");
let accepts = req.header("Accept").unwrap_or("");
let token = req.header("Authorization").map_or("", |h| h.as_str());
let accepts = req.header("Accept").map_or("", |h| h.as_str());
let outbound = surf::get(full_url)
.set_header("Authorization", token)
.set_header("Accept", accepts);
debug!("Preparing to send request");
if let Ok(mut u_res) = outbound.await {
let status = u_res.status().as_u16();
let body = u_res.body_string().await;
debug!("Body received");
match body {
Ok(body) => {
/*
@ -99,22 +101,23 @@ async fn proxy_upstream(req: Request<AppState>) -> Response {
* that we're sending back a v1 manifest schema and complain about a "missing
* signature key"
*/
debug!("upstream headers: {:?}", u_res.headers());
debug!("upstream response for {}:\n{}", req.uri(), body);
let content_type = u_res.header("Content-Type").unwrap_or("text/plain");
Response::new(status)
.set_header("Content-Type", content_type)
.body_string(body)
//debug!("upstream headers: {:?}", u_res.headers());
debug!("upstream response for {}:\n{}", req.url(), body);
let content_type = u_res.header("Content-Type").map_or("text/plain", |h| h.as_str());
let mut res = Response::new(u_res.status());
res.set_body(body);
res.insert_header("Content-Type", content_type);
Ok(res)
},
Err(err) => {
error!("Failed to make upstream request: {:?}", err);
Response::new(500)
Ok(Response::new(500))
},
}
}
else {
error!("Failed to make request upstream to {}", req.uri());
Response::new(500)
error!("Failed to make request upstream to {}", req.url());
Ok(Response::new(500))
}
}
@ -126,23 +129,23 @@ async fn proxy_upstream(req: Request<AppState>) -> Response {
* This will return a Response to the client which conforms to the manifest
* specification.
*/
async fn fetch_digest(req: Request<AppState>) -> Response {
async fn fetch_digest(req: Request<AppState>) -> Result<Response, tide::Error> {
let org: String = req.param("org").unwrap_or("".to_string());
let image: String = req.param("image").unwrap_or("".to_string());
let digest: String = req.param("digest").unwrap_or("".to_string());
if req.state().override_exists(org, image, digest) {
error!("We should not proxy");
Response::new(200)
Ok(Response::new(200))
}
else {
error!("We SHOULD proxy");
Response::new(200)
Ok(Response::new(200))
}
}
async fn fetch_blob(req: Request<AppState>) -> Response {
info!("fetch_blob: {}", req.uri());
Response::new(200)
async fn fetch_blob(req: Request<AppState>) -> Result<Response, tide::Error> {
info!("fetch_blob: {}", req.url());
Ok(Response::new(200))
}
fn main() -> Result<(), std::io::Error> {
@ -169,7 +172,7 @@ fn main() -> Result<(), std::io::Error> {
task::block_on(async {
let mut app = tide::with_state(state);
app.at("/").get(|_| async move { "Hello, world!" });
app.at("/").get(|_| async move { Ok("Hello, world!") });
/*
* This route works for "normal" images, which have name of org/image
*/

View File

@ -11,9 +11,10 @@ use log::*;
use tide::Request;
async fn generic_json(req: Request<()>) -> String {
info!("Received request to: {}", req.uri());
format!(r#"{{"url" : "{}"}}"#, req.uri())
async fn generic_json(req: Request<()>) -> Result<String, tide::Error> {
info!("Received request to: {}", req.url());
Ok(format!(r#"{{"url" : "{}"}}"#, req.url()))
}
fn main() -> Result<(), std::io::Error> {