Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: Enable passing a hostname instead of IP #866

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions keylime-agent/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
};
Expand Down Expand Up @@ -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();
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 @@ -921,12 +921,22 @@

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 926 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L925-L926

Added lines #L925 - L926 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 929 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L928-L929

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

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

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L931

Added line #L931 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 936 in keylime-agent/src/main.rs

View check run for this annotation

Codecov / codecov/patch

keylime-agent/src/main.rs#L936

Added line #L936 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
Loading