kftray-portforward 0.30.0

KFtray library with port forwarding logic
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
use std::path::PathBuf;

use anyhow::{
    Context,
    Result,
};
use kftray_commons::config_dir::get_kubeconfig_paths;
use kube::config::{
    Config,
    KubeConfigOptions,
    Kubeconfig,
};
use log::{
    error,
    info,
};
use openssl::base64::{
    decode_block,
    encode_block,
};
use secrecy::ExposeSecret;

use super::utils::{
    convert_pkcs8_to_pkcs1,
    is_pkcs8_key,
};

pub trait ConfigExtClone {
    fn clone_with_invalid_certs(&self, accept_invalid_certs: bool) -> Self;
}

impl ConfigExtClone for Config {
    fn clone_with_invalid_certs(&self, accept_invalid_certs: bool) -> Self {
        let mut config = self.clone();
        config.accept_invalid_certs = accept_invalid_certs;
        config
    }
}

pub fn get_kubeconfig_paths_from_option(kubeconfig: Option<String>) -> Result<Vec<PathBuf>> {
    match kubeconfig {
        Some(path) if path == "default" => {
            info!("Using default kubeconfig paths.");
            get_kubeconfig_paths()
        }
        Some(path) => {
            info!("Using provided kubeconfig paths: {path}");
            Ok(path
                .split(if cfg!(windows) { ';' } else { ':' })
                .map(PathBuf::from)
                .collect())
        }
        None => {
            info!("No kubeconfig path provided, using default paths.");
            get_kubeconfig_paths()
        }
    }
}

pub fn merge_kubeconfigs(paths: &[PathBuf]) -> Result<(Kubeconfig, Vec<String>)> {
    let mut errors = Vec::new();
    let mut merged_kubeconfig = Kubeconfig::default();

    for path in paths {
        info!("Attempting to read kubeconfig from path: {path:?}");
        match Kubeconfig::read_from(path) {
            Ok(kubeconfig) => {
                info!("Successfully read kubeconfig from {path:?}");
                match merged_kubeconfig.clone().merge(kubeconfig) {
                    Ok(merged) => merged_kubeconfig = merged,
                    Err(e) => {
                        let error_msg = format!("Failed to merge kubeconfig from {path:?}: {e}");
                        error!("{error_msg}");
                        errors.push(error_msg);
                    }
                }
            }
            Err(e) => {
                let error_msg = format!("Failed to read kubeconfig from {path:?}: {e}");
                error!("{error_msg}");
                errors.push(error_msg);
            }
        }
    }

    Ok((merged_kubeconfig, errors))
}

