forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | //! Black-box tests for the container registry. They start the real `forge` binary and drive the |
| 2 | //! OCI Distribution API over HTTP the way a container client does: the auth probe, the token |
| 3 | //! exchange, monolithic and chunked blob uploads, manifests by tag and digest, the tag list, mounts, |
| 4 | //! deletes and the garbage collection they trigger. Private images and the packages scope are set |
| 5 | //! up through the store before the server starts, since the web has no way to make one private. |
| 6 | //! The last test drives podman when it is installed; it is ignored by default. |
| 7 | |
| 8 | use crate::common::{Server, bin, describe, launch, pick_ports, tool_on_path, write_config}; |
| 9 | use std::path::Path; |
| 10 | use std::process::Command; |
| 11 | use std::time::Duration; |
| 12 | |
| 13 | use forge_core::packages::KIND_OCI; |
| 14 | use forge_core::token::{generate_token, hash_token}; |
| 15 | use forge_core::{Store, User}; |
| 16 | use sha2::{Digest, Sha256}; |
| 17 | |
| 18 | const PW: &str = "tahinli-password-123"; |
| 19 | const MANIFEST_TYPE: &str = "application/vnd.oci.image.manifest.v1+json"; |
| 20 | |
| 21 | struct Resp { |
| 22 | status: u16, |
| 23 | headers: Vec<(String, String)>, |
| 24 | body: Vec<u8>, |
| 25 | } |
| 26 | |
| 27 | impl Resp { |
| 28 | fn header(&self, name: &str) -> Option<&str> { |
| 29 | self.headers |
| 30 | .iter() |
| 31 | .find(|(k, _)| k.eq_ignore_ascii_case(name)) |
| 32 | .map(|(_, v)| v.as_str()) |
| 33 | } |
| 34 | |
| 35 | fn text(&self) -> String { |
| 36 | String::from_utf8_lossy(&self.body).into_owned() |
| 37 | } |
| 38 | |
| 39 | fn json(&self) -> serde_json::Value { |
| 40 | serde_json::from_slice(&self.body).expect("json body") |
| 41 | } |
| 42 | |
| 43 | /// The `code` of the first OCI error, for asserting error codes. |
| 44 | fn error_code(&self) -> String { |
| 45 | self.json()["errors"][0]["code"] |
| 46 | .as_str() |
| 47 | .unwrap_or_default() |
| 48 | .to_owned() |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | struct Http { |
| 53 | client: reqwest::Client, |
| 54 | base: String, |
| 55 | } |
| 56 | |
| 57 | impl Http { |
| 58 | fn new(port: u16) -> Self { |
| 59 | let client = reqwest::Client::builder() |
| 60 | .redirect(reqwest::redirect::Policy::none()) |
| 61 | .timeout(Duration::from_secs(60)) |
| 62 | .build() |
| 63 | .expect("http client"); |
| 64 | Self { |
| 65 | client, |
| 66 | base: format!("http://127.0.0.1:{port}"), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Sends one request. `auth` is the whole Authorization value, when there is one. |
| 71 | async fn send( |
| 72 | &self, |
| 73 | method: reqwest::Method, |
| 74 | path: &str, |
| 75 | auth: Option<&str>, |
| 76 | headers: &[(&str, &str)], |
| 77 | body: Option<Vec<u8>>, |
| 78 | ) -> Resp { |
| 79 | let url = if path.starts_with("http") { |
| 80 | path.to_owned() |
| 81 | } else { |
| 82 | format!("{}{path}", self.base) |
| 83 | }; |
| 84 | let mut req = self.client.request(method, url); |
| 85 | if let Some(value) = auth { |
| 86 | req = req.header("Authorization", value); |
| 87 | } |
| 88 | for (name, value) in headers { |
| 89 | req = req.header(*name, *value); |
| 90 | } |
| 91 | if let Some(bytes) = body { |
| 92 | req = req.body(bytes); |
| 93 | } |
| 94 | let res = req.send().await.expect("http request"); |
| 95 | let status = res.status().as_u16(); |
| 96 | let headers = res |
| 97 | .headers() |
| 98 | .iter() |
| 99 | .map(|(k, v)| (k.as_str().to_owned(), v.to_str().unwrap_or("").to_owned())) |
| 100 | .collect(); |
| 101 | let body = res.bytes().await.expect("read body").to_vec(); |
| 102 | Resp { |
| 103 | status, |
| 104 | headers, |
| 105 | body, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | async fn get(&self, path: &str, auth: Option<&str>) -> Resp { |
| 110 | self.send(reqwest::Method::GET, path, auth, &[], None).await |
| 111 | } |
| 112 | |
| 113 | async fn post(&self, path: &str, auth: Option<&str>, body: Option<Vec<u8>>) -> Resp { |
| 114 | self.send(reqwest::Method::POST, path, auth, &[], body) |
| 115 | .await |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Runs an admin subcommand with the password in FORGE_PASSWORD and asserts it succeeded. |
| 120 | fn admin(config: &Path, args: &[&str]) { |
| 121 | let out = Command::new(bin()) |
| 122 | .arg("--config") |
| 123 | .arg(config) |
| 124 | .arg("admin") |
| 125 | .args(args) |
| 126 | .env("FORGE_PASSWORD", PW) |
| 127 | .output() |
| 128 | .expect("run admin command"); |
| 129 | assert!( |
| 130 | out.status.success(), |
| 131 | "admin {args:?} failed:\n{}", |
| 132 | describe(&out) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /// A running server. The temp dir lives as long as the test. |
| 137 | struct Env { |
| 138 | _root: tempfile::TempDir, |
| 139 | _server: Server, |
| 140 | http: Http, |
| 141 | port: u16, |
| 142 | } |
| 143 | |
| 144 | /// Creates the users `tahinli` and `bob`, runs `seed` on the store, then starts the server. |
| 145 | async fn start<F, Fut>(seed: F) -> Env |
| 146 | where |
| 147 | F: FnOnce(Store) -> Fut, |
| 148 | Fut: std::future::Future<Output = ()>, |
| 149 | { |
| 150 | let root = tempfile::Builder::new() |
| 151 | .prefix("foci") |
| 152 | .tempdir_in("/tmp") |
| 153 | .expect("temp dir"); |
| 154 | let dir = root.path().to_path_buf(); |
| 155 | let ports = pick_ports(); |
| 156 | let config = write_config(&dir, ports); |
| 157 | admin(&config, &["create-user", "tahinli"]); |
| 158 | admin(&config, &["create-user", "bob"]); |
| 159 | let store = Store::open(&dir.join("forge.db")) |
| 160 | .await |
| 161 | .expect("open store"); |
| 162 | seed(store).await; |
| 163 | let (server, ports) = launch(&dir, ports).await; |
| 164 | Env { |
| 165 | _root: root, |
| 166 | _server: server, |
| 167 | http: Http::new(ports.http), |
| 168 | port: ports.http, |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Stores a token for `user` with the given scopes. `token` is the plaintext a client will send. |
| 173 | async fn grant(store: &Store, user: &User, scopes: &[&str], token: &str) { |
| 174 | let scopes: Vec<String> = scopes.iter().map(|s| (*s).to_owned()).collect(); |
| 175 | store |
| 176 | .create_token(&user.id, "ci", &hash_token(token), &scopes, None) |
| 177 | .await |
| 178 | .expect("create token"); |
| 179 | } |
| 180 | |
| 181 | async fn user(store: &Store, name: &str) -> User { |
| 182 | store |
| 183 | .user_by_username(name) |
| 184 | .await |
| 185 | .expect("lookup user") |
| 186 | .expect("user exists") |
| 187 | } |
| 188 | |
| 189 | fn sha256_digest(bytes: &[u8]) -> String { |
| 190 | format!("sha256:{}", hex::encode(Sha256::digest(bytes))) |
| 191 | } |
| 192 | |
| 193 | /// The bearer header a client gets from the token endpoint with Basic `tahinli:<token>`. |
| 194 | async fn bearer_for(http: &Http, token: &str) -> String { |
| 195 | let res = http |
| 196 | .send( |
| 197 | reqwest::Method::GET, |
| 198 | "/v2/token?service=127.0.0.1&scope=repository:tahinli/app:pull,push", |
| 199 | Some(&basic("tahinli", token)), |
| 200 | &[], |
| 201 | None, |
| 202 | ) |
| 203 | .await; |
| 204 | assert_eq!(res.status, 200, "{}", res.text()); |
| 205 | let token = res.json()["token"].as_str().expect("token").to_owned(); |
| 206 | format!("Bearer {token}") |
| 207 | } |
| 208 | |
| 209 | fn basic(user: &str, password: &str) -> String { |
| 210 | let encoded = { |
| 211 | use base64::Engine as _; |
| 212 | base64::engine::general_purpose::STANDARD.encode(format!("{user}:{password}")) |
| 213 | }; |
| 214 | format!("Basic {encoded}") |
| 215 | } |
| 216 | |
| 217 | /// Pushes a blob in one request with its digest. |
| 218 | async fn push_blob(http: &Http, repo: &str, auth: &str, bytes: &[u8]) -> Resp { |
| 219 | let digest = sha256_digest(bytes); |
| 220 | http.post( |
| 221 | &format!("/v2/{repo}/blobs/uploads/?digest={digest}"), |
| 222 | Some(auth), |
| 223 | Some(bytes.to_vec()), |
| 224 | ) |
| 225 | .await |
| 226 | } |
| 227 | |
| 228 | /// Pushes a blob in three chunks, then closes the upload with its digest. |
| 229 | async fn push_blob_chunked(http: &Http, repo: &str, auth: &str, bytes: &[u8]) -> Resp { |
| 230 | let start = http |
| 231 | .post(&format!("/v2/{repo}/blobs/uploads/"), Some(auth), None) |
| 232 | .await; |
| 233 | assert_eq!(start.status, 202, "{}", start.text()); |
| 234 | let location = start.header("location").expect("location").to_owned(); |
| 235 | let third = bytes.len() / 3; |
| 236 | let mut offset = 0; |
| 237 | for chunk in [ |
| 238 | &bytes[..third], |
| 239 | &bytes[third..2 * third], |
| 240 | &bytes[2 * third..], |
| 241 | ] { |
| 242 | let range = format!("{offset}-{}", offset + chunk.len() - 1); |
| 243 | let res = http |
| 244 | .send( |
| 245 | reqwest::Method::PATCH, |
| 246 | &location, |
| 247 | Some(auth), |
| 248 | &[ |
| 249 | ("Content-Range", &range), |
| 250 | ("Content-Type", "application/octet-stream"), |
| 251 | ], |
| 252 | Some(chunk.to_vec()), |
| 253 | ) |
| 254 | .await; |
| 255 | assert_eq!(res.status, 202, "{}", res.text()); |
| 256 | offset += chunk.len(); |
| 257 | } |
| 258 | let digest = sha256_digest(bytes); |
| 259 | http.send( |
| 260 | reqwest::Method::PUT, |
| 261 | &format!("{location}?digest={digest}"), |
| 262 | Some(auth), |
| 263 | &[], |
| 264 | Some(Vec::new()), |
| 265 | ) |
| 266 | .await |
| 267 | } |
| 268 | |
| 269 | /// An image manifest naming `config` and `layers`, as the JSON a client pushes. |
| 270 | fn manifest(config: &[u8], layers: &[&[u8]]) -> Vec<u8> { |
| 271 | serde_json::to_vec(&serde_json::json!({ |
| 272 | "schemaVersion": 2, |
| 273 | "mediaType": MANIFEST_TYPE, |
| 274 | "config": { |
| 275 | "mediaType": "application/vnd.oci.image.config.v1+json", |
| 276 | "digest": sha256_digest(config), |
| 277 | "size": config.len(), |
| 278 | }, |
| 279 | "layers": layers.iter().map(|layer| serde_json::json!({ |
| 280 | "mediaType": "application/vnd.oci.image.layer.v1.tar", |
| 281 | "digest": sha256_digest(layer), |
| 282 | "size": layer.len(), |
| 283 | })).collect::<Vec<_>>(), |
| 284 | })) |
| 285 | .expect("manifest json") |
| 286 | } |
| 287 | |
| 288 | async fn put_manifest(http: &Http, repo: &str, reference: &str, auth: &str, body: &[u8]) -> Resp { |
| 289 | http.send( |
| 290 | reqwest::Method::PUT, |
| 291 | &format!("/v2/{repo}/manifests/{reference}"), |
| 292 | Some(auth), |
| 293 | &[("Content-Type", MANIFEST_TYPE)], |
| 294 | Some(body.to_vec()), |
| 295 | ) |
| 296 | .await |
| 297 | } |
| 298 | |
| 299 | #[tokio::test] |
| 300 | async fn probe_challenges_and_the_token_endpoint_issues_a_bearer_token() { |
| 301 | let token = generate_token(); |
| 302 | let seeded = token.clone(); |
| 303 | let env = start(move |store| async move { |
| 304 | let tahinli = user(&store, "tahinli").await; |
| 305 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 306 | }) |
| 307 | .await; |
| 308 | let port = env.port; |
| 309 | |
| 310 | let probe = env.http.get("/v2/", None).await; |
| 311 | assert_eq!(probe.status, 401, "{}", probe.text()); |
| 312 | let challenge = probe.header("www-authenticate").expect("challenge"); |
| 313 | assert!( |
| 314 | challenge.starts_with(&format!( |
| 315 | "Bearer realm=\"http://127.0.0.1:{port}/v2/token\"" |
| 316 | )), |
| 317 | "{challenge}" |
| 318 | ); |
| 319 | assert!( |
| 320 | challenge.contains(&format!("service=\"127.0.0.1:{port}\"")), |
| 321 | "{challenge}" |
| 322 | ); |
| 323 | |
| 324 | let auth = bearer_for(&env.http, &token).await; |
| 325 | assert_eq!(env.http.get("/v2/", Some(&auth)).await.status, 200); |
| 326 | |
| 327 | let bad = env |
| 328 | .http |
| 329 | .get("/v2/", Some(&basic("tahinli", "forge_wrong"))) |
| 330 | .await; |
| 331 | assert_eq!(bad.status, 401, "{}", bad.text()); |
| 332 | assert_eq!(bad.error_code(), "UNAUTHORIZED"); |
| 333 | } |
| 334 | |
| 335 | #[tokio::test] |
| 336 | async fn push_by_monolithic_and_chunked_upload_then_pull_by_tag_and_digest() { |
| 337 | let token = generate_token(); |
| 338 | let seeded = token.clone(); |
| 339 | let env = start(move |store| async move { |
| 340 | let tahinli = user(&store, "tahinli").await; |
| 341 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 342 | }) |
| 343 | .await; |
| 344 | let http = &env.http; |
| 345 | let auth = bearer_for(http, &token).await; |
| 346 | |
| 347 | let config = br#"{"architecture":"amd64","os":"linux"}"#.to_vec(); |
| 348 | let layer_one = b"the first layer, pushed in one request".to_vec(); |
| 349 | let layer_two: Vec<u8> = (0..3000u32).map(|i| (i % 251) as u8).collect(); |
| 350 | |
| 351 | let res = push_blob(http, "tahinli/app", &auth, &config).await; |
| 352 | assert_eq!(res.status, 201, "{}", res.text()); |
| 353 | assert_eq!( |
| 354 | res.header("docker-content-digest"), |
| 355 | Some(sha256_digest(&config).as_str()) |
| 356 | ); |
| 357 | assert_eq!( |
| 358 | push_blob(http, "tahinli/app", &auth, &layer_one) |
| 359 | .await |
| 360 | .status, |
| 361 | 201 |
| 362 | ); |
| 363 | let res = push_blob_chunked(http, "tahinli/app", &auth, &layer_two).await; |
| 364 | assert_eq!(res.status, 201, "{}", res.text()); |
| 365 | assert_eq!( |
| 366 | res.header("location"), |
| 367 | Some(format!("/v2/tahinli/app/blobs/{}", sha256_digest(&layer_two)).as_str()) |
| 368 | ); |
| 369 | |
| 370 | let head = http |
| 371 | .send( |
| 372 | reqwest::Method::HEAD, |
| 373 | &format!("/v2/tahinli/app/blobs/{}", sha256_digest(&layer_two)), |
| 374 | Some(&auth), |
| 375 | &[], |
| 376 | None, |
| 377 | ) |
| 378 | .await; |
| 379 | assert_eq!(head.status, 200); |
| 380 | assert_eq!(head.header("content-length"), Some("3000")); |
| 381 | let blob = http |
| 382 | .get( |
| 383 | &format!("/v2/tahinli/app/blobs/{}", sha256_digest(&layer_two)), |
| 384 | Some(&auth), |
| 385 | ) |
| 386 | .await; |
| 387 | assert_eq!(blob.status, 200); |
| 388 | assert_eq!(blob.body, layer_two); |
| 389 | |
| 390 | let image = manifest(&config, &[&layer_one, &layer_two]); |
| 391 | let res = put_manifest(http, "tahinli/app", "v1", &auth, &image).await; |
| 392 | assert_eq!(res.status, 201, "{}", res.text()); |
| 393 | let digest = sha256_digest(&image); |
| 394 | assert_eq!(res.header("docker-content-digest"), Some(digest.as_str())); |
| 395 | |
| 396 | let by_tag = http.get("/v2/tahinli/app/manifests/v1", Some(&auth)).await; |
| 397 | assert_eq!(by_tag.status, 200, "{}", by_tag.text()); |
| 398 | assert_eq!(by_tag.body, image); |
| 399 | assert_eq!(by_tag.header("content-type"), Some(MANIFEST_TYPE)); |
| 400 | let by_digest = http |
| 401 | .get(&format!("/v2/tahinli/app/manifests/{digest}"), Some(&auth)) |
| 402 | .await; |
| 403 | assert_eq!(by_digest.status, 200); |
| 404 | assert_eq!(by_digest.body, image); |
| 405 | let head = http |
| 406 | .send( |
| 407 | reqwest::Method::HEAD, |
| 408 | "/v2/tahinli/app/manifests/v1", |
| 409 | Some(&auth), |
| 410 | &[], |
| 411 | None, |
| 412 | ) |
| 413 | .await; |
| 414 | assert_eq!(head.status, 200); |
| 415 | assert!(head.body.is_empty()); |
| 416 | |
| 417 | let missing = http.get("/v2/tahinli/app/manifests/v9", Some(&auth)).await; |
| 418 | assert_eq!(missing.status, 404); |
| 419 | assert_eq!(missing.error_code(), "MANIFEST_UNKNOWN"); |
| 420 | } |
| 421 | |
| 422 | #[tokio::test] |
| 423 | async fn the_tag_list_pages_with_n_and_last() { |
| 424 | let token = generate_token(); |
| 425 | let seeded = token.clone(); |
| 426 | let env = start(move |store| async move { |
| 427 | let tahinli = user(&store, "tahinli").await; |
| 428 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 429 | }) |
| 430 | .await; |
| 431 | let http = &env.http; |
| 432 | let auth = bearer_for(http, &token).await; |
| 433 | let config = b"{}".to_vec(); |
| 434 | assert_eq!( |
| 435 | push_blob(http, "tahinli/app", &auth, &config).await.status, |
| 436 | 201 |
| 437 | ); |
| 438 | let image = manifest(&config, &[]); |
| 439 | for tag in ["v1", "v2", "v3"] { |
| 440 | assert_eq!( |
| 441 | put_manifest(http, "tahinli/app", tag, &auth, &image) |
| 442 | .await |
| 443 | .status, |
| 444 | 201 |
| 445 | ); |
| 446 | } |
| 447 | |
| 448 | let first = http.get("/v2/tahinli/app/tags/list?n=2", Some(&auth)).await; |
| 449 | assert_eq!(first.status, 200); |
| 450 | assert_eq!(first.json()["tags"], serde_json::json!(["v1", "v2"])); |
| 451 | let link = first.header("link").expect("next page link"); |
| 452 | assert!( |
| 453 | link.contains("last=v2") && link.contains("rel=\"next\""), |
| 454 | "{link}" |
| 455 | ); |
| 456 | |
| 457 | let second = http |
| 458 | .get("/v2/tahinli/app/tags/list?n=2&last=v2", Some(&auth)) |
| 459 | .await; |
| 460 | assert_eq!(second.json()["tags"], serde_json::json!(["v3"])); |
| 461 | assert!(second.header("link").is_none()); |
| 462 | |
| 463 | let all = http.get("/v2/tahinli/app/tags/list", Some(&auth)).await; |
| 464 | assert_eq!(all.json()["name"], "tahinli/app"); |
| 465 | assert_eq!(all.json()["tags"], serde_json::json!(["v1", "v2", "v3"])); |
| 466 | } |
| 467 | |
| 468 | #[tokio::test] |
| 469 | async fn bad_digests_and_missing_blobs_are_refused_with_oci_codes() { |
| 470 | let token = generate_token(); |
| 471 | let seeded = token.clone(); |
| 472 | let env = start(move |store| async move { |
| 473 | let tahinli = user(&store, "tahinli").await; |
| 474 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 475 | }) |
| 476 | .await; |
| 477 | let http = &env.http; |
| 478 | let auth = bearer_for(http, &token).await; |
| 479 | let config = b"{}".to_vec(); |
| 480 | |
| 481 | let wrong = format!( |
| 482 | "/v2/tahinli/app/blobs/uploads/?digest={}", |
| 483 | sha256_digest(b"other") |
| 484 | ); |
| 485 | let res = http.post(&wrong, Some(&auth), Some(config.clone())).await; |
| 486 | assert_eq!(res.status, 400, "{}", res.text()); |
| 487 | assert_eq!(res.error_code(), "DIGEST_INVALID"); |
| 488 | |
| 489 | let image = manifest(&config, &[]); |
| 490 | let res = put_manifest(http, "tahinli/app", "v1", &auth, &image).await; |
| 491 | assert_eq!(res.status, 400); |
| 492 | assert_eq!(res.error_code(), "MANIFEST_BLOB_UNKNOWN"); |
| 493 | |
| 494 | assert_eq!( |
| 495 | push_blob(http, "tahinli/app", &auth, &config).await.status, |
| 496 | 201 |
| 497 | ); |
| 498 | let res = put_manifest( |
| 499 | http, |
| 500 | "tahinli/app", |
| 501 | &sha256_digest(b"not this"), |
| 502 | &auth, |
| 503 | &image, |
| 504 | ) |
| 505 | .await; |
| 506 | assert_eq!(res.status, 400); |
| 507 | assert_eq!(res.error_code(), "DIGEST_INVALID"); |
| 508 | |
| 509 | let res = http.get("/v2/tahinli/nope/manifests/v1", Some(&auth)).await; |
| 510 | assert_eq!(res.status, 404); |
| 511 | assert_eq!(res.error_code(), "NAME_UNKNOWN"); |
| 512 | |
| 513 | let res = http.get("/v2/Tahinli/App/tags/list", Some(&auth)).await; |
| 514 | assert_eq!(res.status, 400); |
| 515 | assert_eq!(res.error_code(), "NAME_INVALID"); |
| 516 | |
| 517 | let res = http |
| 518 | .get( |
| 519 | &format!("/v2/tahinli/app/blobs/{}", sha256_digest(b"absent")), |
| 520 | Some(&auth), |
| 521 | ) |
| 522 | .await; |
| 523 | assert_eq!(res.status, 404); |
| 524 | assert_eq!(res.error_code(), "BLOB_UNKNOWN"); |
| 525 | } |
| 526 | |
| 527 | #[tokio::test] |
| 528 | async fn an_aborted_chunked_upload_is_gone_after_a_bad_digest() { |
| 529 | let token = generate_token(); |
| 530 | let seeded = token.clone(); |
| 531 | let env = start(move |store| async move { |
| 532 | let tahinli = user(&store, "tahinli").await; |
| 533 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 534 | }) |
| 535 | .await; |
| 536 | let http = &env.http; |
| 537 | let auth = bearer_for(http, &token).await; |
| 538 | |
| 539 | let start = http |
| 540 | .post("/v2/tahinli/app/blobs/uploads/", Some(&auth), None) |
| 541 | .await; |
| 542 | assert_eq!(start.status, 202); |
| 543 | let location = start.header("location").expect("location").to_owned(); |
| 544 | let patched = http |
| 545 | .send( |
| 546 | reqwest::Method::PATCH, |
| 547 | &location, |
| 548 | Some(&auth), |
| 549 | &[], |
| 550 | Some(b"some bytes".to_vec()), |
| 551 | ) |
| 552 | .await; |
| 553 | assert_eq!(patched.status, 202); |
| 554 | assert_eq!(patched.header("range"), Some("0-9")); |
| 555 | |
| 556 | let res = http |
| 557 | .send( |
| 558 | reqwest::Method::PUT, |
| 559 | &format!("{location}?digest={}", sha256_digest(b"different")), |
| 560 | Some(&auth), |
| 561 | &[], |
| 562 | Some(Vec::new()), |
| 563 | ) |
| 564 | .await; |
| 565 | assert_eq!(res.status, 400); |
| 566 | assert_eq!(res.error_code(), "DIGEST_INVALID"); |
| 567 | |
| 568 | let status = http |
| 569 | .send(reqwest::Method::GET, &location, Some(&auth), &[], None) |
| 570 | .await; |
| 571 | assert_eq!(status.status, 404, "{}", status.text()); |
| 572 | assert_eq!(status.error_code(), "BLOB_UPLOAD_UNKNOWN"); |
| 573 | } |
| 574 | |
| 575 | #[tokio::test] |
| 576 | async fn a_public_image_pulls_anonymously_and_a_private_one_does_not() { |
| 577 | let token = generate_token(); |
| 578 | let seeded = token.clone(); |
| 579 | let env = start(move |store| async move { |
| 580 | let tahinli = user(&store, "tahinli").await; |
| 581 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 582 | let secret = store |
| 583 | .ensure_package(&tahinli.id, KIND_OCI, "secret", "secret") |
| 584 | .await |
| 585 | .expect("package"); |
| 586 | store |
| 587 | .set_package_private(&secret.id, true) |
| 588 | .await |
| 589 | .expect("make private"); |
| 590 | }) |
| 591 | .await; |
| 592 | let http = &env.http; |
| 593 | let auth = bearer_for(http, &token).await; |
| 594 | let config = b"{}".to_vec(); |
| 595 | assert_eq!( |
| 596 | push_blob(http, "tahinli/app", &auth, &config).await.status, |
| 597 | 201 |
| 598 | ); |
| 599 | let image = manifest(&config, &[]); |
| 600 | assert_eq!( |
| 601 | put_manifest(http, "tahinli/app", "v1", &auth, &image) |
| 602 | .await |
| 603 | .status, |
| 604 | 201 |
| 605 | ); |
| 606 | assert_eq!( |
| 607 | push_blob(http, "tahinli/secret", &auth, &config) |
| 608 | .await |
| 609 | .status, |
| 610 | 201 |
| 611 | ); |
| 612 | assert_eq!( |
| 613 | put_manifest(http, "tahinli/secret", "v1", &auth, &image) |
| 614 | .await |
| 615 | .status, |
| 616 | 201 |
| 617 | ); |
| 618 | |
| 619 | let anon = http.get("/v2/tahinli/app/manifests/v1", None).await; |
| 620 | assert_eq!(anon.status, 200, "{}", anon.text()); |
| 621 | assert_eq!(anon.body, image); |
| 622 | let anon_blob = http |
| 623 | .get( |
| 624 | &format!("/v2/tahinli/app/blobs/{}", sha256_digest(&config)), |
| 625 | None, |
| 626 | ) |
| 627 | .await; |
| 628 | assert_eq!(anon_blob.status, 200); |
| 629 | |
| 630 | let hidden = http.get("/v2/tahinli/secret/manifests/v1", None).await; |
| 631 | assert_eq!(hidden.status, 401); |
| 632 | assert!(hidden.header("www-authenticate").is_some()); |
| 633 | assert_eq!(hidden.error_code(), "UNAUTHORIZED"); |
| 634 | let owner = http |
| 635 | .get("/v2/tahinli/secret/manifests/v1", Some(&auth)) |
| 636 | .await; |
| 637 | assert_eq!(owner.status, 200); |
| 638 | |
| 639 | let missing = http.get("/v2/tahinli/nothing/manifests/v1", None).await; |
| 640 | assert_eq!( |
| 641 | missing.status, 401, |
| 642 | "anonymous callers learn nothing about missing images" |
| 643 | ); |
| 644 | } |
| 645 | |
| 646 | #[tokio::test] |
| 647 | async fn writes_need_the_packages_scope_and_the_owner() { |
| 648 | let token = generate_token(); |
| 649 | let bob_token = generate_token(); |
| 650 | let no_scope = generate_token(); |
| 651 | let (seeded, seeded_bob, seeded_none) = (token.clone(), bob_token.clone(), no_scope.clone()); |
| 652 | let env = start(move |store| async move { |
| 653 | let tahinli = user(&store, "tahinli").await; |
| 654 | let bob = user(&store, "bob").await; |
| 655 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 656 | grant(&store, &bob, &["packages"], &seeded_bob).await; |
| 657 | grant(&store, &tahinli, &["repo"], &seeded_none).await; |
| 658 | }) |
| 659 | .await; |
| 660 | let http = &env.http; |
| 661 | |
| 662 | let anon = http |
| 663 | .post("/v2/tahinli/app/blobs/uploads/", None, None) |
| 664 | .await; |
| 665 | assert_eq!(anon.status, 401); |
| 666 | assert!(anon.header("www-authenticate").is_some()); |
| 667 | |
| 668 | let without = bearer_for_scope_less(http, &no_scope).await; |
| 669 | let res = http |
| 670 | .post("/v2/tahinli/app/blobs/uploads/", Some(&without), None) |
| 671 | .await; |
| 672 | assert_eq!( |
| 673 | res.status, 401, |
| 674 | "a token without the packages scope cannot push" |
| 675 | ); |
| 676 | |
| 677 | let bob_auth = bearer_for_as(http, "bob", &bob_token).await; |
| 678 | let res = http |
| 679 | .post("/v2/tahinli/app/blobs/uploads/", Some(&bob_auth), None) |
| 680 | .await; |
| 681 | assert_eq!(res.status, 403); |
| 682 | assert_eq!(res.error_code(), "DENIED"); |
| 683 | |
| 684 | let owner_auth = bearer_for(http, &token).await; |
| 685 | let res = http |
| 686 | .post("/v2/tahinli/app/blobs/uploads/", Some(&owner_auth), None) |
| 687 | .await; |
| 688 | assert_eq!(res.status, 202); |
| 689 | } |
| 690 | |
| 691 | /// A bearer header for `token` with no scope, read through the token endpoint as Basic. |
| 692 | async fn bearer_for_scope_less(http: &Http, token: &str) -> String { |
| 693 | bearer_for(http, token).await |
| 694 | } |
| 695 | |
| 696 | /// A bearer header for `token` owned by `who`, read through the token endpoint as Basic. |
| 697 | async fn bearer_for_as(http: &Http, who: &str, token: &str) -> String { |
| 698 | let res = http |
| 699 | .send( |
| 700 | reqwest::Method::GET, |
| 701 | "/v2/token", |
| 702 | Some(&basic(who, token)), |
| 703 | &[], |
| 704 | None, |
| 705 | ) |
| 706 | .await; |
| 707 | assert_eq!(res.status, 200, "{}", res.text()); |
| 708 | format!("Bearer {}", res.json()["token"].as_str().expect("token")) |
| 709 | } |
| 710 | |
| 711 | #[tokio::test] |
| 712 | async fn mount_copies_a_blob_across_repositories() { |
| 713 | let token = generate_token(); |
| 714 | let seeded = token.clone(); |
| 715 | let env = start(move |store| async move { |
| 716 | let tahinli = user(&store, "tahinli").await; |
| 717 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 718 | }) |
| 719 | .await; |
| 720 | let http = &env.http; |
| 721 | let auth = bearer_for(http, &token).await; |
| 722 | let layer = b"shared layer bytes".to_vec(); |
| 723 | assert_eq!( |
| 724 | push_blob(http, "tahinli/app", &auth, &layer).await.status, |
| 725 | 201 |
| 726 | ); |
| 727 | |
| 728 | let digest = sha256_digest(&layer); |
| 729 | let mounted = http |
| 730 | .post( |
| 731 | &format!("/v2/tahinli/other/blobs/uploads/?mount={digest}&from=tahinli/app"), |
| 732 | Some(&auth), |
| 733 | None, |
| 734 | ) |
| 735 | .await; |
| 736 | assert_eq!(mounted.status, 201, "{}", mounted.text()); |
| 737 | let read = http |
| 738 | .get(&format!("/v2/tahinli/other/blobs/{digest}"), Some(&auth)) |
| 739 | .await; |
| 740 | assert_eq!(read.body, layer); |
| 741 | |
| 742 | let not_there = http |
| 743 | .post( |
| 744 | &format!( |
| 745 | "/v2/tahinli/other/blobs/uploads/?mount={}&from=tahinli/app", |
| 746 | sha256_digest(b"x") |
| 747 | ), |
| 748 | Some(&auth), |
| 749 | None, |
| 750 | ) |
| 751 | .await; |
| 752 | assert_eq!( |
| 753 | not_there.status, 202, |
| 754 | "a mount that is not possible opens an upload instead" |
| 755 | ); |
| 756 | } |
| 757 | |
| 758 | #[tokio::test] |
| 759 | async fn deleting_tags_and_manifests_keeps_fresh_blobs_for_the_grace_period() { |
| 760 | let token = generate_token(); |
| 761 | let seeded = token.clone(); |
| 762 | let env = start(move |store| async move { |
| 763 | let tahinli = user(&store, "tahinli").await; |
| 764 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 765 | }) |
| 766 | .await; |
| 767 | let http = &env.http; |
| 768 | let auth = bearer_for(http, &token).await; |
| 769 | let config = b"{\"shared\":true}".to_vec(); |
| 770 | let layer_a = b"only in v1".to_vec(); |
| 771 | let layer_b = b"only in v2".to_vec(); |
| 772 | for blob in [&config, &layer_a, &layer_b] { |
| 773 | assert_eq!( |
| 774 | push_blob(http, "tahinli/app", &auth, blob).await.status, |
| 775 | 201 |
| 776 | ); |
| 777 | } |
| 778 | let v1 = manifest(&config, &[&layer_a]); |
| 779 | let v2 = manifest(&config, &[&layer_b]); |
| 780 | assert_eq!( |
| 781 | put_manifest(http, "tahinli/app", "v1", &auth, &v1) |
| 782 | .await |
| 783 | .status, |
| 784 | 201 |
| 785 | ); |
| 786 | assert_eq!( |
| 787 | put_manifest(http, "tahinli/app", "v2", &auth, &v2) |
| 788 | .await |
| 789 | .status, |
| 790 | 201 |
| 791 | ); |
| 792 | |
| 793 | let blob_url = |bytes: &[u8]| format!("/v2/tahinli/app/blobs/{}", sha256_digest(bytes)); |
| 794 | let res = http |
| 795 | .send( |
| 796 | reqwest::Method::DELETE, |
| 797 | "/v2/tahinli/app/manifests/v2", |
| 798 | Some(&auth), |
| 799 | &[], |
| 800 | None, |
| 801 | ) |
| 802 | .await; |
| 803 | assert_eq!(res.status, 202, "{}", res.text()); |
| 804 | assert_eq!( |
| 805 | http.get("/v2/tahinli/app/manifests/v2", Some(&auth)) |
| 806 | .await |
| 807 | .status, |
| 808 | 404 |
| 809 | ); |
| 810 | // The blobs were pushed just now: the sweep keeps a link newer than the grace period, since a |
| 811 | // push can still be on its way to a manifest. The store test covers the sweep after it. |
| 812 | assert_eq!(http.get(&blob_url(&layer_b), Some(&auth)).await.status, 200); |
| 813 | assert_eq!(http.get(&blob_url(&layer_a), Some(&auth)).await.status, 200); |
| 814 | assert_eq!(http.get(&blob_url(&config), Some(&auth)).await.status, 200); |
| 815 | |
| 816 | let v1_digest = sha256_digest(&v1); |
| 817 | let res = http |
| 818 | .send( |
| 819 | reqwest::Method::DELETE, |
| 820 | &format!("/v2/tahinli/app/manifests/{v1_digest}"), |
| 821 | Some(&auth), |
| 822 | &[], |
| 823 | None, |
| 824 | ) |
| 825 | .await; |
| 826 | assert_eq!(res.status, 202); |
| 827 | assert_eq!(http.get(&blob_url(&layer_a), Some(&auth)).await.status, 200); |
| 828 | assert_eq!(http.get(&blob_url(&config), Some(&auth)).await.status, 200); |
| 829 | let tags = http.get("/v2/tahinli/app/tags/list", Some(&auth)).await; |
| 830 | assert_eq!(tags.json()["tags"], serde_json::json!([])); |
| 831 | } |
| 832 | |
| 833 | #[tokio::test] |
| 834 | async fn a_blob_a_manifest_uses_cannot_be_deleted_and_an_unused_one_can() { |
| 835 | let token = generate_token(); |
| 836 | let seeded = token.clone(); |
| 837 | let env = start(move |store| async move { |
| 838 | let tahinli = user(&store, "tahinli").await; |
| 839 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 840 | }) |
| 841 | .await; |
| 842 | let http = &env.http; |
| 843 | let auth = bearer_for(http, &token).await; |
| 844 | let config = b"{}".to_vec(); |
| 845 | let layer = b"kept by the manifest".to_vec(); |
| 846 | let spare = b"never used".to_vec(); |
| 847 | assert_eq!( |
| 848 | push_blob(http, "tahinli/app", &auth, &config).await.status, |
| 849 | 201 |
| 850 | ); |
| 851 | assert_eq!( |
| 852 | push_blob(http, "tahinli/app", &auth, &layer).await.status, |
| 853 | 201 |
| 854 | ); |
| 855 | assert_eq!( |
| 856 | push_blob(http, "tahinli/app", &auth, &spare).await.status, |
| 857 | 201 |
| 858 | ); |
| 859 | assert_eq!( |
| 860 | put_manifest( |
| 861 | http, |
| 862 | "tahinli/app", |
| 863 | "v1", |
| 864 | &auth, |
| 865 | &manifest(&config, &[&layer]) |
| 866 | ) |
| 867 | .await |
| 868 | .status, |
| 869 | 201 |
| 870 | ); |
| 871 | |
| 872 | let delete = |bytes: &[u8]| format!("/v2/tahinli/app/blobs/{}", sha256_digest(bytes)); |
| 873 | let refused = http |
| 874 | .send( |
| 875 | reqwest::Method::DELETE, |
| 876 | &delete(&layer), |
| 877 | Some(&auth), |
| 878 | &[], |
| 879 | None, |
| 880 | ) |
| 881 | .await; |
| 882 | assert_eq!(refused.status, 400); |
| 883 | assert_eq!(refused.error_code(), "UNSUPPORTED"); |
| 884 | |
| 885 | let gone = http |
| 886 | .send( |
| 887 | reqwest::Method::DELETE, |
| 888 | &delete(&spare), |
| 889 | Some(&auth), |
| 890 | &[], |
| 891 | None, |
| 892 | ) |
| 893 | .await; |
| 894 | assert_eq!(gone.status, 202); |
| 895 | assert_eq!(http.get(&delete(&spare), Some(&auth)).await.status, 404); |
| 896 | } |
| 897 | |
| 898 | #[tokio::test] |
| 899 | #[ignore = "runs podman; run with --ignored"] |
| 900 | async fn podman_logs_in_pushes_and_pulls_an_image() { |
| 901 | if !tool_on_path("podman") { |
| 902 | eprintln!("podman is not installed; skipping"); |
| 903 | return; |
| 904 | } |
| 905 | let token = generate_token(); |
| 906 | let seeded = token.clone(); |
| 907 | let env = start(move |store| async move { |
| 908 | let tahinli = user(&store, "tahinli").await; |
| 909 | grant(&store, &tahinli, &["packages"], &seeded).await; |
| 910 | }) |
| 911 | .await; |
| 912 | let host = format!("127.0.0.1:{}", env.port); |
| 913 | let work = tempfile::tempdir().expect("work dir"); |
| 914 | let dir = work.path(); |
| 915 | let auth_file = dir.join("auth.json"); |
| 916 | std::fs::write(dir.join("hello.txt"), "hello from the forge\n").expect("file"); |
| 917 | std::fs::write( |
| 918 | dir.join("Containerfile"), |
| 919 | "FROM scratch\nCOPY hello.txt /hello.txt\n", |
| 920 | ) |
| 921 | .expect("containerfile"); |
| 922 | let root = dir.join("store"); |
| 923 | let run = dir.join("run"); |
| 924 | let auth_path = auth_file.to_str().expect("utf8").to_owned(); |
| 925 | let root_arg = root.to_str().expect("utf8").to_owned(); |
| 926 | let run_arg = run.to_str().expect("utf8").to_owned(); |
| 927 | let podman = |args: &[&str]| -> String { |
| 928 | let out = Command::new("podman") |
| 929 | .args(["--root", &root_arg, "--runroot", &run_arg]) |
| 930 | .args(args) |
| 931 | .env("REGISTRY_AUTH_FILE", &auth_path) |
| 932 | .current_dir(dir) |
| 933 | .output() |
| 934 | .expect("run podman"); |
| 935 | assert!( |
| 936 | out.status.success(), |
| 937 | "podman {args:?} failed:\n{}", |
| 938 | describe(&out) |
| 939 | ); |
| 940 | format!( |
| 941 | "{}{}", |
| 942 | String::from_utf8_lossy(&out.stdout), |
| 943 | String::from_utf8_lossy(&out.stderr) |
| 944 | ) |
| 945 | }; |
| 946 | |
| 947 | podman(&[ |
| 948 | "build", |
| 949 | "-f", |
| 950 | "Containerfile", |
| 951 | "-t", |
| 952 | "localhost/tiny:v1", |
| 953 | ".", |
| 954 | ]); |
| 955 | podman(&[ |
| 956 | "login", |
| 957 | "--tls-verify=false", |
| 958 | "-u", |
| 959 | "tahinli", |
| 960 | "-p", |
| 961 | &token, |
| 962 | &host, |
| 963 | ]); |
| 964 | let target = format!("{host}/tahinli/tiny:v1"); |
| 965 | podman(&["push", "--tls-verify=false", "localhost/tiny:v1", &target]); |
| 966 | podman(&["rmi", "localhost/tiny:v1"]); |
| 967 | podman(&["pull", "--tls-verify=false", &target]); |
| 968 | let listed = podman(&["images", "--format", "{{.Repository}}:{{.Tag}}"]); |
| 969 | assert!( |
| 970 | listed.contains(&format!("{host}/tahinli/tiny:v1")), |
| 971 | "{listed}" |
| 972 | ); |
| 973 | } |