From 162b979394893566f72997b361679ec7aed8f63f Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 27 Jan 2019 10:33:42 +0100 Subject: [PATCH] RpcEnvironment: add environment type enum RpcEnvironmentType --- src/api/router.rs | 12 ++++++++++++ src/cli/environment.rs | 5 +++++ src/server/environment.rs | 12 ++++++++++-- src/server/rest.rs | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/api/router.rs b/src/api/router.rs index ef973b60..281aec22 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -16,6 +16,18 @@ pub trait RpcEnvironment { fn set_result_attrib(&mut self, name: &str, value: Value); fn get_result_attrib(&self, name: &str) -> Option<&Value>; + + fn env_type(&self) -> RpcEnvironmentType; +} + +#[derive(Copy, Clone)] +pub enum RpcEnvironmentType { + /// command started from command line + CLI, + /// access from public acessable server + PUBLIC, + /// ... access from priviledged server (run as root) + PRIVILEDGED, } type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result; diff --git a/src/cli/environment.rs b/src/cli/environment.rs index 3ff85508..3227f870 100644 --- a/src/cli/environment.rs +++ b/src/cli/environment.rs @@ -22,4 +22,9 @@ impl RpcEnvironment for CliEnvironment { fn get_result_attrib(&self, name: &str) -> Option<&Value> { self.result_attributes.get(name) } + + fn env_type(&self) -> RpcEnvironmentType { + RpcEnvironmentType::CLI + } + } diff --git a/src/server/environment.rs b/src/server/environment.rs index 18346be4..7345cc8d 100644 --- a/src/server/environment.rs +++ b/src/server/environment.rs @@ -4,12 +4,16 @@ use std::collections::HashMap; use serde_json::Value; pub struct RestEnvironment { + env_type: RpcEnvironmentType, result_attributes: HashMap, } impl RestEnvironment { - pub fn new() -> Self { - Self { result_attributes: HashMap::new() } + pub fn new(env_type: RpcEnvironmentType) -> Self { + Self { + result_attributes: HashMap::new(), + env_type, + } } } @@ -22,4 +26,8 @@ impl RpcEnvironment for RestEnvironment { fn get_result_attrib(&self, name: &str) -> Option<&Value> { self.result_attributes.get(name) } + + fn env_type(&self) -> RpcEnvironmentType { + self.env_type + } } diff --git a/src/server/rest.rs b/src/server/rest.rs index 8dc80d85..eb938c53 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -397,7 +397,7 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { println!("REQUEST {} {}", method, path); println!("COMPO {:?}", components); - let mut rpcenv = RestEnvironment::new(); + let rpcenv = RestEnvironment::new(RpcEnvironmentType::PRIVILEDGED); if comp_len >= 1 && components[0] == "api2" { println!("GOT API REQUEST");