Skip to content

Commit

Permalink
feat: point the integs at a remote trustification server
Browse files Browse the repository at this point in the history
Fixes #401

Setting the TRUST_URL env var to a remote trustification server
triggers the integs to be run against it.

If it's set, other env vars will be required:
  TRUST_USER_ID -- the client id of the user
  TRUST_MANAGER_ID -- the client id of the manager
  TRUST_SECRET -- the secret is assumed to be the same for user & mgr

If KAFKA_BOOTSTRAP_SERVERS is set, its value will be used to configure
the event bus. Otherwise, SQS is assumed and valid AWS credentials
will be required.

Signed-off-by: Jim Crossley <jim@crossleys.org>
  • Loading branch information
jcrossley3 authored and ctron committed Aug 23, 2023
1 parent 5ea4712 commit 918c159
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 40 deletions.
2 changes: 1 addition & 1 deletion deploy/compose/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TRUST_IMAGE=ghcr.io/trustification/trust
TRUST_VERSION=0.1.0-nightly.a5badbb9
TRUST_VERSION=0.1.0-nightly.d8b1c815
TRUST_UI_IMAGE=ghcr.io/trustification/trust-ui
VEXINATION_API_PORT=8081
BOMBASTIC_API_PORT=8082
Expand Down
28 changes: 17 additions & 11 deletions integration-tests/src/bom.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::*;
use crate::runner::Runner;
use crate::{config::Config, runner::Runner};
use async_trait::async_trait;
use reqwest::Url;
use test_context::AsyncTestContext;

#[async_trait]
impl AsyncTestContext for BombasticContext {
async fn setup() -> Self {
let provider = create_provider_context().await;
start_bombastic(provider).await
let config = Config::new().await;
start_bombastic(&config).await
}
}

Expand All @@ -22,12 +22,21 @@ pub struct BombasticContext {
pub url: Url,
pub provider: ProviderContext,
pub events: EventBusConfig,
_runner: Runner,
_runner: Option<Runner>,
}

