stakpak 0.3.58

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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Login command - authenticate with LLM providers

use crate::onboarding::menu::{prompt_password, select_option_no_header};
use crate::onboarding::navigation::NavResult;
use stakpak_shared::auth_manager::AuthManager;
use stakpak_shared::models::auth::ProviderAuth;
use stakpak_shared::oauth::{AuthMethodType, OAuthFlow, OAuthProvider, ProviderRegistry};
use std::io::{self, Write};
use std::path::Path;

/// Handle the login command
pub async fn handle_login(
    config_dir: &Path,
    provider: &str,
    profile: Option<&str>,
    api_key: Option<String>,
    endpoint: Option<String>,
    region: Option<String>,
    aws_profile_name: Option<String>,
) -> Result<(), String> {
    // Bedrock has its own non-interactive flow (no API key needed)
    if provider == "bedrock" || provider == "amazon-bedrock" {
        return handle_bedrock_setup(config_dir, profile, region, aws_profile_name, endpoint).await;
    }

    // Non-interactive mode when --api-key is provided
    if let Some(key) = api_key {
        return handle_non_interactive_setup(config_dir, provider, profile, key, endpoint).await;
    }

    if endpoint.is_some() {
        let _validated = validate_login_endpoint(endpoint)?;
        eprintln!(
            "Warning: --endpoint is currently applied only in non-interactive mode (--api-key). Ignoring in interactive flow."
        );
    }

    // Interactive mode (existing behavior)
    // Select profile if not specified
    let profile = match profile {
        Some(p) => p.to_string(),
        None => select_profile_for_auth(config_dir).await?,
    };

    let registry = ProviderRegistry::new();

    // Always prompt for provider selection in interactive mode
    let providers = registry.list();
    let options: Vec<(String, String, bool)> = providers
        .iter()
        .map(|p| (p.id().to_string(), p.name().to_string(), false))
        .collect();

    let options_refs: Vec<(String, &str, bool)> = options
        .iter()
        .map(|(id, name, recommended)| (id.clone(), name.as_str(), *recommended))
        .collect();

    println!();
    println!("Select provider:");
    println!();

    let provider_id = match select_option_no_header(&options_refs, false) {
        NavResult::Forward(selected) => selected,
        NavResult::Back | NavResult::Cancel => {
            println!("Cancelled.");
            return Ok(());
        }
    };

    let provider = registry
        .get(&provider_id)
        .ok_or_else(|| format!("Unknown provider: {}", provider_id))?;

    // Select authentication method
    let methods = provider.auth_methods();
    let options: Vec<(String, String, bool)> = methods
        .iter()
        .enumerate()
        .map(|(i, m)| (m.id.clone(), m.display(), i == 0)) // First option is recommended
        .collect();

    let options_refs: Vec<(String, &str, bool)> = options
        .iter()
        .map(|(id, display, recommended)| (id.clone(), display.as_str(), *recommended))
        .collect();

    println!();
    println!("Select authentication method:");
    println!();

    let method_id = match select_option_no_header(&options_refs, true) {
        NavResult::Forward(selected) => selected,
        NavResult::Back | NavResult::Cancel => {
            println!("Cancelled.");
            return Ok(());
        }
    };

    let method = methods
        .iter()
        .find(|m| m.id == method_id)
        .ok_or_else(|| format!("Unknown method: {}", method_id))?;

    match method.method_type {
        AuthMethodType::OAuth => {
            handle_oauth_login(config_dir, provider, &method_id, &profile).await
        }
        AuthMethodType::ApiKey => handle_api_key_login(config_dir, provider, &profile).await,
    }
}

