Skip to content

Commit

Permalink
feat: add TLS support to most of the clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron authored and bobmcwhirter committed Sep 18, 2023
1 parent 04f0f9c commit eacdac8
Show file tree
Hide file tree
Showing 30 changed files with 259 additions and 69 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ log = "0.4"
clap = { version = "4", features = ["derive"] }
anyhow = "1"
reqwest = { version = "0.11.16", features = ["stream"] }

trustification-common = { path = "../common" }
6 changes: 5 additions & 1 deletion admin/src/reindex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::process::ExitCode;

use reqwest::StatusCode;
use trustification_common::tls::ClientConfig;

#[derive(clap::Subcommand, Debug)]
pub enum Reindex {
Expand All @@ -25,11 +26,14 @@ pub struct ReindexStart {

#[arg(short = 'i', long = "indexer", default_value = "http://localhost:8080/")]
pub indexer_url: String,

#[command(flatten)]
pub client: ClientConfig,
}

impl ReindexStart {
pub async fn run(self) -> anyhow::Result<ExitCode> {
let client = reqwest::Client::new();
let client = self.client.build_client()?;
match client.post(self.indexer_url).send().await {
Ok(response) => {
if response.status() == StatusCode::OK {
Expand Down
10 changes: 7 additions & 3 deletions collector/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ impl CollectorUrl {
}

pub struct CollectorClient {
client: reqwest::Client,
url: CollectorUrl,
provider: Box<dyn TokenProvider>,
}

impl CollectorClient {
pub fn new<P>(url: Url, provider: P) -> Self
pub fn new<P>(client: reqwest::Client, url: Url, provider: P) -> Self
where
P: TokenProvider + 'static,
{
Self {
client,
url: CollectorUrl::new(url),
provider: Box::new(provider),
}
Expand All @@ -65,7 +67,8 @@ impl CollectorClient {
&self,
request: CollectPackagesRequest,
) -> Result<CollectPackagesResponse, anyhow::Error> {
let response = reqwest::Client::new()
let response = self
.client
.post(self.url.packages_url())
.inject_token(self.provider.as_ref())
.await?
Expand All @@ -80,7 +83,8 @@ impl CollectorClient {
&self,
request: CollectVulnerabilitiesRequest,
) -> Result<CollectVulnerabilitiesResponse, anyhow::Error> {
let response = reqwest::Client::new()
let response = self
.client
.post(self.url.vulnerabilities_url())
.inject_token(self.provider.as_ref())
.await?
Expand Down
1 change: 1 addition & 0 deletions collector/nvd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
actix-web = "4"
async-trait = "0.1"
trustification-auth = { path = "../../auth" }
trustification-common = { path = "../../common" }
trustification-collector-common = { path = "../common" }
trustification-infrastructure = { path = "../../infrastructure" }
collectorist-api = { path = "../../collectorist/api"}
Expand Down
28 changes: 22 additions & 6 deletions collector/nvd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use trustification_auth::{
client::{OpenIdTokenProviderConfigArguments, TokenProvider},
};
use trustification_collector_common::{CollectorRegistration, CollectorStateHandler, RegistrationConfig};
use trustification_common::tls::ClientConfig;
use trustification_infrastructure::{
app::http::HttpServerConfig,
endpoint::{self, CollectorNvd, Endpoint},
Expand Down Expand Up @@ -64,6 +65,9 @@ pub struct Run {

#[command(flatten)]
pub(crate) http: HttpServerConfig<CollectorNvd>,

#[command(flatten)]
pub(crate) client: ClientConfig,
}

impl Run {
Expand All @@ -82,9 +86,16 @@ impl Run {
|_context| async { Ok(()) },
|context| async move {
let provider = self.oidc.into_provider_or_devmode(self.devmode).await?;
let state = Self::configure(self.v11y_url, self.nvd_api_key, provider.clone()).await?;
let state = Self::configure(
self.client.build_client()?,
self.v11y_url,
self.nvd_api_key,
provider.clone(),
)
.await?;

let client = CollectoristClient::new("nvd", self.collectorist_url, provider);
let client =
CollectoristClient::new(self.client.build_client()?, "nvd", self.collectorist_url, provider);
let (collector, collector_state) = CollectorRegistration::new(
client,
RegistrationConfig {
Expand Down Expand Up @@ -125,11 +136,16 @@ impl Run {
Ok(ExitCode::SUCCESS)
}

async fn configure<P>(v11y_url: Url, nvd_api_key: String, provider: P) -> anyhow::Result<Arc<AppState>>
async fn configure<P>(
client: reqwest::Client,
v11y_url: Url,
nvd_api_key: String,
provider: P,
) -> anyhow::Result<Arc<AppState>>
where
P: TokenProvider + Clone + 'static,
{
let state = Arc::new(AppState::new(v11y_url, nvd_api_key, provider));
let state = Arc::new(AppState::new(client, v11y_url, nvd_api_key, provider));
Ok(state)
}
}
Expand All @@ -152,12 +168,12 @@ impl CollectorStateHandler for AppState {
}

impl AppState {
pub fn new<P>(v11y_url: Url, nvd_api_key: String, provider: P) -> Self
pub fn new<P>(client: reqwest::Client, v11y_url: Url, nvd_api_key: String, provider: P) -> Self
where
P: TokenProvider + Clone + 'static,
{
Self {
v11y_client: v11y_client::V11yClient::new(v11y_url, provider),
v11y_client: v11y_client::V11yClient::new(client, v11y_url, provider),
guac_url: RwLock::new(None),
nvd: NvdClient::new(&nvd_api_key),
}
Expand Down
1 change: 1 addition & 0 deletions collector/osv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
actix-web = "4"
async-trait = "0.1"
trustification-auth = { path = "../../auth" }
trustification-common = { path = "../../common" }
trustification-collector-common = { path = "../common" }
trustification-infrastructure = { path = "../../infrastructure" }
collectorist-api = { path = "../../collectorist/api"}
Expand Down
17 changes: 11 additions & 6 deletions collector/osv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use trustification_auth::{
client::{OpenIdTokenProviderConfigArguments, TokenProvider},
};
use trustification_collector_common::{CollectorRegistration, CollectorStateHandler, RegistrationConfig};
use trustification_common::tls::ClientConfig;
use trustification_infrastructure::{
app::http::HttpServerConfig,
endpoint::{self, CollectorOsv, Endpoint},
Expand Down Expand Up @@ -59,6 +60,9 @@ pub struct Run {

#[command(flatten)]
pub(crate) http: HttpServerConfig<CollectorOsv>,

#[command(flatten)]
pub(crate) client: ClientConfig,
}

impl Run {
Expand All @@ -77,9 +81,10 @@ impl Run {
|_context| async { Ok(()) },
|context| async move {
let provider = self.oidc.into_provider_or_devmode(self.devmode).await?;
let state = Self::configure(self.v11y_url, provider.clone()).await?;
let state = Self::configure(self.client.build_client()?, self.v11y_url, provider.clone()).await?;

let client = CollectoristClient::new("osv", self.collectorist_url, provider);
let client =
CollectoristClient::new(self.client.build_client()?, "osv", self.collectorist_url, provider);
let (collector, collector_state) = CollectorRegistration::new(
client,
RegistrationConfig {
Expand Down Expand Up @@ -120,11 +125,11 @@ impl Run {
Ok(ExitCode::SUCCESS)
}

async fn configure<P>(v11y_url: Url, provider: P) -> anyhow::Result<Arc<AppState>>
async fn configure<P>(client: reqwest::Client, v11y_url: Url, provider: P) -> anyhow::Result<Arc<AppState>>
where
P: TokenProvider + Clone + 'static,
{
let state = Arc::new(AppState::new(v11y_url, provider));
let state = Arc::new(AppState::new(client, v11y_url, provider));
Ok(state)
}
}
Expand All @@ -136,12 +141,12 @@ pub struct AppState {
}

impl AppState {
pub fn new<P>(v11y_url: Url, provider: P) -> Self
pub fn new<P>(client: reqwest::Client, v11y_url: Url, provider: P) -> Self
where
P: TokenProvider + Clone + 'static,
{
Self {
v11y_client: v11y_client::V11yClient::new(v11y_url, provider),
v11y_client: v11y_client::V11yClient::new(client, v11y_url, provider),
guac_url: RwLock::new(None),
osv: OsvClient::new(),
}
Expand Down
1 change: 1 addition & 0 deletions collector/snyk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
actix-web = "4"
async-trait = "0.1"
trustification-auth = { path = "../../auth" }
trustification-common = { path = "../../common" }
trustification-collector-common = { path = "../common" }
trustification-infrastructure = { path = "../../infrastructure" }
collectorist-api = { path = "../../collectorist/api"}
Expand Down
24 changes: 18 additions & 6 deletions collector/snyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use trustification_auth::{
client::{OpenIdTokenProviderConfigArguments, TokenProvider},
};
use trustification_collector_common::{CollectorRegistration, CollectorStateHandler, RegistrationConfig};
use trustification_common::tls::ClientConfig;
use trustification_infrastructure::{
app::http::HttpServerConfig,
endpoint::CollectorSnyk,
Expand Down Expand Up @@ -72,6 +73,9 @@ pub struct Run {

#[command(flatten)]
pub(crate) http: HttpServerConfig<CollectorSnyk>,

#[command(flatten)]
pub(crate) client: ClientConfig,
}

impl Run {
Expand All @@ -90,10 +94,17 @@ impl Run {
|_context| async { Ok(()) },
|context| async move {
let provider = self.oidc.into_provider_or_devmode(self.devmode).await?;
let state =
Self::configure(self.snyk_org_id, self.snyk_token, self.v11y_url, provider.clone()).await?;
let state = Self::configure(
self.client.build_client()?,
self.snyk_org_id,
self.snyk_token,
self.v11y_url,
provider.clone(),
)
.await?;

let client = CollectoristClient::new("snyk", self.collectorist_url, provider);
let client =
CollectoristClient::new(self.client.build_client()?, "snyk", self.collectorist_url, provider);
let (collector, collector_state) = CollectorRegistration::new(
client,
RegistrationConfig {
Expand Down Expand Up @@ -135,6 +146,7 @@ impl Run {
}

async fn configure<P>(
client: reqwest::Client,
snyk_org_id: String,
snyk_token: String,
v11y_url: Url,
Expand All @@ -143,7 +155,7 @@ impl Run {
where
P: TokenProvider + Clone + 'static,
{
let state = Arc::new(AppState::new(snyk_org_id, snyk_token, v11y_url, provider));
let state = Arc::new(AppState::new(client, snyk_org_id, snyk_token, v11y_url, provider));
Ok(state)
}
}
Expand All @@ -156,12 +168,12 @@ pub struct AppState {
}

impl AppState {
pub fn new<P>(snyk_org_id: String, snyk_token: String, v11y_url: Url, provider: P) -> Self
pub fn new<P>(client: reqwest::Client, snyk_org_id: String, snyk_token: String, v11y_url: Url, provider: P) -> Self
where
P: TokenProvider + Clone + 'static,
{
Self {
v11y_client: v11y_client::V11yClient::new(v11y_url, provider),
v11y_client: v11y_client::V11yClient::new(client, v11y_url, provider),
guac_url: RwLock::new(None),
snyk_org_id,
snyk_token,
Expand Down
1 change: 1 addition & 0 deletions collectorist/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
actix-web = "4"
trustification-auth = { path = "../../auth", features = ["swagger"] }
trustification-common = { path = "../../common" }
trustification-infrastructure = { path = "../../infrastructure" }
collectorist-client = { path = "../client" }
collector-client = { path = "../../collector/client" }
Expand Down
10 changes: 8 additions & 2 deletions collectorist/api/src/coordinator/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ pub struct Collector {
}

impl Collector {
pub fn new<P>(state: Arc<AppState>, id: String, config: CollectorConfig, provider: P) -> Self
pub fn new<P>(
client: reqwest::Client,
state: Arc<AppState>,
id: String,
config: CollectorConfig,
provider: P,
) -> Self
where
P: TokenProvider + 'static,
{
let client = Arc::new(CollectorClient::new(config.url.clone(), provider));
let client = Arc::new(CollectorClient::new(client, config.url.clone(), provider));
let update = tokio::spawn(Collector::update(client.clone(), state, id.clone()));
Self {
id,
Expand Down

0 comments on commit eacdac8

Please sign in to comment.