use std::net::Ipv4Addr;
use super::sigv4::{region_refusal, valid_region};
use super::{Address, Addressing, Resolved};
use crate::{Error, Result};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Location {
address: Address,
region: String,
endpoint: String,
addressing: Addressing,
}
impl Location {
pub fn new(
address: Address,
region: &str,
endpoint: Option<&str>,
addressing: Option<Addressing>,
) -> Result<Location> {
if !valid_region(region) {
return Err(Error::InvalidInput(region_refusal(region, "S3 location")));
}
let aws = endpoint.is_none();
let endpoint = match endpoint {
Some(endpoint) => checked_endpoint(endpoint)?,
None => format!("https://s3.{region}.amazonaws.com"),
};
let addressing = addressing.unwrap_or(if aws && !address.bucket().contains('.') {
Addressing::Virtual
} else {
Addressing::Path
});
let location = Location {
address,
region: region.to_owned(),
endpoint,
addressing,
};
if location.addressing == Addressing::Virtual {
location.check_virtual()?;
}
Ok(location)
}
pub fn from_resolved(address: Address, resolved: &Resolved) -> Result<Location> {
let endpoint = resolved.endpoint.as_ref();
Location::new(
address,
&resolved.region.value,
endpoint.map(|endpoint| endpoint.value.as_str()),
resolved.addressing.as_ref().map(|style| style.value),
)
.map_err(|e| match (e, endpoint) {
(Error::InvalidInput(message), Some(endpoint)) if message.contains("endpoint") => {
Error::InvalidInput(format!("{message} (from {})", endpoint.origin))
}
(e, _) => e,
})
}
pub fn address(&self) -> &Address {
&self.address
}
pub fn region(&self) -> &str {
&self.region
}
pub fn addressing(&self) -> Addressing {
self.addressing
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
pub fn bucket_url(&self) -> String {
let bucket = self.address.bucket();
match self.addressing {
Addressing::Path => format!("{}/{bucket}", self.endpoint),
Addressing::Virtual => {
let (scheme, authority) = split_scheme(&self.endpoint);
format!("{scheme}://{bucket}.{authority}")
}
}
}
pub fn repository_url(&self) -> String {
let prefix = self.address.prefix();
if prefix.is_empty() {
self.bucket_url()
} else {
format!("{}/{prefix}", self.bucket_url())
}
}
fn check_virtual(&self) -> Result<()> {
let (scheme, authority) = split_scheme(&self.endpoint);
if scheme.eq_ignore_ascii_case("https") && self.address.bucket().contains('.') {
return Err(Error::InvalidInput(format!(
"S3 location: the bucket name {:?} holds a '.', so virtual-host style over \
https names a host that the certificate of the endpoint does not match: use \
path style",
self.address.bucket()
)));
}
let host = match authority.rfind(':') {
Some(at) if !authority.ends_with(']') => &authority[..at],
_ => authority,
};
if host.starts_with('[') || host.parse::<Ipv4Addr>().is_ok() {
return Err(Error::InvalidInput(format!(
"S3 location: the endpoint host {host} is an IP address, so it cannot carry the \
bucket name in virtual-host style: use path style"
)));
}
Ok(())
}
}
fn checked_endpoint(endpoint: &str) -> Result<String> {
ostrya_fetch::check_base_url(endpoint)
.map_err(|e| Error::InvalidInput(format!("S3 endpoint: {e}")))?;
let (_, rest) = endpoint
.split_once("://")
.expect("a checked base URL has a scheme");
let path = rest.find('/').map_or("", |at| &rest[at..]);
if !path.is_empty() && path != "/" {
return Err(Error::InvalidInput(format!(
"S3 endpoint: the endpoint {endpoint} has the path {path}: an endpoint has no path"
)));
}
Ok(endpoint.strip_suffix('/').unwrap_or(endpoint).to_owned())
}
fn split_scheme(url: &str) -> (&str, &str) {
url.split_once("://").unwrap_or(("https", url))
}
#[cfg(test)]
mod tests {
use super::*;
fn location(
address: &str,
endpoint: Option<&str>,
addressing: Option<Addressing>,
) -> Result<Location> {
Location::new(
Address::parse(address).unwrap(),
"eu-central-1",
endpoint,
addressing,
)
}
fn refusal(address: &str, endpoint: Option<&str>, addressing: Option<Addressing>) -> String {
match location(address, endpoint, addressing) {
Ok(found) => panic!("{address} {endpoint:?}: {found:?}"),
Err(Error::InvalidInput(message)) => message,
Err(other) => panic!("{other}"),
}
}
#[test]
fn aws_is_the_endpoint_when_none_is_set() {
let aws = location("s3://bucket/repo", None, None).unwrap();
assert_eq!(aws.endpoint(), "https://s3.eu-central-1.amazonaws.com");
assert_eq!(aws.addressing(), Addressing::Virtual);
assert_eq!(
aws.bucket_url(),
"https://bucket.s3.eu-central-1.amazonaws.com"
);
assert_eq!(
aws.repository_url(),
"https://bucket.s3.eu-central-1.amazonaws.com/repo"
);
let path = location("s3://bucket/repo", None, Some(Addressing::Path)).unwrap();
assert_eq!(
path.bucket_url(),
"https://s3.eu-central-1.amazonaws.com/bucket"
);
assert_eq!(
path.repository_url(),
"https://s3.eu-central-1.amazonaws.com/bucket/repo"
);
}
#[test]
fn the_default_style_follows_the_endpoint_and_the_bucket_name() {
let cases = [
("s3://bucket", None, None, Addressing::Virtual),
("s3://my.bucket", None, None, Addressing::Path),
(
"s3://bucket",
Some("http://127.0.0.1:9000"),
None,
Addressing::Path,
),
(
"s3://bucket",
Some("https://s3.example.com/"),
None,
Addressing::Path,
),
(
"s3://bucket",
Some("https://s3.example.com"),
Some(Addressing::Virtual),
Addressing::Virtual,
),
(
"s3://bucket",
None,
Some(Addressing::Path),
Addressing::Path,
),
(
"s3://my.bucket",
Some("http://s3.example.com"),
Some(Addressing::Virtual),
Addressing::Virtual,
),
];
for (address, endpoint, set, style) in cases {
let found = location(address, endpoint, set).unwrap();
assert_eq!(found.addressing(), style, "{address} {endpoint:?} {set:?}");
}
}
#[test]
fn the_urls_of_an_endpoint_of_the_caller() {
let path = location("s3://b-1", Some("http://127.0.0.1:9000/"), None).unwrap();
assert_eq!(path.endpoint(), "http://127.0.0.1:9000");
assert_eq!(path.bucket_url(), "http://127.0.0.1:9000/b-1");
assert_eq!(path.repository_url(), "http://127.0.0.1:9000/b-1");
let virtual_host = location(
"s3://b-1/a/b",
Some("https://s3.example.com:8443"),
Some(Addressing::Virtual),
)
.unwrap();
assert_eq!(virtual_host.bucket_url(), "https://b-1.s3.example.com:8443");
assert_eq!(
virtual_host.repository_url(),
"https://b-1.s3.example.com:8443/a/b"
);
}
#[test]
fn an_endpoint_out_of_the_rule_is_refused() {
for endpoint in [
"ftp://s3.example.com",
"https://s3.example.com/path",
"https://s3.example.com/?x=1",
"https://key:secret@s3.example.com",
"https://s3.example.com/#f",
"https://",
"s3.example.com",
] {
let message = refusal("s3://bucket", Some(endpoint), None);
assert!(message.contains("S3 endpoint"), "{endpoint}: {message}");
assert!(!message.contains("secret"), "{message}");
}
}
#[test]
fn virtual_host_style_is_refused_where_the_host_cannot_carry_it() {
let dotted = refusal(
"s3://my.bucket",
Some("https://s3.example.com"),
Some(Addressing::Virtual),
);
assert!(dotted.contains("certificate"), "{dotted}");
let dotted_aws = refusal("s3://my.bucket", None, Some(Addressing::Virtual));
assert!(dotted_aws.contains("certificate"), "{dotted_aws}");
for endpoint in [
"http://127.0.0.1:9000",
"http://[::1]:9000",
"http://10.0.0.1",
] {
let message = refusal("s3://bucket", Some(endpoint), Some(Addressing::Virtual));
assert!(message.contains("IP address"), "{endpoint}: {message}");
}
}
#[test]
fn a_region_out_of_the_rule_is_refused() {
let address = Address::parse("s3://bucket").unwrap();
for region in ["", "eu central", "eu/central"] {
let err = Location::new(address.clone(), region, None, None).unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{err}");
}
}
}