@@ -18,6 +18,10 @@ pub struct PDPConfig {
1818 #[ config( env = "PDP_DEBUG" ) ]
1919 pub debug : Option < bool > ,
2020
21+ /// The host the PDP server will listen to (default: 0.0.0.0)
22+ #[ config( env = "PDP_HOST" , default = "0.0.0.0" ) ]
23+ pub host : String ,
24+
2125 /// The port the PDP server will listen to (default: 7766)
2226 #[ config( env = "PDP_PORT" , default = 7766 ) ]
2327 pub port : u16 ,
@@ -63,6 +67,7 @@ impl PDPConfig {
6367 Self {
6468 api_key : "test_api_key" . to_string ( ) ,
6569 debug : Some ( true ) ,
70+ host : "0.0.0.0" . to_string ( ) ,
6671 port : 0 ,
6772 use_new_authorized_users : false ,
6873 healthcheck_timeout : 3.0 ,
@@ -118,7 +123,10 @@ impl PDPConfig {
118123#[ cfg( test) ]
119124mod tests {
120125 use super :: * ;
121- use std:: sync:: Mutex ;
126+ use std:: {
127+ net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr } ,
128+ sync:: Mutex ,
129+ } ;
122130
123131 // This mutex ensures tests don't interfere with each other's environment variables
124132 static ENV_MUTEX : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
@@ -194,6 +202,7 @@ mod tests {
194202 || {
195203 let config = PDPConfig :: new ( ) . unwrap ( ) ;
196204 println ! ( "Config loaded: api_key='{}'" , config. api_key) ;
205+ assert_eq ! ( config. host, "0.0.0.0" ) ;
197206 assert_eq ! ( config. port, 7766 ) ;
198207 assert_eq ! ( config. cache. ttl, 3600 ) ;
199208 assert_eq ! ( config. horizon. host, "0.0.0.0" ) ;
@@ -256,6 +265,7 @@ mod tests {
256265 & [
257266 // Top level config
258267 ( "PDP_API_KEY" , "env-test-api-key" ) ,
268+ ( "PDP_HOST" , "::1" ) ,
259269 ( "PDP_PORT" , "7777" ) ,
260270 ( "PDP_DEBUG" , "true" ) ,
261271 ( "PDP_USE_NEW_AUTHORIZED_USERS" , "true" ) ,
@@ -285,6 +295,7 @@ mod tests {
285295
286296 // Test top level config
287297 assert_eq ! ( config. api_key, "env-test-api-key" ) ;
298+ assert_eq ! ( config. host, "::1" ) ;
288299 assert_eq ! ( config. port, 7777 ) ;
289300 assert_eq ! ( config. debug, Some ( true ) ) ;
290301 assert ! ( config. use_new_authorized_users) ;
@@ -315,6 +326,44 @@ mod tests {
315326 ) ;
316327 }
317328
329+ #[ test]
330+ fn test_host_config ( ) {
331+ with_env_vars (
332+ & [
333+ ( "PDP_API_KEY" , "test-api-key" ) ,
334+ ( "PDP_HOST" , "::" ) ,
335+ ( "PDP_PORT" , "7766" ) ,
336+ ] ,
337+ || {
338+ let config = PDPConfig :: new ( ) . unwrap ( ) ;
339+ assert_eq ! ( config. host, "::" ) ;
340+ assert_eq ! ( config. port, 7766 ) ;
341+ let expected_addr = SocketAddr :: from ( ( Ipv6Addr :: UNSPECIFIED , config. port ) ) ;
342+ assert_eq ! (
343+ SocketAddr :: from( ( config. host. parse:: <IpAddr >( ) . unwrap( ) , config. port) ) ,
344+ expected_addr
345+ ) ;
346+ } ,
347+ ) ;
348+ }
349+
350+ #[ test]
351+ fn test_ipv4_default ( ) {
352+ with_env_vars (
353+ & [ ( "PDP_API_KEY" , "test-api-key" ) , ( "PDP_PORT" , "7766" ) ] ,
354+ || {
355+ let config = PDPConfig :: new ( ) . unwrap ( ) ;
356+ assert_eq ! ( config. host, "0.0.0.0" ) ;
357+ assert_eq ! ( config. port, 7766 ) ;
358+ let expected_addr = SocketAddr :: from ( ( Ipv4Addr :: UNSPECIFIED , config. port ) ) ;
359+ assert_eq ! (
360+ SocketAddr :: from( ( config. host. parse:: <IpAddr >( ) . unwrap( ) , config. port) ) ,
361+ expected_addr
362+ ) ;
363+ } ,
364+ ) ;
365+ }
366+
318367 #[ test]
319368 fn test_confique_template_generation ( ) {
320369 // Test that we can generate configuration templates
@@ -324,6 +373,7 @@ mod tests {
324373
325374 // Verify that the template contains our configuration fields
326375 assert ! ( toml_template. contains( "PDP_API_KEY" ) ) ;
376+ assert ! ( toml_template. contains( "PDP_HOST" ) ) ;
327377 assert ! ( toml_template. contains( "PDP_PORT" ) ) ;
328378 assert ! ( toml_template. contains( "PDP_DEBUG" ) ) ;
329379 assert ! ( toml_template. contains( "PDP_CACHE_TTL" ) ) ;
@@ -343,7 +393,11 @@ mod tests {
343393 #[ test]
344394 fn test_confique_builder_pattern ( ) {
345395 with_env_vars (
346- & [ ( "PDP_API_KEY" , "builder-test-key" ) , ( "PDP_PORT" , "8080" ) ] ,
396+ & [
397+ ( "PDP_API_KEY" , "builder-test-key" ) ,
398+ ( "PDP_HOST" , "0.0.0.0" ) ,
399+ ( "PDP_PORT" , "8080" ) ,
400+ ] ,
347401 || {
348402 // Test the builder pattern directly
349403 let config = PDPConfig :: builder ( )
@@ -352,6 +406,7 @@ mod tests {
352406 . expect ( "Failed to load config" ) ;
353407
354408 assert_eq ! ( config. api_key, "builder-test-key" ) ;
409+ assert_eq ! ( config. host, "0.0.0.0" ) ;
355410 assert_eq ! ( config. port, 8080 ) ;
356411 assert_eq ! ( config. cache. ttl, 3600 ) ; // Default value
357412 assert_eq ! ( config. opa. url, "http://localhost:8181" ) ; // Default value
0 commit comments