click 0.3.2

A command-line REPL for Kubernetes that integrates into existing cli workflows
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
// Copyright 2017 Databricks, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Handle reading .kube/config files

use chrono::{DateTime, Local, TimeZone};
use chrono::offset::Utc;
use duct::cmd;
use serde_json::{self, Value};
use serde_yaml;

use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::From;
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::{self, BufReader, Read};

use error::{KubeErrNo, KubeError};
use kube::{Kluster, KlusterAuth};
use certs::{get_cert, get_cert_from_pem, get_key_from_str, get_private_key};

/// Kubernetes cluster config
#[derive(Debug, Deserialize)]
struct IConfig {
    clusters: Vec<ICluster>,
    contexts: Vec<IContext>,
    users: Vec<IUser>,
}

#[derive(Debug, Deserialize)]
struct ICluster {
    name: String,
    #[serde(rename = "cluster")] conf: IClusterConf,
}

#[derive(Debug, Deserialize)]
struct IClusterConf {
    #[serde(rename = "certificate-authority")] pub cert: Option<String>,
    #[serde(rename = "certificate-authority-data")] pub cert_data: Option<String>,
    #[serde(rename = "insecure-skip-tls-verify")] pub skip_tls: Option<bool>,
    pub server: String,
}

#[derive(Debug)]
pub struct ClusterConf {
    pub cert: Option<String>,
    pub server: String,
    pub insecure_skip_tls_verify: bool,
}

impl ClusterConf {
    fn new(cert: Option<String>, server: String) -> ClusterConf {
        ClusterConf {
            cert: cert,
            server: server,
            insecure_skip_tls_verify: false,
        }
    }

    fn new_insecure(cert: Option<String>, server: String) -> ClusterConf {
        ClusterConf {
            cert: cert,
            server: server,
            insecure_skip_tls_verify: true,
        }
    }
}

#[derive(Debug, Deserialize)]
struct IContext {
    name: String,
    #[serde(rename = "context")] conf: ContextConf,
}

#[derive(Debug, Deserialize, Clone)]
pub struct ContextConf {
    pub cluster: String,
    //#[serde(default = "default")]
    pub namespace: Option<String>,
    pub user: String,
}

#[derive(Debug, Deserialize)]
struct IUser {
    name: String,
    #[serde(rename = "user")] conf: IUserConf,
}

// Classes to hold deserialized data for auth
#[derive(Debug, Deserialize, Clone)]
pub struct AuthProvider {
    name: String,
    pub token: RefCell<Option<String>>,
    pub expiry: RefCell<Option<String>>,
    pub config: AuthProviderConfig,
}

impl AuthProvider {
    // Copy the token and expiry out of the config into the refcells
    fn copy_up(&self) {
        let mut token = self.token.borrow_mut();
        *token = self.config.access_token.clone();
        let mut expiry = self.expiry.borrow_mut();
        *expiry = self.config.expiry.clone();
    }

    // true if expiry is before now
    fn check_dt<T: TimeZone>(&self, expiry: DateTime<T>) -> bool {
        let etime = expiry.with_timezone(&Utc);
        let now = Utc::now();
        etime < now
    }

    fn is_expired(&self) -> bool {
        let expiry = self.expiry.borrow();
        match *expiry {
            Some(ref e) => {
                // Somehow google sometimes puts a date like "2018-03-31 22:22:01" in the config
                // and other times like "2018-04-01T05:57:31Z", so we have to try both.  wtf google.
                if let Ok(expiry) = DateTime::parse_from_rfc3339(e) {
                    self.check_dt(expiry)
                } else if let Ok(expiry) = Local.datetime_from_str(e, "%Y-%m-%d %H:%M:%S") {
                    self.check_dt(expiry)
                } else {
                    true
                }
            }
            None => {
                println!("No expiry set, cannot validate if token is still valid");
                false
            }
        }
    }

    // Turn a {.credential.expiry_key} type string into a serde_json pointer string like
    // /credential/expiry_key
    fn make_pointer(&self, s: &str) -> String {
        let l = s.len() - 1;
        let split = &s[1..l].split('.');
        split.clone().collect::<Vec<&str>>().join("/")
    }