/// Select profile interactively for auth commands
/// Shows: "All profiles (shared)" and existing profiles
async fn select_profile_for_auth(config_dir: &Path) -> Result<String, String> {
    use crate::config::AppConfig;

    // Get available profiles from config
    let config_path = config_dir.join("config.toml");
    let available_profiles = AppConfig::list_available_profiles(Some(&config_path))
        .unwrap_or_else(|_| vec!["default".to_string()]);

    // Build options: "all" (shared) + existing profiles
    let mut options: Vec<(String, String, bool)> = vec![(
        "all".to_string(),
        "All profiles (shared credentials)".to_string(),
        true, // recommended
    )];

    for profile in &available_profiles {
        options.push((profile.clone(), format!("Profile: {}", profile), false));
    }

    let options_refs: Vec<(String, &str, bool)> = options
        .iter()
        .map(|(id, display, recommended)| (id.clone(), display.as_str(), *recommended))
        .collect();

    println!();
    println!("Save credentials to:");
    println!();

    match select_option_no_header(&options_refs, true) {
        NavResult::Forward(selected) => Ok(selected),
        NavResult::Back | NavResult::Cancel => Err("Cancelled.".to_string()),
    }
}

/// Handle OAuth login flow
async fn handle_oauth_login(
    config_dir: &Path,
    provider: &dyn OAuthProvider,
    method_id: &str,
    profile: &str,
) -> Result<(), String> {
    let oauth_config = provider
        .oauth_config(method_id)
        .ok_or("OAuth not supported for this method")?;

    let mut flow = OAuthFlow::new(oauth_config);
    let auth_url = flow.generate_auth_url();

    println!();
    println!("Opening browser for {} authentication...", provider.name());
    println!();
    println!("If browser doesn't open, visit:");
    // Use OSC 8 escape sequence to make the URL clickable in supported terminals
    println!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", auth_url, auth_url);
    println!();

    // Try to open browser
    let _ = open::that(&auth_url);

    // Prompt for authorization code
    print!("Paste the authorization code: ");
    io::stdout().flush().map_err(|e| e.to_string())?;

    let mut code = String::new();
    io::stdin()
        .read_line(&mut code)
        .map_err(|e| format!("Failed to read input: {}", e))?;
    let code = code.trim();

    if code.is_empty() {
        println!("Cancelled.");
        return Ok(());
    }

    println!();
    println!("Exchanging code for tokens...");

    let tokens = flow
        .exchange_code(code)
        .await
        .map_err(|e| format!("Token exchange failed: {}", e))?;

    let auth = provider
        .post_authorize(method_id, &tokens)
        .await
        .map_err(|e| format!("Post-authorization failed: {}", e))?;

    // Save credentials
    let mut auth_manager =
        AuthManager::new(config_dir).map_err(|e| format!("Failed to load auth manager: {}", e))?;

    auth_manager
        .set(profile, provider.id(), auth)
        .map_err(|e| format!("Failed to save credentials: {}", e))?;

    println!();
    println!("Successfully logged in to {}!", provider.name());

    if profile == "all" {
        println!("Credentials saved as shared default (all profiles).");
    } else {
        println!("Credentials saved for profile '{}'.", profile);
    }

    Ok(())
}

fn validate_login_endpoint(endpoint: Option<String>) -> Result<Option<String>, String> {
    let Some(endpoint) = endpoint else {
        return Ok(None);
    };

    let trimmed = endpoint.trim();
    if trimmed.is_empty() {
        return Err("--endpoint cannot be empty".to_string());
    }

    let parsed =
        reqwest::Url::parse(trimmed).map_err(|e| format!("Invalid --endpoint format: {}", e))?;

    if parsed.scheme() != "http" && parsed.scheme() != "https" {
        return Err(
            "Invalid --endpoint scheme: only http:// or https:// endpoints are supported"
                .to_string(),
        );
    }

    Ok(Some(trimmed.to_string()))
}

