Skip to content

Commit d03c142

Browse files
authored
More user friendly certificate permission denied errors (#256)
* errors * fmt
1 parent a2dc75a commit d03c142

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

src/http.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
io::ErrorKind,
23
net::{IpAddr, Ipv4Addr, SocketAddr},
34
path::Path,
45
sync::{atomic::Ordering, Arc, RwLock},
@@ -171,7 +172,16 @@ pub async fn run_setup(
171172
let setup_server = ProxySetupServer::new(logs_rx);
172173
let cert_dir = Path::new(&env_config.cert_dir);
173174
if !cert_dir.exists() {
174-
tokio::fs::create_dir_all(cert_dir).await?;
175+
tokio::fs::create_dir_all(cert_dir).await.map_err(|err| {
176+
if err.kind() == ErrorKind::PermissionDenied {
177+
anyhow::anyhow!(
178+
"Cannot create certificate directory {}. Permission denied.",
179+
cert_dir.display()
180+
)
181+
} else {
182+
err.into()
183+
}
184+
})?;
175185
}
176186

177187
// Only attempt setup if not already configured
@@ -197,8 +207,32 @@ pub async fn run_setup(
197207

198208
let cert_path = cert_dir.join(GRPC_CERT_NAME);
199209
let key_path = cert_dir.join(GRPC_KEY_NAME);
200-
tokio::fs::write(&cert_path, grpc_cert_pem).await?;
201-
tokio::fs::write(&key_path, grpc_key_pem).await?;
210+
tokio::fs::write(&cert_path, grpc_cert_pem)
211+
.await
212+
.map_err(|err| {
213+
if err.kind() == ErrorKind::PermissionDenied {
214+
anyhow::anyhow!(
215+
"Cannot write certificate file {}. Permission denied for certificate directory {}.",
216+
cert_path.display(),
217+
cert_dir.display()
218+
)
219+
} else {
220+
err.into()
221+
}
222+
})?;
223+
tokio::fs::write(&key_path, grpc_key_pem)
224+
.await
225+
.map_err(|err| {
226+
if err.kind() == ErrorKind::PermissionDenied {
227+
anyhow::anyhow!(
228+
"Cannot write key file {}. Permission denied for certificate directory {}.",
229+
key_path.display(),
230+
cert_dir.display()
231+
)
232+
} else {
233+
err.into()
234+
}
235+
})?;
202236

203237
Ok(configuration)
204238
}

src/main.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs::read_to_string, sync::Arc};
1+
use std::{fs::read_to_string, io::ErrorKind, path::Path, sync::Arc};
22

33
use defguard_proxy::{
44
config::get_env_config,
@@ -10,6 +10,29 @@ use defguard_proxy::{
1010
use defguard_version::Version;
1111
use tokio::sync::{mpsc, Mutex};
1212

13+
fn read_optional_cert_file(
14+
file_path: &Path,
15+
cert_dir: &Path,
16+
file_label: &'static str,
17+
) -> anyhow::Result<Option<String>> {
18+
match read_to_string(file_path) {
19+
Ok(content) => Ok(Some(content)),
20+
Err(err) if err.kind() == ErrorKind::NotFound => Ok(None),
21+
Err(err) if err.kind() == ErrorKind::PermissionDenied => anyhow::bail!(
22+
"Cannot access {file_label} file {}. Permission denied for certificate directory {}.",
23+
file_path.display(),
24+
cert_dir.display()
25+
),
26+
Err(err) => {
27+
tracing::warn!(
28+
"Failed to read gRPC {file_label} at {}: {err}",
29+
file_path.display()
30+
);
31+
Ok(None)
32+
}
33+
}
34+
}
35+
1336
#[tokio::main]
1437
async fn main() -> anyhow::Result<()> {
1538
// configuration
@@ -19,10 +42,11 @@ async fn main() -> anyhow::Result<()> {
1942

2043
let env_config = get_env_config()?;
2144
let cert_dir = env_config.cert_dir.clone();
22-
let (grpc_cert, grpc_key) = (
23-
read_to_string(cert_dir.join(GRPC_CERT_NAME)).ok(),
24-
read_to_string(cert_dir.join(GRPC_KEY_NAME)).ok(),
25-
);
45+
let grpc_cert_path = cert_dir.join(GRPC_CERT_NAME);
46+
let grpc_key_path = cert_dir.join(GRPC_KEY_NAME);
47+
48+
let grpc_cert = read_optional_cert_file(&grpc_cert_path, &cert_dir, "certificate")?;
49+
let grpc_key = read_optional_cert_file(&grpc_key_path, &cert_dir, "key")?;
2650

2751
let proxy_configuration = if let (Some(grpc_cert), Some(grpc_key)) = (grpc_cert, grpc_key) {
2852
Some(Configuration {

0 commit comments

Comments
 (0)