feat: save hostname when creating challenges

This commit is contained in:
Aravinth Manivannan 2022-05-19 19:14:31 +05:30
parent 0a4b5dd62c
commit 60f76b63d4
4 changed files with 80 additions and 62 deletions

View file

@ -107,6 +107,17 @@ pub struct AddRepository<'a> {
pub website: Option<&'a str>, pub website: Option<&'a str>,
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// represents a DNS challenge
pub struct Challenge {
/// hostname of the forge instance
pub hostname: String,
/// key of TXT record
pub key: String,
/// value of TXT record
pub value: String,
}
#[async_trait] #[async_trait]
/// Starchart's database requirements. To implement support for $Database, kindly implement this /// Starchart's database requirements. To implement support for $Database, kindly implement this
/// trait. /// trait.
@ -115,16 +126,16 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
async fn ping(&self) -> bool; async fn ping(&self) -> bool;
/// check if a DNS challenge exists /// check if a DNS challenge exists
async fn dns_challenge_exists(&self, hostname: &str) -> DBResult<bool>; async fn dns_challenge_exists(&self, key: &str) -> DBResult<bool>;
/// create DNS challenge /// create DNS challenge
async fn create_dns_challenge(&self, hostname: &str, challenge: &str) -> DBResult<()>; async fn create_dns_challenge(&self, challenge: &Challenge) -> DBResult<()>;
/// get DNS challenge /// get DNS challenge
async fn get_dns_challenge_solution(&self, hostname: &str) -> DBResult<String>; async fn get_dns_challenge(&self, key: &str) -> DBResult<Challenge>;
/// delete DNS challenge /// delete DNS challenge
async fn delete_dns_challenge(&self, hostname: &str) -> DBResult<()>; async fn delete_dns_challenge(&self, key: &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

@ -15,7 +15,8 @@ CREATE TABLE IF NOT EXISTS starchart_forges (
CREATE TABLE IF NOT EXISTS starchart_dns_challenges ( CREATE TABLE IF NOT EXISTS starchart_dns_challenges (
hostname TEXT NOT NULL UNIQUE, hostname TEXT NOT NULL UNIQUE,
challenge TEXT NOT NULL UNIQUE, key TEXT NOT NULL UNIQUE,
value TEXT NOT NULL UNIQUE,
created INTEGER NOT NULL, created INTEGER NOT NULL,
ID INTEGER PRIMARY KEY NOT NULL ID INTEGER PRIMARY KEY NOT NULL
); );

View file

@ -64,16 +64,6 @@
}, },
"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": [],
@ -84,24 +74,6 @@
}, },
"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": [
@ -120,23 +92,45 @@
}, },
"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": { "76f49b3e5e0c6d16daeb09afca427cbf29cd477bd647fee04c2067f7f898721a": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 1
}
},
"query": "DELETE FROM starchart_dns_challenges WHERE key = $1"
},
"891a09656a04fdabfbf51b15204e224221cea8d30782170d3d8503c9275b3d58": {
"describe": { "describe": {
"columns": [ "columns": [
{ {
"name": "challenge", "name": "key",
"ordinal": 0, "ordinal": 0,
"type_info": "Text" "type_info": "Text"
},
{
"name": "value",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "hostname",
"ordinal": 2,
"type_info": "Text"
} }
], ],
"nullable": [ "nullable": [
false,
false,
false false
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 1
} }
}, },
"query": "SELECT challenge FROM starchart_dns_challenges WHERE hostname = $1" "query": "SELECT key, value, hostname FROM starchart_dns_challenges WHERE key = $1"
}, },
"8c78e074d78291f9d3c4ef3526bae00cb356591edad79a7fb1f20aa7bb681216": { "8c78e074d78291f9d3c4ef3526bae00cb356591edad79a7fb1f20aa7bb681216": {
"describe": { "describe": {
@ -148,15 +142,33 @@
}, },
"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": { "a75419ce6d1248944a7bdd63ddadd6f1017a27b8490fb2746a0f7a5d35ce889a": {
"describe": { "describe": {
"columns": [], "columns": [],
"nullable": [], "nullable": [],
"parameters": {
"Right": 4
}
},
"query": "INSERT INTO\n starchart_dns_challenges (hostname, value, key, created ) \n VALUES ($1, $2, $3, $4);"
},
"a77477b2f3c383c2c3e849e8aef47dab411f6c8f9cfe5cb6f850e28314eb1a47": {
"describe": {
"columns": [
{
"name": "ID",
"ordinal": 0,
"type_info": "Int64"
}
],
"nullable": [
false
],
"parameters": { "parameters": {
"Right": 1 "Right": 1
} }
}, },
"query": "DELETE FROM starchart_dns_challenges WHERE hostname = $1" "query": "SELECT ID FROM starchart_dns_challenges WHERE key = $1"
}, },
"a81dd4b5df666e22fac211092e7b8425d838dd9023aa2b17659352f30831944d": { "a81dd4b5df666e22fac211092e7b8425d838dd9023aa2b17659352f30831944d": {
"describe": { "describe": {

View file

@ -331,10 +331,10 @@ impl SCDatabase for Database {
Ok(()) Ok(())
} }
async fn dns_challenge_exists(&self, hostname: &str) -> DBResult<bool> { async fn dns_challenge_exists(&self, key: &str) -> DBResult<bool> {
match sqlx::query!( match sqlx::query!(
"SELECT ID FROM starchart_dns_challenges WHERE hostname = $1", "SELECT ID FROM starchart_dns_challenges WHERE key = $1",
hostname key
) )
.fetch_one(&self.pool) .fetch_one(&self.pool)
.await .await
@ -345,27 +345,20 @@ impl SCDatabase for Database {
} }
} }
async fn get_dns_challenge_solution(&self, hostname: &str) -> DBResult<String> { async fn get_dns_challenge(&self, key: &str) -> DBResult<Challenge> {
struct Challenge {
challenge: String,
}
let res = sqlx::query_as!( let res = sqlx::query_as!(
Challenge, Challenge,
"SELECT challenge FROM starchart_dns_challenges WHERE hostname = $1", "SELECT key, value, hostname FROM starchart_dns_challenges WHERE key = $1",
hostname key
) )
.fetch_one(&self.pool) .fetch_one(&self.pool)
.await .await
.map_err(|e| DBError::DBError(Box::new(e)))?; .map_err(|e| DBError::DBError(Box::new(e)))?;
Ok(res.challenge) Ok(res)
} }
async fn delete_dns_challenge(&self, hostname: &str) -> DBResult<()> { async fn delete_dns_challenge(&self, key: &str) -> DBResult<()> {
sqlx::query!( sqlx::query!("DELETE FROM starchart_dns_challenges WHERE key = $1", key)
"DELETE FROM starchart_dns_challenges WHERE hostname = $1",
hostname
)
.execute(&self.pool) .execute(&self.pool)
.await .await
.map_err(map_register_err)?; .map_err(map_register_err)?;
@ -373,14 +366,15 @@ impl SCDatabase for Database {
} }
/// create DNS challenge /// create DNS challenge
async fn create_dns_challenge(&self, hostname: &str, challenge: &str) -> DBResult<()> { async fn create_dns_challenge(&self, challenge: &Challenge) -> DBResult<()> {
let now = now_unix_time_stamp(); let now = now_unix_time_stamp();
sqlx::query!( sqlx::query!(
"INSERT INTO "INSERT INTO
starchart_dns_challenges (hostname, challenge, created ) starchart_dns_challenges (hostname, value, key, created )
VALUES ($1, $2, $3);", VALUES ($1, $2, $3, $4);",
hostname, challenge.hostname,
challenge, challenge.value,
challenge.key,
now, now,
) )
.execute(&self.pool) .execute(&self.pool)