Skip to content

Commit

Permalink
Merge pull request #32 from filecoin-project/cli/rkh-validation
Browse files Browse the repository at this point in the history
add validate rkh gh handle
  • Loading branch information
kokal33 authored Oct 6, 2023
2 parents 6b757e5 + 5ad84cf commit 74ea0f9
Show file tree
Hide file tree
Showing 40 changed files with 263 additions and 142 deletions.
112 changes: 57 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[workspace]

members = [
"lib",
"http-server",
"fplus",
"database"
"fplus-lib",
"fplus-http-server",
"fplus-cli",
"fplus-database"
]

resolver = "2"
16 changes: 16 additions & 0 deletions fplus-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "fplus-cli"
authors = ["jbesraa"]
version = "0.1.0"
edition = "2021"
description = "FPlus cli tool to validate different states of an application"
license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.4.0"
clap = { version = "4.4.6", features = ["derive"] }
fplus-database = { path = "../fplus-database", version = "0.1.0" }
fplus-lib = { path = "../fplus-lib" , version = "0.1.0" }
mongodb = "2.7.0"
66 changes: 66 additions & 0 deletions fplus-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use clap::{arg, Command};
use validators::{validate_trigger, validate_proposal};

use crate::validators::validate_approval;

pub mod validators;

fn cli() -> Command {
Command::new("filplus")
.about("Fil+ CLI - Management tool for fil+ applications")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("validate-trigger")
.about("Validates triggering an application")
.arg(arg!(<PR_NUMBER> "Pull Request Number"))
.arg(arg!(<RKH_GITHUB_HANDLE> "Github handle of Root Key Holder"))
.arg_required_else_help(true),
)
.subcommand(
Command::new("validate-proposal")
.about("Validates proposing an application")
.arg(arg!(<PR_NUMBER> "Pull Request Number"))
.arg(arg!(<Notary_GITHUB_HANDLE> "Github handle of Notary"))
.arg_required_else_help(true),
)
.subcommand(
Command::new("validate-approval")
.about("Validates approving an application")
.arg(arg!(<PR_NUMBER> "Pull Request Number"))
.arg_required_else_help(true),
)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let matches = cli().get_matches();

Ok(match matches.subcommand() {
Some(("validate-trigger", sub_matches)) => {
let pull_request_number = sub_matches.get_one::<String>("PR_NUMBER").expect("required");
let rkh_gh_handle = sub_matches
.get_one::<String>("RKH_GITHUB_HANDLE")
.expect("required");
validate_trigger(rkh_gh_handle.to_string(), pull_request_number.to_string()).await;
}
Some(("validate-proposal", sub_matches)) => {
let pull_request_number = sub_matches.get_one::<String>("PR_NUMBER").expect("required");
let notary_gh_handle = sub_matches
.get_one::<String>("NOTARY_GITHUB_HANDLE")
.expect("required");
validate_proposal(notary_gh_handle.to_string(), pull_request_number.to_string()).await;
}
Some(("validate-approval", sub_matches)) => {
let pull_request_number = sub_matches.get_one::<String>("PR_NUMBER").expect("required");
let notary_gh_handle = sub_matches
.get_one::<String>("NOTARY_GITHUB_HANDLE")
.expect("required");
validate_approval(notary_gh_handle.to_string(), pull_request_number.to_string()).await;
println!("Validated approval {}", pull_request_number);
}
_ => {
println!("No subcommand was used");
}
})
}
73 changes: 73 additions & 0 deletions fplus-cli/src/validators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use actix_web::web;
use fplus_lib::core::application::ApplicationFile;
use std::sync::Mutex;

pub async fn validate_trigger(github_handle: String, pull_request_number: String) {
if validate_rkh_holder(&github_handle).await {
println!(
"Validated Root Key Holder {} for application {}",
&github_handle, pull_request_number
);
} else {
println!(
"No Root Key Holder found with github handle {}",
github_handle
);
}
}

pub async fn validate_proposal(github_handle: String, pull_request_number: String) {
if validate_notary(&github_handle).await {
println!(
"Validated Notary {} Proposal for application {}",
&github_handle, pull_request_number
);
} else {
println!("No Notary found with github handle {}", github_handle);
}
}

