stakpak 0.3.68

Stakpak: Your DevOps AI Agent. Generate infrastructure code, debug Kubernetes, configure CI/CD, automate deployments, without giving an LLM the keys to production.
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
use std::fmt::Write;
use std::path::{Path, PathBuf};

/// Discover cloud account configurations by reading config files directly.
/// No CLI calls — pure filesystem reads for speed. Cross-platform.
pub fn discover() -> String {
    let mut out = String::with_capacity(2048);

    let home = match dirs::home_dir() {
        Some(h) => h,
        None => return "(cannot determine home directory)\n".to_string(),
    };

    discover_aws(&home, &mut out);
    discover_gcp(&home, &mut out);
    discover_azure(&home, &mut out);
    discover_kubernetes(&home, &mut out);
    discover_docker_registries(&home, &mut out);
    discover_other_platforms(&home, &mut out);

    if out.is_empty() {
        return "(no cloud account configurations found)\n".to_string();
    }
    out
}

/// Parse AWS config/credentials to enumerate profiles, then call
/// `aws sts get-caller-identity` per profile (in parallel) to get
/// definitive account IDs and validate credentials are live.
fn discover_aws(home: &Path, out: &mut String) {
    let config_path = home.join(".aws/config");
    let creds_path = home.join(".aws/credentials");

    if !config_path.exists() && !creds_path.exists() {
        return;
    }

    let _ = writeln!(out, "### AWS\n");

    // Step 1: Parse config file for profile metadata
    let mut profiles: Vec<AwsProfile> = Vec::new();

    if let Ok(content) = std::fs::read_to_string(&config_path) {
        let mut current_name: Option<String> = None;
        let mut region: Option<String> = None;
        let mut sso_url: Option<String> = None;
        let mut role_arn: Option<String> = None;
        let mut source_profile: Option<String> = None;
        let mut sso_account_id: Option<String> = None;

        let flush = |profiles: &mut Vec<AwsProfile>,
                     name: &Option<String>,
                     region: &Option<String>,
                     sso_url: &Option<String>,
                     role_arn: &Option<String>,
                     source_profile: &Option<String>,
                     sso_account_id: &Option<String>| {
            if let Some(n) = name {
                let method = if sso_url.is_some() {
                    "SSO"
                } else if role_arn.is_some() {
                    "assume-role"
                } else {
                    "credentials"
                };
                profiles.push(AwsProfile {
                    name: n.clone(),
                    method: method.to_string(),
                    region: region.clone(),
                    role_arn: role_arn.clone(),
                    source_profile: source_profile.clone(),
                    sso_account_id: sso_account_id.clone(),
                    // Will be filled by sts call
                    live_account_id: None,
                    live_arn: None,
                    auth_ok: None,
                });
            }
        };

        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with('[') && trimmed.ends_with(']') {
                flush(
                    &mut profiles,
                    &current_name,
                    &region,
                    &sso_url,
                    &role_arn,
                    &source_profile,
                    &sso_account_id,
                );
                let section = &trimmed[1..trimmed.len() - 1];
                current_name = Some(
                    section
                        .strip_prefix("profile ")
                        .unwrap_or(section)
                        .to_string(),
                );
                region = None;
                sso_url = None;
                role_arn = None;
                source_profile = None;
                sso_account_id = None;
            } else if let Some((key, value)) = trimmed.split_once('=') {
                let key = key.trim();
                let value = value.trim();
                match key {
                    "region" => region = Some(value.to_string()),
                    "sso_start_url" => sso_url = Some(value.to_string()),
                    "role_arn" => role_arn = Some(value.to_string()),
                    "source_profile" => source_profile = Some(value.to_string()),
                    "sso_account_id" => sso_account_id = Some(value.to_string()),
                    _ => {}
                }
            }
        }
        flush(
            &mut profiles,
            &current_name,
            &region,
            &sso_url,
            &role_arn,
            &source_profile,
            &sso_account_id,
        );
    }

    // Step 2: Call `aws sts get-caller-identity --profile X --output json` per profile in parallel
    if which::which("aws").is_ok() && !profiles.is_empty() {
        use std::thread;

        let handles: Vec<_> = profiles
            .iter()
            .map(|p| {
                let name = p.name.clone();
                thread::spawn(move || {
                    let output = std::process::Command::new("aws")
                        .args([
                            "sts",
                            "get-caller-identity",
                            "--profile",
                            &name,
                            "--output",
                            "json",
                        ])
                        .output();
                    (name, output)
                })
            })
            .collect();

        for handle in handles {
            if let Ok((name, output)) = handle.join()
                && let Some(profile) = profiles.iter_mut().find(|p| p.name == name)
            {
                match output {
                    Ok(o) if o.status.success() => {
                        let stdout = String::from_utf8_lossy(&o.stdout);
                        if let Ok(json) = serde_json::from_str::<serde_json::Value>(&stdout) {
                            profile.live_account_id = json
                                .get("Account")
                                .and_then(|v| v.as_str())
                                .map(|s| s.to_string());
                            profile.live_arn = json
                                .get("Arn")
                                .and_then(|v| v.as_str())
                                .map(|s| s.to_string());
                        }
                        profile.auth_ok = Some(true);
                    }
                    _ => {
                        profile.auth_ok = Some(false);
                    }
                }
            }
        }
    }

    // Step 3: Format output
    for p in &profiles {
        // Best account ID: live > sso_account_id > extracted from role_arn
        let extracted_from_arn = p
            .role_arn
            .as_ref()
            .and_then(|arn| extract_account_from_arn(arn));
        let account_id = p
            .live_account_id
            .as_ref()
            .or(p.sso_account_id.as_ref())
            .or(extracted_from_arn.as_ref());

        let _ = write!(out, "- Profile: {}  method:{}", p.name, p.method);
        if let Some(acct) = account_id {
            let _ = write!(out, "  account:{}", acct);
        }
        if let Some(r) = &p.region {
            let _ = write!(out, "  region:{}", r);
        }
        if let Some(arn) = &p.live_arn {
            let _ = write!(out, "  arn:{}", arn);
        }
        if let Some(role) = &p.role_arn {
            let _ = write!(out, "  role:{}", role);
        }
        if let Some(src) = &p.source_profile {
            let _ = write!(out, "  source:{}", src);
        }
        match p.auth_ok {
            Some(true) => {
                let _ = write!(out, "  status:✓");
            }
            Some(false) => {
                let _ = write!(out, "  status:✗ auth-failed");
            }
            None => {} // aws CLI not available, don't show status
        }
        let _ = writeln!(out);
    }

    // Check env vars
    if let Ok(profile) = std::env::var("AWS_PROFILE") {
        let _ = writeln!(out, "- ENV: AWS_PROFILE={}", profile);
    }
    if let Ok(region) = std::env::var("AWS_REGION") {
        let _ = writeln!(out, "- ENV: AWS_REGION={}", region);
    }
    if let Ok(region) = std::env::var("AWS_DEFAULT_REGION") {
        let _ = writeln!(out, "- ENV: AWS_DEFAULT_REGION={}", region);
    }
    out.push('\n');
}