    fn update_token(&self, token: &mut Option<String>, expiry: &mut Option<String>) {
        match self.config.cmd_path {
            Some(ref conf_cmd) => {
                let args = self.config
                    .cmd_args
                    .as_ref()
                    .map(|argstr| argstr.split_whitespace().collect())
                    .unwrap_or(vec![]);
                match cmd(conf_cmd, &args).read() {
                    Ok(output) => {
                        let v: Value = serde_json::from_str(output.as_str()).unwrap();
                        let mut updated_token = false;
                        match self.config.token_key.as_ref() {
                            Some(ref tk) => {
                                let token_pntr = self.make_pointer(tk.as_str());
                                let extracted_token =
                                    v.pointer(token_pntr.as_str()).and_then(|tv| tv.as_str());
                                *token = extracted_token.map(|t| t.to_owned());
                                updated_token = true;
                            }
                            None => {
                                println!("No token-key in auth-provider, cannot extract token");
                            }
                        }

                        if updated_token {
                            match self.config.expiry_key.as_ref() {
                                Some(ref ek) => {
                                    let expiry_pntr = self.make_pointer(ek.as_str());
                                    let extracted_expiry =
                                        v.pointer(expiry_pntr.as_str()).and_then(|ev| ev.as_str());
                                    *expiry = extracted_expiry.map(|e| e.to_owned());
                                }
                                None => {
                                    println!(
                                        "No expiry-key in config, will have to pull a new \
                                         token on every command"
                                    );
                                }
                            }
                        }
                    }
                    Err(e) => {
                        println!("Failed to run update command: {}", e);
                    }
                }
            }
            None => {
                println!("No update command specified, can't update");
            }
        }
    }

