Skip to content

Commit

Permalink
Refactor/rename auth headers
Browse files Browse the repository at this point in the history
  • Loading branch information
lablans committed Dec 3, 2024
1 parent e0ec064 commit 924b7f2
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 39 deletions.
50 changes: 34 additions & 16 deletions src/blaze.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use reqwest::header;
use reqwest::header::HeaderName;
use reqwest::header::HeaderValue;
use reqwest::StatusCode;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use tracing::{debug, warn, info};

use crate::config::FocusBackend;
use crate::errors::FocusError;
use crate::util;
use crate::util::get_json_field;
Expand All @@ -21,28 +25,42 @@ pub struct AstQuery {
pub payload: String,
}

pub async fn check_availability() -> bool {
pub(crate) struct Blaze;

debug!("Checking Blaze availability...");
impl FocusBackend for Blaze {
fn make_authheader(apikey: &str) -> Result<(HeaderName, HeaderValue), FocusError> {
let name = header::AUTHORIZATION;
let value = HeaderValue::from_str(apikey)
.map_err(|e| FocusError::ConfigurationError(format!("Invalid value \"{}\" in apikey for blaze backend: {}", apikey, e)))?;
Ok((name, value))
}

let resp = match CONFIG.client
.get(format!("{}metadata", CONFIG.endpoint_url))
.send()
.await
{
Ok(response) => response,
Err(e) => {
warn!("Error making Blaze request: {:?}", e);
return false;
}
};
async fn check_availability() -> bool {
debug!("Checking Blaze availability...");

if resp.status().is_success() {
return true;
let resp = match CONFIG.client
.get(format!("{}metadata", CONFIG.endpoint_url))
.send()
.await
{
Ok(response) => response,
Err(e) => {
warn!("Error making Blaze request: {:?}", e);
return false;
}
};

if resp.status().is_success() {
return true;
}
false
}
false

// TODO: Refactor other functions into a focus backend following a common trait.
}

// TODO: Respect blaze auth header

pub async fn post_library(library: String) -> Result<(), FocusError> {
debug!("Creating a Library...");

Expand Down
57 changes: 50 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::path::PathBuf;

use beam_lib::AppId;
use clap::Parser;
use reqwest::{header::HeaderValue, Url};
use reqwest::{header::{HeaderName, HeaderValue}, Url};
use once_cell::sync::Lazy;
use reqwest::{Certificate, Client, Proxy};
use tracing::{debug, info, warn};

use crate::errors::FocusError;
use crate::{blaze, errors::FocusError, exporter};

#[derive(clap::ValueEnum, Clone, PartialEq, Debug)]
pub enum Obfuscate {
Expand Down Expand Up @@ -65,7 +65,7 @@ struct CliArgs {
#[clap(long, env, value_parser)]
beam_app_id_long: String,

/// This applications beam API key
/// This application's beam API key
#[clap(long, env, value_parser)]
api_key: String,

Expand Down Expand Up @@ -158,9 +158,18 @@ struct CliArgs {
#[clap(long, env, value_parser)]
provider_icon: Option<String>,

/// Authorization header
//TODO: Support blaze auth
/* /// Blaze Backend: API key for authentication
#[clap(long, env, value_parser)]
auth_header: Option<String>,
backend_blaze_apikey: Option<String>,*/

/// Exporter backend: API key for authentication
#[clap(long, env, value_parser)]
backend_exporter_apikey: Option<String>,

/// AST2SQL transformer backend: API key for authentication
#[clap(long, env, value_parser)]
backend_ast2sql_apikey: Option<String>,

/// Postgres connection string
#[cfg(feature = "query-sql")]
Expand Down Expand Up @@ -197,7 +206,9 @@ pub(crate) struct Config {
pub client: Client,
pub provider: Option<String>,
pub provider_icon: Option<String>,
pub auth_header: Option<String>,
pub backend_blaze_authheader: Option<(HeaderName, HeaderValue)>,
pub backend_exporter_authheader: Option<(HeaderName, HeaderValue)>,
pub backend_ast2sql_authheader: Option<(HeaderName, HeaderValue)>,
#[cfg(feature = "query-sql")]
pub postgres_connection_string: Option<String>,
#[cfg(feature = "query-sql")]
Expand All @@ -219,6 +230,31 @@ impl Config {
let client = prepare_reqwest_client(&tls_ca_certificates)?;
dbg!(cli_args.endpoint_url.clone());
dbg!(cli_args.blaze_url.clone());

let backend_blaze_authheader = {
// if let Some(apikey) = cli_args.backend_blaze_apikey.as_ref() {
// Some(blaze::Blaze::make_authheader(apikey)?)
// } else {
None
// }
};

let backend_exporter_authheader = {
if let Some(apikey) = cli_args.backend_exporter_apikey.as_ref() {
Some(exporter::Exporter::make_authheader(apikey)?)
} else {
None
}
};

let backend_ast2sql_authheader = {
if let Some(apikey) = cli_args.backend_ast2sql_apikey.as_ref() {
Some(exporter::Exporter::make_authheader(apikey)?)
} else {
None
}
};

let config = Config {
beam_proxy_url: cli_args.beam_proxy_url,
beam_app_id_long: AppId::new_unchecked(cli_args.beam_app_id_long),
Expand All @@ -242,7 +278,9 @@ impl Config {
queries_to_cache: cli_args.queries_to_cache,
provider: cli_args.provider,
provider_icon: cli_args.provider_icon,
auth_header: cli_args.auth_header,
backend_blaze_authheader,
backend_exporter_authheader,
backend_ast2sql_authheader,
#[cfg(feature = "query-sql")]
postgres_connection_string: cli_args.postgres_connection_string,
#[cfg(feature = "query-sql")]
Expand Down Expand Up @@ -313,3 +351,8 @@ pub fn prepare_reqwest_client(certs: &Vec<Certificate>) -> Result<reqwest::Clien
.build()
.map_err(|e| FocusError::ConfigurationError(format!("Cannot create http client: {}", e)))
}

pub(crate) trait FocusBackend {
fn make_authheader(apikey: &str) -> Result<(HeaderName, HeaderValue), FocusError>;
async fn check_availability() -> bool;
}
30 changes: 22 additions & 8 deletions src/exporter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use reqwest::{header::{self, HeaderMap, HeaderValue}, StatusCode};
use reqwest::{header::{self, HeaderMap, HeaderName, HeaderValue}, StatusCode};
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::str;
use tracing::{debug, warn};

use crate::config::CONFIG;
use crate::config::{FocusBackend, CONFIG};
use crate::errors::FocusError;
use crate::util;

Expand Down Expand Up @@ -35,19 +35,33 @@ const EXECUTE: Params = Params {
done: "executed",
};

pub(crate) struct Exporter;

impl FocusBackend for Exporter {
fn make_authheader(apikey: &str) -> Result<(HeaderName, HeaderValue), FocusError> {
let name = HeaderName::from_static("x-api-key");
let value = HeaderValue::from_str(apikey)
.map_err(|e| FocusError::ConfigurationError(format!("Invalid value \"{}\" in apikey for exporter backend: {}", apikey, e)))?;
Ok((name, value))
}

async fn check_availability() -> bool {
// TODO: Implement
true
}

// TODO: Refactor other functions into a focus backend following a common trait.
}

pub async fn post_exporter_query(body: &String, task_type: TaskType) -> Result<String, FocusError> {
let Some(exporter_url) = &CONFIG.exporter_url else {
return Err(FocusError::MissingExporterEndpoint);
};

let mut headers = HeaderMap::new();

if let Some(auth_header_value) = CONFIG.auth_header.clone() {
headers.insert(
"x-api-key",
HeaderValue::from_str(auth_header_value.as_str())
.map_err(FocusError::InvalidHeaderValue)?,
);
if let Some(authheader) = CONFIG.backend_exporter_authheader.clone() {
headers.insert(authheader.0, authheader.1);
}

if task_type == TaskType::Status {
Expand Down
26 changes: 19 additions & 7 deletions src/intermediate_rep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Deserialize;
use serde::Serialize;
use tracing::{debug, warn};

use crate::ast;
use crate::{ast, config::FocusBackend};
use crate::config::CONFIG;
use crate::errors::FocusError;

Expand All @@ -13,6 +13,22 @@ pub struct IntermediateRepQuery {
pub query: String,
}

pub(crate) struct IntermediateRep;

impl FocusBackend for IntermediateRep {
fn make_authheader(apikey: &str) -> Result<(header::HeaderName, HeaderValue), FocusError> {
let name = header::AUTHORIZATION;
let value = HeaderValue::from_str(apikey)
.map_err(|e| FocusError::ConfigurationError(format!("Invalid value \"{}\" in apikey for ast2sql backend: {}", apikey, e)))?;
Ok((name, value))
}

async fn check_availability() -> bool {
// TODO: Implement
true
}
}

pub async fn post_ast(ast: ast::Ast) -> Result<String, FocusError> {
debug!("Posting AST...");

Expand All @@ -26,12 +42,8 @@ pub async fn post_ast(ast: ast::Ast) -> Result<String, FocusError> {
HeaderValue::from_static("application/json"),
);

if let Some(auth_header_value) = CONFIG.auth_header.clone() {
headers.insert(
header::AUTHORIZATION,
HeaderValue::from_str(auth_header_value.as_str())
.map_err(FocusError::InvalidHeaderValue)?,
);
if let Some(auth_header) = CONFIG.backend_ast2sql_authheader.clone() {
headers.insert(auth_header.0, auth_header.1);
}

let resp = CONFIG
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod db;
use base64::engine::general_purpose;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use beam_lib::{TaskRequest, TaskResult};
use config::FocusBackend;
use futures_util::future::BoxFuture;
use futures_util::FutureExt;
use laplace_rs::ObfCache;
Expand Down Expand Up @@ -159,7 +160,7 @@ async fn main_loop() -> ExitCode {
},
};
let endpoint_service_available: fn() -> BoxFuture<'static, bool> = match CONFIG.endpoint_type {
EndpointType::Blaze => || blaze::check_availability().boxed(),
EndpointType::Blaze => || blaze::Blaze::check_availability().boxed(),
EndpointType::Omop => || async { true }.boxed(), // TODO health check
#[cfg(feature = "query-sql")]
EndpointType::BlazeAndSql => || blaze::check_availability().boxed(),
Expand Down

0 comments on commit 924b7f2

Please sign in to comment.