1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
// Copyright (c) 2021 Jason White
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#![deny(clippy::all)]
mod app;
mod error;
mod hyperext;
mod lfs;
mod logger;
mod lru;
mod sha256;
mod storage;
mod util;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
use futures::future::{self, Future, TryFutureExt};
use hyper::{
self,
server::conn::{AddrIncoming, AddrStream},
service::make_service_fn,
};
use crate::app::App;
use crate::error::Error;
use crate::logger::Logger;
use crate::storage::{Cached, Disk, Encrypted, Retrying, Storage, Verify, S3};
#[cfg(feature = "faulty")]
use crate::storage::Faulty;
/// Represents a running LFS server.
pub trait Server: Future<Output = hyper::Result<()>> {
/// Returns the local address this server is bound to.
fn addr(&self) -> SocketAddr;
}
impl<S, E> Server for hyper::Server<AddrIncoming, S, E>
where
hyper::Server<AddrIncoming, S, E>: Future<Output = hyper::Result<()>>,
{
fn addr(&self) -> SocketAddr {
self.local_addr()
}
}
#[derive(Debug)]
pub struct Cache {
/// Path to the cache.
dir: PathBuf,
/// Maximum size of the cache, in bytes.
max_size: u64,
}
impl Cache {
pub fn new(dir: PathBuf, max_size: u64) -> Self {
Self { dir, max_size }
}
}
#[derive(Debug)]
pub struct S3ServerBuilder {
bucket: String,
key: Option<[u8; 32]>,
prefix: Option<String>,
cdn: Option<String>,
cache: Option<Cache>,
}
impl S3ServerBuilder {
pub fn new(bucket: String) -> Self {
Self {
bucket,
prefix: None,
cdn: None,
key: None,
cache: None,
}
}
/// Sets the bucket to use.
pub fn bucket(&mut self, bucket: String) -> &mut Self {
self.bucket = bucket;
self
}
/// Sets the encryption key to use.
pub fn key(&mut self, key: [u8; 32]) -> &mut Self {
self.key = Some(key);
self
}
/// Sets the prefix to use.
pub fn prefix(&mut self, prefix: String) -> &mut Self {
self.prefix = Some(prefix);
self
}
/// Sets the base URL of the CDN to use. This is incompatible with
/// encryption since the LFS object is not sent to Rudolfs.
pub fn cdn(&mut self, url: String) -> &mut Self {
self.cdn = Some(url);
self
}
/// Sets the cache to use. If not specified, then no local disk cache is
/// used. All objects will get sent directly to S3.
pub fn cache(&mut self, cache: Cache) -> &mut Self {
self.cache = Some(cache);
self
}
/// Spawns the server. The server must be awaited on in order to accept
/// incoming client connections and run.
pub async fn spawn(
mut self,
addr: SocketAddr,
) -> Result<Box<dyn Server + Unpin + Send>, Box<dyn std::error::Error>>
{
let prefix = self.prefix.unwrap_or_else(|| String::from("lfs"));
if self.cdn.is_some() {
log::warn!(
"A CDN was specified. Since uploads and downloads do not flow \
through Rudolfs in this case, they will *not* be encrypted."
);
if self.cache.take().is_some() {
log::warn!(
"A local disk cache does not work with a CDN and will be \
disabled."
);
}
}
let s3 = S3::new(self.bucket, prefix, self.cdn)
.map_err(Error::from)
.await?;
// Retry certain operations to S3 to make it more reliable.
let s3 = Retrying::new(s3);
// Add a little instability for testing purposes.
#[cfg(feature = "faulty")]
let s3 = Faulty::new(s3);
match self.cache {
Some(cache) => {
// Use disk storage as a cache.
let disk = Disk::new(cache.dir).map_err(Error::from).await?;
#[cfg(feature = "faulty")]
let disk = Faulty::new(disk);
let cache = Cached::new(cache.max_size, disk, s3).await?;
match self.key {
Some(key) => {
let storage = Verify::new(Encrypted::new(key, cache));
Ok(Box::new(spawn_server(storage, &addr)))
}
None => {
let storage = Verify::new(cache);
Ok(Box::new(spawn_server(storage, &addr)))
}
}
}
None => match self.key {
Some(key) => {
let storage = Verify::new(Encrypted::new(key, s3));
Ok(Box::new(spawn_server(storage, &addr)))
}
None => {
let storage = Verify::new(s3);
Ok(Box::new(spawn_server(storage, &addr)))
}
},
}
}
/// Spawns the server and runs it to completion. This will run forever
/// unless there is an error or the server shuts down gracefully.
pub async fn run(
self,
addr: SocketAddr,
) -> Result<(), Box<dyn std::error::Error>> {
let server = self.spawn(addr).await?;
log::info!("Listening on {}", server.addr());
server.await?;
Ok(())
}
}
#[derive(Debug)]
pub struct LocalServerBuilder {
path: PathBuf,
key: Option<[u8; 32]>,
cache: Option<Cache>,
}
impl LocalServerBuilder {
/// Creates a local server builder. `path` is the path to the folder where
/// all of the LFS data will be stored.
pub fn new(path: PathBuf) -> Self {
Self {
path,
key: None,
cache: None,
}
}
/// Sets the encryption key to use.
pub fn key(&mut self, key: [u8; 32]) -> &mut Self {
self.key = Some(key);
self
}
/// Sets the cache to use. If not specified, then no local disk cache is
/// used. It is uncommon to want to use this when the object storage is
/// already local. However, a cache may be useful when the data storage path
/// is on a mounted network file system. In such a case, the network file
/// system could be slow and the local disk storage could be fast.
pub fn cache(&mut self, cache: Cache) -> &mut Self {
self.cache = Some(cache);
self
}
/// Spawns the server. The server must be awaited on in order to accept
/// incoming client connections and run.
pub async fn spawn(
self,
addr: SocketAddr,
) -> Result<Box<dyn Server + Unpin + Send>, Box<dyn std::error::Error>>
{
let storage = Disk::new(self.path).map_err(Error::from).await?;
match self.key {
Some(key) => {
let storage = Verify::new(Encrypted::new(key, storage));
log::info!("Local disk storage initialized (with encryption).");
Ok(Box::new(spawn_server(storage, &addr)))
}
None => {
let storage = Verify::new(storage);
log::info!(
"Local disk storage initialized (without encryption)."
);
Ok(Box::new(spawn_server(storage, &addr)))
}
}
}
/// Spawns the server and runs it to completion. This will run forever
/// unless there is an error or the server shuts down gracefully.
pub async fn run(
self,
addr: SocketAddr,
) -> Result<(), Box<dyn std::error::Error>> {
let server = self.spawn(addr).await?;
log::info!("Listening on {}", server.addr());
server.await?;
Ok(())
}
}
fn spawn_server<S>(storage: S, addr: &SocketAddr) -> impl Server
where
S: Storage + Send + Sync + 'static,
S::Error: Into<Error>,
Error: From<S::Error>,
{
let storage = Arc::new(storage);
let new_service = make_service_fn(move |socket: &AddrStream| {
// Create our app.
let service = App::new(storage.clone());
// Add logging middleware
future::ok::<_, Infallible>(Logger::new(socket.remote_addr(), service))
});
hyper::Server::bind(addr).serve(new_service)
}