pub async fn validate_approval(github_handle: String, pull_request_number: String) {
if validate_notary(&github_handle).await {
println!(
"Validated Notary {} Approval for application {}",
&github_handle, pull_request_number
);
} else {
println!("No Notary found with github handle {}", github_handle);
}
let application_file: ApplicationFile =
fplus_lib::core::LDNApplication::get_by_pr_number(pull_request_number.parse().unwrap()).await.unwrap();
}

async fn validate_rkh_holder(github_handle: &str) -> bool {
let db_connection: web::Data<Mutex<mongodb::Client>> =
web::Data::new(Mutex::new(fplus_database::core::setup::setup().await.unwrap()));
let rkh_holders = fplus_database::core::collections::rkh::find(db_connection)
.await
.unwrap();
let rkh_holders: Option<fplus_database::core::collections::rkh::RootKeyHolder> = rkh_holders
.into_iter()
.find(|rkh| &rkh.github_handle == github_handle);
if rkh_holders.is_none() {
false
} else {
true
}
}

async fn validate_notary(github_handle: &str) -> bool {
let db_connection: web::Data<Mutex<mongodb::Client>> =
web::Data::new(Mutex::new(fplus_database::core::setup::setup().await.unwrap()));
let notary = fplus_database::core::collections::notary::find(db_connection)
.await
.unwrap();
let notary: Option<fplus_database::core::collections::notary::Notary> = notary
.into_iter()
.find(|n| &n.github_handle == github_handle);
if notary.is_none() {
false
} else {
true
}
}
3 changes: 1 addition & 2 deletions database/Cargo.toml → fplus-database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "database"
name = "fplus-database"
version = "0.1.0"
edition = "2021"

Expand All @@ -8,6 +8,5 @@ edition = "2021"
[dependencies]
actix-web = "4.4.0"
anyhow = "1.0.75"
dotenv = "0.15.0"
mongodb = "2.7.0"
serde = "1.0.188"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use dotenv;
use mongodb::{
bson::doc,
options::{ClientOptions, ServerApi, ServerApiVersion},
Expand All @@ -18,7 +17,7 @@ pub async fn db_health_check(client: Client) -> mongodb::error::Result<()> {

pub async fn setup() -> mongodb::error::Result<Client> {
let key = "MONGODB_URL";
let value = dotenv::var(key).expect("Expected a MONGODB_URL in the environment");
let value = std::env::var(key).expect("Expected a MONGODB_URL in the environment");
let mut client_options = ClientOptions::parse(value).await?;

// Set the server_api field of the client_options object to Stable API version 1
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions http-server/Cargo.toml → fplus-http-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[package]
name = "http-server"
name = "fplus-http-server"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.3.1"
dotenv = "0.15.0"
serde = { version = "1.0.164", features = ["derive", "std",
"serde_derive", "alloc", "rc"] }
tokio = { version = "1.29.1", features = ["full"] }
Expand All @@ -18,8 +17,8 @@ actix-cors = "0.6.4"
reqwest = { version = "0.11.18", features = ["json"] }
futures = "0.3.28"
mongodb = "2.6.1"
lib = { path = "../lib" }
database = { path = "../database" }
fplus-lib = { path = "../fplus-lib", version = "0.1.0" }
fplus-database = { path = "../fplus-database", version = "0.1.0" }
anyhow = "1.0.75"
async-trait = "0.1.73"

2 changes: 1 addition & 1 deletion http-server/src/main.rs → fplus-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) mod router;
#[tokio::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));
let client = match database::core::setup::setup().await {
let client = match fplus_database::core::setup::setup().await {
Ok(client) => client,
Err(e) => panic!("Error setting up database: {}", e),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::{get, post, web, HttpResponse, Responder};
use lib::core::{
use fplus_lib::core::{
CompleteGovernanceReviewInfo, CompleteNewApplicationProposalInfo, CreateApplicationInfo,
LDNApplication,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::{get, web, HttpResponse, Responder};
use lib::external_services::blockchain::BlockchainData;
use fplus_lib::external_services::blockchain::BlockchainData;

/// Address Allowance.
///
Expand Down
Loading

0 comments on commit 74ea0f9

Please sign in to comment.