Skip to content

Commit

Permalink
config: Enable passing a hostname instead of IP
Browse files Browse the repository at this point in the history
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 <ansasaki@redhat.com>
  • Loading branch information
ansasaki committed Nov 12, 2024
1 parent a3bfd5a commit e535e16
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
44 changes: 42 additions & 2 deletions keylime-agent/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,24 @@ 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();
debug!("Will parse IP");

let ip = match parse_ip(config.agent.ip.as_ref()) {
Ok(ip) => ip.to_string(),
Err(_) => {
debug!("Will parse 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(_) => {
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(_) => {
Expand Down Expand Up @@ -711,6 +727,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();
Expand Down
20 changes: 15 additions & 5 deletions keylime-agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,12 +968,22 @@ async fn main() -> Result<()> {

let server;

// Add bracket if IPv6
let ip = if config.agent.ip.parse::<IpAddr>()?.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::<IpAddr>() {
Ok(ip_addr) => {

Check warning on line 973 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L972-L973

Added lines #L972 - L973 were not covered by tests
// Add bracket if IPv6, otherwise use as it is
if ip_addr.is_ipv6() {
format!("[{}]", ip_addr)

Check warning on line 976 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L975-L976

Added lines #L975 - L976 were not covered by tests
} else {
ip_addr.to_string()

Check warning on line 978 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L978

Added line #L978 was not covered by tests
}
}
Err(_) => {
// If the address was not an IP address, treat as a hostname
config.agent.ip.to_string()

Check warning on line 983 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L983

Added line #L983 was not covered by tests
}
};

let port = config.agent.port;
if config.agent.enable_agent_mtls && ssl_context.is_some() {
server = actix_server
Expand Down

0 comments on commit e535e16

Please sign in to comment.