config work

This commit is contained in:
John Smith 2022-01-18 21:21:11 -05:00
parent 205a6a8fd1
commit effc4aeeac
11 changed files with 196 additions and 50 deletions

1
Cargo.lock generated
View File

@ -3776,6 +3776,7 @@ version = "0.1.0"
dependencies = [
"async-std",
"async-tungstenite 0.8.0",
"async_executors",
"bugsalot",
"capnp",
"capnp-rpc",

View File

@ -13,6 +13,7 @@ path = "src/main.rs"
[dependencies]
async-std = { version = "^1.9", features = ["unstable", "attributes"] }
async-tungstenite = { version = "^0.8", features = ["async-std-runtime"] }
async_executors = { version = "^0", default-features = false, features = [ "async_std" ]}
cursive = { path = "../external/cursive/cursive", default-features = false, features = ["crossterm-backend", "toml"]}
cursive-flexi-logger-view = { path = "../external/cursive-flexi-logger-view" }
cursive_buffered_backend = { path = "../external/cursive_buffered_backend" }

View File

@ -1,13 +1,13 @@
use crate::command_processor::*;
use crate::veilid_client_capnp::*;
use veilid_core::xx::*;
use async_executors::{AsyncStd, LocalSpawnHandleExt};
use capnp::capability::Promise;
use capnp_rpc::{pry, rpc_twoparty_capnp, twoparty, Disconnector, RpcSystem};
use futures::AsyncReadExt;
use std::cell::RefCell;
use std::net::SocketAddr;
use std::rc::Rc;
use veilid_core::xx::*;
struct VeilidClientImpl {
comproc: CommandProcessor,
@ -152,6 +152,8 @@ impl ClientApiConnection {
));
}
// Process the rpc system until we decide we're done
if let Ok(rpc_jh) = AsyncStd.spawn_handle_local(rpc_system) {
// Send the request and get the state object and the registration object
if let Ok(response) = request.send().promise.await {
if let Ok(response) = response.get() {
@ -159,13 +161,21 @@ impl ClientApiConnection {
if let Ok(state) = response.get_state() {
// Set up our state for the first time
if self.process_veilid_state(state).await.is_ok() {
// Don't drop the registration
rpc_system.await.map_err(map_to_string)?;
// Don't drop the registration, doing so will remove the client
// object mapping from the server which we need for the update backchannel
// Wait until rpc system completion or disconnect was requested
if let Err(e) = rpc_jh.await {
error!("Client RPC system error: {}", e);
}
}
}
}
}
}
} else {
error!("Failed to spawn client RPC system");
}
// Drop the server and disconnector too (if we still have it)
let mut inner = self.inner.borrow_mut();

View File

