Skip to content

Commit 564b877

Browse files
initial commit
1 parent a0c1c6c commit 564b877

8 files changed

Lines changed: 238 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "kubesecret"
3+
version = "0.1.0"
4+
authors = ["Ben Watkins <ben@outdatedversion.com>"]
5+
edition = "2018"
6+
"description" = "Utility to quickly view Kubernetes secrets"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
json = "0.12.4"
12+
base64 = "0.13.0"
13+
clap = "2.33.3"

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# kubesecret
2+
3+
View decoded Kubernetes secrets quickly.
4+
5+
## Wish List
6+
7+
- More comprehensive secret type support
8+
- Support defined namespaces

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tab_spaces = 2

src/kube.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::process::Command;
2+
3+
pub fn get_secret(secret_name: &str) -> String {
4+
let cmd = Command::new("kubectl")
5+
.args(&["get", "secret", &secret_name, "-o", "json"])
6+
.output()
7+
.expect("Could not run kubectl");
8+
9+
return String::from_utf8_lossy(&cmd.stdout).into_owned();
10+
}

src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[macro_use]
2+
extern crate clap;
3+
4+
use clap::{App, Arg, ArgMatches};
5+
6+
pub mod kube;
7+
8+
pub fn read_args() -> ArgMatches<'static> {
9+
App::new(crate_name!())
10+
.version(crate_version!())
11+
.about(crate_description!())
12+
.arg(
13+
Arg::with_name("secretName")
14+
.short("s")
15+
.long("secret")
16+
.value_name("secret name")
17+
.help("Name of the Kubernetes secret")
18+
.takes_value(true)
19+
.required(true)
20+
.index(1),
21+
)
22+
.arg(
23+
Arg::with_name("secretKey")
24+
.value_name("secret key")
25+
.help("Key within the secret to get")
26+
.required(false)
27+
.multiple(true)
28+
.takes_value(true)
29+
.index(2),
30+
)
31+
.get_matches()
32+
}

src/main.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate base64;
2+
extern crate json;
3+
4+
fn main() {
5+
let args = kubesecret::read_args();
6+
let secret_name = args.value_of("secretName").unwrap();
7+
8+
let cmd_output = kubesecret::kube::get_secret(secret_name);
9+
10+
if cmd_output == "" {
11+
eprintln!("No such secret '{}'", secret_name);
12+
std::process::exit(1);
13+
}
14+
15+
let parsed_json = json::parse(&cmd_output).unwrap();
16+
let secret_data = &parsed_json["data"];
17+
18+
if args.is_present("secretKey") {
19+
let secret_key = args
20+
.values_of("secretKey")
21+
.unwrap()
22+
.map(|k| k.to_ascii_uppercase())
23+
.collect::<Vec<String>>()
24+
.join("_");
25+
26+
if !secret_data.has_key(&secret_key) {
27+
eprintln!("Secret ({}) does not have '{}'", secret_name, secret_key);
28+
std::process::exit(1);
29+
}
30+
31+
print!("{}", b64_encoded_json_value_to_string(&secret_data[secret_key]));
32+
std::process::exit(0);
33+
}
34+
35+
for entry in secret_data.entries() {
36+
let value = b64_encoded_json_value_to_string(entry.1);
37+
print!("{}: {}", entry.0, value);
38+
}
39+
}
40+
41+
fn b64_encoded_json_value_to_string(json: &json::JsonValue) -> String {
42+
let base64_encoded_value = json.as_str().expect("test");
43+
let decoded_value = base64::decode(base64_encoded_value).unwrap();
44+
45+
return String::from_utf8_lossy(&decoded_value).into_owned();
46+
}

0 commit comments

Comments
 (0)