1
0
Fork 1
Spiegel von https://github.com/dani-garcia/vaultwarden.git synchronisiert 2024-11-13 03:42:54 +01:00

Implemented config form and fixed config priority

Dieser Commit ist enthalten in:
Daniel García 2019-02-03 00:22:18 +01:00
Ursprung ade293cf52
Commit 3db815b969
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: FC8A7D14C3CD543A
6 geänderte Dateien mit 259 neuen und 154 gelöschten Zeilen

80
Cargo.lock generiert
Datei anzeigen

@ -135,15 +135,14 @@ dependencies = [
"num-derive 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"oath 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
"paste 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rmpv 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket_contrib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"soup 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syslog 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -796,7 +795,7 @@ dependencies = [
"pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1009,8 +1008,8 @@ dependencies = [
"base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1053,8 +1052,8 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1140,8 +1139,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"phf 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)",
"phf_codegen 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"string_cache 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"string_cache_codegen 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1295,7 +1294,7 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1346,7 +1345,7 @@ dependencies = [
"mustache 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1525,26 +1524,6 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "paste"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"paste-impl 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro-hack 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "paste-impl"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro-hack 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pear"
version = "0.1.2"
@ -1662,16 +1641,6 @@ name = "precomputed-hash"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro-hack"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "0.4.27"
@ -1874,7 +1843,7 @@ dependencies = [
"mime 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 2.0.0-alpha.6 (registry+https://github.com/rust-lang/crates.io-index)",
"native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1956,7 +1925,7 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"notify 4.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2099,12 +2068,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.85"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
version = "1.0.85"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2119,7 +2088,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -2129,7 +2098,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2235,7 +2204,7 @@ dependencies = [
"new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)",
"precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"string_cache_codegen 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2496,7 +2465,7 @@ name = "toml"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -2545,8 +2514,8 @@ dependencies = [
"bytes 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2973,8 +2942,6 @@ dependencies = [
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
"checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa"
"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
"checksum paste 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f50392d1265092fbee9273414cc40eb6d47d307bd66222c477bb8450c8504f9d"
"checksum paste-impl 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3cd512fe3a55e8933b2dcad913e365639db86d512e4004c3084b86864d9467a"
"checksum pear 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c26d2b92e47063ffce70d3e3b1bd097af121a9e0db07ca38a6cc1cf0cc85ff25"
"checksum pear_codegen 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "336db4a192cc7f54efeb0c4e11a9245394824cc3bcbd37ba3ff51240c35d7a6e"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
@ -2989,7 +2956,6 @@ dependencies = [
"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
"checksum plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1a6a0dc3910bc8db877ffed8e457763b317cf880df4ae19109b9f77d277cf6e0"
"checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
"checksum proc-macro-hack 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3e90aa19cd73dedc2d0e1e8407473f073d735fef0ab521438de6da8ee449ab66"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
@ -3035,8 +3001,8 @@ dependencies = [
"checksum security-framework-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3d6696852716b589dff9e886ff83778bb635150168e83afa8ac6b8a78cb82abc"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752"
"checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4"
"checksum serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)" = "52ab457c27b091c27b887eef7181b3ea11ab4f92f66e3a99b2e556b77f9cc6bd"
"checksum serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)" = "51eac71e1171246f337655221882f2f55a9c2e1d8ddc6990cee766509f15b702"
"checksum serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "27dce848e7467aa0e2fcaf0a413641499c0b745452aaca1194d24dedde9e13c9"
"checksum serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d48f9f99cd749a2de71d29da5f948de7f2764cc5a9d7f3c97e3514d4ee6eabf2"
"checksum sha-1 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8347606816471548cd60f0abd5ef0d513a81f5202dbdab9c09f17a15b5248484"

Datei anzeigen

@ -95,9 +95,6 @@ native-tls = "0.2.2"
# Template library
handlebars = "1.1.0"
# Macro ident-combining library
paste = "0.1.4"
# For favicon extraction from main website
soup = "0.3.0"
regex = "1.1.0"

Datei anzeigen

@ -90,7 +90,7 @@ fn _validate_token(token: &str) -> bool {
struct AdminTemplateData {
users: Vec<Value>,
page_content: String,
config: String,
config: Value,
}
impl AdminTemplateData {
@ -98,7 +98,7 @@ impl AdminTemplateData {
Self {
users,
page_content: String::from("admin/page"),
config: CONFIG.get_config(),
config: CONFIG.prepare_json(),
}
}
@ -170,9 +170,6 @@ fn deauth_user(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
#[post("/config", data = "<data>")]
fn post_config(data: Json<ConfigBuilder>, _token: AdminToken) -> EmptyResult {
let data: ConfigBuilder = data.into_inner();
info!("CONFIG: {:#?}", data);
CONFIG.update_config(data)
}

Datei anzeigen

@ -1,8 +1,6 @@
use std::process::exit;
use std::sync::RwLock;
use handlebars::Handlebars;
use crate::error::Error;
lazy_static! {
@ -14,18 +12,24 @@ lazy_static! {
}
macro_rules! make_config {
( $( $name:ident : $ty:ty $(, $default_fn:expr)? );+ $(;)? ) => {
( $( $name:ident : $ty:ty, $editable:literal, $none_action:ident $(, $default:expr)? );+ $(;)? ) => {
pub struct Config { inner: RwLock<Inner> }
struct Inner {
templates: Handlebars,
config: ConfigItems,
_env: ConfigBuilder,
_usr: ConfigBuilder,
}
#[derive(Debug, Default, Deserialize)]
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ConfigBuilder {
$($name: Option<$ty>),+
$(
#[serde(skip_serializing_if = "Option::is_none")]
$name: Option<$ty>
),+
}
impl ConfigBuilder {
@ -35,8 +39,7 @@ macro_rules! make_config {
let mut builder = ConfigBuilder::default();
$(
let $name = stringify!($name).to_uppercase();
builder.$name = make_config!{ @env &$name, $($default_fn)? };
builder.$name = get_env(&stringify!($name).to_uppercase());
)+
builder
@ -48,19 +51,36 @@ macro_rules! make_config {
serde_json::from_str(&config_str).map_err(Into::into)
}
fn merge(&mut self, other: Self) {
/// Merges the values of both builders into a new builder.
/// If both have the same element, `other` wins.
fn merge(&self, other: &Self) -> Self {
let mut builder = self.clone();
$(
if let v @Some(_) = other.$name {
self.$name = v;
if let v @Some(_) = &other.$name {
builder.$name = v.clone();
}
)+
builder
}
fn build(self) -> ConfigItems {
/// Returns a new builder with all the elements from self,
/// except those that are equal in both sides
fn remove(&self, other: &Self) -> Self {
let mut builder = ConfigBuilder::default();
$(
if &self.$name != &other.$name {
builder.$name = self.$name.clone();
}
)+
builder
}
fn build(&self) -> ConfigItems {
let mut config = ConfigItems::default();
let _domain_set = self.domain.is_some();
$(
config.$name = make_config!{ @build self.$name, &config, $($default_fn)? };
config.$name = make_config!{ @build self.$name.clone(), &config, $none_action, $($default)? };
)+
config.domain_set = _domain_set;
@ -68,33 +88,28 @@ macro_rules! make_config {
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ConfigItems { $(pub $name: $ty),+ }
#[derive(Debug, Clone, Default)]
pub struct ConfigItems { $(pub $name: make_config!{@type $ty, $none_action} ),+ }
paste::item! {
#[allow(unused)]
impl Config {
$(
pub fn $name(&self) -> $ty {
pub fn $name(&self) -> make_config!{@type $ty, $none_action} {
self.inner.read().unwrap().config.$name.clone()
}
pub fn [<set_ $name>](&self, value: $ty) {
self.inner.write().unwrap().config.$name = value;
}
)+
pub fn load() -> Result<Self, Error> {
// TODO: Get config.json from CONFIG_PATH env var or -c <CONFIG> console option
// Loading from file
let mut builder = match ConfigBuilder::from_file(&CONFIG_PATH) {
Ok(builder) => builder,
Err(_) => ConfigBuilder::default()
};
// Loading from env and file
let _env = ConfigBuilder::from_env();
let _usr = ConfigBuilder::from_file(&CONFIG_PATH).unwrap_or_default();
// Env variables overwrite config file
builder.merge(ConfigBuilder::from_env());
// Create merged config, config file overwrites env
let builder = _env.merge(&_usr);
// Fill any missing with defaults
let config = builder.build();
validate_config(&config)?;
@ -102,24 +117,46 @@ macro_rules! make_config {
inner: RwLock::new(Inner {
templates: load_templates(&config.templates_folder),
config,
_env,
_usr,
}),
})
}
}
}
};
pub fn prepare_json(&self) -> serde_json::Value {
let cfg = {
let inner = &self.inner.read().unwrap();
inner._env.merge(&inner._usr)
};
( @env $name:expr, $default_fn:expr ) => { get_env($name) };
( @env $name:expr, ) => {
match get_env($name) {
v @ Some(_) => Some(v),
None => None
fn _get_form_type(rust_type: &str) -> &'static str {
match rust_type {
"String" => "text",
"bool" => "checkbox",
_ => "number"
}
}
json!([ $( {
"editable": $editable,
"name": stringify!($name),
"value": cfg.$name,
"default": make_config!{ @default &cfg, $none_action, $($default)? },
"type": _get_form_type(stringify!($ty)),
}, )+ ])
}
}
};
( @build $value:expr,$config:expr, $default_fn:expr ) => {
// Wrap the optionals in an Option type
( @type $ty:ty, option) => { Option<$ty> };
( @type $ty:ty, $id:ident) => { $ty };
// Generate the values depending on none_action
( @build $value:expr, $config:expr, option, ) => { $value };
( @build $value:expr, $config:expr, def, $default:expr ) => { $value.unwrap_or($default) };
( @build $value:expr, $config:expr, auto, $default_fn:expr ) => {{
match $value {
Some(v) => v,
None => {
@ -127,61 +164,70 @@ macro_rules! make_config {
f($config)
}
}
};
}};
// Get a default value
( @default $config:expr, option, ) => { serde_json::Value::Null };
( @default $config:expr, def, $default:expr ) => { $default };
( @default $config:expr, auto, $default_fn:expr ) => {{
let f: &Fn(ConfigItems) -> _ = &$default_fn;
f($config.build())
}};
( @build $value:expr, $config:expr, ) => { $value.unwrap_or(None) };
}
//STRUCTURE: name: type, is_editable, none_action, <default_value (Optional)>
// Where none_action applied when the value wasn't provided and can be:
// def: Use a default value
// auto: Value is auto generated based on other values
// option: Value is optional
make_config! {
data_folder: String, |_| "data".to_string();
database_url: String, |c| format!("{}/{}", c.data_folder, "db.sqlite3");
icon_cache_folder: String, |c| format!("{}/{}", c.data_folder, "icon_cache");
attachments_folder: String, |c| format!("{}/{}", c.data_folder, "attachments");
templates_folder: String, |c| format!("{}/{}", c.data_folder, "templates");
data_folder: String, false, def, "data".to_string();
rsa_key_filename: String, |c| format!("{}/{}", c.data_folder, "rsa_key");
private_rsa_key: String, |c| format!("{}.der", c.rsa_key_filename);
private_rsa_key_pem: String, |c| format!("{}.pem", c.rsa_key_filename);
public_rsa_key: String, |c| format!("{}.pub.der", c.rsa_key_filename);
database_url: String, false, auto, |c| format!("{}/{}", c.data_folder, "db.sqlite3");
icon_cache_folder: String, false, auto, |c| format!("{}/{}", c.data_folder, "icon_cache");
attachments_folder: String, false, auto, |c| format!("{}/{}", c.data_folder, "attachments");
templates_folder: String, false, auto, |c| format!("{}/{}", c.data_folder, "templates");
rsa_key_filename: String, false, auto, |c| format!("{}/{}", c.data_folder, "rsa_key");
websocket_enabled: bool, |_| false;
websocket_address: String, |_| "0.0.0.0".to_string();
websocket_port: u16, |_| 3012;
websocket_enabled: bool, false, def, false;
websocket_address: String, false, def, "0.0.0.0".to_string();
websocket_port: u16, false, def, 3012;
web_vault_folder: String, |_| "web-vault/".to_string();
web_vault_enabled: bool, |_| true;
web_vault_folder: String, false, def, "web-vault/".to_string();
web_vault_enabled: bool, true, def, true;
icon_cache_ttl: u64, |_| 2_592_000;
icon_cache_negttl: u64, |_| 259_200;
icon_cache_ttl: u64, true, def, 2_592_000;
icon_cache_negttl: u64, true, def, 259_200;
disable_icon_download: bool, |_| false;
signups_allowed: bool, |_| true;
invitations_allowed: bool, |_| true;
password_iterations: i32, |_| 100_000;
show_password_hint: bool, |_| true;
disable_icon_download: bool, true, def, false;
signups_allowed: bool, true, def, true;
invitations_allowed: bool, true, def, true;
password_iterations: i32, true, def, 100_000;
show_password_hint: bool, true, def, true;
domain: String, |_| "http://localhost".to_string();
domain_set: bool, |_| false;
domain: String, true, def, "http://localhost".to_string();
domain_set: bool, false, def, false;
reload_templates: bool, |_| false;
reload_templates: bool, true, def, false;
extended_logging: bool, |_| true;
log_file: Option<String>;
extended_logging: bool, false, def, true;
log_file: String, false, option;
admin_token: Option<String>;
admin_token: String, true, option;
yubico_client_id: Option<String>;
yubico_secret_key: Option<String>;
yubico_server: Option<String>;
yubico_client_id: String, true, option;
yubico_secret_key: String, true, option;
yubico_server: String, true, option;
// Mail settings
smtp_host: Option<String>;
smtp_ssl: bool, |_| true;
smtp_port: u16, |c| if c.smtp_ssl {587} else {25};
smtp_from: String, |_| String::new();
smtp_from_name: String, |_| "Bitwarden_RS".to_string();
smtp_username: Option<String>;
smtp_password: Option<String>;
smtp_host: String, true, option;
smtp_ssl: bool, true, def, true;
smtp_port: u16, true, auto, |c| if c.smtp_ssl {587} else {25};
smtp_from: String, true, def, String::new();
smtp_from_name: String, true, def, "Bitwarden_RS".to_string();
smtp_username: String, true, option;
smtp_password: String, true, option;
}
fn validate_config(cfg: &ConfigItems) -> Result<(), Error> {
@ -201,18 +247,26 @@ fn validate_config(cfg: &ConfigItems) -> Result<(), Error> {
}
impl Config {
pub fn get_config(&self) -> String {
let cfg = &self.inner.read().unwrap().config;
serde_json::to_string_pretty(cfg).unwrap()
}
pub fn update_config(&self, other: ConfigBuilder) -> Result<(), Error> {
let config = other.build();
// Remove default values
let builder = other.remove(&self.inner.read().unwrap()._env);
// Serialize now before we consume the builder
let config_str = serde_json::to_string_pretty(&builder)?;
// Prepare the combined config
let config = {
let env = &self.inner.read().unwrap()._env;
env.merge(&builder).build()
};
validate_config(&config)?;
let config_str = serde_json::to_string_pretty(&config)?;
self.inner.write().unwrap().config = config.clone();
// Save both the user and the combined config
{
let mut writer = self.inner.write().unwrap();
writer.config = config;
writer._usr = builder;
}
//Save to file
use std::{fs::File, io::Write};
@ -222,6 +276,15 @@ impl Config {
Ok(())
}
pub fn private_rsa_key(&self) -> String {
format!("{}.der", CONFIG.rsa_key_filename())
}
pub fn private_rsa_key_pem(&self) -> String {
format!("{}.pem", CONFIG.rsa_key_filename())
}
pub fn public_rsa_key(&self) -> String {
format!("{}.pub.der", CONFIG.rsa_key_filename())
}
pub fn mail_enabled(&self) -> bool {
self.inner.read().unwrap().config.smtp_host.is_some()
}
@ -242,10 +305,15 @@ impl Config {
}
}
use handlebars::{
Context, Handlebars, Helper, HelperDef, HelperResult, Output, RenderContext, RenderError, Renderable,
};
fn load_templates(path: &str) -> Handlebars {
let mut hb = Handlebars::new();
// Error on missing params
hb.set_strict_mode(true);
hb.register_helper("case", Box::new(CaseHelper));
macro_rules! reg {
($name:expr) => {{
@ -272,3 +340,28 @@ fn load_templates(path: &str) -> Handlebars {
hb
}
#[derive(Clone, Copy)]
pub struct CaseHelper;
impl HelperDef for CaseHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
r: &'reg Handlebars,
ctx: &Context,
rc: &mut RenderContext<'reg>,
out: &mut Output,
) -> HelperResult {
let param = h
.param(0)
.ok_or_else(|| RenderError::new("Param not found for helper \"case\""))?;
let value = param.value().clone();
if h.params().iter().skip(1).any(|x| x.value() == &value) {
h.template().map(|t| t.render(r, ctx, rc, out)).unwrap_or(Ok(()))
} else {
Ok(())
}
}
}

Datei anzeigen

@ -1,5 +1,5 @@
#![feature(proc_macro_hygiene, decl_macro, vec_remove_item, try_trait)]
#![recursion_limit = "128"]
#![recursion_limit = "256"]
#[macro_use]
extern crate rocket;

Datei anzeigen

@ -56,16 +56,45 @@
<div id="config-block" class="align-items-center p-3 mb-3 text-white-50 bg-secondary rounded shadow">
<div>
<h6 class="mb-0 text-white">Configuration</h6>
<h6 class="text-white">Configuration</h6>
<form class="form" id="config-form">
<textarea id="config-text" class="form-control" style="height: 300px;">{{config}}</textarea>
{{#each config}}
{{#if editable}}
<div class="form-group row">
{{#case type "text" "number"}}
<label for="input_{{name}}" class="col-sm-2 col-form-label">{{name}}</label>
<div class="col-sm-9">
<input class="form-control" id="input_{{name}}" type="{{type}}" name="{{name}}" value="{{value}}"
{{#if default}} placeholder="Default: {{default}}" {{/if}}>
</div>
{{/case}}
{{#case type "checkbox"}}
<div class="col-sm-2">{{name}}</div>
<div class="col-sm-9">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="input_{{name}}" name="{{name}}"
{{#if value}} checked {{/if}}>
<label class="form-check-label" for="input_{{name}}"> Default: {{default}} </label>
</div>
</div>
{{/case}}
</div>
{{/if}}
{{/each}}
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</main>
<style>
#config-block ::placeholder {
/* Most modern browsers support this now. */
color: orangered;
}
</style>
<script>
function reload() { window.location.reload(); }
function identicon(email) {
@ -73,7 +102,12 @@
return "data:image/svg+xml;base64," + data.toString();
}
function _post(url, successMsg, errMsg, data) {
$.post(url, data, function () {
$.post({
url: url,
data: data,
//async: false,
contentType: "application/json",
}).done(function () {
alert(successMsg);
}).fail(function (e) {
const r = e.responseJSON;
@ -108,8 +142,26 @@
"Error inviting user", data);
return false;
}
function getFormData(form) {
var form_array = form.serializeArray();
var indexed_array = {};
$.each(form_array, function (i, v) {
indexed_array[v.name] = process_value(v.value);
});
return indexed_array;
}
function process_value(val) {
val = val.trim();
if (val === "") { return null; }
if (!isNaN(val)) { return +val; }
if (val === "true" || val === "on") { return true; }
if (val === "false" || val === "off") { return false; }
return val;
}
function saveConfig() {
data = $("#config-text").val();
data = JSON.stringify(getFormData($("#config-form")));
_post("/admin/config/", "Config saved correctly",
"Error saving config", data);
return false;