Share address and TLS types across backend configurations
This commit is contained in:
@@ -7,6 +7,7 @@ mod format;
|
||||
mod object_storage;
|
||||
mod resource;
|
||||
mod sql;
|
||||
mod transport;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
@@ -19,7 +20,8 @@ pub use object_storage::ObjectStorage;
|
||||
pub use resource::{ResolvedResource, Resource, ResourceRef};
|
||||
/// Compatibility name for the SQL cluster configuration.
|
||||
pub use sql::SqlCluster as Cluster;
|
||||
pub use sql::{Address, ResolvedSql, SqlCluster, Tls};
|
||||
pub use sql::{ResolvedSql, SqlCluster};
|
||||
pub use transport::{Address, Tls};
|
||||
|
||||
pub const DOCUMENTS: &str = "documents";
|
||||
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,13 @@
|
||||
use std::{fmt, path::Path};
|
||||
use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
use crate::{ConfigError, driver::SqlDriver};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Tls {
|
||||
#[default]
|
||||
VerifyFull,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
/// A database address. Debug output includes the full value, which may contain credentials.
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct Address(String);
|
||||
|
||||
impl From<&str> for Address {
|
||||
fn from(value: &str) -> Self {
|
||||
Self(value.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Address {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Address {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self.0, formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl Address {
|
||||
pub fn connection_string(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
use crate::{
|
||||
ConfigError,
|
||||
driver::SqlDriver,
|
||||
transport::{Address, Tls},
|
||||
};
|
||||
|
||||
/// A SQL target with a primary connection and optional read replicas.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
@@ -153,7 +120,7 @@ fn resolve_address(
|
||||
Tls::Disabled => tls_disabled,
|
||||
};
|
||||
url.query_pairs_mut().append_pair(tls_key, tls_mode);
|
||||
Ok(Address(url.into()))
|
||||
Ok(Address::from(String::from(url)))
|
||||
}
|
||||
|
||||
fn sqlite_url(path: &str, read_only: bool) -> Result<Address, ConfigError> {
|
||||
@@ -167,5 +134,5 @@ fn sqlite_url(path: &str, read_only: bool) -> Result<Address, ConfigError> {
|
||||
.strip_prefix("file:")
|
||||
.ok_or(ConfigError::SqlitePath)?;
|
||||
let mode = if read_only { "ro" } else { "rwc" };
|
||||
Ok(Address(format!("sqlite:{file}?mode={mode}")))
|
||||
Ok(Address::from(format!("sqlite:{file}?mode={mode}")))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//! Address and TLS settings shared by backend configurations.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Tls {
|
||||
#[default]
|
||||
VerifyFull,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
/// A backend connection string, endpoint URL, or local path.
|
||||
/// Debug output includes the full value, which may contain credentials.
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct Address(String);
|
||||
|
||||
impl From<&str> for Address {
|
||||
fn from(value: &str) -> Self {
|
||||
Self(value.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Address {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Address {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self.0, formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl Address {
|
||||
pub fn connection_string(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user