aws-lite-rs 0.1.1

Lightweight HTTP client for AWS 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! AWS credential resolution.

use crate::error::AwsError;
use std::path::PathBuf;

/// AWS credentials (access key + secret + optional session token).
#[derive(Debug, Clone)]
pub struct AwsCredentials {
    pub access_key_id: String,
    pub secret_access_key: String,
    pub session_token: Option<String>,
    pub region: String,
}

impl AwsCredentials {
    /// Resolve credentials from the default chain:
    /// 1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN)
    /// 2. Shared credentials file (~/.aws/credentials) using AWS_PROFILE or "default"
    /// 3. ECS container credentials (AWS_CONTAINER_CREDENTIALS_RELATIVE_URI)
    /// 4. EC2 instance metadata (IMDSv2)
    pub fn from_default_chain(region: &str) -> Result<Self, AwsError> {
        // 1. Environment variables
        if let Ok(access_key) = std::env::var("AWS_ACCESS_KEY_ID")
            && let Ok(secret_key) = std::env::var("AWS_SECRET_ACCESS_KEY")
        {
            return Ok(Self {
                access_key_id: access_key,
                secret_access_key: secret_key,
                session_token: std::env::var("AWS_SESSION_TOKEN").ok(),
                region: std::env::var("AWS_REGION")
                    .or_else(|_| std::env::var("AWS_DEFAULT_REGION"))
                    .unwrap_or_else(|_| region.to_string()),
            });
        }

        // 2. Shared credentials file (respects AWS_PROFILE)
        let profile = std::env::var("AWS_PROFILE").unwrap_or_else(|_| "default".to_string());
        if let Ok(creds) = Self::from_credentials_file_with_profile(region, &profile) {
            return Ok(creds);
        }

        // 3. ECS container credentials
        if std::env::var("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI").is_ok()
            || std::env::var("AWS_CONTAINER_CREDENTIALS_FULL_URI").is_ok()
        {
            // ECS credentials require async HTTP — return a marker error so the caller
            // can use `from_ecs_container` instead, or fall through to IMDS.
            // We can't do async here in a sync function, so we return an error and let
            // the async `from_default_chain_async` handle it.
        }

        Err(AwsError::Auth {
            message: format!(
                "No credentials found. Checked: environment variables, credentials file (profile: {profile}). \
                 For ECS/IMDS credentials, use AwsCredentials::from_default_chain_async()."
            ),
        })
    }

    /// Async version of credential resolution that includes ECS and IMDS sources.
    ///
    /// Resolution order:
    /// 1. Environment variables
    /// 2. Shared credentials file (~/.aws/credentials)
    /// 3. ECS container credentials
    /// 4. EC2 instance metadata (IMDSv2)
    pub async fn from_default_chain_async(region: &str) -> Result<Self, AwsError> {
        // 1. Environment variables
        if let Ok(access_key) = std::env::var("AWS_ACCESS_KEY_ID")
            && let Ok(secret_key) = std::env::var("AWS_SECRET_ACCESS_KEY")
        {
            return Ok(Self {
                access_key_id: access_key,
                secret_access_key: secret_key,
                session_token: std::env::var("AWS_SESSION_TOKEN").ok(),
                region: std::env::var("AWS_REGION")
                    .or_else(|_| std::env::var("AWS_DEFAULT_REGION"))
                    .unwrap_or_else(|_| region.to_string()),
            });
        }

        // 2. Shared credentials file
        let profile = std::env::var("AWS_PROFILE").unwrap_or_else(|_| "default".to_string());
        if let Ok(creds) = Self::from_credentials_file_with_profile(region, &profile) {
            return Ok(creds);
        }

        // 3. ECS container credentials
        if let Ok(creds) = Self::from_ecs_container(region).await {
            return Ok(creds);
        }

        // 4. EC2 instance metadata (IMDSv2)
        if let Ok(creds) = Self::from_imds_v2(region).await {
            return Ok(creds);
        }

        Err(AwsError::Auth {
            message: format!(
                "No credentials found. Checked: environment variables, credentials file (profile: {profile}), \
                 ECS container credentials, EC2 instance metadata (IMDSv2)."
            ),
        })
    }

    /// Create credentials from explicit values.
    pub fn new(
        access_key_id: String,
        secret_access_key: String,
        session_token: Option<String>,
        region: String,
    ) -> Self {
        Self {
            access_key_id,
            secret_access_key,
            session_token,
            region,
        }
    }

    /// Load credentials from a named profile in ~/.aws/credentials.
    pub fn from_profile(region: &str, profile: &str) -> Result<Self, AwsError> {
        Self::from_credentials_file_with_profile(region, profile)
    }

