1- use std:: { fs:: read_to_string, sync:: Arc } ;
1+ use std:: { fs:: read_to_string, io :: ErrorKind , path :: Path , sync:: Arc } ;
22
33use defguard_proxy:: {
44 config:: get_env_config,
@@ -10,6 +10,29 @@ use defguard_proxy::{
1010use defguard_version:: Version ;
1111use 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]
1437async 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