From 8cd61cf7d3110fd924da9f3344f51076aabc32e8 Mon Sep 17 00:00:00 2001 From: C0ffeeCode Date: Wed, 1 May 2024 16:22:52 +0200 Subject: [PATCH 1/2] tada --- Cargo.lock | 17 ++++++++--------- Cargo.toml | 2 +- migrations/20240428160456_init.sql | 6 ++++++ src/engines/kv.rs | 24 ++++++++++++------------ src/main.rs | 6 ++---- src/storage.rs | 25 +++++++++++++++++-------- 6 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 migrations/20240428160456_init.sql diff --git a/Cargo.lock b/Cargo.lock index efff153..c7a93d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,9 +260,9 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cc" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" +checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd" [[package]] name = "cfg-if" @@ -585,9 +585,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ "ahash", "allocator-api2", @@ -827,9 +827,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.153" +version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" [[package]] name = "libm" @@ -1449,9 +1449,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", "windows-sys 0.52.0", @@ -1579,7 +1579,6 @@ dependencies = [ "sha2", "sqlx-core", "sqlx-mysql", - "sqlx-postgres", "sqlx-sqlite", "syn 1.0.109", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index ec7b5ad..7634e26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ json-patch = "1.2.0" # utoipa = { version = "4.2.0", features = ["axum_extras"] } sqlx = { version = "0.7.4", features = [ "sqlite", - "postgres", + # "postgres", "any", "macros", "runtime-tokio", diff --git a/migrations/20240428160456_init.sql b/migrations/20240428160456_init.sql new file mode 100644 index 0000000..91d683d --- /dev/null +++ b/migrations/20240428160456_init.sql @@ -0,0 +1,6 @@ +-- Add migration script here + +CREATE TABLE secret_engines ( + mount_point TEXT PRIMARY KEY NOT NULL, + engine_type TEXT NOT NULL +); diff --git a/src/engines/kv.rs b/src/engines/kv.rs index e143650..00a5cfa 100644 --- a/src/engines/kv.rs +++ b/src/engines/kv.rs @@ -9,18 +9,18 @@ pub fn kv_router() -> Router { Router::new() .route("/:mount_path/config", get(get_config)) .route("/:mount_path/config", post(post_config)) - .route("/:mount_path/data/:path", get(get_data)) - .route("/:mount_path/data/:path/", get(get_data)) - .route("/:mount_path/data/:path", get(post_data)) - .route("/:mount_path/data/:path", delete(delete_data)) - .route("/:mount_path/delete/:path", post(delete_path)) - .route("/:mount_path/destroy/:path", post(destroy_path)) - .route("/:mount_path/metadata/:path", get(get_meta)) - .route("/:mount_path/metadata/:path/", get(get_meta)) - .route("/:mount_path/metadata/:path", post(post_meta)) - .route("/:mount_path/metadata/:path", delete(delete_meta)) - .route("/:mount_path/subkeys/:path", get(get_subkeys)) - .route("/:mount_path/undelete/:path", post(post_undelete)) + .route("/:mount_path/data/*path", get(get_data)) + // .route("/:mount_path/data/*path/", get(get_data)) + .route("/:mount_path/data/*path", post(post_data)) + .route("/:mount_path/data/*path", delete(delete_data)) + .route("/:mount_path/delete/*path", post(delete_path)) + .route("/:mount_path/destroy/*path", post(destroy_path)) + .route("/:mount_path/metadata/*path", get(get_meta)) + // .route("/:mount_path/metadata/*path/", get(get_meta)) + .route("/:mount_path/metadata/*path", post(post_meta)) + .route("/:mount_path/metadata/*path", delete(delete_meta)) + .route("/:mount_path/subkeys/*path", get(get_subkeys)) + .route("/:mount_path/undelete/*path", post(post_undelete)) } async fn get_config() -> &'static str { diff --git a/src/main.rs b/src/main.rs index aca393a..617aace 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,8 @@ -use axum::{ - extract::Request, http::StatusCode, routing::get, Router -}; +use axum::{extract::Request, http::StatusCode, routing::get, Router}; use log::*; use sqlx::{Any, Pool}; -use tokio::net::TcpListener; use std::{env, net::SocketAddr, str::FromStr}; +use tokio::net::TcpListener; use crate::storage::create_pool; diff --git a/src/storage.rs b/src/storage.rs index eae773a..123afb8 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,18 +1,27 @@ -use sqlx::{ - any::{install_default_drivers, AnyPoolOptions}, - AnyPool, -}; +use std::{fs::File, path::Path}; -pub async fn create_pool(connection_string: String) -> AnyPool { +use log::*; +use sqlx::{any::AnyPoolOptions, AnyPool}; + +pub async fn create_pool(db_url: String) -> AnyPool { // TODO: Change to solely install applicable driver - install_default_drivers(); + sqlx::any::install_default_drivers(); + + // Create SQLite database file if it does not exist + if db_url.starts_with("sqlite:") { + let path = db_url.replace("sqlite:", ""); + if !Path::new(&path).exists() { + warn!("Sqlite database does not exist, creating file {}", path); + File::create(&path).expect("Failed to create database file"); + } + } let pool = AnyPoolOptions::new() .max_connections(5) .test_before_acquire(true) - .connect(&connection_string) + .connect(&db_url) .await - .expect(&connection_string); + .expect(&db_url); pool } From 8f05131aacdfd6217a78ebf53005578d7686cdde Mon Sep 17 00:00:00 2001 From: C0ffeeCode Date: Wed, 1 May 2024 16:49:25 +0200 Subject: [PATCH 2/2] Fix Containerfile of go tests --- go_client/Containerfile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/go_client/Containerfile b/go_client/Containerfile index 2ec6107..6cbe75f 100644 --- a/go_client/Containerfile +++ b/go_client/Containerfile @@ -5,10 +5,11 @@ WORKDIR /src COPY go.mod go.sum ./ RUN go mod download -COPY *.go ./ -RUN go build -o /app +COPY . . +# RUN go build -o /app +CMD go test tests/* -FROM docker.io/library/alpine:3.19 +# FROM docker.io/library/alpine:3.19 -COPY --from=builder /app /app -CMD ["/app"] +# COPY --from=builder /app /app +# CMD ["/app"]