/// Handle non-interactive setup with --api-key and --provider flags
/// This initializes config and saves credentials in one step, mirroring interactive setup
async fn handle_non_interactive_setup(
    config_dir: &Path,
    provider_id: &str,
    profile: Option<&str>,
    api_key: String,
    endpoint: Option<String>,
) -> Result<(), String> {
    use crate::config::{ProfileConfig, ProviderType};
    use crate::onboarding::config_templates::{
        generate_anthropic_profile, generate_gemini_profile, generate_openai_profile,
    };
    use crate::onboarding::save_config::save_to_profile;

    // Default to "default" profile for non-interactive setup
    let profile_name = profile.unwrap_or("default");

    // Ensure config directory exists
    std::fs::create_dir_all(config_dir)
        .map_err(|e| format!("Failed to create config directory: {}", e))?;

    let validated_endpoint = validate_login_endpoint(endpoint)?;

    // Determine profile config based on provider
    let mut profile_config = match provider_id {
        "stakpak" => {
            // Stakpak API key -> Remote provider (key stored in config.toml)
            ProfileConfig {
                provider: Some(ProviderType::Remote),
                api_key: Some(api_key.clone()),
                api_endpoint: validated_endpoint.clone(),
                ..ProfileConfig::default()
            }
        }
        "anthropic" => generate_anthropic_profile(),
        "openai" => generate_openai_profile(),
        "gemini" => generate_gemini_profile(),
        _ => {
            return Err(format!(
                "Unsupported provider '{}'. Supported: anthropic, openai, gemini, stakpak, amazon-bedrock\n\
                 For bedrock, use: stakpak auth login --provider amazon-bedrock --region <region>",
                provider_id
            ));
        }
    };

    if provider_id != "stakpak"
        && let Some(endpoint) = validated_endpoint
    {
        let provider = profile_config
            .providers
            .get_mut(provider_id)
            .ok_or_else(|| format!("Provider '{}' not found in generated profile", provider_id))?;
        provider.set_api_endpoint(Some(endpoint));
    }

    // Save API key to auth.toml for local providers (not stakpak)
    if provider_id != "stakpak" {
        let mut auth_manager = AuthManager::new(config_dir)
            .map_err(|e| format!("Failed to load auth manager: {}", e))?;

        let auth = ProviderAuth::api_key(api_key);
        auth_manager
            .set(profile_name, provider_id, auth)
            .map_err(|e| format!("Failed to save credentials: {}", e))?;
    }

    // Save profile config to config.toml (this also creates readonly profile)
    let config_path = config_dir.join("config.toml");
    let config_path_str = config_path
        .to_str()
        .ok_or_else(|| "Invalid config path".to_string())?;

    save_to_profile(config_path_str, profile_name, profile_config)
        .map_err(|e| format!("Failed to save config: {}", e))?;

    println!(
        "Successfully configured {} for profile '{}'.",
        provider_id, profile_name
    );
    println!("Config saved to: {}", config_path.display());
    if provider_id != "stakpak" {
        println!(
            "Credentials saved to: {}",
            config_dir.join("auth.toml").display()
        );
    }

    Ok(())
}

