gcp-lite-rs 0.1.1

Lightweight HTTP client for Google Cloud Platform APIs
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Gcloud CLI authentication support for ADC.
//!
//! This module provides `GcloudCredential`, which uses the `gcloud` CLI tool
//! to obtain access tokens from an active `gcloud auth login` session.
//!
//! Tokens are cached to avoid shelling out on every request. The cache is
//! invalidated when tokens are rejected or after approximately 55 minutes
//! (gcloud tokens typically expire after 1 hour).

use crate::auth::types::{AccessToken, CachedToken};
use crate::token::{TokenError, TokenProvider};
use async_trait::async_trait;

/// Token expiry margin in seconds. We refresh 5 minutes before actual expiry.
const TOKEN_EXPIRY_MARGIN_SECS: u64 = 300;

/// Assumed token lifetime in seconds. Gcloud tokens typically last 1 hour.
const TOKEN_LIFETIME_SECS: u64 = 3600;

/// Errors that can occur when using gcloud credentials.
#[derive(Debug, thiserror::Error)]
pub enum GcloudError {
    /// Gcloud command not found in PATH.
    #[error("gcloud command not found in PATH")]
    NotInstalled,

    /// Gcloud command execution failed.
    #[error("gcloud command failed: {0}")]
    CommandFailed(String),

    /// Gcloud authentication failed.
    #[error("gcloud auth failed: {0}")]
    AuthFailed(String),

    /// Gcloud returned an empty token.
    #[error("gcloud returned empty token")]
    EmptyToken,

    /// Gcloud returned invalid token format.
    #[error("gcloud returned invalid token format")]
    InvalidTokenFormat,
}

/// Credential provider using gcloud CLI.
///
/// Tokens are cached to reduce latency. The cache is automatically
/// refreshed when tokens expire or are rejected.
#[derive(Debug)]
pub struct GcloudCredential {
    quota_project_id: Option<String>,
    cached_token: CachedToken,
}

impl GcloudCredential {
    /// Check if gcloud is installed and accessible.
    #[allow(dead_code)]
    fn check_gcloud_installed() -> Result<(), GcloudError> {
        Self::check_gcloud_installed_impl("gcloud")
    }

    /// Internal implementation for testing.
    fn check_gcloud_installed_impl(command: &str) -> Result<(), GcloudError> {
        which::which(command).map_err(|e| {
            tracing::debug!("gcloud command '{}' not found in PATH: {:?}", command, e);
            GcloudError::NotInstalled
        })?;
        Ok(())
    }

    /// Get access token from gcloud.
    async fn get_access_token() -> Result<String, GcloudError> {
        Self::get_access_token_impl("gcloud").await
    }

    /// Internal implementation for testing.
    async fn get_access_token_impl(command: &str) -> Result<String, GcloudError> {
        Self::get_access_token_impl_with_args(command, &["auth", "print-access-token", "--quiet"])
            .await
    }

    /// Internal implementation with custom args for testing.
    async fn get_access_token_impl_with_args(
        command: &str,
        args: &[&str],
    ) -> Result<String, GcloudError> {
        let output = tokio::process::Command::new(command)
            .args(args)
            .output()
            .await
            .map_err(|e| GcloudError::CommandFailed(e.to_string()))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(GcloudError::AuthFailed(stderr.to_string()));
        }

        let token = String::from_utf8_lossy(&output.stdout).trim().to_string();

        if token.is_empty() {
            return Err(GcloudError::EmptyToken);
        }

        Ok(token)
    }

    /// Get quota project from gcloud config.
    #[allow(dead_code)]
    async fn get_quota_project() -> Result<String, GcloudError> {
        Self::get_quota_project_impl("gcloud").await
    }

    /// Internal implementation for testing.
    async fn get_quota_project_impl(command: &str) -> Result<String, GcloudError> {
        Self::get_quota_project_impl_with_args(
            command,
            &["config", "get-value", "project", "--quiet"],
        )
        .await
    }

    /// Internal implementation with custom args for testing.
    async fn get_quota_project_impl_with_args(
        command: &str,
        args: &[&str],
    ) -> Result<String, GcloudError> {
        let output = tokio::process::Command::new(command)
            .args(args)
            .output()
            .await
            .map_err(|e| GcloudError::CommandFailed(e.to_string()))?;

        if !output.status.success() {
            return Err(GcloudError::CommandFailed(
                String::from_utf8_lossy(&output.stderr).to_string(),
            ));
        }

        let project = String::from_utf8_lossy(&output.stdout).trim().to_string();

        if project.is_empty() {
            return Err(GcloudError::CommandFailed(
                "no project configured".to_string(),
            ));
        }

        Ok(project)
    }

    /// Create a new gcloud credential.
    ///
    /// Verifies gcloud is installed and authenticated, then retrieves
    /// the quota project from gcloud config.
    pub async fn new() -> Result<Self, GcloudError> {
        Self::new_impl("gcloud").await
    }

    /// Internal implementation for testing.
    async fn new_impl(command: &str) -> Result<Self, GcloudError> {
        // Check gcloud is installed
        Self::check_gcloud_installed_impl(command)?;

        // Verify authentication by attempting to get a token
        let token = Self::get_access_token_impl(command).await?;

        // Try to get quota project (non-fatal if not set)
        let quota_project_id = Self::get_quota_project_impl(command).await.ok();

        // Initialize with the token we just fetched (it's valid)
        let cached_token = CachedToken::new();
        let expires_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
            + TOKEN_LIFETIME_SECS;
        cached_token.set(AccessToken::new(token, expires_at)).await;

        Ok(Self {
            quota_project_id,
            cached_token,
        })
    }
}

