From 0359465e49a5880d2dce633ffa520799494ef57e Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Thu, 7 Dec 2023 09:02:44 +0000 Subject: [PATCH] feedback Signed-off-by: Zahari Dichev --- Cargo.lock | 28 +++++++++++++++++--------- linkerd/identity/Cargo.toml | 2 +- linkerd/identity/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e496931f0e..3b45ae3667 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -582,9 +582,9 @@ checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" [[package]] name = "form_urlencoded" -version = "1.1.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" dependencies = [ "percent-encoding", ] @@ -899,6 +899,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -1513,10 +1523,10 @@ dependencies = [ name = "linkerd-identity" version = "0.1.0" dependencies = [ - "http", "linkerd-dns-name", "linkerd-error", "thiserror", + "url", ] [[package]] @@ -2455,9 +2465,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.2.0" +version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "petgraph" @@ -3461,7 +3471,7 @@ dependencies = [ "futures-channel", "futures-io", "futures-util", - "idna", + "idna 0.2.3", "ipnet", "lazy_static", "rand", @@ -3540,12 +3550,12 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "url" -version = "2.3.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22fe195a4f217c25b25cb5058ced57059824a678474874038dc88d211bf508d3" +checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" dependencies = [ "form_urlencoded", - "idna", + "idna 0.5.0", "percent-encoding", ] diff --git a/linkerd/identity/Cargo.toml b/linkerd/identity/Cargo.toml index 525ff5a444..2a520e309c 100644 --- a/linkerd/identity/Cargo.toml +++ b/linkerd/identity/Cargo.toml @@ -9,5 +9,5 @@ publish = false [dependencies] linkerd-dns-name = { path = "../dns/name" } linkerd-error = { path = "../error" } -http = "0.2" +url = "2.5.0" thiserror = "1" diff --git a/linkerd/identity/src/lib.rs b/linkerd/identity/src/lib.rs index e8c446b93d..bba2bb8050 100644 --- a/linkerd/identity/src/lib.rs +++ b/linkerd/identity/src/lib.rs @@ -19,7 +19,7 @@ use std::str::FromStr; #[derive(Clone, Debug, Eq, PartialEq, Hash)] pub enum Id { Dns(linkerd_dns_name::Name), - Uri(http::Uri), + Uri(url::Url), } #[derive(Debug, thiserror::Error)] @@ -52,7 +52,7 @@ impl Id { } pub fn parse_uri(s: &str) -> Result { - http::Uri::try_from(s) + url::Url::parse(s) .map(Self::Uri) .map_err(|e| InvalidId(e.into())) } @@ -60,7 +60,7 @@ impl Id { pub fn to_str(&self) -> std::borrow::Cow<'_, str> { match self { Self::Dns(dns) => dns.as_str().into(), - Self::Uri(uri) => uri.to_string().into(), + Self::Uri(uri) => uri.as_str().into(), } } } @@ -79,3 +79,36 @@ impl std::fmt::Display for Id { } } } + +#[cfg(test)] +mod tests { + use super::Id; + + #[test] + fn roundtrip_uri_id_parsing_spiffe() { + let id: Id = "spiffe://host:1234/path".parse().unwrap(); + assert_eq!(id, id.to_string().parse().unwrap()); + } + + #[test] + fn roundtrip_uri_id_parsing_non_spiffe() { + let id: Id = "http://host:1234/path".parse().unwrap(); + assert_eq!(id, id.to_string().parse().unwrap()); + } + + #[test] + fn roundtrip_uri_dns_parsing() { + let id: Id = "some-svc.svc.cluster.local".parse().unwrap(); + assert_eq!(id, id.to_string().parse().unwrap()); + } + + #[test] + fn cannot_parse_uri_as_dns() { + assert!(Id::parse_dns_name("uri://host:1234/path").is_err()); + } + + #[test] + fn cannot_parse_dns_name_as_uri() { + assert!(Id::parse_uri("some-svc.svc.cluster.local").is_err()); + } +}