    /// Load credentials from ~/.aws/credentials using a specific profile.
    fn from_credentials_file_with_profile(region: &str, profile: &str) -> Result<Self, AwsError> {
        let path = credentials_file_path().ok_or_else(|| AwsError::Auth {
            message: "Could not determine AWS credentials file path".into(),
        })?;

        let content = std::fs::read_to_string(&path).map_err(|e| AwsError::Auth {
            message: format!("Failed to read {}: {}", path.display(), e),
        })?;

        parse_credentials_file(&content, region, profile)
    }

    /// Fetch credentials from ECS container metadata.
    ///
    /// Checks `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI` (relative to `http://169.254.170.2`)
    /// and `AWS_CONTAINER_CREDENTIALS_FULL_URI` (absolute URL) in that order.
    async fn from_ecs_container(region: &str) -> Result<Self, AwsError> {
        let (url, auth_token) =
            if let Ok(relative_uri) = std::env::var("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI") {
                (
                    format!("http://169.254.170.2{relative_uri}"),
                    std::env::var("AWS_CONTAINER_AUTHORIZATION_TOKEN").ok(),
                )
            } else if let Ok(full_uri) = std::env::var("AWS_CONTAINER_CREDENTIALS_FULL_URI") {
                (
                    full_uri,
                    std::env::var("AWS_CONTAINER_AUTHORIZATION_TOKEN").ok(),
                )
            } else {
                return Err(AwsError::Auth {
                    message: "No ECS container credentials URI set".into(),
                });
            };

        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(2))
            .build()
            .map_err(|e| AwsError::Auth {
                message: format!("Failed to create HTTP client for ECS credentials: {e}"),
            })?;

        let mut request = client.get(&url);
        if let Some(token) = auth_token {
            request = request.header("Authorization", token);
        }

        let response = request.send().await.map_err(|e| AwsError::Auth {
            message: format!("Failed to fetch ECS container credentials from {url}: {e}"),
        })?;

        if !response.status().is_success() {
            return Err(AwsError::Auth {
                message: format!(
                    "ECS container credentials returned HTTP {}",
                    response.status()
                ),
            });
        }

        let text = response.text().await.map_err(|e| AwsError::Auth {
            message: format!("Failed to read ECS container credentials: {e}"),
        })?;
        let body: serde_json::Value = serde_json::from_str(&text).map_err(|e| AwsError::Auth {
            message: format!("Failed to parse ECS container credentials: {e}"),
        })?;

        let access_key = body["AccessKeyId"]
            .as_str()
            .ok_or_else(|| AwsError::Auth {
                message: "Missing AccessKeyId in ECS credentials response".into(),
            })?
            .to_string();

        let secret_key = body["SecretAccessKey"]
            .as_str()
            .ok_or_else(|| AwsError::Auth {
                message: "Missing SecretAccessKey in ECS credentials response".into(),
            })?
            .to_string();

        let session_token = body["Token"].as_str().map(String::from);

