Skip to content

Commit

Permalink
chore: make it running without GCP env
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasburtey committed Mar 1, 2025
1 parent 7d0859d commit 2d20fae
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
2 changes: 2 additions & 0 deletions lana/app/src/report/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum ReportError {
JobError(#[from] crate::job::error::JobError),
#[error("ReportError - StorageError: {0}")]
StorageError(#[from] crate::storage::StorageError),
#[error("ReportError - ServiceAccountError: {0}")]
ServiceAccountError(#[from] crate::service_account::error::ServiceAccountError),
}

es_entity::from_es_entity_error!(ReportError);
12 changes: 6 additions & 6 deletions lana/app/src/report/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ pub(super) mod bq {
pub(super) async fn find_report_outputs(
config: &ReportConfig,
) -> Result<Vec<String>, ReportError> {
let client =
Client::from_service_account_key(config.service_account().service_account_key(), false)
.await?;
let sa_key = config.service_account().service_account_key()?;

let client = Client::from_service_account_key(sa_key, false).await?;
let tables = client
.table()
.list(
Expand All @@ -129,9 +129,9 @@ pub(super) mod bq {
config: &ReportConfig,
report: &str,
) -> Result<Vec<QueryRow>, ReportError> {
let client =
Client::from_service_account_key(config.service_account().service_account_key(), false)
.await?;
let sa_key = config.service_account().service_account_key()?;

let client = Client::from_service_account_key(sa_key, false).await?;
let gcp_project = &config.service_account().gcp_project;
let query = format!(
"SELECT * FROM `{}.{}.{}`",
Expand Down
2 changes: 2 additions & 0 deletions lana/app/src/service_account/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub enum ServiceAccountError {
Deserialization(#[from] serde_json::Error),
#[error("ServiceAccountError - Base64Decode: {0}")]
Base64Decode(#[from] base64::DecodeError),
#[error("ServiceAccountError - CredentialsNotProvided")]
CredentialsNotProvided,
}
38 changes: 26 additions & 12 deletions lana/app/src/service_account/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod error;
pub mod error;

use error::ServiceAccountError;
use gcp_bigquery_client::yup_oauth2::ServiceAccountKey;
Expand All @@ -9,7 +9,7 @@ pub struct ServiceAccountConfig {
#[serde(skip)]
pub gcp_project: String,
#[serde(skip)]
pub sa_creds_base64: String,
pub sa_creds_base64: Option<String>,
#[serde(skip)]
service_account_key: Option<ServiceAccountKey>,

Expand All @@ -21,7 +21,7 @@ impl Default for ServiceAccountConfig {
fn default() -> Self {
Self {
gcp_project: "".to_string(),
sa_creds_base64: "".to_string(),
sa_creds_base64: None,
service_account_key: None,
gcp_location: default_gcp_location(),
}
Expand All @@ -31,8 +31,14 @@ impl Default for ServiceAccountConfig {
impl ServiceAccountConfig {
pub fn set_sa_creds_base64(
mut self,
sa_creds_base64: String,
sa_creds_base64: Option<String>,
) -> Result<Self, ServiceAccountError> {
if sa_creds_base64.is_none() {
return Ok(self);
}

println!("sa_creds_base64: {:?}", sa_creds_base64);

self.sa_creds_base64 = sa_creds_base64;

let creds = self.get_json_creds()?;
Expand All @@ -48,19 +54,27 @@ impl ServiceAccountConfig {
Ok(self)
}

pub fn service_account_key(&self) -> ServiceAccountKey {
pub fn service_account_key(&self) -> Result<ServiceAccountKey, ServiceAccountError> {
self.service_account_key
.clone()
.expect("Service Account not set")
.as_ref()
.cloned()
.ok_or(ServiceAccountError::CredentialsNotProvided)
}

pub fn get_json_creds(&self) -> Result<String, ServiceAccountError> {
fn get_json_creds(&self) -> Result<String, ServiceAccountError> {
if self.sa_creds_base64.is_none() {
return Err(ServiceAccountError::CredentialsNotProvided);
}

let creds = self
.sa_creds_base64
.as_ref()
.ok_or(ServiceAccountError::CredentialsNotProvided)?
.as_bytes();

use base64::{engine::general_purpose, Engine as _};

Ok(std::str::from_utf8(
&general_purpose::STANDARD.decode(self.sa_creds_base64.as_bytes())?,
)?
.to_string())
Ok(std::str::from_utf8(&general_purpose::STANDARD.decode(creds)?)?.to_string())
}
}

Expand Down
12 changes: 7 additions & 5 deletions lana/app/tests/gcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use lana_app::{

#[tokio::test]
async fn upload_doc() -> anyhow::Result<()> {
let sa_creds_base64 = if let Ok(sa_creds_base64) = std::env::var("SA_CREDS_BASE64") {
sa_creds_base64
} else {
return Ok(());
let sa_creds_base64 = match std::env::var("SA_CREDS_BASE64") {
Ok(value) if !value.trim().is_empty() => value,
_ => {
println!("Skipping GCS test: SA_CREDS_BASE64 not set or empty");
return Ok(());
}
};

let sa = ServiceAccountConfig::default().set_sa_creds_base64(sa_creds_base64)?;
let sa = ServiceAccountConfig::default().set_sa_creds_base64(Some(sa_creds_base64))?;

let config = if let Ok(name_prefix) = std::env::var("DEV_ENV_NAME_PREFIX") {
StorageConfig::new_dev_mode(name_prefix, sa)
Expand Down
2 changes: 1 addition & 1 deletion lana/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct EnvSecrets {
pub pg_con: String,
pub sumsub_key: String,
pub sumsub_secret: String,
pub sa_creds_base64: String,
pub sa_creds_base64: Option<String>,
}

impl Config {
Expand Down
10 changes: 8 additions & 2 deletions lana/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,27 @@ struct Cli {
#[clap(env = "SUMSUB_SECRET", default_value = "")]
sumsub_secret: String,
#[clap(env = "SA_CREDS_BASE64", default_value = "")]
sa_creds_base64: String,
sa_creds_base64_raw: String,
#[clap(env = "DEV_ENV_NAME_PREFIX")]
dev_env_name_prefix: Option<String>,
}

pub async fn run() -> anyhow::Result<()> {
let cli = Cli::parse();

let sa_creds_base64 = if cli.sa_creds_base64_raw.is_empty() {
None
} else {
Some(cli.sa_creds_base64_raw)
};

let config = Config::init(
cli.config,
EnvSecrets {
pg_con: cli.pg_con,
sumsub_key: cli.sumsub_key,
sumsub_secret: cli.sumsub_secret,
sa_creds_base64: cli.sa_creds_base64,
sa_creds_base64,
},
cli.dev_env_name_prefix,
)?;
Expand Down

0 comments on commit 2d20fae

Please # to comment.