Share address and TLS types across backend configurations
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
use crate::{ConfigError, driver::ObjectStoreDriver};
|
||||
use crate::{
|
||||
ConfigError,
|
||||
driver::ObjectStoreDriver,
|
||||
transport::{Address, Tls},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -9,7 +13,7 @@ pub struct ObjectStorage {
|
||||
pub driver: ObjectStoreDriver,
|
||||
pub bucket: String,
|
||||
pub region: String,
|
||||
pub endpoint: Option<String>,
|
||||
pub endpoint: Option<Address>,
|
||||
}
|
||||
|
||||
impl ObjectStorage {
|
||||
@@ -17,18 +21,29 @@ impl ObjectStorage {
|
||||
if self.bucket.trim().is_empty() || self.region.trim().is_empty() {
|
||||
return Err(ConfigError::ObjectStorage);
|
||||
}
|
||||
if let Some(endpoint) = &self.endpoint {
|
||||
let url = Url::parse(endpoint).map_err(|_| ConfigError::ObjectStorage)?;
|
||||
if !matches!(url.scheme(), "http" | "https")
|
||||
|| url.host_str().is_none()
|
||||
|| !url.username().is_empty()
|
||||
|| url.password().is_some()
|
||||
|| url.query().is_some()
|
||||
|| url.fragment().is_some()
|
||||
{
|
||||
return Err(ConfigError::ObjectStorage);
|
||||
}
|
||||
}
|
||||
self.tls()?;
|
||||
Ok(self.clone())
|
||||
}
|
||||
|
||||
/// The TLS policy implied by this endpoint. The default S3 endpoint uses HTTPS.
|
||||
pub fn tls(&self) -> Result<Tls, ConfigError> {
|
||||
let Some(endpoint) = &self.endpoint else {
|
||||
return Ok(Tls::VerifyFull);
|
||||
};
|
||||
let url =
|
||||
Url::parse(endpoint.connection_string()).map_err(|_| ConfigError::ObjectStorage)?;
|
||||
if url.host_str().is_none()
|
||||
|| !url.username().is_empty()
|
||||
|| url.password().is_some()
|
||||
|| url.query().is_some()
|
||||
|| url.fragment().is_some()
|
||||
{
|
||||
return Err(ConfigError::ObjectStorage);
|
||||
}
|
||||
match url.scheme() {
|
||||
"https" => Ok(Tls::VerifyFull),
|
||||
"http" => Ok(Tls::Disabled),
|
||||
_ => Err(ConfigError::ObjectStorage),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user