Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Zahari Dichev <[email protected]>
  • Loading branch information
zaharidichev committed Dec 7, 2023
1 parent 3e31ee8 commit 0359465
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
28 changes: 19 additions & 9 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -1513,10 +1523,10 @@ dependencies = [
name = "linkerd-identity"
version = "0.1.0"
dependencies = [
"http",
"linkerd-dns-name",
"linkerd-error",
"thiserror",
"url",
]

[[package]]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -3461,7 +3471,7 @@ dependencies = [
"futures-channel",
"futures-io",
"futures-util",
"idna",
"idna 0.2.3",
"ipnet",
"lazy_static",
"rand",
Expand Down Expand Up @@ -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",
]

Expand Down
2 changes: 1 addition & 1 deletion linkerd/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
39 changes: 36 additions & 3 deletions linkerd/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -52,15 +52,15 @@ impl Id {
}

pub fn parse_uri(s: &str) -> Result<Self, InvalidId> {
http::Uri::try_from(s)
url::Url::parse(s)
.map(Self::Uri)
.map_err(|e| InvalidId(e.into()))
}

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(),
}
}
}
Expand All @@ -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());
}
}

0 comments on commit 0359465

Please sign in to comment.