struct AwsProfile {
    name: String,
    method: String,
    region: Option<String>,
    role_arn: Option<String>,
    source_profile: Option<String>,
    sso_account_id: Option<String>,
    live_account_id: Option<String>,
    live_arn: Option<String>,
    auth_ok: Option<bool>,
}

/// Parse GCP config to enumerate projects and configurations.
fn discover_gcp(home: &Path, out: &mut String) {
    let gcloud_dir = home.join(".config/gcloud");
    if !gcloud_dir.exists() {
        return;
    }

    let _ = writeln!(out, "### GCP\n");

    // Read active config
    let active_config = gcloud_dir.join("active_config");
    let active = std::fs::read_to_string(&active_config)
        .ok()
        .map(|s| s.trim().to_string());

    if let Some(ref name) = active {
        let _ = writeln!(out, "- Active config: {}", name);
    }

    // Read properties from active config or default
    let configs_dir = gcloud_dir.join("configurations");
    if configs_dir.exists()
        && let Ok(entries) = std::fs::read_dir(&configs_dir)
    {
        for entry in entries.flatten() {
            let name = entry.file_name().to_string_lossy().to_string();
            if !name.starts_with("config_") {
                continue;
            }
            let config_name = name.strip_prefix("config_").unwrap_or(&name);
            if let Ok(content) = std::fs::read_to_string(entry.path()) {
                let project = extract_ini_value(&content, "project");
                let account = extract_ini_value(&content, "account");
                let region = extract_ini_value(&content, "region");
                let is_active = active.as_deref() == Some(config_name);
                let _ = write!(out, "- Config: {}", config_name);
                if is_active {
                    let _ = write!(out, " (active)");
                }
                if let Some(p) = project {
                    let _ = write!(out, "  project:{}", p);
                }
                if let Some(a) = account {
                    let _ = write!(out, "  account:{}", a);
                }
                if let Some(r) = region {
                    let _ = write!(out, "  region:{}", r);
                }
                let _ = writeln!(out);
            }
        }
    }

    // Check env vars
    if let Ok(project) = std::env::var("GCLOUD_PROJECT") {
        let _ = writeln!(out, "- ENV: GCLOUD_PROJECT={}", project);
    }
    if let Ok(project) = std::env::var("GOOGLE_CLOUD_PROJECT") {
        let _ = writeln!(out, "- ENV: GOOGLE_CLOUD_PROJECT={}", project);
    }
    out.push('\n');
}

