Skip to content

Commit

Permalink
imdsclient: return a single hostname from fetch_hostname
Browse files Browse the repository at this point in the history
The `fetch_hostname` method queries the `metadata/local-hostname` IMDS
target, which has the potential to return multiple space-delimited
hostnames.  This changes the method to return only the first hostname in
the list.
  • Loading branch information
zmrow authored and rpkelly committed Apr 20, 2023
1 parent dfb0027 commit 5bf14ac
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sources/imdsclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,14 @@ impl ImdsClient {
Ok(Some(public_keys))
}

/// Gets the hostname from instance metadata.
/// Gets the hostname from instance metadata. The`metadata/local-hostname` IMDS target may
/// potentially return multiple space-delimited hostnames; choose the first one.
pub async fn fetch_hostname(&mut self) -> Result<Option<String>> {
let hostname_target = "meta-data/local-hostname";
self.fetch_string(&hostname_target).await
Ok(self
.fetch_string(&hostname_target)
.await?
.and_then(|h| h.split_whitespace().next().map(String::from)))
}

/// Helper to fetch bytes from IMDS using the pinned schema version.
Expand Down

0 comments on commit 5bf14ac

Please sign in to comment.