#[async_trait]
impl TokenProvider for GcloudCredential {
    async fn get_token(&self, _scopes: &[&str]) -> Result<String, TokenError> {
        // Scopes are ignored - gcloud tokens have broad cloud-platform scope

        // Check cache first
        if let Some(token) = self.cached_token.get(TOKEN_EXPIRY_MARGIN_SECS).await {
            return Ok(token);
        }

        // Cache miss or expired - fetch new token
        let token = Self::get_access_token()
            .await
            .map_err(|e| TokenError::RefreshFailed {
                message: e.to_string(),
            })?;

        // Cache the new token
        let expires_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
            + TOKEN_LIFETIME_SECS;
        self.cached_token
            .set(AccessToken::new(token.clone(), expires_at))
            .await;

        Ok(token)
    }

    fn on_token_rejected(&self) {
        // Clear cache so next get_token() fetches fresh
        self.cached_token.clear_sync();
    }

    fn quota_project_id(&self) -> Option<&str> {
        self.quota_project_id.as_deref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gcloud_error_display() {
        let err = GcloudError::NotInstalled;
        assert_eq!(err.to_string(), "gcloud command not found in PATH");

        let err = GcloudError::CommandFailed("exec error".to_string());
        assert!(err.to_string().contains("gcloud command failed"));
        assert!(err.to_string().contains("exec error"));

        let err = GcloudError::AuthFailed("not logged in".to_string());
        assert!(err.to_string().contains("gcloud auth failed"));
        assert!(err.to_string().contains("not logged in"));

        let err = GcloudError::EmptyToken;
        assert_eq!(err.to_string(), "gcloud returned empty token");

        let err = GcloudError::InvalidTokenFormat;
        assert_eq!(err.to_string(), "gcloud returned invalid token format");
    }

    #[test]
    fn test_check_gcloud_installed_not_found() {
        // This test assumes gcloud is not at /nonexistent/gcloud
        // We can't easily mock PATH, so we test the error type
        let result = GcloudCredential::check_gcloud_installed_impl("/nonexistent/gcloud");
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GcloudError::NotInstalled));
    }

    #[test]
    fn test_check_gcloud_installed_success() {
        // Test with 'ls' which should exist on all systems
        let result = GcloudCredential::check_gcloud_installed_impl("ls");
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_access_token_command_fails() {
        // Mock command that fails
        let result = GcloudCredential::get_access_token_impl("false").await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GcloudError::AuthFailed(_)));
    }

    #[tokio::test]
    async fn test_get_access_token_empty_output() {
        // Mock command that succeeds but returns empty (echo with -n and empty string)
        let result = GcloudCredential::get_access_token_impl_with_args("printf", &[""]).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GcloudError::EmptyToken));
    }

    #[tokio::test]
    async fn test_get_access_token_success() {
        // Mock with echo to simulate token
        let result =
            GcloudCredential::get_access_token_impl_with_args("echo", &["test-token"]).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "test-token");
    }

    #[tokio::test]
    async fn test_get_quota_project_success() {
        // Mock with echo to simulate project
        let result =
            GcloudCredential::get_quota_project_impl_with_args("echo", &["my-project"]).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "my-project");
    }

    #[tokio::test]
    async fn test_get_quota_project_empty() {
        // Mock command that returns empty (unset project)
        let result = GcloudCredential::get_quota_project_impl_with_args("printf", &[""]).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GcloudError::CommandFailed(_)));
    }

    #[tokio::test]
    async fn test_get_quota_project_command_fails() {
        let result = GcloudCredential::get_quota_project_impl("false").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_gcloud_credential_new_not_installed() {
        // This will fail in CI where gcloud isn't installed
        // Test the error path
        let result = GcloudCredential::new_impl("nonexistent-command").await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), GcloudError::NotInstalled));
    }

    #[tokio::test]
    async fn test_token_provider_get_token() {
        // Create credential with mock quota project
        let cred = GcloudCredential {
            quota_project_id: Some("test-project".to_string()),
            cached_token: CachedToken::new(),
        };

        // We can't easily test actual token retrieval without mocking
        // Test that quota_project_id works
        assert_eq!(cred.quota_project_id(), Some("test-project"));
    }

    #[test]
    fn test_token_provider_quota_project_none() {
        let cred = GcloudCredential {
            quota_project_id: None,
            cached_token: CachedToken::new(),
        };
        assert_eq!(cred.quota_project_id(), None);
    }

    #[tokio::test]
    async fn test_on_token_rejected_clears_cache() {
        use crate::auth::types::AccessToken;

        let cred = GcloudCredential {
            quota_project_id: None,
            cached_token: CachedToken::new(),
        };

        // Pre-populate cache with a test token
        let expires_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
            + 3600;
        cred.cached_token
            .set(AccessToken::new("test-token", expires_at))
            .await;

        // Should not panic and should clear cache
        cred.on_token_rejected();

        // Give spawned task time to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        // Cache should be cleared
        assert!(cred.cached_token.get(0).await.is_none());
    }

    #[tokio::test]
    async fn test_token_caching() {
        use crate::auth::types::AccessToken;

        let cred = GcloudCredential {
            quota_project_id: None,
            cached_token: CachedToken::new(),
        };

        // Pre-populate cache with a test token
        let expires_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0)
            + 3600; // 1 hour from now
        cred.cached_token
            .set(AccessToken::new("cached-token", expires_at))
            .await;

        // get_token should return cached token without shelling out
        let result = cred.get_token(&[]).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "cached-token");

        // Clear cache via on_token_rejected
        cred.on_token_rejected();

        // Give spawned task time to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        // Cache should be empty now (verified by checking the cache directly)
        assert!(cred.cached_token.get(0).await.is_none());
    }
}