From b884d361b097578c7a221681978df47142954c52 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 19 May 2022 14:12:57 +0530 Subject: [PATCH] feat: define and implement(sqlx sqlite) interfaces for CRUD of DNS challenges --- db/db-core/src/lib.rs | 12 +++++++ db/db-sqlx-sqlite/sqlx-data.json | 56 ++++++++++++++++++++++++++++++ db/db-sqlx-sqlite/src/lib.rs | 59 ++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index 3ac9a99..4b664e9 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -114,6 +114,18 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// ping DB async fn ping(&self) -> bool; + /// check if a DNS challenge exists + async fn dns_challenge_exists(&self, hostname: &str) -> DBResult; + + /// create DNS challenge + async fn create_dns_challenge(&self, hostname: &str, challenge: &str) -> DBResult<()>; + + /// get DNS challenge + async fn get_dns_challenge_solution(&self, hostname: &str) -> DBResult; + + /// delete DNS challenge + async fn delete_dns_challenge(&self, hostname: &str) -> DBResult<()>; + /// create forge isntance async fn create_forge_isntance(&self, f: &CreateForge) -> DBResult<()>; diff --git a/db/db-sqlx-sqlite/sqlx-data.json b/db/db-sqlx-sqlite/sqlx-data.json index 0fa3741..9342be6 100644 --- a/db/db-sqlx-sqlite/sqlx-data.json +++ b/db/db-sqlx-sqlite/sqlx-data.json @@ -64,6 +64,16 @@ }, "query": " DELETE FROM starchart_users WHERE username = $1 AND \n hostname_id = (SELECT ID FROM starchart_forges WHERE hostname = $2)" }, + "645e4910364f2593792c43948c0a3dc29ccae3e7906dee0e1c416d3109bde3d3": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 3 + } + }, + "query": "INSERT INTO\n starchart_dns_challenges (hostname, challenge, created ) \n VALUES ($1, $2, $3);" + }, "6f5ca3d71a541eb6f33e37a5889c048536ab6ad7e81a6236d73aa71433c13717": { "describe": { "columns": [], @@ -74,6 +84,24 @@ }, "query": "INSERT OR IGNORE INTO starchart_project_topics ( name ) VALUES ( $1 );" }, + "70cc631c5a46082a3ee9beaca94efed4fd55669a511f81480a7204c1de2886db": { + "describe": { + "columns": [ + { + "name": "ID", + "ordinal": 0, + "type_info": "Int64" + } + ], + "nullable": [ + false + ], + "parameters": { + "Right": 1 + } + }, + "query": "SELECT ID FROM starchart_dns_challenges WHERE hostname = $1" + }, "71079442588dfaece04582acdb14d2c8928c695d4eab5332d09b82cefc880d54": { "describe": { "columns": [ @@ -92,6 +120,24 @@ }, "query": "SELECT ID FROM starchart_repositories\n WHERE \n name = $1\n AND\n owner_id = ( SELECT ID FROM starchart_users WHERE username = $2)\n AND\n hostname_id = (SELECT ID FROM starchart_forges WHERE hostname = $3)" }, + "75b058c72afe36c68ae85235e98b098bb5e1a9e7e87bcb7d8d7840a7b235ff23": { + "describe": { + "columns": [ + { + "name": "challenge", + "ordinal": 0, + "type_info": "Text" + } + ], + "nullable": [ + false + ], + "parameters": { + "Right": 1 + } + }, + "query": "SELECT challenge FROM starchart_dns_challenges WHERE hostname = $1" + }, "8c78e074d78291f9d3c4ef3526bae00cb356591edad79a7fb1f20aa7bb681216": { "describe": { "columns": [], @@ -102,6 +148,16 @@ }, "query": "INSERT INTO\n starchart_forges (hostname, verified_on, forge_type ) \n VALUES ($1, $2, (SELECT ID FROM starchart_forge_type WHERE name = $3))" }, + "a048298cb2223f3eb300b1998dbf6a2a2758e3eb0464330e448dc8d53b9a1644": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 1 + } + }, + "query": "DELETE FROM starchart_dns_challenges WHERE hostname = $1" + }, "a81dd4b5df666e22fac211092e7b8425d838dd9023aa2b17659352f30831944d": { "describe": { "columns": [ diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index b74aedb..b832de5 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -330,6 +330,65 @@ impl SCDatabase for Database { .map_err(map_register_err)?; Ok(()) } + + async fn dns_challenge_exists(&self, hostname: &str) -> DBResult { + match sqlx::query!( + "SELECT ID FROM starchart_dns_challenges WHERE hostname = $1", + hostname + ) + .fetch_one(&self.pool) + .await + { + Ok(_) => Ok(true), + Err(Error::RowNotFound) => Ok(false), + Err(e) => Err(DBError::DBError(Box::new(e).into())), + } + } + + async fn get_dns_challenge_solution(&self, hostname: &str) -> DBResult { + struct Challenge { + challenge: String, + } + + let res = sqlx::query_as!( + Challenge, + "SELECT challenge FROM starchart_dns_challenges WHERE hostname = $1", + hostname + ) + .fetch_one(&self.pool) + .await + .map_err(|e| DBError::DBError(Box::new(e)))?; + Ok(res.challenge) + } + + async fn delete_dns_challenge(&self, hostname: &str) -> DBResult<()> { + sqlx::query!( + "DELETE FROM starchart_dns_challenges WHERE hostname = $1", + hostname + ) + .execute(&self.pool) + .await + .map_err(map_register_err)?; + Ok(()) + } + + /// create DNS challenge + async fn create_dns_challenge(&self, hostname: &str, challenge: &str) -> DBResult<()> { + let now = now_unix_time_stamp(); + sqlx::query!( + "INSERT INTO + starchart_dns_challenges (hostname, challenge, created ) + VALUES ($1, $2, $3);", + hostname, + challenge, + now, + ) + .execute(&self.pool) + .await + .map_err(map_register_err)?; + + Ok(()) + } } fn now_unix_time_stamp() -> i64 {