| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
use crate::StoreUrl; |
| 11 |
use object_store::gcp::{GoogleCloudStorage, GoogleCloudStorageBuilder}; |
| 12 |
use oxy_core::strata::{Error, Result}; |
| 13 |
|
| 14 |
pub const HINT: &str = "run `gcloud auth application-default login`, or set GOOGLE_APPLICATION_CREDENTIALS (or add ?credentials=PATH to the remote URL) to a service account key file"; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
fn is_service_account_key(path: &str) -> bool { |
| 19 |
std::fs::read_to_string(path) |
| 20 |
.ok() |
| 21 |
.and_then(|t| serde_json::from_str::<serde_json::Value>(&t).ok()) |
| 22 |
.and_then(|v| { |
| 23 |
v.get("type") |
| 24 |
.and_then(|t| t.as_str()) |
| 25 |
.map(|t| t == "service_account") |
| 26 |
}) |
| 27 |
.unwrap_or(false) |
| 28 |
} |
| 29 |
|
| 30 |
pub fn open(url: &StoreUrl) -> Result<GoogleCloudStorage> { |
| 31 |
let mut b = GoogleCloudStorageBuilder::from_env().with_bucket_name(&url.host); |
| 32 |
let file = url.param("credentials").map(str::to_string).or_else(|| { |
| 33 |
std::env::var("GOOGLE_APPLICATION_CREDENTIALS") |
| 34 |
.ok() |
| 35 |
.filter(|v| !v.is_empty()) |
| 36 |
}); |
| 37 |
if let Some(path) = file { |
| 38 |
if !std::path::Path::new(&path).is_file() { |
| 39 |
return Err(Error::Backend(format!( |
| 40 |
"{}: the credentials file {path} does not exist: {HINT}", |
| 41 |
url.plain() |
| 42 |
))); |
| 43 |
} |
| 44 |
b = if is_service_account_key(&path) { |
| 45 |
b.with_service_account_path(path) |
| 46 |
} else { |
| 47 |
b.with_application_credentials(path) |
| 48 |
}; |
| 49 |
} |
| 50 |
if let Some(endpoint) = url.param("endpoint") { |
| 51 |
b = b.with_base_url(endpoint); |
| 52 |
} |
| 53 |
if url.param("anonymous").is_some_and(|v| v != "0") { |
| 54 |
|
| 55 |
|
| 56 |
b = b.with_bearer_token("anonymous").with_skip_signature(true); |
| 57 |
} |
| 58 |
b.build() |
| 59 |
.map_err(|e| Error::Backend(format!("{}: {e}: {HINT}", url.plain()))) |
| 60 |
} |