    /// Checks that we have a valid token, and if not, attempts to update it based on the config
    pub fn ensure_token(&self) -> String {
        let mut token = self.token.borrow_mut();
        if token.is_none() || self.is_expired() {
            // update
            let mut expiry = self.expiry.borrow_mut();
            *token = None;
            self.update_token(&mut token, &mut expiry)
        }
        match token.as_ref() {
            Some(t) => t.clone(),
            None => {
                println!(
                    "Couldn't get an authentication token. You can try exiting Click and \
                     running a kubectl command against the cluster to refresh it. Also please \
                     report this error on the Click github page."
                );
                "".to_owned()
            }
        }
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct AuthProviderConfig {
    #[serde(rename = "access-token")] pub access_token: Option<String>,
    expiry: Option<String>,

    #[serde(rename = "cmd-args")] cmd_args: Option<String>,
    #[serde(rename = "cmd-path")] cmd_path: Option<String>,
    #[serde(rename = "expiry-key")] expiry_key: Option<String>,
    #[serde(rename = "token-key")] token_key: Option<String>,
}

/// This represents what we can find in a user in the actual config file (note the Deserialize).
/// Hence all the optional fields.  At some point we should write a custom deserializer for this to
/// make it cleaner
#[derive(Debug, Deserialize, Clone)]
pub struct IUserConf {
    pub token: Option<String>,

    #[serde(rename = "client-certificate")] pub client_cert: Option<String>,
    #[serde(rename = "client-key")] pub client_key: Option<String>,
    #[serde(rename = "client-certificate-data")] pub client_cert_data: Option<String>,
    #[serde(rename = "client-key-data")] pub client_key_data: Option<String>,

    pub username: Option<String>,
    pub password: Option<String>,

    #[serde(rename = "auth-provider")] pub auth_provider: Option<AuthProvider>,
}

// These are the different kinds of authentication data  a user might have

/// KeyCert can be either raw data from a "client-*-data" field, or a path to a file with the data
/// from a "client-*" field.
#[derive(Debug)]
pub enum UserConf {
    Token(String),
    KeyCertPath(String, String),
    KeyCertData(String, String),
    UserPass(String, String),
    AuthProvider(AuthProvider),
    Unsupported,
}

impl From<IUserConf> for UserConf {
    fn from(iconf: IUserConf) -> UserConf {
        if let Some(token) = iconf.token {
            UserConf::Token(token)
        } else if let (Some(username), Some(password)) = (iconf.username, iconf.password) {
            UserConf::UserPass(username, password)
        } else if let (Some(client_cert_path), Some(key_path)) =
            (iconf.client_cert, iconf.client_key)
        {
            UserConf::KeyCertPath(client_cert_path, key_path)
        } else if let (Some(client_cert_data), Some(key_data)) =
            (iconf.client_cert_data, iconf.client_key_data)
        {
            UserConf::KeyCertData(client_cert_data, key_data)
        } else if let Some(auth_provider) = iconf.auth_provider {
            UserConf::AuthProvider(auth_provider)
        } else {
            UserConf::Unsupported
        }
    }
}

impl IConfig {
    fn from_file(path: &str) -> Result<IConfig, io::Error> {
        let f = File::open(path)?;
        serde_yaml::from_reader(f).map_err(|e| {
            io::Error::new(
                io::ErrorKind::Other,
                format!("Couldn't read yaml: {}", e.description()),
            )
        })
    }
}

/// A kubernetes config
// This is actual config we expose
#[derive(Debug)]
pub struct Config {
    pub source_file: String,
    pub clusters: HashMap<String, ClusterConf>,
    pub contexts: HashMap<String, ContextConf>,
    pub users: HashMap<String, UserConf>,
}

// some utility functions
fn get_full_path(path: String) -> Result<String, KubeError> {
    if path.is_empty() {
        return Err(KubeError::ConfigFileError(
            "Empty certificate/key path".to_owned(),
        ));
    }
    // unwrap okay, validated above
    if path.chars().next().unwrap() == '/' {
        Ok(path)
    } else if let Some(home_dir) = env::home_dir() {
        Ok(format!("{}/.kube/{}", home_dir.as_path().display(), path))
    } else {
        return Err(KubeError::ConfigFileError(
            "Could not get path kubernetes \
             certificates/keys (not fully specified, and \
             your home directory is not known."
                .to_owned(),
        ));
    }
}

fn auth_from_paths(
    client_cert_path: String,
    key_path: String,
    context: &str,
) -> Result<KlusterAuth, KubeError> {
    if client_cert_path.len() == 0 {
        return Err(KubeError::ConfigFileError(format!(
            "Empty client certificate path for {}, can't continue",
            context
        )));
    }
    if key_path.len() == 0 {
        return Err(KubeError::ConfigFileError(format!(
            "Empty client key path for {}, can't continue",
            context
        )));
    }

    let cert_full_path = get_full_path(client_cert_path)?;
    let key_full_path = get_full_path(key_path)?;
    if let (Some(cert), Some(private_key)) = (
        get_cert(cert_full_path.as_str()),
        get_private_key(key_full_path.as_str()),
    ) {
        Ok(KlusterAuth::with_cert_and_key(cert, private_key))
    } else {
        Err(KubeError::ConfigFileError(format!(
            "Can't read/convert cert or private key for {}",
            context
        )))
    }
}

fn auth_from_data(
    client_cert_data: &String,
    key_data: &String,
    context: &str,
) -> Result<KlusterAuth, KubeError> {
    if client_cert_data.len() == 0 {
        Err(KubeError::ConfigFileError(format!(
            "Empty client certificate data for {}, can't continue",
            context
        )))
    } else if key_data.len() == 0 {
        Err(KubeError::ConfigFileError(format!(
            "Empty client key data for {}, can't continue",
            context
        )))
    } else {
        let mut cert_enc = ::base64::decode(client_cert_data.as_str())?;
        cert_enc.retain(|&i| i != 0);
        let cert_pem = String::from_utf8(cert_enc).map_err(|e| {
            KubeError::ConfigFileError(format!(
                "Invalid utf8 data in certificate: {}",
                e.description()
            ))
        })?;
        let cert = get_cert_from_pem(cert_pem.as_str());
        let mut key_enc = ::base64::decode(key_data.as_str())?;
        key_enc.retain(|&i| i != 0);
        let key_str = String::from_utf8(key_enc).map_err(|e| {
            KubeError::ConfigFileError(format!("Invalid utf8 data in key: {}", e.description()))
        })?;
        let key = get_key_from_str(key_str.as_str());
        match (cert, key) {
            (Some(c), Some(k)) => Ok(KlusterAuth::with_cert_and_key(c, k)),
            _ => Err(KubeError::ConfigFileError(format!(
                "Invalid certificate or key data for context: {}",
                context
            ))),
        }
    }
}

impl Config {
    pub fn from_file(path: &str) -> Result<Config, KubeError> {
        let iconf = IConfig::from_file(path)?;

        // copy over clusters
        let mut cluster_map = HashMap::new();
        for cluster in iconf.clusters.into_iter() {
            // make sure we've specified one of:
            //  - a cert file
            //  - cert data
            //  - insecure-skip-tls-verify
            let has_cd = cluster.conf.cert_data.is_some();
            match (cluster.conf.cert, cluster.conf.cert_data) {
                (Some(cert_config_path), _) => {
                    if has_cd {
                        println!(
                            "Cluster {} specifies a certificate path and certificate data, \
                             ignoring data and using the path",
                            cluster.name
                        );
                    }
                    let cert_path = get_full_path(cert_config_path)?;
                    match File::open(cert_path) {
                        Ok(f) => {
                            let mut br = BufReader::new(f);
                            let mut s = String::new();
                            br.read_to_string(&mut s).expect("Couldn't read cert");
                            cluster_map.insert(
                                cluster.name.clone(),
                                ClusterConf::new(Some(s), cluster.conf.server),
                            );
                        }
                        Err(e) => {
                            println!(
                                "Invalid server cert path for cluster {}: {}.\nAny contexts using \
                                 this cluster will be unavailable.",
                                cluster.name,
                                e.description()
                            );
                        }
                    }
                }
                (None, Some(cert_data)) => match ::base64::decode(cert_data.as_str()) {
                    Ok(mut cert) => {
                        cert.retain(|&i| i != 0);
                        let cert_pem = String::from_utf8(cert).map_err(|e| {
                            KubeError::ConfigFileError(format!(
                                "Invalid utf8 data in certificate: {}",
                                e.description()
                            ))
                        })?;
                        cluster_map.insert(
                            cluster.name.clone(),
                            ClusterConf::new(Some(cert_pem), cluster.conf.server),
                        );
                    }
                    Err(e) => {
                        println!(
                            "Invalid certificate data, could not base64 decode: {}",
                            e.description()
                        );
                    }
                },
                (None, None) => {
                    let conf = if cluster.conf.skip_tls.unwrap_or(false) {
                        ClusterConf::new_insecure(None, cluster.conf.server)
                    } else {
                        ClusterConf::new(None, cluster.conf.server)
                    };
                    cluster_map.insert(cluster.name.clone(), conf);
                }
            }
        }

        // copy over contexts
        let mut context_map = HashMap::new();
        for context in iconf.contexts.iter() {
            context_map.insert(context.name.clone(), context.conf.clone());
        }

        // copy over users
        let mut user_map = HashMap::new();
        for user in iconf.users.into_iter() {
            user_map.insert(user.name.clone(), user.conf.into());
        }

        Ok(Config {
            source_file: path.to_owned(),
            clusters: cluster_map,
            contexts: context_map,
            users: user_map,
        })
    }

    pub fn cluster_for_context(&self, context: &str) -> Result<Kluster, KubeError> {
        self.contexts
            .get(context)
            .map(|ctx| {
                self.clusters
                    .get(&ctx.cluster)
                    .map(|cluster| {
                        self.users
                            .get(&ctx.user)
                            .map(|user| {
                                let auth = match user {
                                    &UserConf::Token(ref token) => {
                                        Ok(KlusterAuth::with_token(token.as_str()))
                                    }
                                    &UserConf::UserPass(ref username, ref password) => {
                                        Ok(KlusterAuth::with_userpass(username, password))
                                    }
                                    &UserConf::KeyCertPath(ref cert_path, ref key_path) => {
                                        auth_from_paths(
                                            cert_path.clone(),
                                            key_path.clone(),
                                            context,
                                        )
                                    }
                                    &UserConf::KeyCertData(ref cert_data, ref key_data) => {
                                        auth_from_data(cert_data, key_data, context)
                                    }
                                    &UserConf::AuthProvider(ref provider) => {
                                        provider.copy_up();
                                        Ok(KlusterAuth::with_auth_provider(provider.clone()))
                                    }
                                    _ => Err(KubeError::ConfigFileError(format!(
                                        "Invalid context {}.  Each user must have either a token, \
                                         a username AND password, or a client-certificate AND \
                                         a client-key, or an auth-provider",
                                        context
                                    ))),
                                }?;
                                Kluster::new(
                                    context,
                                    cluster.cert.clone(),
                                    cluster.server.as_str(),
                                    auth,
                                    cluster.insecure_skip_tls_verify,
                                )
                            })
                            .ok_or(KubeError::Kube(KubeErrNo::InvalidUser))
                    })
                    .ok_or(KubeError::Kube(KubeErrNo::InvalidCluster))
            })
            .ok_or(KubeError::Kube(KubeErrNo::InvalidContextName))
            .and_then(|n| n)
            .and_then(|n| n)
            .and_then(|n| n)
    }
}

/// Click config
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct ClickConfig {
    pub namespace: Option<String>,
    pub context: Option<String>,
    pub editor: Option<String>,
    pub terminal: Option<String>,
}

impl ClickConfig {
    pub fn from_file(path: &str) -> ClickConfig {
        match File::open(path) {
            Ok(f) => match serde_yaml::from_reader(f) {
                Ok(c) => c,
                Err(e) => {
                    println!("Could not read config file {:?}, using default values", e);
                    ClickConfig::default()
                }
            },
            Err(e) => {
                println!(
                    "Could not open config file at '{}': {}. Using default values",
                    path, e
                );
                ClickConfig::default()
            }
        }
    }

    pub fn save_to_file(&self, path: &str) -> Result<(), KubeError> {
        let mut file = try!(File::create(path));
        try!(serde_yaml::to_writer(&mut file, &self));
        Ok(())
    }
}