| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | | use crate::db::{now_ms, Pool}; |
| 11 | | use crate::Shared; |
| 12 | | use oxy_strata::Credentials; |
| 13 | | use std::collections::HashMap; |
| 14 | | use std::sync::RwLock; |
| 15 | |
|
| 16 | | #[derive(Clone, Debug, sqlx::FromRow)] |
| 17 | | pub struct Storage { |
| 18 | | pub repo_id: i64, |
| 19 | | pub url: String, |
| 20 | | |
| 21 | | pub credentials: Vec<u8>, |
| 22 | | pub created_ms: i64, |
| 23 | | pub checked_ms: Option<i64>, |
| 24 | | pub check_result: String, |
| 25 | | } |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | | #[derive(Default)] |
| 30 | | struct Known { |
| 31 | | credentials: HashMap<String, Credentials>, |
| 32 | | |
| 33 | | urls: HashMap<std::path::PathBuf, String>, |
| 34 | | } |
| 35 | |
|
| 36 | | static KNOWN: RwLock<Option<Known>> = RwLock::new(None); |
| 37 | |
|
| 38 | 0× | fn lookup(url: &str) -> Option<Credentials> { |
| 39 | 0× | KNOWN.read().ok()?.as_ref()?.credentials.get(url).cloned() |
| 40 | 0× | } |
| 41 | |
|
| 42 | 0× | fn remember(dir: std::path::PathBuf, url: &str, credentials: Credentials) { |
| 43 | 0× | let mut known = KNOWN.write().unwrap_or_else(|p| p.into_inner()); |
| 44 | 0× | let known = known.get_or_insert_with(Known::default); |
| 45 | 0× | known.credentials.insert(url.to_string(), credentials); |
| 46 | 0× | known.urls.insert(dir, url.to_string()); |
| 47 | 0× | } |
| 48 | |
|
| 49 | 1× | fn forget(dir: &std::path::Path) { |
| 50 | 1× | let mut known = KNOWN.write().unwrap_or_else(|p| p.into_inner()); |
| 51 | 1× | if let Some(k) = known.as_mut() { |
| 52 | 0× | if let Some(url) = k.urls.remove(dir) { |
| 53 | 0× | k.credentials.remove(&url); |
| 54 | 0× | } |
| 55 | 1× | } |
| 56 | 1× | } |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | | #[derive(Clone, Debug, PartialEq)] |
| 62 | | pub struct StoreBadge { |
| 63 | | |
| 64 | | pub kind: &'static str, |
| 65 | | pub provider: &'static str, |
| 66 | | |
| 67 | | pub place: String, |
| 68 | | pub prefix: String, |
| 69 | | pub region: Option<String>, |
| 70 | | |
| 71 | | pub url: String, |
| 72 | | } |
| 73 | |
|
| 74 | | impl StoreBadge { |
| 75 | | |
| 76 | | |
| 77 | 6× | pub fn of(url: &str) -> Option<StoreBadge> { |
| 78 | 6× | let (scheme, rest) = url.split_once("://")?; |
| 79 | 6× | let (path, query) = rest.split_once('?').unwrap_or((rest, "")); |
| 80 | 6× | let region = query.split('&').find_map(|kv| kv.strip_prefix("region=")).filter(|r| !r.is_empty()).map(str::to_string); |
| 81 | 6× | let mut parts = path.trim_matches('/').splitn(if scheme == "az" { 3 } else { 2 }, '/'); |
| 82 | 6× | let (kind, provider, place) = match scheme { |
| 83 | 6× | "gs" => ("gcs", "Google Cloud Storage", parts.next()?.to_string()), |
| 84 | 5× | "s3" => ("s3", "Amazon S3", parts.next()?.to_string()), |
| 85 | 3× | "az" => ("azure", "Azure Blob Storage", format!("{} / {}", parts.next()?, parts.next()?)), |
| 86 | 1× | _ => return None, |
| 87 | | }; |
| 88 | 4× | Some(StoreBadge { kind, provider, place, prefix: parts.next().unwrap_or("").to_string(), region, url: url.to_string() }) |
| 89 | 6× | } |
| 90 | | } |
| 91 | |
|
| 92 | 535× | pub fn location(app: &Shared, owner: &str, name: &str) -> Option<String> { |
| 93 | 535× | let dir = app.repos.dir(&crate::repos::RepoName::new(owner, name)?); |
| 94 | 535× | KNOWN.read().ok()?.as_ref()?.urls.get(&dir).cloned() |
| 95 | 535× | } |
| 96 | |
|
| 97 | |
|
| 98 | 0× | async fn dir_of(app: &Shared, repo_id: i64) -> Option<std::path::PathBuf> { |
| 99 | 0× | let row = crate::db::repos::get_by_id(&app.db, repo_id).await.ok()??; |
| 100 | 0× | Some(app.repos.dir(&crate::repos::RepoName::new(&row.owner, &row.name)?)) |
| 101 | 0× | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | 201× | pub async fn load(app: &Shared) -> sqlx::Result<()> { |
| 106 | 201× | oxy_strata::register(); |
| 107 | 201× | oxy_strata::set_credentials_lookup(lookup); |
| 108 | 201× | for s in all(&app.db).await? { |
| 109 | 0× | match app.secret_key.open(&s.credentials).map(|j| Credentials::from_json(&j)) { |
| 110 | 0× | Some(Ok(c)) => match dir_of(app, s.repo_id).await { |
| 111 | 0× | Some(dir) => remember(dir, &s.url, c), |
| 112 | 0× | None => tracing::warn!("storage: repository {} has a store but no row", s.repo_id), |
| 113 | | }, |
| 114 | 0× | _ => tracing::warn!("storage: the credentials of repository {} do not open under keys/secrets.key", s.repo_id), |
| 115 | | } |
| 116 | | } |
| 117 | 201× | Ok(()) |
| 118 | 201× | } |
| 119 | |
|
| 120 | 201× | pub async fn all(pool: &Pool) -> sqlx::Result<Vec<Storage>> { |
| 121 | 201× | sqlx::query_as("SELECT * FROM repo_storage").fetch_all(pool).await |
| 122 | 201× | } |
| 123 | |
|
| 124 | 141× | pub async fn of(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<Storage>> { |
| 125 | 141× | sqlx::query_as("SELECT * FROM repo_storage WHERE repo_id = $1").bind(repo_id).fetch_optional(pool).await |
| 126 | 141× | } |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | 17× | pub fn check_url(url: &str) -> Result<oxy_strata::StoreUrl, String> { |
| 133 | 17× | let scheme = url.trim().split_once("://").map(|(s, _)| s).unwrap_or(""); |
| 134 | 17× | if !matches!(scheme, "s3" | "gs" | "az") { |
| 135 | 3× | return Err("a store is s3://BUCKET/PREFIX, gs://BUCKET/PREFIX or az://ACCOUNT/CONTAINER/PREFIX".into()); |
| 136 | 14× | } |
| 137 | 14× | let parsed = oxy_strata::StoreUrl::parse(url.trim()).map_err(|e| e.to_string())?; |
| 138 | 14× | if parsed.scheme == "az" && parsed.path.is_empty() { |
| 139 | 2× | return Err("an Azure store names the account and the container: az://ACCOUNT/CONTAINER/PREFIX".into()); |
| 140 | 12× | } |
| 141 | 12× | if let Some(k) = parsed.params.keys().find(|k| k.as_str() != "region") { |
| 142 | 6× | return Err(format!("?{k}= is not allowed here: the only setting a hub accepts is ?region=")); |
| 143 | 6× | } |
| 144 | 6× | Ok(parsed) |
| 145 | 17× | } |
| 146 | |
|
| 147 | |
|
| 148 | 1× | pub fn credentials_from( |
| 149 | 1× | scheme: &str, |
| 150 | 1× | access_key_id: &str, |
| 151 | 1× | secret_access_key: &str, |
| 152 | 1× | service_account_key: &str, |
| 153 | 1× | tenant_id: &str, |
| 154 | 1× | client_id: &str, |
| 155 | 1× | client_secret: &str, |
| 156 | 1× | ) -> Result<Credentials, String> { |
| 157 | 1× | let json = match scheme { |
| 158 | 1× | "s3" => serde_json::json!({ "kind": "aws", "access_key_id": access_key_id, "secret_access_key": secret_access_key }), |
| 159 | 0× | "gs" => serde_json::json!({ "kind": "gcp", "service_account_key": service_account_key }), |
| 160 | 0× | "az" => serde_json::json!({ "kind": "azure", "tenant_id": tenant_id, "client_id": client_id, "client_secret": client_secret }), |
| 161 | 0× | _ => return Err("unknown kind of store".into()), |
| 162 | | }; |
| 163 | 1× | Credentials::from_json(&json.to_string()).map_err(|e| e.to_string()) |
| 164 | 1× | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | 0× | pub async fn probe(url: &str, credentials: &Credentials, must_be_empty: bool) -> Result<String, String> { |
| 170 | 0× | let (url, credentials) = (url.to_string(), credentials.clone()); |
| 171 | 0× | tokio::task::spawn_blocking(move || { |
| 172 | | use oxy_core::strata::Store as _; |
| 173 | 0× | let store = oxy_strata::CloudStore::open_with(&url, Some(&credentials)).map_err(|e| e.to_string())?; |
| 174 | 0× | if must_be_empty { |
| 175 | 0× | let found = store.list("").map_err(|e| e.to_string())?; |
| 176 | 0× | if !found.is_empty() { |
| 177 | 0× | return Err(format!( |
| 178 | 0× | "{} already holds {} object(s): a new repository needs an empty bucket or prefix", |
| 179 | 0× | store.describe(), |
| 180 | 0× | found.len() |
| 181 | 0× | )); |
| 182 | 0× | } |
| 183 | 0× | } |
| 184 | 0× | let mut nonce = [0u8; 6]; |
| 185 | 0× | rand::RngCore::fill_bytes(&mut rand::rngs::OsRng, &mut nonce); |
| 186 | 0× | let prefix = format!("oxy-probe-{}", hex::encode(nonce)); |
| 187 | 0× | let passed = oxy_core::strata::conformance(&store, &prefix).map_err(|e| { |
| 188 | 0× | for e in store.list(&prefix).unwrap_or_default() { |
| 189 | 0× | let _ = store.delete(&e.key); |
| 190 | 0× | } |
| 191 | 0× | e.to_string() |
| 192 | 0× | })?; |
| 193 | 0× | Ok(format!("{} checks passed", passed.len())) |
| 194 | 0× | }) |
| 195 | 0× | .await |
| 196 | 0× | .map_err(|e| e.to_string())? |
| 197 | 0× | } |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | 0× | pub async fn create(app: &Shared, name: &crate::repos::RepoName, repo_id: i64, url: &str, credentials: Credentials) -> Result<(), String> { |
| 203 | 0× | let result = probe(url, &credentials, true).await?; |
| 204 | 0× | let sealed = app.secret_key.seal(&credentials.to_json()); |
| 205 | 0× | let dir = app.repos.dir(name); |
| 206 | 0× | remember(dir.clone(), url, credentials); |
| 207 | 0× | let made = { |
| 208 | 0× | let (app, name, url) = (app.clone(), name.clone(), url.to_string()); |
| 209 | 0× | tokio::task::spawn_blocking(move || app.repos.create_on(&name, &url).map(drop).map_err(|e| e.to_string())) |
| 210 | 0× | .await |
| 211 | 0× | .map_err(|e| e.to_string()) |
| 212 | 0× | .and_then(|r| r) |
| 213 | | }; |
| 214 | 0× | if let Err(e) = made { |
| 215 | 0× | forget(&dir); |
| 216 | 0× | return Err(format!("the repository could not be made in {url}: {e}")); |
| 217 | 0× | } |
| 218 | 0× | let now = now_ms(); |
| 219 | 0× | sqlx::query( |
| 220 | 0× | "INSERT INTO repo_storage (repo_id, url, credentials, created_ms, checked_ms, check_result) VALUES ($1, $2, $3, $4, $5, $6)", |
| 221 | 0× | ) |
| 222 | 0× | .bind(repo_id) |
| 223 | 0× | .bind(url) |
| 224 | 0× | .bind(sealed) |
| 225 | 0× | .bind(now) |
| 226 | 0× | .bind(now) |
| 227 | 0× | .bind(&result) |
| 228 | 0× | .execute(&app.db) |
| 229 | 0× | .await |
| 230 | 0× | .map_err(|e| e.to_string())?; |
| 231 | 0× | Ok(()) |
| 232 | 0× | } |
| 233 | |
|
| 234 | |
|
| 235 | 0× | pub async fn check(app: &Shared, s: &Storage) -> Result<String, String> { |
| 236 | 0× | let json = app.secret_key.open(&s.credentials).ok_or("the credentials do not open under this hub's key")?; |
| 237 | 0× | let credentials = Credentials::from_json(&json).map_err(|e| e.to_string())?; |
| 238 | 0× | let result = probe(&s.url, &credentials, false).await; |
| 239 | 0× | let text = match &result { |
| 240 | 0× | Ok(r) => r.clone(), |
| 241 | 0× | Err(e) => format!("failed: {e}"), |
| 242 | | }; |
| 243 | 0× | sqlx::query("UPDATE repo_storage SET checked_ms = $1, check_result = $2 WHERE repo_id = $3") |
| 244 | 0× | .bind(now_ms()) |
| 245 | 0× | .bind(&text) |
| 246 | 0× | .bind(s.repo_id) |
| 247 | 0× | .execute(&app.db) |
| 248 | 0× | .await |
| 249 | 0× | .map_err(|e| e.to_string())?; |
| 250 | 0× | result |
| 251 | 0× | } |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | 0× | pub async fn set_credentials(app: &Shared, s: &Storage, credentials: Credentials) -> Result<String, String> { |
| 256 | 0× | let result = probe(&s.url, &credentials, false).await?; |
| 257 | 0× | sqlx::query("UPDATE repo_storage SET credentials = $1, checked_ms = $2, check_result = $3 WHERE repo_id = $4") |
| 258 | 0× | .bind(app.secret_key.seal(&credentials.to_json())) |
| 259 | 0× | .bind(now_ms()) |
| 260 | 0× | .bind(&result) |
| 261 | 0× | .bind(s.repo_id) |
| 262 | 0× | .execute(&app.db) |
| 263 | 0× | .await |
| 264 | 0× | .map_err(|e| e.to_string())?; |
| 265 | 0× | if let Some(dir) = dir_of(app, s.repo_id).await { |
| 266 | 0× | remember(dir, &s.url, credentials); |
| 267 | 0× | } |
| 268 | 0× | Ok(result) |
| 269 | 0× | } |
| 270 | |
|
| 271 | |
|
| 272 | 1× | pub fn removed(app: &Shared, name: &crate::repos::RepoName) { |
| 273 | 1× | forget(&app.repos.dir(name)); |
| 274 | 1× | } |
| 275 | |
|
| 276 | |
|
| 277 | 1× | pub fn renamed(app: &Shared, from: &crate::repos::RepoName, to: &crate::repos::RepoName) { |
| 278 | 1× | let mut known = KNOWN.write().unwrap_or_else(|p| p.into_inner()); |
| 279 | 1× | if let Some(k) = known.as_mut() { |
| 280 | 0× | if let Some(url) = k.urls.remove(&app.repos.dir(from)) { |
| 281 | 0× | k.urls.insert(app.repos.dir(to), url); |
| 282 | 0× | } |
| 283 | 1× | } |
| 284 | 1× | } |
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 291 | | pub struct Policy { |
| 292 | | pub require_bucket: bool, |
| 293 | | pub allowed: Vec<String>, |
| 294 | | } |
| 295 | |
|
| 296 | | impl Policy { |
| 297 | | |
| 298 | | |
| 299 | 9× | pub fn permits(&self, org: &str, store: Option<&str>) -> Result<(), String> { |
| 300 | 6× | match store { |
| 301 | 2× | None if self.require_bucket => Err(format!( |
| 302 | | "{org} keeps every new repository in a bucket: choose “my own bucket” under Storage{}", |
| 303 | 2× | if self.allowed.is_empty() { String::new() } else { format!(" ({})", self.allowed.join(", ")) } |
| 304 | | )), |
| 305 | 5× | Some(url) if !self.allowed.is_empty() && !self.allowed.iter().any(|p| url.trim().starts_with(p.as_str())) => { |
| 306 | 2× | Err(format!("{org} allows only these stores: {}", self.allowed.join(", "))) |
| 307 | | } |
| 308 | 5× | _ => Ok(()), |
| 309 | | } |
| 310 | 9× | } |
| 311 | | } |
| 312 | |
|
| 313 | 36× | pub async fn policy(pool: &Pool, org_id: i64) -> sqlx::Result<Policy> { |
| 314 | 36× | let row: Option<(bool, String)> = sqlx::query_as("SELECT require_bucket, allowed FROM org_storage_policy WHERE org_id = $1") |
| 315 | 36× | .bind(org_id) |
| 316 | 36× | .fetch_optional(pool) |
| 317 | 36× | .await?; |
| 318 | 36× | Ok(row |
| 319 | 36× | .map(|(r, a)| Policy { require_bucket: r, allowed: a.lines().map(str::trim).filter(|l| !l.is_empty()).map(String::from).collect() }) |
| 320 | 36× | .unwrap_or_default()) |
| 321 | 36× | } |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | 2× | pub async fn set_policy(pool: &Pool, org_id: i64, require_bucket: bool, allowed: &str) -> Result<Policy, String> { |
| 326 | 2× | let mut prefixes = Vec::new(); |
| 327 | 3× | for p in allowed.split([',', '\n']).map(str::trim).filter(|p| !p.is_empty()) { |
| 328 | 3× | if p.contains('?') { |
| 329 | 1× | return Err(format!("{p}: a prefix names a bucket and a path, with no settings")); |
| 330 | 2× | } |
| 331 | 2× | check_url(p).map_err(|e| format!("{p}: {e}"))?; |
| 332 | 2× | prefixes.push(p.to_string()); |
| 333 | | } |
| 334 | 1× | sqlx::query( |
| 335 | 1× | "INSERT INTO org_storage_policy (org_id, require_bucket, allowed) VALUES ($1, $2, $3) \ |
| 336 | 1× | ON CONFLICT (org_id) DO UPDATE SET require_bucket = excluded.require_bucket, allowed = excluded.allowed", |
| 337 | 1× | ) |
| 338 | 1× | .bind(org_id) |
| 339 | 1× | .bind(require_bucket) |
| 340 | 1× | .bind(prefixes.join("\n")) |
| 341 | 1× | .execute(pool) |
| 342 | 1× | .await |
| 343 | 1× | .map_err(|e| e.to_string())?; |
| 344 | 1× | Ok(Policy { require_bucket, allowed: prefixes }) |
| 345 | 2× | } |
| 346 | |
|
| 347 | | #[cfg(test)] |
| 348 | | mod tests { |
| 349 | | use super::*; |
| 350 | |
|
| 351 | | #[test] |
| 352 | 1× | fn store_urls_a_hub_accepts() { |
| 353 | 1× | assert!(check_url("s3://bucket/repos/x?region=eu-west-2").is_ok()); |
| 354 | 1× | assert!(check_url("gs://bucket/x").is_ok()); |
| 355 | 1× | assert!(check_url("az://account/container/x").is_ok()); |
| 356 | 6× | for (bad, why) in [ |
| 357 | 1× | ("s3://b/x?profile=prod", "?profile="), |
| 358 | 1× | ("s3://b/x?region=a&endpoint=http://10.0.0.1", "?endpoint="), |
| 359 | 1× | ("gs://b/x?credentials=/etc/key.json", "?credentials="), |
| 360 | 1× | ("dir:///srv", "a store is"), |
| 361 | 1× | ("file:///srv", "a store is"), |
| 362 | 1× | ("az://account", "the container"), |
| 363 | 1× | ] { |
| 364 | 6× | let e = check_url(bad).unwrap_err(); |
| 365 | 6× | assert!(e.contains(why), "{bad}: {e}"); |
| 366 | | } |
| 367 | 1× | } |
| 368 | |
|
| 369 | | #[test] |
| 370 | 1× | fn a_policy_says_what_it_allows() { |
| 371 | 1× | let open = Policy::default(); |
| 372 | 1× | assert!(open.permits("acme", None).is_ok()); |
| 373 | 1× | let strict = Policy { require_bucket: true, allowed: vec!["s3://acme-london/".into(), "gs://acme-eu/".into()] }; |
| 374 | 1× | let e = strict.permits("acme", None).unwrap_err(); |
| 375 | 1× | assert!(e.contains("acme keeps every new repository in a bucket") && e.contains("s3://acme-london/"), "{e}"); |
| 376 | 1× | assert!(strict.permits("acme", Some("s3://acme-london/repos/x?region=eu-west-2")).is_ok()); |
| 377 | 1× | let e = strict.permits("acme", Some("s3://acme-virginia/x")).unwrap_err(); |
| 378 | 1× | assert!(e.contains("allows only these stores"), "{e}"); |
| 379 | 1× | } |
| 380 | | } |
| 381 | |
|
| 382 | | #[cfg(test)] |
| 383 | | mod badge_tests { |
| 384 | | use super::StoreBadge; |
| 385 | |
|
| 386 | | #[test] |
| 387 | 1× | fn a_store_url_names_its_provider_place_prefix_and_region() { |
| 388 | 1× | let g = StoreBadge::of("gs://doomsdayone-oxy-repos/repos/tetris").unwrap(); |
| 389 | 1× | assert_eq!( |
| 390 | 1× | (g.kind, g.provider, g.place.as_str(), g.prefix.as_str(), g.region), |
| 391 | | ("gcs", "Google Cloud Storage", "doomsdayone-oxy-repos", "repos/tetris", None) |
| 392 | | ); |
| 393 | 1× | let s = StoreBadge::of("s3://my-bucket/repos/name?region=eu-west-2").unwrap(); |
| 394 | 1× | assert_eq!( |
| 395 | 1× | (s.kind, s.place.as_str(), s.prefix.as_str(), s.region.as_deref()), |
| 396 | | ("s3", "my-bucket", "repos/name", Some("eu-west-2")) |
| 397 | | ); |
| 398 | 1× | let a = StoreBadge::of("az://account/container/repos/x").unwrap(); |
| 399 | 1× | assert_eq!((a.kind, a.place.as_str(), a.prefix.as_str()), ("azure", "account / container", "repos/x")); |
| 400 | 1× | assert_eq!(StoreBadge::of("s3://bucket").unwrap().prefix, ""); |
| 401 | 1× | assert!(StoreBadge::of("dir:///tmp/x").is_none()); |
| 402 | 1× | assert!(StoreBadge::of("az://only-account").is_none()); |
| 403 | 1× | } |
| 404 | | } |