Skip to content

Commit 351ef6f

Browse files
committed
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 <[email protected]>
1 parent 58d5dd3 commit 351ef6f

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

keylime-agent/src/config.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,26 @@ fn config_translate_keywords(
565565
s => s.to_string(),
566566
};
567567

568-
let ip = parse_ip(config.agent.ip.as_ref())?.to_string();
569-
let contact_ip = parse_ip(config.agent.contact_ip.as_ref())?.to_string();
568+
let ip = match parse_ip(config.agent.ip.as_ref()) {
569+
Ok(ip) => ip.to_string(),
570+
Err(_) => {
571+
debug!("Parsing configured IP as hostname");
572+
parse_hostname(config.agent.ip.as_ref())?.to_string()
573+
}
574+
};
575+
576+
let contact_ip = match parse_ip(config.agent.contact_ip.as_ref()) {
577+
Ok(ip) => ip.to_string(),
578+
Err(_) => {
579+
debug!("Parsing configured contact IP as hostname");
580+
parse_hostname(config.agent.contact_ip.as_ref())?.to_string()
581+
}
582+
};
583+
570584
let registrar_ip = match parse_ip(config.agent.registrar_ip.as_ref()) {
571585
Ok(ip) => ip.to_string(),
572586
Err(_) => {
587+
debug!("Parsing configured registrar IP as hostname");
573588
parse_hostname(config.agent.registrar_ip.as_ref())?.to_string()
574589
}
575590
};
@@ -711,6 +726,30 @@ mod tests {
711726
assert_eq!(expected, default);
712727
}
713728

729+
#[test]
730+
fn test_hostname_support() {
731+
let default = AgentConfig::default();
732+
733+
let modified = AgentConfig {
734+
ip: "localhost".to_string(),
735+
contact_ip: "contact.ip".to_string(),
736+
registrar_ip: "registrar.ip".to_string(),
737+
..default
738+
};
739+
740+
let c = KeylimeConfig { agent: modified };
741+
742+
let result = config_translate_keywords(&c);
743+
assert!(result.is_ok());
744+
let result = result.unwrap(); //#[allow_ci]
745+
let resulting_ip = result.agent.ip;
746+
let resulting_contact_ip = result.agent.contact_ip;
747+
let resulting_registrar_ip = result.agent.registrar_ip;
748+
assert_eq!(resulting_ip, "localhost");
749+
assert_eq!(resulting_contact_ip, "contact.ip");
750+
assert_eq!(resulting_registrar_ip, "registrar.ip");
751+
}
752+
714753
#[test]
715754
fn get_revocation_cert_path_default() {
716755
let test_config = KeylimeConfig::default();

keylime-agent/src/main.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -921,12 +921,22 @@ async fn main() -> Result<()> {
921921

922922
let server;
923923

924-
// Add bracket if IPv6
925-
let ip = if config.agent.ip.parse::<IpAddr>()?.is_ipv6() {
926-
format!("[{}]", config.agent.ip)
927-
} else {
928-
config.agent.ip.to_string()
924+
// Try to parse as an IP address
925+
let ip = match config.agent.ip.parse::<IpAddr>() {
926+
Ok(ip_addr) => {
927+
// Add bracket if IPv6, otherwise use as it is
928+
if ip_addr.is_ipv6() {
929+
format!("[{}]", ip_addr)
930+
} else {
931+
ip_addr.to_string()
932+
}
933+
}
934+
Err(_) => {
935+
// If the address was not an IP address, treat as a hostname
936+
config.agent.ip.to_string()
937+
}
929938
};
939+
930940
let port = config.agent.port;
931941
if config.agent.enable_agent_mtls && ssl_context.is_some() {
932942
server = actix_server

0 commit comments

Comments
 (0)