@ -29,8 +29,23 @@ impl ProtectedStore {
}
}
pub async fn delete_all(&self) -> Result<(), String> {
// Delete all known keys
if self.remove_user_secret_string("node_id").await? {
debug!("deleted protected_store key 'node_id'");
}
if self.remove_user_secret_string("node_id_secret").await? {
debug!("deleted protected_store key 'node_id_secret'");
}
if self.remove_user_secret_string("_test_key").await? {
debug!("deleted protected_store key '_test_key'");
}
Ok(())
}
pub async fn init(&self) -> Result<(), String> {
let c = self.config.get();
{
let mut inner = self.inner.lock();
if !c.protected_store.always_use_insecure_storage {
cfg_if! {
@ -59,6 +74,11 @@ impl ProtectedStore {
if inner.keyring_manager.is_none() {
return Err("Could not initialize the protected store.".to_owned());
}
}
if c.protected_store.delete {
self.delete_all().await?;
}
Ok(())
}

View File

@ -27,6 +27,12 @@ impl TableStore {
}
}
pub async fn delete_all(&self) -> Result<(), String> {
// Delete all known keys
self.delete("crypto_caches").await?;
Ok(())
}
pub async fn init(&self) -> Result<(), String> {
Ok(())
}

View File

@ -141,6 +141,13 @@ pub struct VeilidConfigNetwork {
#[derive(Default, Clone)]
pub struct VeilidConfigTableStore {
pub directory: String,
pub delete: bool,
}
#[derive(Default, Clone)]
pub struct VeilidConfigBlockStore {
pub directory: String,
pub delete: bool,
}
#[derive(Default, Clone)]
@ -148,6 +155,7 @@ pub struct VeilidConfigProtectedStore {
pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool,
pub insecure_fallback_directory: String,
pub delete: bool,
}
#[derive(Default, Clone)]
@ -168,6 +176,7 @@ pub struct VeilidConfigInner {
pub capabilities: VeilidConfigCapabilities,
pub protected_store: VeilidConfigProtectedStore,
pub table_store: VeilidConfigTableStore,
pub block_store: VeilidConfigBlockStore,
pub network: VeilidConfigNetwork,
}
@ -216,9 +225,13 @@ impl VeilidConfig {
get_config!(inner.capabilities.protocol_connect_wss);
get_config!(inner.capabilities.protocol_accept_wss);
get_config!(inner.table_store.directory);
get_config!(inner.table_store.delete);
get_config!(inner.block_store.directory);
get_config!(inner.block_store.delete);
get_config!(inner.protected_store.allow_insecure_fallback);
get_config!(inner.protected_store.always_use_insecure_storage);
get_config!(inner.protected_store.insecure_fallback_directory);
get_config!(inner.protected_store.delete);
get_config!(inner.network.node_id);
get_config!(inner.network.node_id_secret);
get_config!(inner.network.max_connections);

View File

@ -124,6 +124,12 @@ impl VeilidCore {
crypto.init().await?;
inner.crypto = Some(crypto.clone());
// Set up block store
// trace!("VeilidCore::internal_startup init block store");
// let block_store = BlockStore::new(config.clone());
// block_store.init().await?;
// inner.block_store = Some(block_store.clone();)
// Set up attachment manager
trace!("VeilidCore::internal_startup init attachment manager");
let cb = setup.update_callback;

View File

@ -101,6 +101,13 @@ pub struct VeilidConfigNetwork {
#[derive(Default, Clone)]
pub struct VeilidConfigTableStore {
pub directory: String,
pub delete: bool,
}
#[derive(Default, Clone)]
pub struct VeilidConfigBlockStore {
pub directory: String,
pub delete: bool,
}
#[derive(Default, Clone)]
@ -108,6 +115,7 @@ pub struct VeilidConfigProtectedStore {
pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool,
pub insecure_fallback_directory: String,
pub delete: bool,
}
#[derive(Default, Clone)]
@ -128,6 +136,7 @@ pub struct VeilidConfig {
pub capabilities: VeilidConfigCapabilities,
pub protected_store: VeilidConfigProtectedStore,
pub table_store: VeilidConfigTableStore,
pub block_store: VeilidConfigBlockStore,
pub network: VeilidConfigNetwork,
}

View File

@ -1,10 +1,9 @@
use crate::settings::*;
use std::ffi::OsStr;
use clap::{App, Arg, ArgMatches};
use std::ffi::OsStr;
use std::str::FromStr;
fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap::Error> {
let matches = App::new("veilid-server")
.version("0.1")
.about("Veilid Server")
@ -22,6 +21,7 @@ fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap
.takes_value(true)
.value_name("FILE")
.default_value_os(default_config_path)
.allow_invalid_utf8(true)
.help("Specify a configuration file to use"),
).arg(
Arg::new("attach")
@ -54,7 +54,21 @@ fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap
.long("generate-dht-key")
.help("Only generate a new dht key and print it"),
)
.arg(
Arg::new("delete-protected-store")
.long("delete-protected-store")
.help("Delete the entire contents of the protected store (DANGER, NO UNDO!)"),
)
.arg(
Arg::new("delete-table-store")
.long("delete-table-store")
.help("Delete the entire contents of the table store (DANGER, NO UNDO!)"),
)
.arg(
Arg::new("delete-block-store")
.long("delete-block-store")
.help("Delete the entire contents of the block store (DANGER, NO UNDO!)"),
)
.arg(
Arg::new("dump-config")
.long("dump-config")
@ -84,7 +98,6 @@ fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap
}
pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
// Get command line options
let default_config_path = Settings::get_default_config_path();
let matches = do_clap_matches(default_config_path.as_os_str())
@ -126,6 +139,7 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
}
settingsrw.testing.subnode_index = subnode_index;
}
if matches.occurrences_of("debug") != 0 {
settingsrw.logging.terminal.enabled = true;
settingsrw.logging.terminal.level = LogLevel::Debug;
@ -140,6 +154,15 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
if matches.is_present("local") {
settingsrw.core.network.enable_local_peer_scope = true;
}
if matches.occurrences_of("delete-protected-store") != 0 {
settingsrw.core.protected_store.delete = true;
}
if matches.occurrences_of("delete-block-store") != 0 {
settingsrw.core.block_store.delete = true;
}
if matches.occurrences_of("delete-table-store") != 0 {
settingsrw.core.table_store.delete = true;
}
if matches.occurrences_of("bootstrap") != 0 {
let bootstrap = match matches.value_of("bootstrap") {
Some(x) => {

View File

@ -33,8 +33,8 @@ fn main() -> Result<(), String> {
.map_err(|e| e.to_string());
}
// --- Generate Id ---
if matches.occurrences_of("generate-id") != 0 {
// --- Generate DHT Key ---
if matches.occurrences_of("generate-dht-key") != 0 {
let (key, secret) = veilid_core::generate_secret();
println!("Public: {}\nSecret: {}", key.encode(), secret.encode());
return Ok(());

View File

@ -39,8 +39,13 @@ core:
allow_insecure_fallback: true
always_use_insecure_storage: false
insecure_fallback_directory: '%INSECURE_FALLBACK_DIRECTORY%'
delete: false
table_store:
directory: '%TABLE_STORE_DIRECTORY%'
delete: false
block_store:
directory: '%BLOCK_STORE_DIRECTORY%'
delete: false
network:
max_connections: 16
connection_initial_timeout: 2000000
@ -124,6 +129,10 @@ core:
"%TABLE_STORE_DIRECTORY%",
&Settings::get_default_table_store_path().to_string_lossy(),
)
.replace(
"%BLOCK_STORE_DIRECTORY%",
&Settings::get_default_block_store_path().to_string_lossy(),
)
.replace(
"%INSECURE_FALLBACK_DIRECTORY%",
&Settings::get_default_protected_store_insecure_fallback_directory().to_string_lossy(),
@ -532,6 +541,13 @@ pub struct Testing {
#[derive(Debug, Deserialize, Serialize)]
pub struct TableStore {
pub directory: PathBuf,
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct BlockStore {
pub directory: PathBuf,
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
@ -539,12 +555,14 @@ pub struct ProtectedStore {
pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool,
pub insecure_fallback_directory: PathBuf,
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Core {
pub protected_store: ProtectedStore,
pub table_store: TableStore,
pub block_store: BlockStore,
pub network: Network,
}
@ -693,6 +711,20 @@ impl Settings {
default_config_path
}
pub fn get_default_block_store_path() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.data_local_dir());
} else {
default_config_path = PathBuf::from("./");
}
default_config_path.push("block_store");
default_config_path
}
pub fn get_default_protected_store_insecure_fallback_directory() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
@ -740,6 +772,8 @@ impl Settings {
.to_string_lossy()
.to_string(),
)),
"protected_store.delete" => Ok(Box::new(inner.core.protected_store.delete)),
"table_store.directory" => Ok(Box::new(
inner
.core
@ -748,6 +782,18 @@ impl Settings {
.to_string_lossy()
.to_string(),
)),
"table_store.delete" => Ok(Box::new(inner.core.table_store.delete)),
"block_store.directory" => Ok(Box::new(
inner
.core
.block_store
.directory
.to_string_lossy()
.to_string(),
)),
"block_store.delete" => Ok(Box::new(inner.core.block_store.delete)),
"network.max_connections" => Ok(Box::new(inner.core.network.max_connections)),
"network.connection_initial_timeout" => {
Ok(Box::new(inner.core.network.connection_initial_timeout))
@ -1070,16 +1116,27 @@ mod tests {
assert_eq!(s.logging.client.enabled, true);
assert_eq!(s.logging.client.level, LogLevel::Info);
assert_eq!(s.testing.subnode_index, 0);
assert_eq!(
s.core.table_store.directory,
Settings::get_default_table_store_path()
);
assert_eq!(s.core.table_store.delete, false);
assert_eq!(
s.core.block_store.directory,
Settings::get_default_block_store_path()
);
assert_eq!(s.core.block_store.delete, false);
assert_eq!(s.core.protected_store.allow_insecure_fallback, true);
assert_eq!(s.core.protected_store.always_use_insecure_storage, false);
assert_eq!(
s.core.protected_store.insecure_fallback_directory,
Settings::get_default_protected_store_insecure_fallback_directory()
);
assert_eq!(s.core.protected_store.delete, false);
assert_eq!(s.core.network.max_connections, 16);
assert_eq!(s.core.network.connection_initial_timeout, 2_000_000u64);
assert_eq!(s.core.network.node_id, veilid_core::DHTKey::default());