Skip to content

Commit 536b4d2

Browse files
committed
Fix read for metadata files where the group has only one namespace part
1 parent 8ea59f2 commit 536b4d2

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/authentication/artifact_authenticator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl ArtifactAuthenticator {
2222
let artifact = match self.artifact_repository.get_artifact(pool, artifact).await {
2323
Ok(Some(artifact)) => artifact,
2424
Ok(None) => return Error("The requested artifact does not exist in this registry.".to_owned()),
25-
Err(_) => return Error("".to_owned()),
25+
Err(_) => return Error("Failed to query for artifact.".to_owned()),
2626
};
2727
if artifact.public {
2828
return Granted;

src/route.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use actix_web_httpauth::headers::authorization::{Authorization, Basic};
33

44
use crate::AppState;
55
use crate::authentication::{ArtifactPermission, AuthenticationResult};
6-
use crate::coordinates::Parser;
6+
use crate::coordinates::{Artifact as ArtifactDTO, Parser};
77
use crate::model::maven::File;
88

99
fn is_metadata(uri: &str) -> Result<bool, regex::Error> {
@@ -118,23 +118,43 @@ async fn deploy_metadata(uri: String, app_state: web::Data<AppState>, authentica
118118
#[get("{path:.*}")]
119119
async fn read(path: web::Path<String>, app_state: web::Data<AppState>, authentication: Option<web::Header<Authorization<Basic>>>) -> impl Responder {
120120
let uri = path.into_inner();
121+
match is_metadata(uri.as_ref()) {
122+
Ok(true) => return read_metadata(uri, app_state, authentication).await,
123+
Err(_) => return HttpResponse::InternalServerError().body("Failed to check if the file is metadata."),
124+
_ => {}
125+
};
126+
121127
let coordinates = match Parser::parse_to_file(uri.as_str()) {
122128
Ok(coordinates) => coordinates,
123129
Err(_) => return HttpResponse::BadRequest().body("Coordinates could not be parsed."),
124130
};
125131

132+
return read_file(uri, app_state, authentication, coordinates.to_version().to_artifact()).await;
133+
}
134+
135+
// For now, we assume that we're just serving metadata for artifacts
136+
async fn read_metadata(uri: String, app_state: web::Data<AppState>, authentication: Option<web::Header<Authorization<Basic>>>) -> HttpResponse {
137+
let coordinates = match Parser::parse_to_version(uri.as_str()) {
138+
Ok(coordinates) => coordinates,
139+
Err(_) => return HttpResponse::BadRequest().body("Coordinates could not be parsed."),
140+
};
141+
142+
return read_file(uri, app_state, authentication, coordinates.to_artifact()).await;
143+
}
144+
145+
async fn read_file(uri: String, app_state: web::Data<AppState>, authentication: Option<web::Header<Authorization<Basic>>>, artifact: ArtifactDTO) -> HttpResponse {
146+
match app_state.artifact_authenticator.authenticate_read(artifact, authentication, &app_state.pool).await {
147+
ArtifactPermission::Granted => {},
148+
permission => return permission.as_response(),
149+
};
150+
126151
let file_result = sqlx::query_as!(File, "SELECT file.id, file.version_id, file.name, file.uri, file.path FROM maven_file file WHERE file.uri = ?", uri).fetch_optional(&app_state.pool).await;
127152
let file = match file_result {
128153
Ok(Some(file)) => file,
129154
Ok(None) => return HttpResponse::NotFound().body("The requested file does not exist in this registry."),
130155
Err(_) => return HttpResponse::InternalServerError().body("Querying for the requested file failed."),
131156
};
132157

133-
match app_state.artifact_authenticator.authenticate_read(coordinates.to_version().to_artifact(), authentication, &app_state.pool).await {
134-
ArtifactPermission::Granted => {},
135-
permission => return permission.as_response(),
136-
};
137-
138158
return match app_state.files.read(&file.path) {
139159
Ok(contents) => HttpResponse::Ok().body(contents),
140160
Err(_) => HttpResponse::InternalServerError().body("File contents could not be obtained."),

0 commit comments

Comments
 (0)