feat: define and implement(sqlx sqlite) interfaces for CRUD of DNS challenges

This commit is contained in:
Aravinth Manivannan 2022-05-19 14:12:57 +05:30
parent 6cd0313a01
commit b884d361b0
3 changed files with 127 additions and 0 deletions

View file

@ -114,6 +114,18 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
/// ping DB /// ping DB
async fn ping(&self) -> bool; async fn ping(&self) -> bool;
/// check if a DNS challenge exists
async fn dns_challenge_exists(&self, hostname: &str) -> DBResult<bool>;
/// 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<String>;
/// delete DNS challenge
async fn delete_dns_challenge(&self, hostname: &str) -> DBResult<()>;
/// create forge isntance /// create forge isntance
async fn create_forge_isntance(&self, f: &CreateForge) -> DBResult<()>; async fn create_forge_isntance(&self, f: &CreateForge) -> DBResult<()>;

View file

@ -64,6 +64,16 @@
}, },
"query": " DELETE FROM starchart_users WHERE username = $1 AND \n hostname_id = (SELECT ID FROM starchart_forges WHERE hostname = $2)" "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": { "6f5ca3d71a541eb6f33e37a5889c048536ab6ad7e81a6236d73aa71433c13717": {
"describe": { "describe": {
"columns": [], "columns": [],
@ -74,6 +84,24 @@
}, },
"query": "INSERT OR IGNORE INTO starchart_project_topics ( name ) VALUES ( $1 );" "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": { "71079442588dfaece04582acdb14d2c8928c695d4eab5332d09b82cefc880d54": {
"describe": { "describe": {
"columns": [ "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)" "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": { "8c78e074d78291f9d3c4ef3526bae00cb356591edad79a7fb1f20aa7bb681216": {
"describe": { "describe": {
"columns": [], "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))" "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": { "a81dd4b5df666e22fac211092e7b8425d838dd9023aa2b17659352f30831944d": {
"describe": { "describe": {
"columns": [ "columns": [

View file

@ -330,6 +330,65 @@ impl SCDatabase for Database {
.map_err(map_register_err)?; .map_err(map_register_err)?;
Ok(()) Ok(())
} }
async fn dns_challenge_exists(&self, hostname: &str) -> DBResult<bool> {
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<String> {
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 { fn now_unix_time_stamp() -> i64 {