Brain redirect on app deploy and delete

Improve macro with full crate path for log debug
Simplifies brain URL selection by using lazy static variables for staging and testing environments.
This commit is contained in:
Noor 2025-06-18 19:19:28 +05:30
parent 25eeab6098
commit 9630cd5f95
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
5 changed files with 40 additions and 15 deletions

@ -1,4 +1,4 @@
use crate::constants::{STAGING_BRAIN_URLS, TESTNET_BRAIN_URLS};
use crate::constants::{BRAIN_STAGING, BRAIN_TESTING};
use crate::{general, utils::block_on};
use ed25519_dalek::SigningKey;
use log::{debug, info, warn};
@ -6,7 +6,6 @@ use openssl::bn::BigNum;
use openssl::hash::{Hasher, MessageDigest};
use openssl::pkey::{PKey, Private};
use openssl::rsa::Rsa;
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Write, path::Path};
@ -313,15 +312,13 @@ impl Config {
match Self::init_config().network.as_str() {
"localhost" => ("https://localhost:31337".to_string(), "staging-brain".to_string()),
"staging" => {
let url = STAGING_BRAIN_URLS
[rand::thread_rng().gen_range(0..STAGING_BRAIN_URLS.len())]
.to_string();
let url = BRAIN_STAGING.to_string();
log::info!("Using staging brain URL: {url}");
(url, "staging-brain".to_string())
}
_ => {
let url = TESTNET_BRAIN_URLS
[rand::thread_rng().gen_range(0..TESTNET_BRAIN_URLS.len())]
.to_string();
let url = BRAIN_TESTING.to_string();
log::info!("Using testnet brain URL: {url}");
(url, "testnet-brain".to_string())
}
}

@ -1,3 +1,6 @@
use rand::Rng;
use std::sync::LazyLock;
pub const HRATLS_APP_PORT: u32 = 34500;
pub const MAX_REDIRECTS: u16 = 3;
pub const STAGING_BRAIN_URLS: [&str; 3] = [
@ -11,3 +14,9 @@ pub const TESTNET_BRAIN_URLS: [&str; 3] = [
"https://149.22.95.1:44522", // testnet brain 2
"https://149.36.48.99:48638", // testnet brain 3
];
pub static BRAIN_STAGING: LazyLock<&str> =
LazyLock::new(|| STAGING_BRAIN_URLS[rand::thread_rng().gen_range(0..STAGING_BRAIN_URLS.len())]);
pub static BRAIN_TESTING: LazyLock<&str> =
LazyLock::new(|| TESTNET_BRAIN_URLS[rand::thread_rng().gen_range(0..TESTNET_BRAIN_URLS.len())]);

@ -7,6 +7,7 @@ use detee_shared::sgx::types::brain::AppDeployConfig;
use tokio_stream::StreamExt;
use tonic::transport::Channel;
use crate::call_with_follow_redirect;
use crate::config::Config;
use crate::sgx::utils::calculate_nanolp_for_app;
use crate::utils::{self, sign_request};
@ -25,6 +26,10 @@ pub enum Error {
CorruptedRootCa(#[from] std::io::Error),
#[error("Internal app error: could not parse Brain URL")]
CorruptedBrainUrl,
#[error("Max redirects exceeded: {0}")]
MaxRedirectsExceeded(String),
#[error("Redirect error: {0}")]
RedirectError(String),
}
type Result<T> = std::result::Result<T, Error>;
@ -70,6 +75,10 @@ async fn client() -> Result<BrainAppCliClient<Channel>> {
Ok(BrainAppCliClient::new(Config::connect_brain_channel(default_brain_url).await?))
}
async fn client_from_endpoint(reconnect_endpoint: String) -> Result<BrainAppCliClient<Channel>> {
Ok(BrainAppCliClient::new(Config::connect_brain_channel(reconnect_endpoint).await?))
}
pub async fn new_app(app_deploy_config: AppDeployConfig) -> Result<NewAppRes> {
let resource = app_deploy_config.clone().resource;
let mut req: NewAppReq = app_deploy_config.clone().into();
@ -87,15 +96,21 @@ pub async fn new_app(app_deploy_config: AppDeployConfig) -> Result<NewAppRes> {
req.admin_pubkey = Config::get_detee_wallet()?;
req.hratls_pubkey = Config::get_hratls_pubkey_hex()?;
let res = client().await?.new_app(sign_request(req)?).await?;
Ok(res.into_inner())
let client = client().await?;
match call_with_follow_redirect!(client, req, new_app).await {
Ok(res) => Ok(res.into_inner()),
Err(e) => {
log::error!("Failed to create new app: {}", e);
Err(e.into())
}
}
}
pub async fn delete_app(app_uuid: String) -> Result<()> {
let admin_pubkey = Config::get_detee_wallet()?;
let delete_req = DelAppReq { uuid: app_uuid, admin_pubkey };
let _ = client().await?.delete_app(sign_request(delete_req)?).await?;
let client = client().await?;
let _ = call_with_follow_redirect!(client, delete_req, delete_app).await?;
Ok(())
}

@ -46,7 +46,7 @@ pub async fn connect_app_dtpm_client(app_uuid: &str) -> Result<DtpmConfigManager
let private_key_pem = Config::get_hratls_private_key()?;
let (hratls_uri, package_mr_enclave) = hratls_url_and_mr_enclave_from_app_id(app_uuid).await?;
log::info!("hratls uri: {}\nmr_enclave: {:?}", &hratls_uri, &package_mr_enclave);
log::info!("hratls uri: {} mr_enclave: {:?}", &hratls_uri, &package_mr_enclave);
let hratls_config =
Arc::new(RwLock::new(HRaTlsConfig::new().with_hratls_private_key_pem(private_key_pem)));

@ -54,7 +54,11 @@ macro_rules! call_with_follow_redirect {
let mut client = $client;
for attempt in 0..crate::constants::MAX_REDIRECTS {
debug!("Attempt #{}: Calling method '{}'...", attempt + 1, stringify!($method));
log::debug!(
"Attempt #{}: Calling method '{}'...",
attempt + 1,
stringify!($method)
);
let req_data_clone = $req_data.clone();
let signed_req = crate::utils::sign_request(req_data_clone)?;
@ -76,7 +80,7 @@ macro_rules! call_with_follow_redirect {
)
})?;
info!("Server moved. Redirecting to {}...", redirect_url);
log::info!("Server moved. Redirecting to {}...", redirect_url);
client = client_from_endpoint(format!("https://{}", redirect_url)).await?;
continue;