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
use base64::prelude::*;
use serde::Deserialize;
use tokio::fs;

use crate::error::Error;

const CREDENTIALS_FILE: &str = "application_default_credentials.json";

#[allow(dead_code)]
#[derive(Deserialize, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct ServiceAccountImpersonationInfo {
    pub(crate) token_lifetime_seconds: i32,
}

#[allow(dead_code)]
#[derive(Deserialize, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct ExecutableConfig {
    pub(crate) command: String,
    pub(crate) timeout_millis: Option<i32>,
    pub(crate) output_file: String,
}

#[allow(dead_code)]
#[derive(Deserialize, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct Format {
    #[serde(rename(deserialize = "type"))]
    pub(crate) tp: String,
    pub(crate) subject_token_field_name: String,
}

#[allow(dead_code)]
#[derive(Deserialize, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct CredentialSource {
    pub(crate) file: Option<String>,

    pub(crate) url: Option<String>,
    pub(crate) headers: Option<std::collections::HashMap<String, String>>,

    pub(crate) executable: Option<ExecutableConfig>,

    pub(crate) environment_id: Option<String>,
    pub(crate) region_url: Option<String>,
    pub(crate) regional_cred_verification_url: Option<String>,
    pub(crate) cred_verification_url: Option<String>,
    pub(crate) imdsv2_session_token_url: Option<String>,
    pub(crate) format: Option<Format>,
}

#[allow(dead_code)]
#[derive(Deserialize, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct CredentialsFile {
    #[serde(rename(deserialize = "type"))]
    pub tp: String,

    // Service Account fields
    pub client_email: Option<String>,
    pub private_key_id: Option<String>,
    pub private_key: Option<String>,
    pub auth_uri: Option<String>,
    pub token_uri: Option<String>,
    pub project_id: Option<String>,

    // User Credential fields
    // (These typically come from gcloud auth.)
    pub client_secret: Option<String>,
    pub client_id: Option<String>,
    pub refresh_token: Option<String>,

    // External Account fields
    pub audience: Option<String>,
    pub subject_token_type: Option<String>,
    #[serde(rename = "token_url")]
    pub token_url_external: Option<String>,
    pub token_info_url: Option<String>,
    pub service_account_impersonation_url: Option<String>,
    pub service_account_impersonation: Option<ServiceAccountImpersonationInfo>,
    pub delegates: Option<Vec<String>>,
    pub credential_source: Option<CredentialSource>,
    pub quota_project_id: Option<String>,
    pub workforce_pool_user_project: Option<String>,
}

impl CredentialsFile {
    pub async fn new() -> Result<Self, Error> {
        let credentials_json = {
            if let Ok(credentials) = Self::json_from_env().await {
                credentials
            } else {
                Self::json_from_file().await?
            }
        };

        Ok(serde_json::from_slice(credentials_json.as_slice())?)
    }

    pub async fn new_from_file(filepath: String) -> Result<Self, Error> {
        let credentials_json = fs::read(filepath).await?;
        Ok(serde_json::from_slice(credentials_json.as_slice())?)
    }

    pub async fn new_from_str(str: &str) -> Result<Self, Error> {
        Ok(serde_json::from_str(str)?)
    }

    async fn json_from_env() -> Result<Vec<u8>, ()> {
        let credentials = std::env::var("GOOGLE_APPLICATION_CREDENTIALS_JSON")
            .map_err(|_| ())
            .map(Vec::<u8>::from)?;

        if let Ok(decoded) = BASE64_STANDARD.decode(credentials.clone()) {
            Ok(decoded)
        } else {
            Ok(credentials)
        }
    }

    async fn json_from_file() -> Result<Vec<u8>, Error> {
        let path = match std::env::var("GOOGLE_APPLICATION_CREDENTIALS") {
            Ok(s) => Ok(std::path::Path::new(s.as_str()).to_path_buf()),
            Err(_e) => {
                // get well known file name
                if cfg!(target_os = "windows") {
                    let app_data = std::env::var("APPDATA")?;
                    Ok(std::path::Path::new(app_data.as_str())
                        .join("gcloud")
                        .join(CREDENTIALS_FILE))
                } else {
                    match home::home_dir() {
                        Some(s) => Ok(s.join(".config").join("gcloud").join(CREDENTIALS_FILE)),
                        None => Err(Error::NoHomeDirectoryFound),
                    }
                }
            }
        }?;

        let credentials_json = fs::read(path).await?;

        Ok(credentials_json)
    }