        Ok(Self {
            access_key_id: access_key,
            secret_access_key: secret_key,
            session_token,
            region: region.to_string(),
        })
    }

    /// Fetch credentials from EC2 Instance Metadata Service v2 (IMDSv2).
    ///
    /// 1. PUT to get a session token
    /// 2. GET the IAM role name
    /// 3. GET the credentials for that role
    async fn from_imds_v2(region: &str) -> Result<Self, AwsError> {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(2))
            .build()
            .map_err(|e| AwsError::Auth {
                message: format!("Failed to create HTTP client for IMDS: {e}"),
            })?;

        // Step 1: Get IMDSv2 session token
        let token_response = client
            .put("http://169.254.169.254/latest/api/token")
            .header("X-aws-ec2-metadata-token-ttl-seconds", "21600")
            .send()
            .await
            .map_err(|e| AwsError::Auth {
                message: format!("Failed to get IMDSv2 token: {e}"),
            })?;

        if !token_response.status().is_success() {
            return Err(AwsError::Auth {
                message: format!(
                    "IMDSv2 token request returned HTTP {}",
                    token_response.status()
                ),
            });
        }

        let token = token_response.text().await.map_err(|e| AwsError::Auth {
            message: format!("Failed to read IMDSv2 token: {e}"),
        })?;

        // Step 2: Get IAM role name
        let role_response = client
            .get("http://169.254.169.254/latest/meta-data/iam/security-credentials/")
            .header("X-aws-ec2-metadata-token", &token)
            .send()
            .await
            .map_err(|e| AwsError::Auth {
                message: format!("Failed to get IAM role name from IMDS: {e}"),
            })?;

        if !role_response.status().is_success() {
            return Err(AwsError::Auth {
                message: format!(
                    "IMDS IAM role request returned HTTP {}",
                    role_response.status()
                ),
            });
        }

        let role_name = role_response.text().await.map_err(|e| AwsError::Auth {
            message: format!("Failed to read IAM role name: {e}"),
        })?;
        let role_name = role_name.trim();

        if role_name.is_empty() {
            return Err(AwsError::Auth {
                message: "No IAM role attached to this EC2 instance".into(),
            });
        }

        // Step 3: Get credentials for the role
        let creds_url =
            format!("http://169.254.169.254/latest/meta-data/iam/security-credentials/{role_name}");
        let creds_response = client
            .get(&creds_url)
            .header("X-aws-ec2-metadata-token", &token)
            .send()
            .await
            .map_err(|e| AwsError::Auth {
                message: format!("Failed to get IMDS credentials for role {role_name}: {e}"),
            })?;

        if !creds_response.status().is_success() {
            return Err(AwsError::Auth {
                message: format!(
                    "IMDS credentials request returned HTTP {}",
                    creds_response.status()
                ),
            });
        }

        let text = creds_response.text().await.map_err(|e| AwsError::Auth {
            message: format!("Failed to read IMDS credentials: {e}"),
        })?;
        let body: serde_json::Value = serde_json::from_str(&text).map_err(|e| AwsError::Auth {
            message: format!("Failed to parse IMDS credentials: {e}"),
        })?;

        let access_key = body["AccessKeyId"]
            .as_str()
            .ok_or_else(|| AwsError::Auth {
                message: "Missing AccessKeyId in IMDS credentials".into(),
            })?
            .to_string();

        let secret_key = body["SecretAccessKey"]
            .as_str()
            .ok_or_else(|| AwsError::Auth {
                message: "Missing SecretAccessKey in IMDS credentials".into(),
            })?
            .to_string();

        let session_token = body["Token"].as_str().map(String::from);

        Ok(Self {
            access_key_id: access_key,
            secret_access_key: secret_key,
            session_token,
            region: region.to_string(),
        })
    }
}

fn credentials_file_path() -> Option<PathBuf> {
    if let Ok(path) = std::env::var("AWS_SHARED_CREDENTIALS_FILE") {
        return Some(PathBuf::from(path));
    }
    dirs::home_dir().map(|h| h.join(".aws").join("credentials"))
}

