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
#[derive(serde::Deserialize, Debug)]
pub struct ServiceAccount {
#[serde(rename = "type")]
pub r#type: String,
pub project_id: String,
pub private_key_id: String,
pub private_key: String,
pub client_email: String,
pub client_id: String,
pub auth_uri: String,
pub token_uri: String,
pub auth_provider_x509_cert_url: String,
pub client_x509_cert_url: String,
}
impl ServiceAccount {
pub(crate) fn get() -> Self {
dotenv::dotenv().ok();
let credentials_json = std::env::var("SERVICE_ACCOUNT")
.or_else(|_| std::env::var("GOOGLE_APPLICATION_CREDENTIALS"))
.map(|path| std::fs::read_to_string(path).expect("SERVICE_ACCOUNT file not found"))
.or_else(|_| std::env::var("SERVICE_ACCOUNT_JSON"))
.or_else(|_| std::env::var("GOOGLE_APPLICATION_CREDENTIALS_JSON"))
.expect(
"SERVICE_ACCOUNT(_JSON) or GOOGLE_APPLICATION_CREDENTIALS(_JSON) environment parameter required",
);
let account: Self =
serde_json::from_str(&credentials_json).expect("SERVICE_ACCOUNT file not valid");
assert_eq!(
account.r#type, "service_account",
"`type` parameter of `SERVICE_ACCOUNT` variable is not 'service_account'"
);
account
}
}