From 26311dde9028ef5c513ca3d723b155d3795cccdd Mon Sep 17 00:00:00 2001 From: Anderson Toshiyuki Sasaki Date: Tue, 12 Nov 2024 19:16:58 +0100 Subject: [PATCH] config: Enable passing a hostname instead of IP For the options 'ip', 'contact_ip', and 'registrar_ip', allow a hostname to be used instead of an IP address. Fixes #848 Signed-off-by: Anderson Toshiyuki Sasaki --- keylime-agent/src/config.rs | 43 +++++++++++++++++++++++++++++++++++-- keylime-agent/src/main.rs | 20 ++++++++++++----- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/keylime-agent/src/config.rs b/keylime-agent/src/config.rs index 5a7dc291..32eee7c6 100644 --- a/keylime-agent/src/config.rs +++ b/keylime-agent/src/config.rs @@ -565,11 +565,26 @@ fn config_translate_keywords( s => s.to_string(), }; - let ip = parse_ip(config.agent.ip.as_ref())?.to_string(); - let contact_ip = parse_ip(config.agent.contact_ip.as_ref())?.to_string(); + let ip = match parse_ip(config.agent.ip.as_ref()) { + Ok(ip) => ip.to_string(), + Err(_) => { + debug!("Parsing configured IP as hostname"); + parse_hostname(config.agent.ip.as_ref())?.to_string() + } + }; + + let contact_ip = match parse_ip(config.agent.contact_ip.as_ref()) { + Ok(ip) => ip.to_string(), + Err(_) => { + debug!("Parsing configured contact IP as hostname"); + parse_hostname(config.agent.contact_ip.as_ref())?.to_string() + } + }; + let registrar_ip = match parse_ip(config.agent.registrar_ip.as_ref()) { Ok(ip) => ip.to_string(), Err(_) => { + debug!("Parsing configured registrar IP as hostname"); parse_hostname(config.agent.registrar_ip.as_ref())?.to_string() } }; @@ -711,6 +726,30 @@ mod tests { assert_eq!(expected, default); } + #[test] + fn test_hostname_support() { + let default = AgentConfig::default(); + + let modified = AgentConfig { + ip: "localhost".to_string(), + contact_ip: "contact.ip".to_string(), + registrar_ip: "registrar.ip".to_string(), + ..default + }; + + let c = KeylimeConfig { agent: modified }; + + let result = config_translate_keywords(&c); + assert!(result.is_ok()); + let result = result.unwrap(); //#[allow_ci] + let resulting_ip = result.agent.ip; + let resulting_contact_ip = result.agent.contact_ip; + let resulting_registrar_ip = result.agent.registrar_ip; + assert_eq!(resulting_ip, "localhost"); + assert_eq!(resulting_contact_ip, "contact.ip"); + assert_eq!(resulting_registrar_ip, "registrar.ip"); + } + #[test] fn get_revocation_cert_path_default() { let test_config = KeylimeConfig::default(); diff --git a/keylime-agent/src/main.rs b/keylime-agent/src/main.rs index e331807b..ce8b258e 100644 --- a/keylime-agent/src/main.rs +++ b/keylime-agent/src/main.rs @@ -921,12 +921,22 @@ async fn main() -> Result<()> { let server; - // Add bracket if IPv6 - let ip = if config.agent.ip.parse::()?.is_ipv6() { - format!("[{}]", config.agent.ip) - } else { - config.agent.ip.to_string() + // Try to parse as an IP address + let ip = match config.agent.ip.parse::() { + Ok(ip_addr) => { + // Add bracket if IPv6, otherwise use as it is + if ip_addr.is_ipv6() { + format!("[{}]", ip_addr) + } else { + ip_addr.to_string() + } + } + Err(_) => { + // If the address was not an IP address, treat as a hostname + config.agent.ip.to_string() + } }; + let port = config.agent.port; if config.agent.enable_agent_mtls && ssl_context.is_some() { server = actix_server