/// Parse Azure CLI config to enumerate subscriptions.
fn discover_azure(home: &Path, out: &mut String) {
    let azure_dir = home.join(".azure");
    if !azure_dir.exists() {
        return;
    }

    let _ = writeln!(out, "### Azure\n");

    // azureProfile.json contains subscription info
    let profile_path = azure_dir.join("azureProfile.json");
    if let Ok(content) = std::fs::read_to_string(&profile_path)
        && let Ok(json) = serde_json::from_str::<serde_json::Value>(&content)
        && let Some(subs) = json.get("subscriptions").and_then(|s| s.as_array())
    {
        for sub in subs {
            let name = sub.get("name").and_then(|v| v.as_str()).unwrap_or("?");
            let id = sub.get("id").and_then(|v| v.as_str()).unwrap_or("?");
            let state = sub.get("state").and_then(|v| v.as_str()).unwrap_or("?");
            let is_default = sub
                .get("isDefault")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            let tenant = sub.get("tenantId").and_then(|v| v.as_str()).unwrap_or("?");
            let _ = write!(
                out,
                "- {} ({})  state:{}  tenant:{}",
                name, id, state, tenant
            );
            if is_default {
                let _ = write!(out, "  (default)");
            }
            let _ = writeln!(out);
        }
    }
    out.push('\n');
}

/// Parse kubeconfig to enumerate contexts and clusters.
fn discover_kubernetes(home: &Path, out: &mut String) {
    // Check KUBECONFIG env var first, fall back to default
    let kubeconfig_paths = match std::env::var("KUBECONFIG") {
        Ok(val) => val.split(':').map(PathBuf::from).collect::<Vec<_>>(),
        Err(_) => vec![home.join(".kube/config")],
    };

    let mut found_any = false;
    for kc_path in &kubeconfig_paths {
        if !kc_path.exists() {
            continue;
        }
        if let Ok(content) = std::fs::read_to_string(kc_path)
            && let Ok(yaml) = serde_yaml::from_str::<serde_yaml::Value>(&content)
        {
            if !found_any {
                let _ = writeln!(out, "### Kubernetes\n");
                found_any = true;
            }

            // Current context
            if let Some(current) = yaml.get("current-context").and_then(|v| v.as_str()) {
                let _ = writeln!(out, "- Current context: {}", current);
            }

            // List all contexts
            if let Some(contexts) = yaml.get("contexts").and_then(|v| v.as_sequence()) {
                for ctx in contexts {
                    let name = ctx.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                    let cluster = ctx
                        .get("context")
                        .and_then(|c| c.get("cluster"))
                        .and_then(|v| v.as_str())
                        .unwrap_or("?");
                    let namespace = ctx
                        .get("context")
                        .and_then(|c| c.get("namespace"))
                        .and_then(|v| v.as_str());
                    let _ = write!(out, "- Context: {}  cluster:{}", name, cluster);
                    if let Some(ns) = namespace {
                        let _ = write!(out, "  namespace:{}", ns);
                    }
                    let _ = writeln!(out);
                }
            }
        }
    }
    if found_any {
        out.push('\n');
    }
}