pub async fn start_bombastic(provider: ProviderContext) -> BombasticContext {
let _ = env_logger::try_init();
pub async fn start_bombastic(config: &Config) -> BombasticContext {
// If remote server is configured, use it
if let Some(url) = config.bombastic.clone() {
return BombasticContext {
url,
provider: config.provider().await,
events: config.events(),
_runner: None,
};
}

// No remote server requested, so fire up bombastic on ephemeral port
let listener = TcpListener::bind("localhost:0").unwrap();
let port = listener.local_addr().unwrap().port();
let url = Url::parse(&format!("http://localhost:{port}")).unwrap();
Expand Down Expand Up @@ -60,16 +69,14 @@ pub async fn start_bombastic(provider: ProviderContext) -> BombasticContext {
});

// Create context right after spawning, as we clean up as soon as the context drops

let context = BombasticContext {
url,
provider,
provider: config.provider().await,
events,
_runner: runner,
_runner: Some(runner),
};

// ensure it's initialized

let client = reqwest::Client::new();
loop {
let response = client
Expand All @@ -87,7 +94,6 @@ pub async fn start_bombastic(provider: ProviderContext) -> BombasticContext {
}

// return the context

context
}

Expand Down
69 changes: 69 additions & 0 deletions integration-tests/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use reqwest::Url;
use serde_json::Value;
use trustification_event_bus::{EventBusConfig, EventBusType};

use crate::{create_provider, create_provider_context, ProviderContext};

#[derive(Default)]
pub struct Config {
pub spog: Option<Url>,
pub bombastic: Option<Url>,
pub vexination: Option<Url>,
issuer: String,
user: String,
manager: String,
secret: String,
}

impl Config {
pub async fn new() -> Self {
let _ = env_logger::try_init();
match std::env::var("TRUST_URL") {
Ok(base) => {
let url = Url::parse(&base)
.expect(&format!("Invalid TRUST_URL: '{base}'"))
.join("/endpoints/backend.json")
.unwrap();
let endpoints: Value = reqwest::get(url)
.await
.expect("Missing backend endpoints")
.json()
.await
.unwrap();
Config {
spog: endpoints["url"].as_str().map(Url::parse).unwrap().ok(),
bombastic: endpoints["bombastic"].as_str().map(Url::parse).unwrap().ok(),
vexination: endpoints["vexination"].as_str().map(Url::parse).unwrap().ok(),
issuer: endpoints["oidc"]["issuer"].as_str().unwrap().to_string(),
user: std::env::var("TRUST_USER_ID").expect("TRUST_USER_ID is required"),
manager: std::env::var("TRUST_MANAGER_ID").expect("TRUST_MANAGER_ID is required"),
secret: std::env::var("TRUST_SECRET").expect("TRUST_SECRET is required"),
}
}
_ => Config::default(),
}
}

pub async fn provider(&self) -> ProviderContext {
match self.spog {
Some(_) => ProviderContext {
provider_user: create_provider(&self.user, &self.secret, &self.issuer).await,
provider_manager: create_provider(&self.manager, &self.secret, &self.issuer).await,
},
_ => create_provider_context().await,
}
}

pub fn events(&self) -> EventBusConfig {
match std::env::var("KAFKA_BOOTSTRAP_SERVERS") {
Ok(v) => EventBusConfig {
event_bus: EventBusType::Kafka,
kafka_bootstrap_servers: v,
},
_ => EventBusConfig {
event_bus: EventBusType::Sqs,
kafka_bootstrap_servers: String::new(),
},
}
}
}
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod bom;
mod config;
mod provider;
mod spog;
mod vex;
Expand Down
10 changes: 5 additions & 5 deletions integration-tests/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ pub struct ProviderContext {

pub async fn create_provider_context() -> ProviderContext {
ProviderContext {
provider_user: create_provider("testing-user").await,
provider_manager: create_provider("testing-manager").await,
provider_user: create_provider("testing-user", SSO_TESTING_CLIENT_SECRET, SSO_ENDPOINT).await,
provider_manager: create_provider("testing-manager", SSO_TESTING_CLIENT_SECRET, SSO_ENDPOINT).await,
}
}

async fn create_provider(client_id: &str) -> Arc<OpenIdTokenProvider> {
pub async fn create_provider(client_id: &str, secret: &str, issuer: &str) -> Arc<OpenIdTokenProvider> {
let client_user = openid::Client::discover(
client_id.into(),
Some(SSO_TESTING_CLIENT_SECRET.to_string()),
Some(secret.to_string()),
None,
SSO_ENDPOINT.parse().unwrap(),
issuer.parse().unwrap(),
)
.await
.unwrap();
Expand Down
33 changes: 20 additions & 13 deletions integration-tests/src/spog.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::*;
use crate::runner::Runner;
use crate::{config::Config, runner::Runner};
use async_trait::async_trait;
use test_context::AsyncTestContext;

#[async_trait]
impl AsyncTestContext for SpogContext {
async fn setup() -> Self {
let provider = create_provider_context().await;
start_spog(provider).await
let config = Config::new().await;
start_spog(&config).await
}
}

Expand All @@ -24,18 +24,28 @@ pub struct SpogContext {
pub bombastic: BombasticContext,
pub vexination: VexinationContext,

_runner: Runner,
_runner: Option<Runner>,
}

pub async fn start_spog(provider: ProviderContext) -> SpogContext {
let _ = env_logger::try_init();
pub async fn start_spog(config: &Config) -> SpogContext {
// If remote server is configured, use it
if let Some(url) = config.spog.clone() {
return SpogContext {
url,
provider: config.provider().await,
bombastic: start_bombastic(config).await,
vexination: start_vexination(config).await,
_runner: None,
};
}

// No remote server requested, so fire up spog on ephemeral port
let listener = TcpListener::bind("localhost:0").unwrap();
let port = listener.local_addr().unwrap().port();
let url = Url::parse(&format!("http://localhost:{port}")).unwrap();

let bombastic = start_bombastic(provider.clone()).await;
let vexination = start_vexination(provider.clone()).await;
let bombastic = start_bombastic(config).await;
let vexination = start_vexination(config).await;

let burl = bombastic.url.to_owned();
let vurl = vexination.url.to_owned();
Expand All @@ -59,17 +69,15 @@ pub async fn start_spog(provider: ProviderContext) -> SpogContext {
});

// Create context right after spawning, as we clean up as soon as the context drops

let context = SpogContext {
url,
provider,
provider: config.provider().await,
bombastic,
vexination,
_runner: runner,
_runner: Some(runner),
};

// ensure it's initialized

let client = reqwest::Client::new();
loop {
let response = client
Expand All @@ -87,7 +95,6 @@ pub async fn start_spog(provider: ProviderContext) -> SpogContext {
}

// return the context

context
}

Expand Down
27 changes: 17 additions & 10 deletions integration-tests/src/vex.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::*;
use crate::runner::Runner;
use crate::{config::Config, runner::Runner};
use async_trait::async_trait;
use test_context::AsyncTestContext;

#[async_trait]
impl AsyncTestContext for VexinationContext {
async fn setup() -> Self {
let provider = create_provider_context().await;
start_vexination(provider).await
let config = Config::new().await;
start_vexination(&config).await
}
}

Expand All @@ -21,12 +21,21 @@ pub struct VexinationContext {
pub url: Url,
pub provider: ProviderContext,
pub events: EventBusConfig,
_runner: Runner,
_runner: Option<Runner>,
}

pub async fn start_vexination(provider: ProviderContext) -> VexinationContext {
let _ = env_logger::try_init();
pub async fn start_vexination(config: &Config) -> VexinationContext {
// If remote server is configured, use it
if let Some(url) = config.vexination.clone() {
return VexinationContext {
url,
provider: config.provider().await,
events: config.events(),
_runner: None,
};
}

// No remote server requested, so fire up vexination on ephemeral port
let listener = TcpListener::bind("localhost:0").unwrap();
let port = listener.local_addr().unwrap().port();
let url = Url::parse(&format!("http://localhost:{port}")).unwrap();
Expand Down Expand Up @@ -60,16 +69,15 @@ pub async fn start_vexination(provider: ProviderContext) -> VexinationContext {
});

// Create context right after spawning, as we clean up as soon as the context drops

let provider = config.provider().await;
let context = VexinationContext {
url,
provider,
events,
_runner: runner,
_runner: Some(runner),
};

// ensure it's initialized

let client = reqwest::Client::new();
loop {
let response = client
Expand All @@ -87,7 +95,6 @@ pub async fn start_vexination(provider: ProviderContext) -> VexinationContext {
}

// return the context

context
}

Expand Down

0 comments on commit 918c159

Please sign in to comment.