Skip to content

Commit 3984bde

Browse files
ipv6 support for pdp (#284)
1 parent c9d6635 commit 3984bde

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

pdp-server/src/api/horizon_fallback.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ mod tests {
521521
// Create a modified config for testing
522522
let config = crate::config::PDPConfig {
523523
api_key: "test_api_key".to_string(),
524+
host: "0.0.0.0".to_string(),
524525
debug: None,
525526
port: 0,
526527
use_new_authorized_users: false,
@@ -586,6 +587,7 @@ mod tests {
586587
// Create custom config with very short timeout
587588
let config = crate::config::PDPConfig {
588589
api_key: "test_api_key".to_string(),
590+
host: "0.0.0.0".to_string(),
589591
debug: None,
590592
port: 0,
591593
use_new_authorized_users: false,

pdp-server/src/cache/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct CacheValue {
3434
/// Implementations of this trait should be thread-safe (Send + Sync)
3535
/// and cloneable to support sharing across multiple handlers.
3636
#[async_trait::async_trait]
37+
#[allow(dead_code)]
3738
pub trait CacheBackend: Send + Sync {
3839
/// Store a value in the cache with default TTL
3940
async fn set<T: Serialize + Send + Sync>(&self, key: &str, value: &T)

pdp-server/src/config/mod.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
119124
mod 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

pdp-server/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod test_utils;
1212
use crate::state::AppState;
1313
use axum::Router;
1414
use log::{error, info};
15-
use std::net::SocketAddr;
15+
use std::net::{IpAddr, SocketAddr};
1616
use utoipa::OpenApi;
1717
use utoipa_axum::router::OpenApiRouter;
1818

@@ -46,8 +46,12 @@ async fn main() {
4646
// Create application & Initialize PDPEngine
4747
let app = create_app(state).await;
4848

49+
let host = config.host.parse::<IpAddr>().unwrap_or_else(|e| {
50+
error!("Invalid host: {} ({})", config.host, e);
51+
std::process::exit(1);
52+
});
4953
// Build server address
50-
let addr = SocketAddr::from(([0, 0, 0, 0], config.port));
54+
let addr = SocketAddr::new(host, config.port);
5155

5256
// Start server
5357
let server = match tokio::net::TcpListener::bind(&addr).await {

pdp-server/src/openapi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub(crate) const AUTHZEN_TAG: &str = "AuthZen API";
1414
#[openapi(
1515
tags(
1616
(name = HEALTH_TAG, description = "Health check endpoints"),
17-
(name = AUTHZ_TAG, description = "Authorization endpoints")
17+
(name = AUTHZ_TAG, description = "Authorization endpoints"),
18+
(name = AUTHZEN_TAG, description = "AuthZen endpoints")
1819
),
1920
info(
2021
title = "Permit.io PDP API",

pdp-server/src/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ mod tests {
164164
fn create_test_config() -> PDPConfig {
165165
PDPConfig {
166166
api_key: "test-api-key".to_string(),
167+
host: "0.0.0.0".to_string(),
167168
debug: Some(true),
168169
port: 3000,
169170
use_new_authorized_users: false,

0 commit comments

Comments
 (0)