-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from sarub0b0/425-feature-decoding-helm-secret
feat(k8s/secret): Support decoding `helm.sh/release.v1` secret type
- Loading branch information
Showing
7 changed files
with
195 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"0.2","flagWords":[],"words":["ratatui","crossterm","kubetui","Atext","multispace","nums","arboard","kohashimoto","kubeconfig","hoge","rstest","thiserror","openapi","Kubeconfig","indoc","apimachinery","horizontalpodautoscalers","mockall","configmap","chrono","networkpolicies","networkpolicy","serviceaccount","jdfbz","tolerations","pullable","fuga","coldef","Appender","logfile","appender","Fullscreen","activatable","rustfmt","tesat","tesatb","fullwidth","mabb","peekable","taskbox","ptimized","mhoge","oneline","maaaaaaaaaaaaaaa","impls","showable","mabc","HOGE","hogehoge","subwin","erde","rustls","taplo","kosay","serde","itertools","anychar","statefulset","statefulsets","replicaset","replicasets","daemonset","resoruces"],"language":"en"} | ||
{"version":"0.2","flagWords":[],"words":["ratatui","crossterm","kubetui","Atext","multispace","nums","arboard","kohashimoto","kubeconfig","hoge","rstest","thiserror","openapi","Kubeconfig","indoc","apimachinery","horizontalpodautoscalers","mockall","configmap","chrono","networkpolicies","networkpolicy","serviceaccount","jdfbz","tolerations","pullable","fuga","coldef","Appender","logfile","appender","Fullscreen","activatable","rustfmt","tesat","tesatb","fullwidth","mabb","peekable","taskbox","ptimized","mhoge","oneline","maaaaaaaaaaaaaaa","impls","showable","mabc","HOGE","hogehoge","subwin","erde","rustls","taplo","kosay","serde","itertools","anychar","statefulset","statefulsets","replicaset","replicasets","daemonset","resoruces","flate"],"language":"en"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::collections::{btree_map, BTreeMap}; | ||
|
||
use base64::{engine::general_purpose, Engine}; | ||
use k8s_openapi::ByteString; | ||
|
||
use crate::event::kubernetes::color::Color; | ||
|
||
use super::format::{format_error, format_utf8}; | ||
|
||
/// any type secret | ||
#[derive(Debug, Default)] | ||
pub struct Any { | ||
data: BTreeMap<String, ByteString>, | ||
} | ||
|
||
impl Any { | ||
pub fn new(data: BTreeMap<String, ByteString>) -> Self { | ||
Self { data } | ||
} | ||
|
||
pub fn to_string_key_values(&self) -> Vec<String> { | ||
self.iter() | ||
.flat_map(|key_value| { | ||
key_value | ||
.lines() | ||
.map(ToString::to_string) | ||
.collect::<Vec<String>>() | ||
}) | ||
.collect() | ||
} | ||
|
||
fn iter(&self) -> Iter { | ||
Iter { | ||
iter: self.data.iter(), | ||
color: Color::new(), | ||
} | ||
} | ||
} | ||
|
||
struct Iter<'a> { | ||
iter: btree_map::Iter<'a, String, ByteString>, | ||
color: Color, | ||
} | ||
|
||
impl Iterator for Iter<'_> { | ||
type Item = String; | ||
fn next(&mut self) -> std::option::Option<<Self as Iterator>::Item> { | ||
let Some((key, ByteString(value))) = self.iter.next() else { | ||
return None; | ||
}; | ||
|
||
let color = self.color.next_color(); | ||
|
||
match String::from_utf8(value.to_vec()) { | ||
Ok(utf8_data) => Some(format_utf8(key, &utf8_data, color)), | ||
Err(err) => { | ||
let base64_encoded = general_purpose::STANDARD.encode(value); | ||
|
||
Some(format_error(key, &base64_encoded, &err.to_string(), color)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::event::kubernetes::color::fg::Color; | ||
|
||
pub(super) fn format_utf8(key: &str, value: &str, color: u8) -> String { | ||
if value.contains('\n') { | ||
let mut ret = format!("\x1b[{color}m{key}:\x1b[39m |\n", color = color, key = key); | ||
|
||
value.lines().for_each(|l| { | ||
ret += &format!(" {}\n", l); | ||
}); | ||
|
||
ret.trim_end().to_string() | ||
} else { | ||
format!( | ||
"\x1b[{color}m{key}:\x1b[39m {value}", | ||
color = color, | ||
key = key, | ||
value = value, | ||
) | ||
} | ||
} | ||
|
||
pub(super) fn format_error(key: &str, value: &str, err: &str, color: u8) -> String { | ||
format!( | ||
"\x1b[{color}m{key}:\x1b[39m | \x1b[{error_color}m# {error}\x1b[39m\n [base64-encoded] {value}", | ||
color = color, | ||
key = key, | ||
value = value, | ||
error_color = Color::DarkGray as u8, | ||
error = err | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::{collections::BTreeMap, io::prelude::*}; | ||
|
||
use anyhow::Result; | ||
use base64::{engine::general_purpose, Engine}; | ||
use k8s_openapi::ByteString; | ||
|
||
use crate::event::kubernetes::color::{self, Color}; | ||
|
||
use super::format::{format_error, format_utf8}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Helm { | ||
data: BTreeMap<String, ByteString>, | ||
} | ||
|
||
impl Helm { | ||
pub fn new(data: BTreeMap<String, ByteString>) -> Self { | ||
Self { data } | ||
} | ||
|
||
pub fn to_string_key_values(&self) -> Vec<String> { | ||
let Some(ByteString(value)) = self.data.get("release") else { | ||
return vec!["no release data".into()]; | ||
}; | ||
|
||
let mut color = Color::new(); | ||
|
||
let decoded_release = match decode_release(value) { | ||
Ok(decoded) => { | ||
let color = color.next_color(); | ||
format_utf8("release (decoded)", &decoded, color) | ||
} | ||
Err(err) => { | ||
format!( | ||
"\x1b[{red}m# Failed to decode the 'release' value: {err}\x1b[39m", | ||
red = color::fg::Color::Red as u8, | ||
err = err | ||
) | ||
} | ||
}; | ||
|
||
let color = color.next_color(); | ||
|
||
let release = match String::from_utf8(value.to_vec()) { | ||
Ok(utf8_data) => format_utf8("release", &utf8_data, color), | ||
Err(err) => { | ||
let base64_encoded = general_purpose::STANDARD.encode(value); | ||
format_error("release", &base64_encoded, &err.to_string(), color) | ||
} | ||
}; | ||
|
||
decoded_release | ||
.lines() | ||
.chain(release.lines()) | ||
.map(ToString::to_string) | ||
.collect() | ||
} | ||
} | ||
|
||
fn decode_release(data: &[u8]) -> Result<String> { | ||
let gzip = general_purpose::STANDARD.decode(data)?; | ||
|
||
// decode gzip | ||
let mut decoder = flate2::read::GzDecoder::new(&gzip[..]); | ||
let mut decoded = String::new(); | ||
decoder.read_to_string(&mut decoded)?; | ||
|
||
let yaml = serde_yaml::from_str::<serde_yaml::Value>(&decoded)?; | ||
|
||
serde_yaml::to_string(&yaml).map_err(Into::into) | ||
} |