Skip to content

Commit

Permalink
replace hpos-api call with direct hbs call
Browse files Browse the repository at this point in the history
  • Loading branch information
JettTech committed Aug 3, 2024
1 parent 6a3df55 commit 6519a2d
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 35 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/configure-holochain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ reqwest = { workspace = true }
test-case = "2.2.2"
serial_test = { version = "1.0.0", features = ["async"] }
holochain_env_setup = { path = "../holochain_env_setup" }
dotenv = "0.15.0"
env_logger = "0.10.0"
144 changes: 111 additions & 33 deletions crates/configure-holochain/src/jurisdictions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use holochain_types::{
dna::AgentPubKey,
prelude::{FunctionName, ZomeName},
Expand All @@ -7,39 +7,18 @@ use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, holo_config::Config,
};
use serde::{Deserialize, Serialize};
use std::process::{Command, Output};
use tracing::debug;

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct HostingCriteria {
id: String,
jurisdiction: String,
kyc: String,
}

pub async fn get_jurisdiction() -> Result<String> {
let client = reqwest::Client::new();
let response = client
.get("http://localhost/api/v2/host/hosting_criteria")
.send()
.await?;
let text = response.text().await.unwrap();
println!(" >>> text {:?}", text);
let hosting_criteria: HostingCriteria = serde_json::from_str(&text)?;
debug!("hosting_criteria result {:?}", hosting_criteria);

let output: Output = Command::new("hpos-holochain-client")
.args(["--url=http://localhost/api/v2/", "hosting-criteria"])
.output()?;
use tracing::{debug, error, trace, warn};

let output_str = String::from_utf8_lossy(&output.stdout).to_string();
debug!("called hpos api and got result {}", output_str);

let hosting_criteria: HostingCriteria = serde_json::from_str(&output_str)?;
debug!("hosting_criteria result {:?}", hosting_criteria);

Ok(hosting_criteria.jurisdiction)
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RegistrationRecord {
pub id: String,
email: String,
access_token: String,
permissions: Vec<String>,
pub kyc: String,
pub jurisdiction: String,
public_key: String,
}

pub async fn update_jurisdiction_if_changed(
Expand Down Expand Up @@ -83,3 +62,102 @@ pub async fn update_jurisdiction_if_changed(

Ok(())
}

#[derive(Debug, Clone)]
pub struct HbsClient {
pub client: reqwest::Client,
jwt: String,
url: String,
}
impl HbsClient {
pub async fn connect() -> Result<Self> {
let client = reqwest::Client::builder().build()?;
let hbs_id: String =
std::env::var("HBS_AUTH_ID").expect("HBS_AUTH_ID must be set in the env");
let hbs_secret: String =
std::env::var("HBS_AUTH_SECRET").expect("HBS_AUTH_SECRET must be set in the env");
let url = std::env::var("HBS_URL").context("Failed to read HBS_URL. Is it set in env?")?;
let jwt = match Self::get_auth_token(&url, &client).await {
Ok(jwt) => jwt,
Err(err) => {
error!("HbsClient::Failed to fetch JWT token from HBS. Using HBS_AUTH_ID: {:?} and HBS_AUTH_SECRET: {:?}.", hbs_id, hbs_secret);
return Err(err);
}
};
Ok(Self { client, jwt, url })
}

pub async fn get_auth_token(hbs_url: &str, client: &reqwest::Client) -> Result<String> {
let params = [
("id", std::env::var("HBS_AUTH_ID")?),
("secret", std::env::var("HBS_AUTH_SECRET")?),
];

let request = client
.request(
reqwest::Method::GET,
format!("{}/auth/api/v1/service-account", hbs_url),
)
.query(&params);

match request.send().await {
Ok(res) => {
debug!(
"HbsClient::Received `service-account` response status: {}",
res.status()
);
let res = res.error_for_status()?;
let jwt = res.text().await?;
Ok(jwt)
}
Err(err) => {
warn!(
"HbsClient::Call to `service-account` failed. Error: {:?}",
err
);
Err(err.into())
}
}
}

/// Handles get request to HBS server under `/registration/api/v3/my-registration` path
/// Returns only the host's jurisdiction
pub async fn get_registration_details() -> Result<String> {
trace!("HbsClient::Getting registration details for Host");
let connection = Self::connect().await?;

let mut headers = reqwest::header::HeaderMap::new();
headers.insert("Content-Type", "application/json".parse()?);
let request = connection
.client
.request(
reqwest::Method::GET,
format!("{}/registration/api/v3/my-registration", connection.url),
)
.bearer_auth(connection.jwt.clone())
.headers(headers);

let response = request.send().await?;
let response = response.error_for_status()?;
let record: RegistrationRecord = response
.json()
.await
.context("Failed to parse response body")?;

trace!("HbsClient::Registration record results: {:?}", record);

Ok(record.jurisdiction)
}
}

#[tokio::test]
async fn get_host_registration_details_call() {
env_logger::init();
use dotenv::dotenv;
dotenv().ok();

std::env::set_var("HBS_URL", "https://hbs.dev.holotest.net".to_string());
let res = HbsClient::get_registration_details().await.unwrap();

println!(" >> HbsClient::Registration record results: {:?}", res);
}
7 changes: 5 additions & 2 deletions crates/configure-holochain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ pub use hpos_hc_connect::{
use hpos_hc_connect::{hpos_agent::Agent, hpos_membrane_proof};
use std::sync::Arc;
use tracing::{debug, info, instrument, warn};
pub mod jurisdictions;

mod utils;

pub mod jurisdictions;
use jurisdictions::HbsClient;

#[instrument(err, skip(config))]
pub async fn run(config: Config) -> Result<()> {
debug!("Starting configure holochain...");
Expand Down Expand Up @@ -128,7 +131,7 @@ pub async fn update_host_jurisdiction_if_changed(config: &Config) -> Result<()>
}

// get current jurisdiction in hbs
let hbs_jurisdiction = match jurisdictions::get_jurisdiction().await {
let hbs_jurisdiction = match HbsClient::get_registration_details().await {
Ok(hbs_jurisdiction) => hbs_jurisdiction,
Err(e) => {
debug!("Failed to get jurisdiction from hbs {}", e);
Expand Down

0 comments on commit 6519a2d

Please sign in to comment.