Skip to content

Commit

Permalink
feat: add backend shared crate to orb-attest (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKaravaev authored Feb 26, 2025
1 parent 32aaa2a commit 26d5b13
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion attest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ futures.workspace = true
orb-attest-dbus.workspace = true
orb-build-info.workspace = true
orb-const-concat.workspace = true
orb-endpoints.workspace = true
orb-info.workspace = true
orb-security-utils = { workspace = true, features = ["reqwest"] }
orb-telemetry.workspace = true
reqwest = { workspace = true, features = ["json", "multipart"] }
ring.workspace = true
secrecy = { workspace = true, features = ["serde"] }
serde.workspace = true
serde_json.workspace = true
serde_with = { version = "3.2", features= ["base64"] }
serde.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing-journald.workspace = true
Expand Down
91 changes: 39 additions & 52 deletions attest/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::env;

use eyre::{self, bail};

const ORB_BACKEND_ENV_VAR_NAME: &str = "ORB_BACKEND";
use orb_endpoints::{v1, Backend};
use orb_info::OrbId;
use std::str::FromStr;

pub struct Config {
pub auth_url: url::Url,
Expand All @@ -13,80 +12,68 @@ impl Config {
/// Create a new config for the given `backend` and `orb_id`.
///
/// # Panics
/// - If failed to parse the `auth_url` or `ping_url`
/// - If failed to parse the `orb_id`
#[must_use]
pub fn new(backend: Backend, orb_id: &str) -> Self {
let (auth, ping) = match backend {
Backend::Prod => ("auth.orb", "management.orb"),
Backend::Staging => ("auth.stage.orb", "management.stage.orb"),
};
// Parse the orb_id string into an OrbId
let orb_id = OrbId::from_str(orb_id).expect("Invalid orb_id format");

let endpoints = v1::Endpoints::new(backend, &orb_id);

Config {
auth_url: url::Url::parse(&format!("https://{auth}.worldcoin.org/api/v1/"))
.unwrap(),
ping_url: url::Url::parse(&format!(
"https://{ping}.worldcoin.org/api/v1/orbs/{orb_id}"
))
.unwrap(),
auth_url: endpoints.auth,
ping_url: endpoints.ping,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Backend {
Prod,
Staging,
}

const DEFAULT_BACKEND: Backend = Backend::Prod;

impl Default for Backend {
fn default() -> Self {
Self::new()
}
#[must_use]
pub fn default_backend() -> Backend {
get_backend().unwrap_or(DEFAULT_BACKEND)
}

impl Backend {
/// Create a new backend config instance
#[must_use]
pub fn new() -> Self {
Self::from_env().unwrap_or(DEFAULT_BACKEND)
}

/// Choose the backend based on the `ORB_BACKEND` environment variable.
fn from_env() -> eyre::Result<Self> {
let v = env::var(ORB_BACKEND_ENV_VAR_NAME)?;
match v.trim().to_lowercase().as_str() {
"prod" => Ok(Backend::Prod),
"stage" | "dev" => Ok(Backend::Staging),
_ => {
bail!("unknown value for backend");
}
fn get_backend() -> eyre::Result<Backend> {
match orb_endpoints::Backend::from_env() {
Ok(backend) => Ok(backend),
Err(orb_endpoints::backend::BackendFromEnvError::NotSet) => {
// Default to prod if not set
Ok(DEFAULT_BACKEND)
}
Err(orb_endpoints::backend::BackendFromEnvError::Invalid(_)) => {
bail!("unknown value for backend");
}
}
}

#[cfg(test)]
mod test {
use orb_endpoints::Backend;
use serial_test::serial;

#[test]
#[serial]
fn default_backend() {
assert_eq!(super::Backend::new(), super::DEFAULT_BACKEND);
assert_eq!(super::Backend::default(), super::DEFAULT_BACKEND);
assert_eq!(super::default_backend(), super::DEFAULT_BACKEND);
}

#[test]
#[serial]
fn custom_backend() {
std::env::set_var(super::ORB_BACKEND_ENV_VAR_NAME, "prod");
assert_eq!(super::Backend::new(), super::Backend::Prod);
std::env::set_var(super::ORB_BACKEND_ENV_VAR_NAME, "stage");
assert_eq!(super::Backend::new(), super::Backend::Staging);
std::env::set_var(super::ORB_BACKEND_ENV_VAR_NAME, "dev");
assert_eq!(super::Backend::new(), super::Backend::Staging);
std::env::set_var(super::ORB_BACKEND_ENV_VAR_NAME, "SOME RANDOM STRING");
assert_eq!(super::Backend::new(), super::DEFAULT_BACKEND);
std::env::remove_var(super::ORB_BACKEND_ENV_VAR_NAME);
std::env::set_var(orb_endpoints::backend::ORB_BACKEND_ENV_VAR_NAME, "prod");
assert_eq!(super::default_backend(), Backend::Prod);
std::env::set_var(orb_endpoints::backend::ORB_BACKEND_ENV_VAR_NAME, "stage");
assert_eq!(super::default_backend(), Backend::Staging);
std::env::set_var(orb_endpoints::backend::ORB_BACKEND_ENV_VAR_NAME, "dev");
assert_eq!(super::default_backend(), Backend::Staging);
std::env::set_var(orb_endpoints::backend::ORB_BACKEND_ENV_VAR_NAME, "analysis");
assert_eq!(super::default_backend(), Backend::Analysis);
std::env::set_var(
orb_endpoints::backend::ORB_BACKEND_ENV_VAR_NAME,
"SOME RANDOM STRING",
);
assert_eq!(super::default_backend(), super::DEFAULT_BACKEND);
std::env::remove_var(orb_endpoints::backend::ORB_BACKEND_ENV_VAR_NAME);
}
}
2 changes: 1 addition & 1 deletion attest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn main() -> eyre::Result<()> {

let orb_id =
std::env::var("ORB_ID").wrap_err("env variable `ORB_ID` should be set")?;
let config = config::Config::new(config::Backend::new(), &orb_id);
let config = config::Config::new(config::default_backend(), &orb_id);

let force_refresh_token = Arc::new(Notify::new());

Expand Down

0 comments on commit 26d5b13

Please sign in to comment.