From b52f4867ffc9b35fcaecc9a119476ce03282f342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 23 Aug 2023 10:21:51 +0200 Subject: [PATCH 1/5] chore: Add `create_volume` to swagger docs Also: - Update flake so postgres works in NixOS - Replace ucan path deps with actual UCAN --- Cargo.lock | 8 +- Cargo.toml | 4 +- fission-server/src/docs.rs | 2 +- fission-server/src/routes/volume.rs | 13 ++ flake.nix | 205 ++++++++++++++-------------- 5 files changed, 123 insertions(+), 109 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 065e58a2..da28ab94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,9 +165,9 @@ dependencies = [ [[package]] name = "async-lock" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" dependencies = [ "event-listener", ] @@ -5412,6 +5412,8 @@ checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" [[package]] name = "ucan" version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0237a17cf06465fce3ab4126c91d0745123b0d21a2ef5adbd3da47d10309455" dependencies = [ "anyhow", "async-recursion", @@ -5438,6 +5440,8 @@ dependencies = [ [[package]] name = "ucan-key-support" version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f99720d7a9d4b5a7cf3f2a9d17c951bbaa6331f00c07aa8845e80e9fb4c8faf1" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 8d2ce935..3786e74c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ ed25519-zebra = "3.1" rand = "0.8" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -ucan = { path = "../rs-ucan/ucan" } -ucan-key-support = { path = "../rs-ucan/ucan-key-support" } +ucan = { version = "=0.2.0" } +ucan-key-support = { version = "=0.1.3" } url = "2.3" # Speedup build on macOS diff --git a/fission-server/src/docs.rs b/fission-server/src/docs.rs index 484d9107..1d219e6b 100644 --- a/fission-server/src/docs.rs +++ b/fission-server/src/docs.rs @@ -13,7 +13,7 @@ use utoipa::OpenApi; #[openapi( paths(health::healthcheck, ping::get, auth::request_token, account::create_account, account::get_account, account::update_did, -volume::get_cid, volume::update_cid), +volume::get_cid, volume::create_volume, volume::update_cid), components(schemas(AppError, email_verification::Request, auth::VerificationCodeResponse, AccountRequest, NewVolumeRecord, health::HealthcheckResponse)), modifiers(&UcanAddon), tags( diff --git a/fission-server/src/routes/volume.rs b/fission-server/src/routes/volume.rs index 67e8ec94..50150e4e 100644 --- a/fission-server/src/routes/volume.rs +++ b/fission-server/src/routes/volume.rs @@ -42,6 +42,19 @@ pub async fn get_cid( } } +#[utoipa::path( + post, + path = "/api/account/{username}/volume", + security( + ("ucan_bearer" = []), + ), + responses( + (status = 200, description = "Successfully created Volume", body=NewVolume), + (status = 400, description = "Invalid request", body=AppError), + (status = 401, description = "Unauthorized"), + ) +)] + /// Handler to create a new volume for an account pub async fn create_volume( State(state): State, diff --git a/flake.nix b/flake.nix index fd43003d..004cd934 100644 --- a/flake.nix +++ b/flake.nix @@ -18,117 +18,114 @@ flake-utils, rust-overlay, } @ inputs: - flake-utils.lib.eachDefaultSystem ( - system: let - overlays = [(import rust-overlay)]; - pkgs = import nixpkgs {inherit system overlays;}; - - rust-toolchain = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml).override { + flake-utils.lib.eachDefaultSystem (system: let + overlays = [(import rust-overlay)]; + pkgs = import nixpkgs {inherit system overlays;}; + + rust-toolchain = + (pkgs.rust-bin.fromRustupToolchainFile + ./rust-toolchain.toml) + .override { extensions = ["cargo" "clippy" "rustfmt" "rust-src" "rust-std"]; }; - nightly-rustfmt = pkgs.rust-bin.nightly.latest.rustfmt; - - format-pkgs = with pkgs; [ - nixpkgs-fmt - alejandra - ]; - - cargo-installs = with pkgs; [ - cargo-deny - cargo-expand - cargo-outdated - cargo-sort - cargo-udeps - cargo-watch - diesel-cli - ]; - in rec - { - devShells.default = pkgs.mkShell { - name = "fission-server"; - nativeBuildInputs = with pkgs; - [ - # The ordering of these two items is important. For nightly rustfmt to be used instead of - # the rustfmt provided by `rust-toolchain`, it must appear first in the list. This is - # because native build inputs are added to $PATH in the order they're listed here. - nightly-rustfmt - rust-toolchain - pre-commit - protobuf - postgresql - direnv - self.packages.${system}.irust - kubo - ] - ++ format-pkgs - ++ cargo-installs - ++ lib.optionals stdenv.isDarwin [ - darwin.apple_sdk.frameworks.Security - darwin.apple_sdk.frameworks.CoreFoundation - darwin.apple_sdk.frameworks.Foundation - ]; - - shellHook = '' - [ -e .git/hooks/pre-commit ] || pre-commit install --install-hooks && pre-commit install --hook-type commit-msg - - PGDATA="./.pg"; - PGURL=postgres://postgres@localhost:5432/fission-server - - # Initialize a local database if necessary. - if [ ! -e $PGDATA ]; then - echo -e "\nInitializing PostgreSQL in $PGDATA\n" - initdb $PGDATA --no-instructions -A trust -U postgres - if pg_ctl -D $PGDATA start; then - cd fission-server - diesel database setup --database-url $PGURL - cd .. - pg_ctl -D $PGDATA stop - else - echo "Unable to start PostgreSQL server on default port (:5432). Maybe a local database is already running?" - fi - fi - - # Give instructions on how to start postgresql if it's not already running. - if [ ! -e $PGDATA/postmaster.pid ]; then - echo -e "\nPostgreSQL not running. To start, use the following command:" - echo -e " pg_ctl -D $PGDATA -l postgres.log start\n\n" - else - echo -e "\nPostgreSQL is running. To stop, use the following command:" - echo -e " pg_ctl -D $PGDATA stop\n\n" - - echo -e "\nRunning pending Diesel Migrations..." + nightly-rustfmt = pkgs.rust-bin.nightly.latest.rustfmt; + + format-pkgs = with pkgs; [nixpkgs-fmt alejandra]; + + cargo-installs = with pkgs; [ + cargo-deny + cargo-expand + cargo-outdated + cargo-sort + cargo-udeps + cargo-watch + diesel-cli + ]; + in rec { + devShells.default = pkgs.mkShell { + name = "fission-server"; + nativeBuildInputs = with pkgs; + [ + # The ordering of these two items is important. For nightly rustfmt to be used instead of + # the rustfmt provided by `rust-toolchain`, it must appear first in the list. This is + # because native build inputs are added to $PATH in the order they're listed here. + nightly-rustfmt + rust-toolchain + pre-commit + protobuf + postgresql + direnv + self.packages.${system}.irust + kubo + ] + ++ format-pkgs + ++ cargo-installs + ++ lib.optionals stdenv.isDarwin [ + darwin.apple_sdk.frameworks.Security + darwin.apple_sdk.frameworks.CoreFoundation + darwin.apple_sdk.frameworks.Foundation + ]; + + shellHook = '' + [ -e .git/hooks/pre-commit ] || pre-commit install --install-hooks && pre-commit install --hook-type commit-msg + + PGDATA="./.pg"; + PGURL=postgres://postgres@localhost:5432/fission-server + + # Initialize a local database if necessary. + if [ ! -e $PGDATA ]; then + echo -e "\nInitializing PostgreSQL in $PGDATA\n" + initdb $PGDATA --no-instructions -A trust -U postgres + if pg_ctl -o '-k /tmp' -D $PGDATA start; then cd fission-server - diesel migration run --database-url $PGURL + diesel database setup --database-url $PGURL cd .. - echo - fi - - # Setup local Kubo config - if [ ! -e ./.ipfs ]; then - ipfs --repo-dir ./.ipfs --offline init + pg_ctl -o '-k /tmp' -D $PGDATA stop + else + echo "Unable to start PostgreSQL server on default port (:5432). Maybe a local database is already running?" fi - - # Run Kubo - echo -e "To run Kubo as a local IPFS node, use the following command:" - echo -e " . ipfs --repo-dir ./.ipfs --offline daemon" + fi + + # Give instructions on how to start postgresql if it's not already running. + if [ ! -e $PGDATA/postmaster.pid ]; then + echo -e "\nPostgreSQL not running. To start, use the following command:" + echo -e " pg_ctl -o '-k /tmp' -D $PGDATA -l postgres.log start\n\n" + else + echo -e "\nPostgreSQL is running. To stop, use the following command:" + echo -e " pg_ctl -o '-k /tmp' -D $PGDATA stop\n\n" + + echo -e "\nRunning pending Diesel Migrations..." + cd fission-server + diesel migration run --database-url $PGURL + cd .. echo - ''; + fi + + # Setup local Kubo config + if [ ! -e ./.ipfs ]; then + ipfs --repo-dir ./.ipfs --offline init + fi + + # Run Kubo + echo -e "To run Kubo as a local IPFS node, use the following command:" + echo -e " ipfs --repo-dir ./.ipfs --offline daemon" + echo + ''; + }; + + packages.irust = pkgs.rustPlatform.buildRustPackage rec { + pname = "irust"; + version = "1.65.1"; + src = pkgs.fetchFromGitHub { + owner = "sigmaSd"; + repo = "IRust"; + rev = "v${version}"; + sha256 = "sha256-AMOND5q1XzNhN5smVJp+2sGl/OqbxkGPGuPBCE48Hik="; }; - packages.irust = pkgs.rustPlatform.buildRustPackage rec { - pname = "irust"; - version = "1.65.1"; - src = pkgs.fetchFromGitHub { - owner = "sigmaSd"; - repo = "IRust"; - rev = "v${version}"; - sha256 = "sha256-AMOND5q1XzNhN5smVJp+2sGl/OqbxkGPGuPBCE48Hik="; - }; - - doCheck = false; - cargoSha256 = "sha256-A24O3p85mCRVZfDyyjQcQosj/4COGNnqiQK2a7nCP6I="; - }; - } - ); + doCheck = false; + cargoSha256 = "sha256-A24O3p85mCRVZfDyyjQcQosj/4COGNnqiQK2a7nCP6I="; + }; + }); } From afadf1389c15aa588356035c68649e99d7946f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 23 Aug 2023 10:30:39 +0200 Subject: [PATCH 2/5] chore: Fix formatting --- fission-server/src/models/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fission-server/src/models/account.rs b/fission-server/src/models/account.rs index f6cf9f9d..15a5bf2e 100644 --- a/fission-server/src/models/account.rs +++ b/fission-server/src/models/account.rs @@ -298,7 +298,7 @@ impl RootAccount { // QUESTION: How long should these be valid for? This is basically sign-in expiry/duration. .with_lifetime(60 * 60 * 24 * 365) .claiming_capability(&capability) - .with_fact(json!({"username": username})) + .with_fact(json!({ "username": username })) .build()? .sign() .await From fb7c4032f0f51aeed100a73621f22af1388e8017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 23 Aug 2023 10:45:08 +0200 Subject: [PATCH 3/5] chore: Fix clippy warnings --- fission-server/src/routes/doh.rs | 8 ++++---- fission-server/src/routes/health.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/fission-server/src/routes/doh.rs b/fission-server/src/routes/doh.rs index 70311b33..c8b45a49 100644 --- a/fission-server/src/routes/doh.rs +++ b/fission-server/src/routes/doh.rs @@ -198,7 +198,7 @@ mod tests { Method::GET, format!( "/dns-query?name={}&type={}", - format!("_did.{}.fission.app", username), + format_args!("_did.{}.fission.app", username), "txt" ), ) @@ -250,7 +250,7 @@ mod tests { Method::GET, format!( "/dns-query?name={}&type={}", - format!("_dnslink.{}.fission.app", username), + format_args!("_dnslink.{}.fission.app", username), "txt" ), ) @@ -315,7 +315,7 @@ mod tests { Method::GET, format!( "/dns-query?name={}&type={}", - format!("_dnslink.{}.fission.app", username), + format_args!("_dnslink.{}.fission.app", username), "txt" ), ) @@ -380,7 +380,7 @@ mod tests { Method::GET, format!( "/dns-query?name={}&type={}", - format!("_dnslink.{}.fission.app", username), + format_args!("_dnslink.{}.fission.app", username), "txt" ), ) diff --git a/fission-server/src/routes/health.rs b/fission-server/src/routes/health.rs index 86c10686..54d02919 100644 --- a/fission-server/src/routes/health.rs +++ b/fission-server/src/routes/health.rs @@ -106,7 +106,7 @@ mod tests { .await?; assert_eq!(status, StatusCode::OK); - assert_eq!(body.database_connected, true); + assert!(body.database_connected); assert_eq!(body.database_up_to_date, Some(true)); Ok(()) @@ -125,7 +125,7 @@ mod tests { .await?; assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); - assert_eq!(body.database_connected, false); + assert!(!body.database_connected); assert_eq!(body.database_up_to_date, None); Ok(()) @@ -147,7 +147,7 @@ mod tests { .await?; assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); - assert_eq!(body.database_connected, true); + assert!(body.database_connected); assert_eq!(body.database_up_to_date, Some(false)); Ok(()) From a7978563ca9ed20da578d889082cddf403f0b473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 23 Aug 2023 10:51:19 +0200 Subject: [PATCH 4/5] chore: Remove unneeded mut references --- fission-server/src/middleware/logging.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fission-server/src/middleware/logging.rs b/fission-server/src/middleware/logging.rs index ed388a43..9c83d89b 100644 --- a/fission-server/src/middleware/logging.rs +++ b/fission-server/src/middleware/logging.rs @@ -207,7 +207,7 @@ impl ReqwestMiddleware for Logger { } } -fn log_reqwest(request: &reqwest::Request, extensions: &mut Extensions) { +fn log_reqwest(request: &reqwest::Request, extensions: &Extensions) { let user_agent = request .headers() .get(header::USER_AGENT) @@ -263,7 +263,7 @@ fn log_reqwest(request: &reqwest::Request, extensions: &mut Extensions) { async fn log_reqwest_response( response: reqwest::Response, - extensions: &mut Extensions, + extensions: &Extensions, ) -> Result { /// Turn reqwest body, headers, status, and version into /// a generic [`http::Response`] and to capture body + parts, @@ -325,7 +325,7 @@ async fn log_reqwest_response( Ok(post_log_response) } -fn log_reqwest_error(error: &reqwest::Error, extensions: &mut Extensions) -> Result<()> { +fn log_reqwest_error(error: &reqwest::Error, extensions: &Extensions) -> Result<()> { let url = extensions .get::() .ok_or_else(|| anyhow!("failed to find Url extension"))?; @@ -342,7 +342,7 @@ fn log_reqwest_error(error: &reqwest::Error, extensions: &mut Extensions) -> Res Ok(()) } -fn log_middleware_error(error: &anyhow::Error, extensions: &mut Extensions) -> Result<()> { +fn log_middleware_error(error: &anyhow::Error, extensions: &Extensions) -> Result<()> { let url = extensions .get::() .ok_or_else(|| anyhow!("failed to find Url extension"))?; From d43d34db7ea7ae299317e77ed1da455fb9bed060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Thu, 24 Aug 2023 10:27:27 +0200 Subject: [PATCH 5/5] chore: Use git dependencies on rs-ucan --- Cargo.lock | 6 ++---- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da28ab94..e73c99f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5412,8 +5412,7 @@ checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" [[package]] name = "ucan" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0237a17cf06465fce3ab4126c91d0745123b0d21a2ef5adbd3da47d10309455" +source = "git+https://github.com/blaine/rs-ucan?branch=temporary-unscoped-fix#50ce3e44efc5dc5ad1471552a06d4e9341422a9a" dependencies = [ "anyhow", "async-recursion", @@ -5440,8 +5439,7 @@ dependencies = [ [[package]] name = "ucan-key-support" version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f99720d7a9d4b5a7cf3f2a9d17c951bbaa6331f00c07aa8845e80e9fb4c8faf1" +source = "git+https://github.com/blaine/rs-ucan?branch=temporary-unscoped-fix#50ce3e44efc5dc5ad1471552a06d4e9341422a9a" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 3786e74c..f1406fe9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ ed25519-zebra = "3.1" rand = "0.8" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -ucan = { version = "=0.2.0" } -ucan-key-support = { version = "=0.1.3" } +ucan = { git = "https://github.com/blaine/rs-ucan", branch = "temporary-unscoped-fix" } +ucan-key-support = { git = "https://github.com/blaine/rs-ucan", branch = "temporary-unscoped-fix" } url = "2.3" # Speedup build on macOS