fn parse_credentials_file(
    content: &str,
    region: &str,
    profile: &str,
) -> Result<AwsCredentials, AwsError> {
    let mut access_key = None;
    let mut secret_key = None;
    let mut session_token = None;
    let target_section = format!("[{profile}]");
    // Also handle "profile X" syntax used in ~/.aws/config
    let target_section_alt = format!("[profile {profile}]");
    let mut in_target = false;

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('[') && trimmed.ends_with(']') {
            in_target = trimmed == target_section || trimmed == target_section_alt;
            continue;
        }
        if !in_target {
            continue;
        }
        if let Some((key, value)) = trimmed.split_once('=') {
            let key = key.trim();
            let value = value.trim();
            match key {
                "aws_access_key_id" => access_key = Some(value.to_string()),
                "aws_secret_access_key" => secret_key = Some(value.to_string()),
                "aws_session_token" => session_token = Some(value.to_string()),
                _ => {}
            }
        }
    }

    match (access_key, secret_key) {
        (Some(ak), Some(sk)) => Ok(AwsCredentials {
            access_key_id: ak,
            secret_access_key: sk,
            session_token,
            region: region.to_string(),
        }),
        _ => Err(AwsError::Auth {
            message: format!("No valid [{profile}] credentials found in credentials file"),
        }),
    }
}

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

    #[test]
    fn parse_default_profile() {
        let content = "\
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
aws_session_token = FwoGZXIvYXdzEBYaD

[other]
aws_access_key_id = OTHER
aws_secret_access_key = OTHER_SECRET
";
        let creds = parse_credentials_file(content, "us-east-1", "default").unwrap();
        assert_eq!(creds.access_key_id, "AKIAIOSFODNN7EXAMPLE");
        assert_eq!(
            creds.secret_access_key,
            "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
        );
        assert_eq!(creds.session_token.as_deref(), Some("FwoGZXIvYXdzEBYaD"));
        assert_eq!(creds.region, "us-east-1");
    }

    #[test]
    fn parse_named_profile() {
        let content = "\
[default]
aws_access_key_id = DEFAULT_ID
aws_secret_access_key = DEFAULT_SECRET

[production]
aws_access_key_id = PROD_AKID
aws_secret_access_key = PROD_SECRET
aws_session_token = PROD_TOKEN
";
        let creds = parse_credentials_file(content, "us-west-2", "production").unwrap();
        assert_eq!(creds.access_key_id, "PROD_AKID");
        assert_eq!(creds.secret_access_key, "PROD_SECRET");
        assert_eq!(creds.session_token.as_deref(), Some("PROD_TOKEN"));
        assert_eq!(creds.region, "us-west-2");
    }

    #[test]
    fn parse_profile_syntax_from_config_file() {
        let content = "\
[profile staging]
aws_access_key_id = STAGING_AKID
aws_secret_access_key = STAGING_SECRET
";
        let creds = parse_credentials_file(content, "eu-west-1", "staging").unwrap();
        assert_eq!(creds.access_key_id, "STAGING_AKID");
        assert_eq!(creds.secret_access_key, "STAGING_SECRET");
    }

    #[test]
    fn parse_missing_profile_errors() {
        let content = "\
[default]
aws_access_key_id = DEFAULT_ID
aws_secret_access_key = DEFAULT_SECRET
";
        let result = parse_credentials_file(content, "us-east-1", "nonexistent");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("nonexistent"),
            "Error should mention the profile name: {err}"
        );
    }

    #[test]
    fn parse_missing_default_profile_errors() {
        let content = "\
[other]
aws_access_key_id = OTHER
aws_secret_access_key = OTHER_SECRET
";
        let result = parse_credentials_file(content, "us-east-1", "default");
        assert!(result.is_err());
    }

    #[test]
    fn parse_only_access_key_errors() {
        let content = "\
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
";
        let result = parse_credentials_file(content, "us-east-1", "default");
        assert!(result.is_err());
    }

    #[test]
    fn parse_empty_content_errors() {
        let result = parse_credentials_file("", "us-east-1", "default");
        assert!(result.is_err());
    }

    #[test]
    fn parse_no_session_token() {
        let content = "\
[default]
aws_access_key_id = AKID
aws_secret_access_key = SECRET
";
        let creds = parse_credentials_file(content, "eu-west-1", "default").unwrap();
        assert_eq!(creds.access_key_id, "AKID");
        assert_eq!(creds.secret_access_key, "SECRET");
        assert!(creds.session_token.is_none());
        assert_eq!(creds.region, "eu-west-1");
    }

    #[test]
    fn parse_ignores_other_profiles() {
        let content = "\
[production]
aws_access_key_id = PROD_ID
aws_secret_access_key = PROD_SECRET

[default]
aws_access_key_id = DEFAULT_ID
aws_secret_access_key = DEFAULT_SECRET
";
        let creds = parse_credentials_file(content, "us-west-2", "default").unwrap();
        assert_eq!(creds.access_key_id, "DEFAULT_ID");
        assert_eq!(creds.secret_access_key, "DEFAULT_SECRET");
    }

    #[test]
    fn parse_handles_whitespace_around_equals() {
        let content = "\
[default]
aws_access_key_id  =  AKID
aws_secret_access_key  =  SECRET
";
        let creds = parse_credentials_file(content, "us-east-1", "default").unwrap();
        assert_eq!(creds.access_key_id, "AKID");
        assert_eq!(creds.secret_access_key, "SECRET");
    }

    #[test]
    fn parse_multiple_profiles_correct_selection() {
        let content = "\
[default]
aws_access_key_id = DEFAULT_AKID
aws_secret_access_key = DEFAULT_SECRET

[dev]
aws_access_key_id = DEV_AKID
aws_secret_access_key = DEV_SECRET

[staging]
aws_access_key_id = STAGING_AKID
aws_secret_access_key = STAGING_SECRET

[prod]
aws_access_key_id = PROD_AKID
aws_secret_access_key = PROD_SECRET
";
        let dev = parse_credentials_file(content, "us-east-1", "dev").unwrap();
        assert_eq!(dev.access_key_id, "DEV_AKID");

        let staging = parse_credentials_file(content, "us-east-1", "staging").unwrap();
        assert_eq!(staging.access_key_id, "STAGING_AKID");

        let prod = parse_credentials_file(content, "us-east-1", "prod").unwrap();
        assert_eq!(prod.access_key_id, "PROD_AKID");
    }

    #[test]
    fn from_profile_delegates_correctly() {
        // This just tests the public API surface exists and calls through.
        // Actual file I/O would fail in test env without a real credentials file,
        // so we just verify the error message mentions the profile name.
        let result = AwsCredentials::from_profile("us-east-1", "my-custom-profile");
        // Either succeeds (if a credentials file with this profile exists) or errors
        if let Err(e) = result {
            let msg = e.to_string();
            // Should fail with file-not-found or profile-not-found, not a generic error
            assert!(
                msg.contains("credentials") || msg.contains("my-custom-profile"),
                "Error should be about credentials: {msg}"
            );
        }
    }
}