/// Handle Bedrock provider setup
///
/// Unlike other providers, Bedrock does NOT need an API key.
/// Authentication is handled by the AWS credential chain.
/// We only need the region and optionally an AWS named profile.
async fn handle_bedrock_setup(
    config_dir: &Path,
    profile: Option<&str>,
    region: Option<String>,
    aws_profile_name: Option<String>,
    endpoint: Option<String>,
) -> Result<(), String> {
    use crate::config::{ProfileConfig, ProviderType};
    use crate::onboarding::save_config::save_to_profile;
    use stakpak_shared::models::llm::ProviderConfig;

    if endpoint.is_some() {
        let _validated = validate_login_endpoint(endpoint)?;
        eprintln!(
            "Warning: --endpoint is ignored for amazon-bedrock provider (uses AWS regional endpoints)."
        );
    }

    let region = region.unwrap_or_else(|| {
        println!("No --region specified, defaulting to us-east-1");
        "us-east-1".to_string()
    });

    let profile_name = profile.unwrap_or("default");

    // Ensure config directory exists
    std::fs::create_dir_all(config_dir)
        .map_err(|e| format!("Failed to create config directory: {}", e))?;

    // Bedrock uses the same Anthropic models — use friendly aliases
    // that resolve_bedrock_model_id() will map to full Bedrock IDs
    let smart_model = "amazon-bedrock/claude-sonnet-4-5".to_string();
    let eco_model = "amazon-bedrock/claude-haiku-4-5".to_string();

    let mut profile_config = ProfileConfig {
        provider: Some(ProviderType::Local),
        model: Some(smart_model.clone()),
        smart_model: Some(smart_model),
        eco_model: Some(eco_model),
        ..ProfileConfig::default()
    };

    profile_config.providers.insert(
        "amazon-bedrock".to_string(),
        ProviderConfig::Bedrock {
            region: region.clone(),
            profile_name: aws_profile_name.clone(),
        },
    );

    // Save profile config to config.toml (this also creates readonly profile)
    // NO credentials are saved to auth.toml — AWS credential chain handles auth
    let config_path = config_dir.join("config.toml");
    let config_path_str = config_path
        .to_str()
        .ok_or_else(|| "Invalid config path".to_string())?;

    save_to_profile(config_path_str, profile_name, profile_config)
        .map_err(|e| format!("Failed to save config: {}", e))?;

    println!(
        "Successfully configured Bedrock provider for profile '{}'.",
        profile_name
    );
    println!("Region: {}", region);
    if let Some(ref aws_profile) = aws_profile_name {
        println!("AWS Profile: {}", aws_profile);
    }
    println!("Config saved to: {}", config_path.display());
    println!();
    println!("Authentication uses the AWS credential chain:");
    println!("  1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)");
    println!("  2. Shared credentials file (~/.aws/credentials)");
    println!("  3. AWS SSO / IAM Identity Center");
    println!("  4. EC2/ECS instance roles");
    println!();
    println!("No AWS credentials are stored by stakpak.");

    Ok(())
}

