Verified Commit 4d8ed491 authored by Katharina Fey's avatar Katharina Fey 🏴
Browse files

Adding simple config loading

parent 5a34eaf7
......@@ -604,6 +604,7 @@ dependencies = [
"miscreant 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......
......@@ -14,3 +14,4 @@ miscreant = { version = "0.4", features = ["soft-aes"] }
blake2 = "0.7"
keybob = "0.3"
base64 = "0.10"
textwrap = "0.11"
\ No newline at end of file
use clap::{App, AppSettings, Arg, SubCommand};
use forge::{hash, secret, setup};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
fn main() {
let app = App::new("forge-cli")
......
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
#[derive(Debug, Serialize, Deserialize)]
pub struct Auth {
user: String,
method: String,
key: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Deploy {
cmd: String,
mirror: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
auth: Auth,
deploy: Deploy,
secrets: HashMap<String, String>,
}
/// Open and sanitise a forge config
pub fn load_config(path: &str) -> Option<Config> {
let mut f = File::open(path).ok()?;
let mut s = String::new();
f.read_to_string(&mut s).ok()?;
let yaml: Config = serde_yaml::from_str(&s).ok()?;
let secrets = yaml
.secrets
.into_iter()
.fold(HashMap::new(), |mut map, (k, v)| {
map.insert(k, v.chars().filter(|c| c != &'\n').collect());
map
});
Some(Config { secrets, ..yaml })
}
pub mod hash;
pub mod secret;
pub mod setup;
pub mod secret;
pub mod config;
use blake2::digest::{Input, VariableOutput};
use blake2::Blake2s;
......@@ -11,6 +12,7 @@ use miscreant::siv::Aes256Siv;
use base64;
use serde::{Deserialize, Serialize};
use serde_yaml;
use textwrap::fill;
const BLAKE_16_LENGTH: usize = 16;
const SALT_DIVIDER: &'static str = "=0w0=";
......@@ -45,6 +47,7 @@ impl<'salt> From<&'salt String> for Hash<'salt> {
}
/// Some enrypted data
#[derive(Debug)]
pub struct PackedData {
pub nonce: Vec<u8>,
pub iv: Vec<u8>,
......@@ -129,8 +132,8 @@ pub fn base64_decode(data: &String) -> Vec<u8> {
}
/// Same as `PackedData` but all bas64 encoded because readability
#[derive(Serialize, Deserialize)]
struct YamlData {
#[derive(Serialize, Deserialize, Debug)]
pub struct YamlData {
iv: String,
nonce: String,
data: String,
......@@ -159,11 +162,21 @@ impl From<YamlData> for PackedData {
pub fn to_yaml(pd: PackedData) -> String {
let yd: YamlData = pd.into();
let s = serde_yaml::to_string(&yd).unwrap();
base64_encode(&s.into_bytes())
format!("{}", fill(&base64_encode(&s.into_bytes()), 72))
}
pub fn from_yaml(yaml: String) -> PackedData {
let tmp = base64_decode(&yaml);
let stripped: String = String::from_utf8(
yaml.into_bytes()
.into_iter()
.map(|c| {
println!("Char: `{}`", c);
c
})
.collect(),
)
.unwrap();
let tmp = base64_decode(&stripped);
let yaml = String::from_utf8(tmp).unwrap();
let yd: YamlData = serde_yaml::from_str(&yaml).unwrap();
yd.into()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment