Whitelist APIs for generic Pub-Sub (#5840)

* Whitelisting generic pub-sub APIs.

* Clone enums instead of allocating a vec.
This commit is contained in:
Tomasz Drwięga 2017-07-10 17:42:29 +02:00 committed by Gav Wood
parent 62210fb932
commit 56c275b0cb
2 changed files with 23 additions and 12 deletions

View File

@ -198,7 +198,7 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
rpc::WsDispatcher::new(full_handler), rpc::WsDispatcher::new(full_handler),
Middleware::new(deps.stats.clone(), deps.apis.activity_notifier()) Middleware::new(deps.stats.clone(), deps.apis.activity_notifier())
)); ));
let apis = conf.apis.list_apis().into_iter().collect::<Vec<_>>(); let apis = conf.apis.list_apis();
deps.apis.extend_with_set(&mut handler, &apis); deps.apis.extend_with_set(&mut handler, &apis);
handler handler
@ -324,7 +324,7 @@ fn setup_apis<D>(apis: ApiSet, deps: &Dependencies<D>) -> MetaIoHandler<Metadata
let mut handler = MetaIoHandler::with_middleware( let mut handler = MetaIoHandler::with_middleware(
Middleware::new(deps.stats.clone(), deps.apis.activity_notifier()) Middleware::new(deps.stats.clone(), deps.apis.activity_notifier())
); );
let apis = apis.list_apis().into_iter().collect::<Vec<_>>(); let apis = apis.list_apis();
deps.apis.extend_with_set(&mut handler, &apis); deps.apis.extend_with_set(&mut handler, &apis);
handler handler

View File

@ -106,6 +106,8 @@ pub enum ApiSet {
All, All,
// Local "unsafe" context and accounts access // Local "unsafe" context and accounts access
IpcContext, IpcContext,
// APIs for Parity Generic Pub-Sub
PubSub,
// Fixed list of APis // Fixed list of APis
List(HashSet<Api>), List(HashSet<Api>),
} }
@ -153,7 +155,7 @@ impl FromStr for ApiSet {
} }
} }
fn to_modules(apis: &[Api]) -> BTreeMap<String, String> { fn to_modules(apis: &HashSet<Api>) -> BTreeMap<String, String> {
let mut modules = BTreeMap::new(); let mut modules = BTreeMap::new();
for api in apis { for api in apis {
let (name, version) = match *api { let (name, version) = match *api {
@ -187,7 +189,7 @@ pub trait Dependencies {
fn extend_with_set<S>( fn extend_with_set<S>(
&self, &self,
handler: &mut MetaIoHandler<Metadata, S>, handler: &mut MetaIoHandler<Metadata, S>,
apis: &[Api], apis: &HashSet<Api>,
) where S: core::Middleware<Metadata>; ) where S: core::Middleware<Metadata>;
} }
@ -217,7 +219,7 @@ impl FullDependencies {
fn extend_api<S>( fn extend_api<S>(
&self, &self,
handler: &mut MetaIoHandler<Metadata, S>, handler: &mut MetaIoHandler<Metadata, S>,
apis: &[Api], apis: &HashSet<Api>,
for_generic_pubsub: bool, for_generic_pubsub: bool,
) where S: core::Middleware<Metadata> { ) where S: core::Middleware<Metadata> {
use parity_rpc::v1::*; use parity_rpc::v1::*;
@ -305,7 +307,8 @@ impl FullDependencies {
Api::ParityPubSub => { Api::ParityPubSub => {
if !for_generic_pubsub { if !for_generic_pubsub {
let mut rpc = MetaIoHandler::default(); let mut rpc = MetaIoHandler::default();
self.extend_api(&mut rpc, apis, true); let apis = ApiSet::List(apis.clone()).retain(ApiSet::PubSub).list_apis();
self.extend_api(&mut rpc, &apis, true);
handler.extend_with(PubSubClient::new(rpc, self.remote.clone()).to_delegate()); handler.extend_with(PubSubClient::new(rpc, self.remote.clone()).to_delegate());
} }
}, },
@ -349,7 +352,7 @@ impl Dependencies for FullDependencies {
fn extend_with_set<S>( fn extend_with_set<S>(
&self, &self,
handler: &mut MetaIoHandler<Metadata, S>, handler: &mut MetaIoHandler<Metadata, S>,
apis: &[Api], apis: &HashSet<Api>,
) where S: core::Middleware<Metadata> { ) where S: core::Middleware<Metadata> {
self.extend_api(handler, apis, false) self.extend_api(handler, apis, false)
} }
@ -386,7 +389,7 @@ impl LightDependencies {
fn extend_api<T: core::Middleware<Metadata>>( fn extend_api<T: core::Middleware<Metadata>>(
&self, &self,
handler: &mut MetaIoHandler<Metadata, T>, handler: &mut MetaIoHandler<Metadata, T>,
apis: &[Api], apis: &HashSet<Api>,
for_generic_pubsub: bool, for_generic_pubsub: bool,
) { ) {
use parity_rpc::v1::*; use parity_rpc::v1::*;
@ -486,7 +489,8 @@ impl LightDependencies {
Api::ParityPubSub => { Api::ParityPubSub => {
if !for_generic_pubsub { if !for_generic_pubsub {
let mut rpc = MetaIoHandler::default(); let mut rpc = MetaIoHandler::default();
self.extend_api(&mut rpc, apis, true); let apis = ApiSet::List(apis.clone()).retain(ApiSet::PubSub).list_apis();
self.extend_api(&mut rpc, &apis, true);
handler.extend_with(PubSubClient::new(rpc, self.remote.clone()).to_delegate()); handler.extend_with(PubSubClient::new(rpc, self.remote.clone()).to_delegate());
} }
}, },
@ -525,7 +529,7 @@ impl Dependencies for LightDependencies {
fn extend_with_set<S>( fn extend_with_set<S>(
&self, &self,
handler: &mut MetaIoHandler<Metadata, S>, handler: &mut MetaIoHandler<Metadata, S>,
apis: &[Api], apis: &HashSet<Api>,
) where S: core::Middleware<Metadata> { ) where S: core::Middleware<Metadata> {
self.extend_api(handler, apis, false) self.extend_api(handler, apis, false)
} }
@ -538,9 +542,9 @@ impl ApiSet {
} }
pub fn list_apis(&self) -> HashSet<Api> { pub fn list_apis(&self) -> HashSet<Api> {
let mut public_list = vec![ let mut public_list = [
Api::Web3, Api::Net, Api::Eth, Api::EthPubSub, Api::Parity, Api::Rpc, Api::SecretStore, Api::Web3, Api::Net, Api::Eth, Api::EthPubSub, Api::Parity, Api::Rpc, Api::SecretStore,
].into_iter().collect(); ].into_iter().cloned().collect();
match *self { match *self {
ApiSet::List(ref apis) => apis.clone(), ApiSet::List(ref apis) => apis.clone(),
ApiSet::PublicContext => public_list, ApiSet::PublicContext => public_list,
@ -572,6 +576,13 @@ impl ApiSet {
public_list.insert(Api::Personal); public_list.insert(Api::Personal);
public_list public_list
}, },
ApiSet::PubSub => [
Api::Eth,
Api::Parity,
Api::ParityAccounts,
Api::ParitySet,
Api::Traces,
].into_iter().cloned().collect()
} }
} }
} }