pub async fn create_config_with_context(
    kubeconfig: &Kubeconfig, context_name: &str,
) -> Result<Config> {
    info!("Creating configuration for context: {context_name}");
    let mut kubeconfig = kubeconfig.clone();

    for auth_info in &mut kubeconfig.auth_infos {
        if let Some(ref mut auth_info_data) = auth_info.auth_info
            && let Some(client_key_data) = &auth_info_data.client_key_data
        {
            let decoded_key = decode_block(client_key_data.expose_secret())
                .context("Failed to decode client key data")?;

            if is_pkcs8_key(&decoded_key)
                && let Some(converted_key) = convert_pkcs8_to_pkcs1(&decoded_key)
                    .context("Failed to convert PKCS#8 key to PKCS#1")?
            {
                let encoded_key = encode_block(&converted_key);
                auth_info_data.client_key_data = Some(encoded_key.into());
            }
        }
    }

    let context_to_use = if context_name == "@current" {
        kubeconfig.current_context.clone()
    } else {
        Some(context_name.to_owned())
    };

    Config::from_custom_kubeconfig(
        kubeconfig,
        &KubeConfigOptions {
            context: context_to_use,
            ..Default::default()
        },
    )
    .await
    .context("Failed to create configuration from kubeconfig")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::kube::operations::list_contexts;

    #[test]
    fn test_get_kubeconfig_paths_from_option() {
        use std::env;
        use std::fs;
        use std::sync::Mutex;

        use tempfile::TempDir;

        lazy_static::lazy_static! {
            static ref ENV_LOCK: Mutex<()> = Mutex::new(());
        }
        let _env_guard = ENV_LOCK.lock().unwrap();

        let original_kubeconfig = env::var("KUBECONFIG").ok();
        let original_home = env::var("HOME").ok();

        let temp_dir = TempDir::new().unwrap();

        // Test platform-specific path separator (';' on Windows, ':' on Unix)
        let separator = if cfg!(windows) { ';' } else { ':' };
        let test_paths = if cfg!(windows) {
            format!("C:\\path1{separator}D:\\path2")
        } else {
            format!("/path1{separator}/path2")
        };
        let explicit_paths = get_kubeconfig_paths_from_option(Some(test_paths.clone())).unwrap();
        assert_eq!(explicit_paths.len(), 2);
        if cfg!(windows) {
            assert_eq!(explicit_paths[0], std::path::Path::new("C:\\path1"));
            assert_eq!(explicit_paths[1], std::path::Path::new("D:\\path2"));
        } else {
            assert_eq!(explicit_paths[0], std::path::Path::new("/path1"));
            assert_eq!(explicit_paths[1], std::path::Path::new("/path2"));
        }

        let mock_kubeconfig_path = temp_dir.path().join("mock_kubeconfig");
        fs::write(&mock_kubeconfig_path, "mock kubeconfig content").unwrap();

        unsafe { env::set_var("KUBECONFIG", mock_kubeconfig_path.to_str().unwrap()) };

        let default_path_result = get_kubeconfig_paths_from_option(Some("default".to_string()));
        assert!(default_path_result.is_ok());
        let default_path = default_path_result.unwrap();
        assert!(!default_path.is_empty());
        assert_eq!(default_path[0], mock_kubeconfig_path);

        let none_path_result = get_kubeconfig_paths_from_option(None);
        assert!(none_path_result.is_ok());
        let none_path = none_path_result.unwrap();
        assert!(!none_path.is_empty());
        assert_eq!(none_path[0], mock_kubeconfig_path);

        let fake_home = temp_dir.path().join("fake_home");
        fs::create_dir_all(fake_home.join(".kube")).unwrap();
        let fake_kubeconfig = fake_home.join(".kube").join("config");
        fs::write(&fake_kubeconfig, "home dir kubeconfig content").unwrap();

        unsafe { env::remove_var("KUBECONFIG") };

        unsafe { env::set_var("HOME", fake_home.to_str().unwrap()) };

        let home_fallback_result = get_kubeconfig_paths_from_option(None);
        assert!(home_fallback_result.is_ok());
        let home_fallback_path = home_fallback_result.unwrap();
        assert!(!home_fallback_path.is_empty());
        assert_eq!(home_fallback_path[0], fake_kubeconfig);

        let nonexistent_dir = temp_dir.path().join("nonexistent");

        unsafe { env::set_var("HOME", nonexistent_dir.to_str().unwrap()) };

        unsafe {
            env::set_var(
                "KUBECONFIG",
                temp_dir.path().join("nonexistent_file").to_str().unwrap(),
            )
        };

        let error_result = get_kubeconfig_paths_from_option(None);
        assert!(error_result.is_err());

        match original_kubeconfig {
            Some(val) => unsafe { env::set_var("KUBECONFIG", val) },
            None => unsafe { env::remove_var("KUBECONFIG") },
        }

        match original_home {
            Some(val) => unsafe { env::set_var("HOME", val) },
            None => unsafe { env::remove_var("HOME") },
        }
    }

    #[test]
    fn test_merge_kubeconfigs_empty() {
        let (config, errors) = merge_kubeconfigs(&[]).unwrap();
        assert!(list_contexts(&config).is_empty());
        assert!(errors.is_empty());
    }

    #[test]
    fn test_merge_kubeconfigs_with_invalid_path() {
        let paths = vec![PathBuf::from("/invalid/path/that/should/not/exist")];
        let (config, errors) = merge_kubeconfigs(&paths).unwrap();
        assert!(list_contexts(&config).is_empty());
        assert!(!errors.is_empty());
    }

    #[test]
    fn test_list_contexts() {
        let kubeconfig = Kubeconfig {
            contexts: vec![
                kube::config::NamedContext {
                    name: "context1".to_string(),
                    context: Some(kube::config::Context::default()),
                    ..Default::default()
                },
                kube::config::NamedContext {
                    name: "context2".to_string(),
                    context: Some(kube::config::Context::default()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let contexts = crate::kube::operations::list_contexts(&kubeconfig);
        assert_eq!(contexts.len(), 2);
        assert_eq!(contexts[0], "context1");
        assert_eq!(contexts[1], "context2");
    }

    #[test]
    fn test_config_ext_clone() {
        let mut config = Config::new("https://example.com".parse().unwrap());
        config.accept_invalid_certs = false;

        let cloned_config = config.clone_with_invalid_certs(true);
        assert!(cloned_config.accept_invalid_certs);

        let cloned_config_false = config.clone_with_invalid_certs(false);
        assert!(!cloned_config_false.accept_invalid_certs);
    }

    #[tokio::test]
    async fn test_create_config_with_context() {
        let mut kubeconfig = Kubeconfig::default();
        let context_name = "test-context";

        let named_context = kube::config::NamedContext {
            name: context_name.to_string(),
            context: Some(kube::config::Context::default()),
            ..Default::default()
        };
        kubeconfig.contexts = vec![named_context];

        let result = create_config_with_context(&kubeconfig, context_name).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_create_config_ignores_unused_non_rsa_pkcs8_key() {
        use openssl::ec::{
            EcGroup,
            EcKey,
        };
        use openssl::nid::Nid;
        use openssl::pkey::PKey;

        let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
        let ec_key = PKey::from_ec_key(EcKey::generate(&group).unwrap()).unwrap();
        let ec_key_data = encode_block(&ec_key.private_key_to_pem_pkcs8().unwrap());

        let kubeconfig = Kubeconfig::from_yaml(&format!(
            r#"
apiVersion: v1
kind: Config
clusters:
- name: test-cluster
  cluster:
    server: https://test-server.com
contexts:
- name: token-context
  context:
    cluster: test-cluster
    user: token-user
current-context: token-context
users:
- name: token-user
  user:
    token: test-token
- name: ec-user
  user:
    client-key-data: {ec_key_data}
"#
        ))
        .unwrap();

        let result = create_config_with_context(&kubeconfig, "token-context").await;
        assert!(result.is_ok(), "{:#}", result.unwrap_err());
    }

    #[test]
    fn test_config_ext_clone_preserves_other_fields() {
        let mut config = Config::new("https://example.com".parse().unwrap());
        config.accept_invalid_certs = false;
        config.default_namespace = "test-namespace".to_string();
        config.read_timeout = Some(std::time::Duration::from_secs(30));

        let cloned_config = config.clone_with_invalid_certs(true);
        assert!(cloned_config.accept_invalid_certs);
        assert_eq!(
            cloned_config.default_namespace,
            "test-namespace".to_string()
        );
        assert_eq!(
            cloned_config.read_timeout,
            Some(std::time::Duration::from_secs(30))
        );
        assert_eq!(cloned_config.cluster_url, config.cluster_url);
    }

    #[tokio::test]
    async fn test_create_config_with_insecure_skip_tls_verify_true() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let kubeconfig_path = temp_dir.path().join("kubeconfig");

        let kubeconfig_content = r#"
apiVersion: v1
kind: Config
clusters:
- name: test-cluster
  cluster:
    server: https://test-server.com
    insecure-skip-tls-verify: true
contexts:
- name: test-context
  context:
    cluster: test-cluster
    user: test-user
current-context: test-context
users:
- name: test-user
  user:
    token: test-token
"#;

        std::fs::write(&kubeconfig_path, kubeconfig_content).unwrap();

        let kubeconfig = Kubeconfig::read_from(&kubeconfig_path).unwrap();
        let config = create_config_with_context(&kubeconfig, "test-context")
            .await
            .unwrap();

        assert!(config.accept_invalid_certs);
    }

    #[tokio::test]
    async fn test_create_config_with_insecure_skip_tls_verify_false() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let kubeconfig_path = temp_dir.path().join("kubeconfig");

        let kubeconfig_content = r#"
apiVersion: v1
kind: Config
clusters:
- name: test-cluster
  cluster:
    server: https://test-server.com
    insecure-skip-tls-verify: false
contexts:
- name: test-context
  context:
    cluster: test-cluster
    user: test-user
current-context: test-context
users:
- name: test-user
  user:
    token: test-token
"#;

        std::fs::write(&kubeconfig_path, kubeconfig_content).unwrap();

        let kubeconfig = Kubeconfig::read_from(&kubeconfig_path).unwrap();
        let config = create_config_with_context(&kubeconfig, "test-context")
            .await
            .unwrap();

        assert!(!config.accept_invalid_certs);
    }

    #[tokio::test]
    async fn test_create_config_without_insecure_skip_tls_verify() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let kubeconfig_path = temp_dir.path().join("kubeconfig");

        let kubeconfig_content = r#"
apiVersion: v1
kind: Config
clusters:
- name: test-cluster
  cluster:
    server: https://test-server.com
contexts:
- name: test-context
  context:
    cluster: test-cluster
    user: test-user
current-context: test-context
users:
- name: test-user
  user:
    token: test-token
"#;

        std::fs::write(&kubeconfig_path, kubeconfig_content).unwrap();

        let kubeconfig = Kubeconfig::read_from(&kubeconfig_path).unwrap();
        let config = create_config_with_context(&kubeconfig, "test-context")
            .await
            .unwrap();

        assert!(!config.accept_invalid_certs);
    }

    #[tokio::test]
    async fn test_merge_kubeconfigs_with_insecure_skip_tls() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let kubeconfig_path = temp_dir.path().join("kubeconfig");

        let kubeconfig_content = r#"
apiVersion: v1
kind: Config
clusters:
- name: secure-cluster
  cluster:
    server: https://secure-server.com
    insecure-skip-tls-verify: false
- name: insecure-cluster
  cluster:
    server: https://insecure-server.com
    insecure-skip-tls-verify: true
contexts:
- name: secure-context
  context:
    cluster: secure-cluster
    user: test-user
- name: insecure-context
  context:
    cluster: insecure-cluster
    user: test-user
users:
- name: test-user
  user:
    token: test-token
"#;

        std::fs::write(&kubeconfig_path, kubeconfig_content).unwrap();

        let (merged_kubeconfig, errors) = merge_kubeconfigs(&[kubeconfig_path]).unwrap();

        let contexts = list_contexts(&merged_kubeconfig);
        assert_eq!(contexts.len(), 2);
        assert!(contexts.contains(&"secure-context".to_string()));
        assert!(contexts.contains(&"insecure-context".to_string()));
        assert!(errors.is_empty());

        let insecure_cluster = merged_kubeconfig
            .clusters
            .iter()
            .find(|c| c.name == "insecure-cluster")
            .unwrap();
        assert_eq!(
            insecure_cluster
                .cluster
                .as_ref()
                .unwrap()
                .insecure_skip_tls_verify,
            Some(true)
        );

        let secure_cluster = merged_kubeconfig
            .clusters
            .iter()
            .find(|c| c.name == "secure-cluster")
            .unwrap();
        assert_eq!(
            secure_cluster
                .cluster
                .as_ref()
                .unwrap()
                .insecure_skip_tls_verify,
            Some(false)
        );
    }
}