/// Parse ~/.docker/config.json for configured registries (names only, never creds).
fn discover_docker_registries(home: &Path, out: &mut String) {
    let docker_config = home.join(".docker/config.json");
    if !docker_config.exists() {
        return;
    }

    if let Ok(content) = std::fs::read_to_string(&docker_config)
        && let Ok(json) = serde_json::from_str::<serde_json::Value>(&content)
    {
        let mut registries = Vec::new();

        // Check "auths" section
        if let Some(auths) = json.get("auths").and_then(|v| v.as_object()) {
            for key in auths.keys() {
                if !key.is_empty() {
                    registries.push(key.clone());
                }
            }
        }
        // Check "credHelpers" section
        if let Some(helpers) = json.get("credHelpers").and_then(|v| v.as_object()) {
            for key in helpers.keys() {
                if !registries.contains(key) {
                    registries.push(key.clone());
                }
            }
        }

        if !registries.is_empty() {
            let _ = writeln!(out, "### Container Registries\n");
            registries.sort();
            for reg in &registries {
                let _ = writeln!(out, "- {}", reg);
            }
            out.push('\n');
        }
    }
}

/// Check for other cloud platform CLIs and configs.
fn discover_other_platforms(home: &Path, out: &mut String) {
    let mut found = Vec::new();

    // Check config dirs
    let dir_checks: &[(&str, &str)] = &[
        (".wrangler", "Cloudflare"),
        (".vercel", "Vercel"),
        (".netlify", "Netlify"),
        (".fly", "Fly.io"),
    ];
    for (dir, name) in dir_checks {
        if home.join(dir).exists() {
            found.push(format!("- {} (~/{}/ exists)", name, dir));
        }
    }

    // Check CLIs via which
    let cli_checks: &[(&str, &str)] = &[
        ("doctl", "DigitalOcean"),
        ("hcloud", "Hetzner"),
        ("flyctl", "Fly.io"),
        ("railway", "Railway"),
        ("render", "Render"),
    ];
    for (cli, name) in cli_checks {
        if which::which(cli).is_ok() {
            found.push(format!("- {} ({} CLI installed)", name, cli));
        }
    }

    if !found.is_empty() {
        let _ = writeln!(out, "### Other Platforms\n");
        for entry in &found {
            let _ = writeln!(out, "{}", entry);
        }
        out.push('\n');
    }
}

/// Extract a value from an INI-style config file.
fn extract_ini_value(content: &str, key: &str) -> Option<String> {
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some((k, v)) = trimmed.split_once('=')
            && k.trim() == key
        {
            let val = v.trim().to_string();
            if !val.is_empty() {
                return Some(val);
            }
        }
    }
    None
}

/// Extract the 12-digit account ID from an AWS ARN.
/// ARN format: arn:aws:iam::ACCOUNT_ID:role/RoleName
/// or:         arn:aws:sts::ACCOUNT_ID:assumed-role/...
fn extract_account_from_arn(arn: &str) -> Option<String> {
    let parts: Vec<&str> = arn.split(':').collect();
    if parts.len() >= 5 {
        let account = parts[4];
        // Account IDs are 12 digits
        if !account.is_empty() && account.chars().all(|c| c.is_ascii_digit()) {
            return Some(account.to_string());
        }
    }
    None
}