    pub(crate) fn try_to_private_key(&self) -> Result<jsonwebtoken::EncodingKey, Error> {
        match self.private_key.as_ref() {
            Some(key) => Ok(jsonwebtoken::EncodingKey::from_rsa_pem(key.as_bytes())?),
            None => Err(Error::NoPrivateKeyFound),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    const CREDENTIALS_FILE_CONTENT: &str = r#"{
  "type": "service_account",
  "project_id": "fake_project_id",
  "private_key_id": "fake_private_key_id",
  "private_key": "-----BEGIN PRIVATE KEY-----\nfake_private_key\n-----END PRIVATE KEY-----\n",
  "client_email": "fake@fake_project_id.iam.gserviceaccount.com",
  "client_id": "123456789010111213141516171819",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/fake%40fake_project_id.iam.gserviceaccount.com",
  "universe_domain": "googleapis.com"
}"#;

    #[tokio::test]
    async fn test_credentials_file_new_from_file() {
        // setup:
        let temp_credentials_dir = tempdir().expect("Cannot create temporary directory");
        let temp_credentials_path = temp_credentials_dir.path().join(CREDENTIALS_FILE);
        let mut credentials_file = File::create(&temp_credentials_path).expect("Cannot create temporary file");
        credentials_file
            .write_all(CREDENTIALS_FILE_CONTENT.as_bytes())
            .expect("Cannot write content to file");

        // execute:
        let credentials_file_result =
            CredentialsFile::new_from_file(temp_credentials_path.to_string_lossy().to_string()).await;

        // verify:
        let expected_credentials_file: CredentialsFile =
            serde_json::from_str(CREDENTIALS_FILE_CONTENT).expect("Credentials file JSON deserialization not working");
        match credentials_file_result {
            Err(_) => panic!(),
            Ok(cf) => assert_eq!(expected_credentials_file, cf),
        }
    }

    #[tokio::test]
    async fn test_credentials_file_new_from_str() {
        // execute:
        let credentials_file_result = CredentialsFile::new_from_str(CREDENTIALS_FILE_CONTENT).await;

        // verify:
        let expected_credentials_file: CredentialsFile =
            serde_json::from_str(CREDENTIALS_FILE_CONTENT).expect("Credentials file JSON deserialization not working");
        match credentials_file_result {
            Err(_) => panic!(),
            Ok(cf) => assert_eq!(expected_credentials_file, cf),
        }
    }

    #[tokio::test]
    async fn test_credentials_file_new_from_env_var_json() {
        // setup:
        temp_env::async_with_vars(
            [
                ("GOOGLE_APPLICATION_CREDENTIALS_JSON", Some(CREDENTIALS_FILE_CONTENT)),
                ("GOOGLE_APPLICATION_CREDENTIALS", None), // make sure file env is not interfering
            ],
            async {
                // execute:
                let credentials_file_result = CredentialsFile::new().await;

                // verify:
                let expected_credentials_file: CredentialsFile = serde_json::from_str(CREDENTIALS_FILE_CONTENT)
                    .expect("Credentials file JSON deserialization not working");
                match credentials_file_result {
                    Err(_) => panic!(),
                    Ok(cf) => assert_eq!(expected_credentials_file, cf),
                }
            },
        )
        .await;
    }

    #[tokio::test]
    async fn test_credentials_file_new_from_env_var_json_base_64_encoded() {
        // setup:
        temp_env::async_with_vars(
            [
                (
                    "GOOGLE_APPLICATION_CREDENTIALS_JSON",
                    Some(base64::engine::general_purpose::STANDARD.encode(CREDENTIALS_FILE_CONTENT)),
                ),
                ("GOOGLE_APPLICATION_CREDENTIALS", None), // make sure file env is not interfering
            ],
            async {
                // execute:
                let credentials_file_result = CredentialsFile::new().await;

                // verify:
                let expected_credentials_file: CredentialsFile = serde_json::from_str(CREDENTIALS_FILE_CONTENT)
                    .expect("Credentials file JSON deserialization not working");
                match credentials_file_result {
                    Err(_) => panic!(),
                    Ok(cf) => assert_eq!(expected_credentials_file, cf),
                }
            },
        )
        .await
    }

    #[tokio::test]
    async fn test_credentials_file_new_env_var_file() {
        // setup:
        let temp_credentials_dir = tempdir().expect("Cannot create temporary directory");
        let temp_credentials_path = temp_credentials_dir.path().join(CREDENTIALS_FILE);
        let mut credentials_file = File::create(&temp_credentials_path).expect("Cannot create temporary file");

        temp_env::async_with_vars(
            [
                (
                    "GOOGLE_APPLICATION_CREDENTIALS",
                    Some(temp_credentials_path.to_string_lossy().to_string()),
                ),
                ("GOOGLE_APPLICATION_CREDENTIALS_JSON", None), // make sure file env is not interfering
            ],
            async {
                credentials_file
                    .write_all(CREDENTIALS_FILE_CONTENT.as_bytes())
                    .expect("Cannot write content to file");

                // execute:
                let credentials_file_result = CredentialsFile::new().await;

                // verify:
                let expected_credentials_file: CredentialsFile = serde_json::from_str(CREDENTIALS_FILE_CONTENT)
                    .expect("Credentials file JSON deserialization not working");
                match credentials_file_result {
                    Err(_) => panic!(),
                    Ok(cf) => assert_eq!(expected_credentials_file, cf),
                }
            },
        )
        .await
    }
}