Skip to content

Commit 5e564ff

Browse files
authored
Fix proxy healthceck endpoint availability when waiting for setup (#239)
1 parent 3d7663e commit 5e564ff

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn default_url() -> Url {
99
Url::parse("http://localhost:8080").unwrap()
1010
}
1111

12-
#[derive(Parser, Debug, Deserialize)]
12+
#[derive(Parser, Debug, Deserialize, Clone)]
1313
#[command(version)]
1414
pub struct EnvConfig {
1515
// port the API server will listen on

src/http.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ async fn ensure_configured(
232232
next.run(request).await
233233
}
234234

235-
pub async fn run_server(env_config: EnvConfig, config: Configuration) -> anyhow::Result<()> {
235+
pub async fn run_server(
236+
env_config: EnvConfig,
237+
config: Option<Configuration>,
238+
logs_rx: Option<LogsReceiver>,
239+
) -> anyhow::Result<()> {
236240
info!("Starting Defguard Proxy server");
237241
debug!("Using config: {env_config:?}");
238242

@@ -243,12 +247,26 @@ pub async fn run_server(env_config: EnvConfig, config: Configuration) -> anyhow:
243247
let grpc_server = ProxyServer::new(Arc::clone(&cookie_key));
244248

245249
let server_clone = grpc_server.clone();
246-
grpc_server.configure(config);
250+
let env_config_clone = env_config.clone();
247251

248252
// Start gRPC server.
249-
// TODO: Wait with spawning the HTTP server until gRPC server is ready.
250-
debug!("Spawning gRPC server");
253+
debug!("Spawning gRPC server task");
251254
tasks.spawn(async move {
255+
let proxy_configuration = if let Some(conf) = config {
256+
debug!("Using existing gRPC certificates, skipping setup process");
257+
conf
258+
} else if let Some(logs_rx) = logs_rx {
259+
info!("gRPC certificates not found, running setup process");
260+
let conf = run_setup(&env_config_clone, logs_rx).await?;
261+
info!("Setup process completed successfully");
262+
conf
263+
} else {
264+
anyhow::bail!(
265+
"gRPC certificates not found and logs receiver not available for setup process"
266+
);
267+
};
268+
269+
server_clone.configure(proxy_configuration);
252270
loop {
253271
info!("Starting gRPC server...");
254272
let server_to_run = server_clone.clone();

src/main.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fs::read_to_string, sync::Arc};
33
use defguard_proxy::{
44
config::get_env_config,
55
grpc::Configuration,
6-
http::{run_server, run_setup, GRPC_CERT_NAME, GRPC_KEY_NAME},
6+
http::{run_server, GRPC_CERT_NAME, GRPC_KEY_NAME},
77
logging::init_tracing,
88
VERSION,
99
};
@@ -24,7 +24,16 @@ async fn main() -> anyhow::Result<()> {
2424
read_to_string(cert_dir.join(GRPC_KEY_NAME)).ok(),
2525
);
2626

27-
let needs_setup = grpc_cert.is_none() || grpc_key.is_none();
27+
let proxy_configuration = if let (Some(grpc_cert), Some(grpc_key)) = (grpc_cert, grpc_key) {
28+
Some(Configuration {
29+
grpc_cert_pem: grpc_cert,
30+
grpc_key_pem: grpc_key,
31+
})
32+
} else {
33+
None
34+
};
35+
36+
let needs_setup = proxy_configuration.is_none();
2837

2938
// TODO: The channel size may need to be adjusted or some other approach should be used
3039
// to avoid dropping log messages.
@@ -39,28 +48,13 @@ async fn main() -> anyhow::Result<()> {
3948
// read config from env
4049
tracing::info!("Starting ... version v{}", VERSION);
4150

42-
let proxy_configuration = if needs_setup {
43-
if let Some(logs_rx) = logs_rx {
44-
tracing::info!("gRPC certificates not found, running setup process");
45-
let proxy_configuration = run_setup(&env_config, Arc::new(Mutex::new(logs_rx))).await?;
46-
tracing::info!("Setup process completed successfully");
47-
proxy_configuration
48-
} else {
49-
anyhow::bail!(
50-
"gRPC certificates not found and logs receiver not available for setup process"
51-
);
52-
}
53-
} else if let (Some(grpc_cert), Some(grpc_key)) = (grpc_cert, grpc_key) {
54-
Configuration {
55-
grpc_cert_pem: grpc_cert,
56-
grpc_key_pem: grpc_key,
57-
}
58-
} else {
59-
anyhow::bail!("Failed to load gRPC certificates");
60-
};
61-
6251
// run API web server
63-
run_server(env_config, proxy_configuration).await?;
52+
run_server(
53+
env_config,
54+
proxy_configuration,
55+
logs_rx.map(|r| Arc::new(Mutex::new(r))),
56+
)
57+
.await?;
6458

6559
Ok(())
6660
}

0 commit comments

Comments
 (0)