| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use axum::{Json, extract::State, response::IntoResponse}; |
| 24 |
use serde::{Deserialize, Serialize}; |
| 25 |
use tower_governor::GovernorLayer; |
| 26 |
|
| 27 |
use crate::{ |
| 28 |
AppState, AppStorage, |
| 29 |
config::Config, |
| 30 |
constants, |
| 31 |
csrf::{CsrfRouter, post_csrf_skip}, |
| 32 |
error::{AppError, Result}, |
| 33 |
storage::S3Client, |
| 34 |
synckit_auth::SyncUser, |
| 35 |
}; |
| 36 |
|
| 37 |
#[derive(Deserialize)] |
| 38 |
pub(crate) struct PresignRequest { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
path: String, |
| 43 |
|
| 44 |
|
| 45 |
size: i64, |
| 46 |
|
| 47 |
|
| 48 |
#[serde(default)] |
| 49 |
content_type: Option<String>, |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Serialize)] |
| 53 |
pub(crate) struct PresignResponse { |
| 54 |
|
| 55 |
|
| 56 |
upload_url: String, |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
object_key: String, |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public_url: Option<String>, |
| 65 |
|
| 66 |
|
| 67 |
content_type: String, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
fn content_type_for(key: &str) -> &'static str { |
| 76 |
match key |
| 77 |
.rsplit_once('.') |
| 78 |
.map(|(_, ext)| ext.to_ascii_lowercase()) |
| 79 |
{ |
| 80 |
Some(ext) => match ext.as_str() { |
| 81 |
"rpm" => "application/x-rpm", |
| 82 |
"xml" => "application/xml", |
| 83 |
"yaml" => "application/yaml", |
| 84 |
"gz" => "application/gzip", |
| 85 |
"xz" => "application/x-xz", |
| 86 |
"zst" => "application/zstd", |
| 87 |
"bz2" => "application/x-bzip2", |
| 88 |
"asc" | "key" | "sig" => "text/plain", |
| 89 |
_ => "application/octet-stream", |
| 90 |
}, |
| 91 |
None => "application/octet-stream", |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
fn cache_control_for(key: &str) -> &'static str { |
| 105 |
let filename = key.rsplit('/').next().unwrap_or(key); |
| 106 |
if filename.starts_with("repomd.xml") { |
| 107 |
|
| 108 |
|
| 109 |
"no-cache, must-revalidate" |
| 110 |
} else { |
| 111 |
"public, max-age=31536000, immutable" |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#[tracing::instrument(skip_all, name = "rpm::presign_upload")] |
| 125 |
async fn presign_upload( |
| 126 |
State(config): State<Config>, |
| 127 |
State(storage): State<AppStorage>, |
| 128 |
sync_user: SyncUser, |
| 129 |
Json(req): Json<PresignRequest>, |
| 130 |
) -> Result<impl IntoResponse> { |
| 131 |
require_rpm_admin(&config, &sync_user)?; |
| 132 |
|
| 133 |
if req.size <= 0 { |
| 134 |
return Err(AppError::BadRequest("size must be positive".to_string())); |
| 135 |
} |
| 136 |
if req.size > constants::RPM_MAX_OBJECT_BYTES { |
| 137 |
return Err(AppError::BadRequest(format!( |
| 138 |
"object is {} bytes, over the {} byte ceiling", |
| 139 |
req.size, |
| 140 |
constants::RPM_MAX_OBJECT_BYTES |
| 141 |
))); |
| 142 |
} |
| 143 |
|
| 144 |
let key = S3Client::generate_rpm_key(&req.path)?; |
| 145 |
let rpm_s3 = storage.require_rpm_s3()?; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
let content_type = req |
| 150 |
.content_type |
| 151 |
.clone() |
| 152 |
.unwrap_or_else(|| content_type_for(key.as_str()).to_string()); |
| 153 |
|
| 154 |
let upload_url = rpm_s3 |
| 155 |
.presign_upload( |
| 156 |
&key, |
| 157 |
&content_type, |
| 158 |
Some(constants::RPM_PRESIGN_EXPIRY_SECS), |
| 159 |
Some(cache_control_for(key.as_str())), |
| 160 |
Some(req.size), |
| 161 |
) |
| 162 |
.await?; |
| 163 |
|
| 164 |
tracing::info!( |
| 165 |
admin = %sync_user.user_id, |
| 166 |
key = %key, |
| 167 |
size = req.size, |
| 168 |
"minted RPM repo presign" |
| 169 |
); |
| 170 |
|
| 171 |
let public_url = config |
| 172 |
.rpm_base_url |
| 173 |
.as_ref() |
| 174 |
.map(|base| format!("{base}/{key}")); |
| 175 |
|
| 176 |
Ok(( |
| 177 |
axum::http::StatusCode::CREATED, |
| 178 |
Json(PresignResponse { |
| 179 |
upload_url, |
| 180 |
object_key: key.into_string(), |
| 181 |
public_url, |
| 182 |
content_type, |
| 183 |
}), |
| 184 |
)) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
fn require_rpm_admin(config: &Config, sync_user: &SyncUser) -> Result<()> { |
| 194 |
match config.admin_user_id { |
| 195 |
Some(admin_id) if admin_id == sync_user.user_id => Ok(()), |
| 196 |
_ => Err(AppError::NotFound), |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
pub fn rpm_routes() -> CsrfRouter<AppState> { |
| 202 |
const RPM_SKIP: &str = "rpm publish: bearer auth, no session"; |
| 203 |
|
| 204 |
let write_rate_limit = crate::helpers::rate_limiter_ms( |
| 205 |
constants::RPM_WRITE_RATE_LIMIT_MS, |
| 206 |
constants::RPM_WRITE_RATE_LIMIT_BURST, |
| 207 |
); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
CsrfRouter::new() |
| 213 |
.route( |
| 214 |
"/api/v1/admin/rpm/uploads", |
| 215 |
post_csrf_skip(RPM_SKIP, presign_upload), |
| 216 |
) |
| 217 |
.route_layer(GovernorLayer::new(write_rate_limit)) |
| 218 |
} |
| 219 |
|