Something went wrong. Try again.
Build and run CI pipelines anywhere, consistently. fluentci.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138use axum::{ body::Body, http::{Request, StatusCode, header},};use clap::Parser;use fluentci_cache::{Config, router};use object_store::{ObjectStore, local::LocalFileSystem};use serde_json::{Value, json};use sha2::{Digest, Sha256};use std::sync::Arc;use tower::ServiceExt;
#[derive(Parser)]struct Options { #[command(flatten)] config: Config,}
async fn send(app: &axum::Router, method: &str, path: &str, token: &str, body: &str) -> StatusCode { app.clone() .oneshot( Request::builder() .method(method) .uri(path) .header(header::AUTHORIZATION, format!("Bearer {token}")) .header(header::CONTENT_TYPE, "application/json") .body(Body::from(body.to_owned())) .unwrap(), ) .await .unwrap() .status()}
#[tokio::test]async fn revocation_survives_restart_and_clear_and_denies_every_upload_adapter() { let fixture: Value = serde_json::from_str(include_str!("fixtures/revocation-tokens.json")).unwrap(); let directory = tempfile::tempdir().unwrap(); let pem = directory.path().join("public.pem"); std::fs::write(&pem, fixture["publicKey"].as_str().unwrap()).unwrap(); let storage = directory.path().join("objects"); std::fs::create_dir(&storage).unwrap(); let mut config = Options::parse_from(["test", "--backend", "file", "--token", "admin"]).config; config.upload_token_public_key_file = Some(pem); let store = Arc::new(LocalFileSystem::new_with_prefix(&storage).unwrap()); let app = router(config.clone(), store.clone()).unwrap(); let account = fixture["account"].as_str().unwrap(); let upload = fixture["upload"].as_str().unwrap(); let manage = fixture["manage"].as_str().unwrap(); let base = format!("/accounts/{account}"); let path = format!("{base}/generic/artifact"); let old_marker = object_store::path::Path::parse(format!( "_revocations/{}/{}", hex::encode(Sha256::digest(account.as_bytes())), hex::encode(Sha256::digest(fixture["id"].as_str().unwrap().as_bytes())) )) .unwrap(); store.put(&old_marker, "revoked".into()).await.unwrap(); assert_eq!( send(&app, "PUT", &path, upload, "data").await, StatusCode::UNAUTHORIZED ); store.delete(&old_marker).await.unwrap(); for name in ["missingId", "expired"] { assert_eq!( send(&app, "PUT", &path, fixture[name].as_str().unwrap(), "data").await, StatusCode::UNAUTHORIZED ); } assert_eq!( send( &app, "POST", &format!("{base}/_admin"), fixture["unboundedManage"].as_str().unwrap(), r#"{"action":"list"}"# ) .await, StatusCode::UNAUTHORIZED ); assert_eq!( send(&app, "PUT", &path, upload, "data").await, StatusCode::CREATED ); let revoke = json!({"action":"revokeToken", "id":fixture["id"]}).to_string(); assert_eq!( send(&app, "POST", &format!("{base}/_admin"), upload, &revoke).await, StatusCode::UNAUTHORIZED ); assert_eq!( send(&app, "POST", &format!("{base}/_admin"), manage, &revoke).await, StatusCode::OK ); for (method, path) in [ ("PUT", path.clone()), ("PUT", format!("{base}/nx/v1/cache/key")), ("PUT", format!("{base}/turbo/v8/artifacts/key")), ( "POST", format!("/v2/accounts/{account}/buildkit/project/blobs/uploads/"), ), ] { assert_eq!( send(&app, method, &path, upload, "data").await, StatusCode::UNAUTHORIZED ); } assert_eq!( send( &app, "PUT", &path, fixture["other"].as_str().unwrap(), "data" ) .await, StatusCode::CREATED ); assert_eq!( send( &app, "POST", &format!("{base}/_admin"), manage, r#"{"action":"clear","confirm":true}"# ) .await, StatusCode::OK ); drop(app); let restarted = router(config, store).unwrap(); assert_eq!( send(&restarted, "PUT", &path, upload, "data").await, StatusCode::UNAUTHORIZED );}