From 5aca7a8cdb87cbd00950b99bdff27ebded948446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Baylac=20Jacqu=C3=A9?= Date: Tue, 30 Jan 2024 12:00:39 +0100 Subject: [PATCH] Create binary cache --- Cargo.lock | 56 ++++-------------------------- Cargo.toml | 1 - default.nix | 2 +- migrations/V1__init.sql | 4 +-- src/handlers/binary_cache.rs | 19 ++++++---- src/models/mod.rs | 3 +- src/s3/mod.rs | 23 +++++++++--- src/server/bin/main.rs | 7 ---- src/templates/main.css | 25 ++++++++++++- src/templates/new-binary-cache.hbs | 4 ++- tests/db.rs | 12 +++++-- 11 files changed, 79 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1fb540..a121d42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,7 +8,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "617a8268e3537fe1d8c9ead925fca49ef6400927ee7bc26750e90ecee14ce4b8" dependencies = [ - "bitflags 1.3.2", + "bitflags", "bytes", "futures-core", "futures-sink", @@ -19,29 +19,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "actix-files" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf0bdd6ff79de7c9a021f5d9ea79ce23e108d8bfc9b49b5b4a2cf6fad5a35212" -dependencies = [ - "actix-http", - "actix-service", - "actix-utils", - "actix-web", - "bitflags 2.4.2", - "bytes", - "derive_more", - "futures-core", - "http-range", - "log", - "mime", - "mime_guess", - "percent-encoding", - "pin-project-lite", - "v_htmlescape", -] - [[package]] name = "actix-http" version = "3.3.1" @@ -54,7 +31,7 @@ dependencies = [ "actix-utils", "ahash 0.8.3", "base64 0.21.2", - "bitflags 1.3.2", + "bitflags", "brotli", "bytes", "bytestring", @@ -842,12 +819,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bitflags" -version = "2.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" - [[package]] name = "block-buffer" version = "0.10.3" @@ -952,7 +923,7 @@ version = "4.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d" dependencies = [ - "bitflags 1.3.2", + "bitflags", "clap_derive", "clap_lex", "is-terminal", @@ -1590,12 +1561,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "http-range" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" - [[package]] name = "httparse" version = "1.8.0" @@ -1880,7 +1845,6 @@ dependencies = [ name = "nom-nom-gc" version = "0.1.0" dependencies = [ - "actix-files", "actix-web", "anyhow", "aws-config", @@ -1975,7 +1939,7 @@ version = "0.10.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b102428fd03bc5edf97f62620f7298614c45cedf287c271e7ed450bbaf83f2e1" dependencies = [ - "bitflags 1.3.2", + "bitflags", "cfg-if", "foreign-types", "libc", @@ -2292,7 +2256,7 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" dependencies = [ - "bitflags 1.3.2", + "bitflags", ] [[package]] @@ -2464,7 +2428,7 @@ version = "0.36.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588" dependencies = [ - "bitflags 1.3.2", + "bitflags", "errno", "io-lifetimes", "libc", @@ -2581,7 +2545,7 @@ version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" dependencies = [ - "bitflags 1.3.2", + "bitflags", "core-foundation", "core-foundation-sys", "libc", @@ -3146,12 +3110,6 @@ dependencies = [ "serde", ] -[[package]] -name = "v_htmlescape" -version = "0.15.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e8257fbc510f0a46eb602c10215901938b5c2a7d5e70fc11483b1d3c9b5b18c" - [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index 855e56c..2af3277 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] actix-web = "4" -actix-files = "*" mime = "*" mime_guess = "*" rust-embed = "*" diff --git a/default.nix b/default.nix index 3feb0e9..6eb822c 100644 --- a/default.nix +++ b/default.nix @@ -10,5 +10,5 @@ pkgs.rustPlatform.buildRustPackage { ]; buildInputs = [ pkgs.openssl ]; src = pkgs.lib.cleanSource ./.; - cargoHash = "sha256-ct90VmvzUIJ8d0LO7jz5/kcC3M0rU85DbX81/67T2R4="; + cargoHash = "sha256-+fS2adHUigIgnporYk6rKvxUY2aSKbvssPnQYnzU+4Y="; } diff --git a/migrations/V1__init.sql b/migrations/V1__init.sql index 234b4b5..323e820 100644 --- a/migrations/V1__init.sql +++ b/migrations/V1__init.sql @@ -1,6 +1,6 @@ CREATE TABLE Users ( id UUID PRIMARY KEY NOT NULL, - user_name text NOT NULL + user_name text NOT NULL UNIQUE ); CREATE TABLE Keys ( @@ -19,7 +19,7 @@ CREATE TABLE PendingRegistrations ( CREATE TABLE BinaryCaches ( id SERIAL PRIMARY KEY NOT NULL, - name text NOT NULL, + name text NOT NULL UNIQUE, access_key text NOT NULL, secret_key text NOT NULL, region text NOT NULL, diff --git a/src/handlers/binary_cache.rs b/src/handlers/binary_cache.rs index 71dc541..9a99443 100644 --- a/src/handlers/binary_cache.rs +++ b/src/handlers/binary_cache.rs @@ -1,6 +1,6 @@ use actix_web::{web::{self, Form}, HttpResponse, http::header::{ContentType, self}, Responder, HttpRequest}; -use crate::{models::{AppState, BinaryCache}, templates}; +use crate::{models::{AppState, BinaryCache}, templates, s3::{check_bucket, create_client}}; use super::check_authentication; @@ -21,11 +21,18 @@ pub async fn new_binary_cache(app_state: web::Data>, req: HttpReque pub async fn new_binary_cache_post(app_state: web::Data>, req: HttpRequest, form: Form) -> impl Responder { if check_authentication(&app_state, req).await.is_ok() { - app_state.create_binary_cache(&form).await.unwrap(); - HttpResponse::SeeOther() - .content_type(ContentType::plaintext()) - .append_header((header::LOCATION, "/")) - .body("new binary cache created") + let client = create_client(form.clone()); + if ! check_bucket(&client, &form.bucket).await.is_ok() { + HttpResponse::BadRequest() + .content_type(ContentType::plaintext()) + .body("can't connect to the binary cache") + } else { + app_state.create_binary_cache(&form).await.unwrap(); + HttpResponse::SeeOther() + .content_type(ContentType::plaintext()) + .append_header((header::LOCATION, "/")) + .body("new binary cache created") + } } else { HttpResponse::Forbidden() .finish() diff --git a/src/models/mod.rs b/src/models/mod.rs index de9c20b..b84ce03 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -100,7 +100,8 @@ pub struct BinaryCache { pub access_key: String, pub secret_key: String, pub region: String, - pub endpoint_url: String + pub endpoint_url: String, + pub bucket: String } #[derive(Clone, Debug, Eq, PartialEq)] diff --git a/src/s3/mod.rs b/src/s3/mod.rs index 0e62362..3dcda5d 100644 --- a/src/s3/mod.rs +++ b/src/s3/mod.rs @@ -1,10 +1,23 @@ use anyhow::{anyhow, Result}; -use aws_sdk_s3::{operation::head_bucket::HeadBucketOutput, Client}; +use aws_config::{Region, BehaviorVersion}; +use aws_sdk_s3::{operation::head_bucket::HeadBucketOutput, Client, config::Credentials}; +use aws_types::sdk_config::SharedCredentialsProvider; -use crate::models::Configuration; +use crate::models::BinaryCache; -pub async fn check_bucket(client: &Client, config: &Configuration) -> Result { - println!("{}",&config.s3_bucket); - let res = client.head_bucket().bucket(&config.s3_bucket).send().await; +pub async fn check_bucket(client: &Client, bucket_name: &str) -> Result { + println!("{}", bucket_name); + let res = client.head_bucket().bucket(bucket_name).send().await; res.map_err(|e| anyhow!("Cannot access the binary cache bucket: {}", e)) } + +pub fn create_client(bc: BinaryCache) -> aws_sdk_s3::Client { + let credentials = Credentials::new(bc.access_key, bc.secret_key, None, None, "nom-nom-provider"); + let s3_client_config = aws_config::SdkConfig::builder() + .endpoint_url(bc.endpoint_url) + .region(Some(Region::new(bc.region))) + .credentials_provider(SharedCredentialsProvider::new(credentials)) + .behavior_version(BehaviorVersion::latest()) + .build(); + aws_sdk_s3::Client::new(&s3_client_config) +} diff --git a/src/server/bin/main.rs b/src/server/bin/main.rs index 7a461f0..a4938e5 100644 --- a/src/server/bin/main.rs +++ b/src/server/bin/main.rs @@ -7,7 +7,6 @@ use mime_guess::from_path; use nom_nom_gc::handlers; use nom_nom_gc::models::read_config; use nom_nom_gc::models; -use nom_nom_gc::s3::check_bucket; use rust_embed::RustEmbed; #[derive(Parser, Debug)] @@ -42,12 +41,6 @@ async fn main() -> std::io::Result<()> { let state = models::AppState::new(config.clone()).await; println!("Running DB migrations"); state.run_migrations().await.unwrap_or_else(|e| panic!("Db migration error: {}", e)); - println!("Checking binary cache bucket"); - let bucket = check_bucket(&state.s3_client, &config).await; - match bucket { - Ok(_) => println!("Connection to the bucket successful"), - Err(e) => panic!("Cannot connect to the binary cache bucket: {}", e) - } println!("Server listening to {}", &args.bind); HttpServer::new( move || { diff --git a/src/templates/main.css b/src/templates/main.css index ba24c57..7010bd7 100644 --- a/src/templates/main.css +++ b/src/templates/main.css @@ -52,7 +52,7 @@ form { width: 20em; margin: auto; padding: 1em; - border: 3px solid; + border: 3px solid black; border-radius: 9px; } @@ -85,8 +85,31 @@ form > #submit { font-size: 2em; margin-left: 0; text-align: center; + border-right: 2px solid black; + margin-left: .2em; } nav { display: flex; + padding: 0; +} + +table { + width: 30em; + margin: auto; + margin-top: 2em; + border: solid 3px black; + border-radius: 9px; +} + +td { + padding-left: .5em; +} + +tr:nth-child(even) { + background-color: white; +} + +tr:nth-child(odd) { + background-color: #f1f3f9; } diff --git a/src/templates/new-binary-cache.hbs b/src/templates/new-binary-cache.hbs index f2ee7c9..a660108 100644 --- a/src/templates/new-binary-cache.hbs +++ b/src/templates/new-binary-cache.hbs @@ -1,7 +1,7 @@ {{#> template }}

New Binary Cache

- + @@ -11,6 +11,8 @@ + +
{{ /template }} \ No newline at end of file diff --git a/tests/db.rs b/tests/db.rs index 67a6fc3..8236e2a 100644 --- a/tests/db.rs +++ b/tests/db.rs @@ -55,10 +55,15 @@ async fn run_test_db(db: &TestDB) -> Result<()> { url: "http://localhost:9000".to_string(), db_port: Some(db.port), db_host: Some(dbpath), - db_name: db.db_name.clone() + db_name: db.db_name.clone(), + s3_bucket: "dummy".to_string(), + s3_endpoint: "dummy".to_string(), + s3_region: "dummy".to_string(), + s3_access_key_filepath: "dummy".to_string(), + s3_secret_key_filepath: "dummy".to_string(), }; - let state = AppState::new(conf); + let state = AppState::new(conf).await; state.run_migrations().await?; let test_user = User { uuid: UserUuid(Uuid::new_v4()), name: "test-user".to_owned() }; @@ -74,7 +79,8 @@ async fn run_test_db(db: &TestDB) -> Result<()> { access_key: "access key".to_string(), secret_key: "secret key".to_string(), region: "reg-01".to_string(), - endpoint_url: "localhost:" + endpoint_url: "localhost://".to_string(), + bucket: "nix-cache".to_string() }; state.create_binary_cache(&binary_cache).await?; let project = Project {