/// Handle API key login
async fn handle_api_key_login(
    config_dir: &Path,
    provider: &dyn OAuthProvider,
    profile: &str,
) -> Result<(), String> {
    println!();

    let key = match prompt_password("Enter API key", true) {
        NavResult::Forward(Some(key)) => key,
        NavResult::Forward(None) => {
            println!("API key is required.");
            return Ok(());
        }
        NavResult::Back | NavResult::Cancel => {
            println!("Cancelled.");
            return Ok(());
        }
    };

    let auth = ProviderAuth::api_key(key);

    let mut auth_manager =
        AuthManager::new(config_dir).map_err(|e| format!("Failed to load auth manager: {}", e))?;

    auth_manager
        .set(profile, provider.id(), auth)
        .map_err(|e| format!("Failed to save credentials: {}", e))?;

    println!();
    println!("Successfully saved {} API key!", provider.name());

    if profile == "all" {
        println!("Credentials saved as shared default (all profiles).");
    } else {
        println!("Credentials saved for profile '{}'.", profile);
    }

    Ok(())
}

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

    fn load_config(config_dir: &Path) -> Result<ConfigFile, String> {
        let config_path = config_dir.join("config.toml");
        let content = std::fs::read_to_string(&config_path)
            .map_err(|e| format!("Failed to read {}: {}", config_path.display(), e))?;
        toml::from_str(&content)
            .map_err(|e| format!("Failed to parse {}: {}", config_path.display(), e))
    }

    fn temp_dir() -> tempfile::TempDir {
        match tempfile::TempDir::new() {
            Ok(dir) => dir,
            Err(error) => panic!("failed to create temp dir: {error}"),
        }
    }

    async fn assert_non_interactive_provider_endpoint(provider_id: &str, endpoint: &str) {
        let temp_dir = temp_dir();
        let result = handle_non_interactive_setup(
            temp_dir.path(),
            provider_id,
            Some("default"),
            "test-key".to_string(),
            Some(endpoint.to_string()),
        )
        .await;
        assert!(result.is_ok());

        let config = load_config(temp_dir.path());
        assert!(config.is_ok());

        if let Ok(config) = config {
            let profile = config.profiles.get("default");
            assert!(profile.is_some());
            if let Some(profile) = profile {
                let endpoint_in_config = profile
                    .providers
                    .get(provider_id)
                    .and_then(|provider| provider.api_endpoint());
                assert_eq!(endpoint_in_config, Some(endpoint));
            }
        }
    }

    #[test]
    fn validate_login_endpoint_rejects_invalid_url() {
        let result = validate_login_endpoint(Some("not-a-url".to_string()));
        assert!(result.is_err());
    }

    #[test]
    fn validate_login_endpoint_rejects_empty_url() {
        let result = validate_login_endpoint(Some("   ".to_string()));
        assert!(result.is_err());
    }

    #[test]
    fn validate_login_endpoint_rejects_unsupported_scheme() {
        let result = validate_login_endpoint(Some("ftp://proxy.example.com".to_string()));
        assert!(result.is_err());
    }

    #[test]
    fn validate_login_endpoint_accepts_http_and_https() {
        let http = validate_login_endpoint(Some("http://localhost:4000".to_string()));
        assert!(http.is_ok());

        let https = validate_login_endpoint(Some("https://proxy.example.com/v1".to_string()));
        assert!(https.is_ok());
    }

    #[tokio::test]
    async fn non_interactive_stakpak_sets_profile_api_endpoint() {
        let temp_dir = temp_dir();

        let endpoint = "https://self-hosted.example.com";
        let result = handle_non_interactive_setup(
            temp_dir.path(),
            "stakpak",
            Some("default"),
            "spk-test".to_string(),
            Some(endpoint.to_string()),
        )
        .await;
        assert!(result.is_ok());

        let config = load_config(temp_dir.path());
        assert!(config.is_ok());
        if let Ok(config) = config {
            let profile = config.profiles.get("default");
            assert!(profile.is_some());
            if let Some(profile) = profile {
                assert_eq!(profile.api_endpoint.as_deref(), Some(endpoint));
            }
        }
    }

    #[tokio::test]
    async fn non_interactive_openai_sets_provider_api_endpoint() {
        assert_non_interactive_provider_endpoint("openai", "https://openai-proxy.example.com/v1")
            .await;
    }

    #[tokio::test]
    async fn non_interactive_anthropic_sets_provider_api_endpoint() {
        assert_non_interactive_provider_endpoint(
            "anthropic",
            "https://anthropic-proxy.example.com",
        )
        .await;
    }

    #[tokio::test]
    async fn non_interactive_gemini_sets_provider_api_endpoint() {
        assert_non_interactive_provider_endpoint("gemini", "https://gemini-proxy.example.com")
            .await;
    }

    #[tokio::test]
    async fn bedrock_ignores_valid_url_after_validation() {
        let temp_dir = temp_dir();

        let result = handle_bedrock_setup(
            temp_dir.path(),
            Some("default"),
            Some("us-east-1".to_string()),
            None,
            Some("https://ignored.example.com".to_string()),
        )
        .await;
        assert!(result.is_ok());

        let config = load_config(temp_dir.path());
        assert!(config.is_ok());
        if let Ok(config) = config {
            let profile = config.profiles.get("default");
            assert!(profile.is_some());
            if let Some(profile) = profile {
                let bedrock = profile
                    .providers
                    .get("amazon-bedrock")
                    .and_then(|provider| provider.api_endpoint());
                assert_eq!(bedrock, None);
            }
        }
    }

    #[tokio::test]
    async fn bedrock_rejects_invalid_url_when_provided() {
        let temp_dir = temp_dir();
        let result = handle_bedrock_setup(
            temp_dir.path(),
            Some("default"),
            Some("us-east-1".to_string()),
            None,
            Some("not-a-url".to_string()),
        )
        .await;
        assert!(result.is_err());
    }
}