|
| 1 | +use std::net::SocketAddr; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | +use std::sync::Arc; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use anyhow::Context; |
| 7 | +use hyper::client::HttpConnector; |
| 8 | +use sha256::try_digest; |
| 9 | +use tonic::transport::Channel; |
| 10 | + |
| 11 | +use crate::auth::{self, Auth}; |
| 12 | +use crate::net::{AddrIncoming, Connector}; |
| 13 | + |
| 14 | +pub struct RpcClientConfig<C = HttpConnector> { |
| 15 | + pub remote_url: String, |
| 16 | + pub connector: C, |
| 17 | + pub tls_config: Option<TlsConfig>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<C: Connector> RpcClientConfig<C> { |
| 21 | + pub(crate) async fn configure(self) -> anyhow::Result<(Channel, tonic::transport::Uri)> { |
| 22 | + let uri = tonic::transport::Uri::from_maybe_shared(self.remote_url)?; |
| 23 | + let mut builder = Channel::builder(uri.clone()); |
| 24 | + if let Some(ref tls_config) = self.tls_config { |
| 25 | + let cert_pem = std::fs::read_to_string(&tls_config.cert)?; |
| 26 | + let key_pem = std::fs::read_to_string(&tls_config.key)?; |
| 27 | + let identity = tonic::transport::Identity::from_pem(cert_pem, key_pem); |
| 28 | + |
| 29 | + let ca_cert_pem = std::fs::read_to_string(&tls_config.ca_cert)?; |
| 30 | + let ca_cert = tonic::transport::Certificate::from_pem(ca_cert_pem); |
| 31 | + |
| 32 | + let tls_config = tonic::transport::ClientTlsConfig::new() |
| 33 | + .identity(identity) |
| 34 | + .ca_certificate(ca_cert) |
| 35 | + .domain_name("sqld"); |
| 36 | + builder = builder.tls_config(tls_config)?; |
| 37 | + } |
| 38 | + |
| 39 | + let channel = builder.connect_with_connector_lazy(self.connector); |
| 40 | + |
| 41 | + Ok((channel, uri)) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Clone)] |
| 46 | +pub struct TlsConfig { |
| 47 | + pub cert: PathBuf, |
| 48 | + pub key: PathBuf, |
| 49 | + pub ca_cert: PathBuf, |
| 50 | +} |
| 51 | + |
| 52 | +pub struct RpcServerConfig<A = AddrIncoming> { |
| 53 | + pub acceptor: A, |
| 54 | + pub addr: SocketAddr, |
| 55 | + pub tls_config: Option<TlsConfig>, |
| 56 | +} |
| 57 | + |
| 58 | +pub struct UserApiConfig<A = AddrIncoming> { |
| 59 | + pub hrana_ws_acceptor: Option<A>, |
| 60 | + pub http_acceptor: Option<A>, |
| 61 | + pub enable_http_console: bool, |
| 62 | + pub self_url: Option<String>, |
| 63 | + pub http_auth: Option<String>, |
| 64 | + pub auth_jwt_key: Option<String>, |
| 65 | +} |
| 66 | + |
| 67 | +impl<A> UserApiConfig<A> { |
| 68 | + pub fn get_auth(&self) -> anyhow::Result<Auth> { |
| 69 | + let mut auth = Auth::default(); |
| 70 | + |
| 71 | + if let Some(arg) = self.http_auth.as_deref() { |
| 72 | + if let Some(param) = auth::parse_http_basic_auth_arg(arg)? { |
| 73 | + auth.http_basic = Some(param); |
| 74 | + tracing::info!("Using legacy HTTP basic authentication"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if let Some(jwt_key) = self.auth_jwt_key.as_deref() { |
| 79 | + let jwt_key = |
| 80 | + auth::parse_jwt_key(jwt_key).context("Could not parse JWT decoding key")?; |
| 81 | + auth.jwt_key = Some(jwt_key); |
| 82 | + tracing::info!("Using JWT-based authentication"); |
| 83 | + } |
| 84 | + |
| 85 | + auth.disabled = auth.http_basic.is_none() && auth.jwt_key.is_none(); |
| 86 | + if auth.disabled { |
| 87 | + tracing::warn!( |
| 88 | + "No authentication specified, the server will not require authentication" |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + Ok(auth) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub struct AdminApiConfig<A = AddrIncoming> { |
| 97 | + pub acceptor: A, |
| 98 | +} |
| 99 | + |
| 100 | +#[derive(Clone)] |
| 101 | +pub struct DbConfig { |
| 102 | + pub extensions_path: Option<Arc<Path>>, |
| 103 | + pub bottomless_replication: Option<bottomless::replicator::Options>, |
| 104 | + pub max_log_size: u64, |
| 105 | + pub max_log_duration: Option<f32>, |
| 106 | + pub soft_heap_limit_mb: Option<usize>, |
| 107 | + pub hard_heap_limit_mb: Option<usize>, |
| 108 | + pub max_response_size: u64, |
| 109 | + pub max_total_response_size: u64, |
| 110 | + pub snapshot_exec: Option<String>, |
| 111 | + pub checkpoint_interval: Option<Duration>, |
| 112 | +} |
| 113 | + |
| 114 | +impl DbConfig { |
| 115 | + pub fn validate_extensions(&self) -> anyhow::Result<Arc<[PathBuf]>> { |
| 116 | + let mut valid_extensions = vec![]; |
| 117 | + if let Some(ext_dir) = &self.extensions_path { |
| 118 | + let extensions_list = ext_dir.join("trusted.lst"); |
| 119 | + |
| 120 | + let file_contents = std::fs::read_to_string(&extensions_list) |
| 121 | + .with_context(|| format!("can't read {}", &extensions_list.display()))?; |
| 122 | + |
| 123 | + let extensions = file_contents.lines().filter(|c| !c.is_empty()); |
| 124 | + |
| 125 | + for line in extensions { |
| 126 | + let mut ext_info = line.trim().split_ascii_whitespace(); |
| 127 | + |
| 128 | + let ext_sha = ext_info.next().ok_or_else(|| { |
| 129 | + anyhow::anyhow!("invalid line on {}: {}", &extensions_list.display(), line) |
| 130 | + })?; |
| 131 | + let ext_fname = ext_info.next().ok_or_else(|| { |
| 132 | + anyhow::anyhow!("invalid line on {}: {}", &extensions_list.display(), line) |
| 133 | + })?; |
| 134 | + |
| 135 | + anyhow::ensure!( |
| 136 | + ext_info.next().is_none(), |
| 137 | + "extension list seem to contain a filename with whitespaces. Rejected" |
| 138 | + ); |
| 139 | + |
| 140 | + let extension_full_path = ext_dir.join(ext_fname); |
| 141 | + let digest = try_digest(extension_full_path.as_path()).with_context(|| { |
| 142 | + format!( |
| 143 | + "Failed to get sha256 digest, while trying to read {}", |
| 144 | + extension_full_path.display() |
| 145 | + ) |
| 146 | + })?; |
| 147 | + |
| 148 | + anyhow::ensure!( |
| 149 | + digest == ext_sha, |
| 150 | + "sha256 differs for {}. Got {}", |
| 151 | + ext_fname, |
| 152 | + digest |
| 153 | + ); |
| 154 | + valid_extensions.push(extension_full_path); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + Ok(valid_extensions.into()) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +pub struct HeartbeatConfig { |
| 163 | + pub heartbeat_url: String, |
| 164 | + pub heartbeat_period: Duration, |
| 165 | + pub heartbeat_auth: Option<String>, |
| 166 | +} |
0 commit comments