use object_store::ClientOptions;
use object_store::gcp::GoogleCloudStorageBuilder;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum GcsCredentials {
Connection,
Named { path: String },
Gcloud { path: String },
Machine,
}
impl GcsCredentials {
pub(crate) fn describe(&self) -> String {
match self {
Self::Connection => "the connection's service_account_key".to_owned(),
Self::Named { path } => format!("GOOGLE_APPLICATION_CREDENTIALS ({path})"),
Self::Gcloud { path } => format!("gcloud's application default credentials ({path})"),
Self::Machine => "the machine's service account, from its metadata server".to_owned(),
}
}
}
pub(crate) fn gcs_credentials(
has_key: bool,
variable: &dyn Fn(&str) -> Option<String>,
) -> Result<GcsCredentials, String> {
if has_key {
return Ok(GcsCredentials::Connection);
}
if let Some(path) = variable("GOOGLE_APPLICATION_CREDENTIALS") {
return match std::path::Path::new(&path).is_file() {
true => Ok(GcsCredentials::Named { path }),
false => Err(format!(
"GOOGLE_APPLICATION_CREDENTIALS names {path}, which is not a file"
)),
};
}
let (home, below) = match cfg!(windows) {
true => ("APPDATA", "gcloud/application_default_credentials.json"),
false => (
"HOME",
".config/gcloud/application_default_credentials.json",
),
};
Ok(variable(home)
.map(|home| std::path::Path::new(&home).join(below))
.filter(|path| path.is_file())
.map_or(GcsCredentials::Machine, |path| GcsCredentials::Gcloud {
path: path.display().to_string(),
}))
}
pub(crate) fn gcs_builder(
bucket: &str,
key: Option<&str>,
endpoint: Option<&str>,
variable: &dyn Fn(&str) -> Option<String>,
) -> Result<GoogleCloudStorageBuilder, String> {
let mut builder = GoogleCloudStorageBuilder::new().with_bucket_name(bucket);
match (key, gcs_credentials(key.is_some(), variable)?) {
(Some(key), _) => builder = builder.with_service_account_key(key),
(None, GcsCredentials::Named { path }) => {
builder = builder.with_application_credentials(path);
}
(None, _) => {}
}
if let Some(endpoint) = endpoint {
builder = builder.with_base_url(endpoint).with_client_options(
ClientOptions::new().with_allow_http(endpoint.starts_with("http://")),
);
}
Ok(builder)
}