2017-02-20 16:13:21 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-12-20 14:02:21 +01:00
|
|
|
use std::sync::Arc;
|
2017-07-27 14:48:07 +02:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use parking_lot::{Mutex, RwLock};
|
2017-04-03 17:46:51 +02:00
|
|
|
use ethkey::public_to_address;
|
2017-12-20 14:02:21 +01:00
|
|
|
use ethcore::client::{BlockChainClient, BlockId, ChainNotify};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{H256, Address};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2017-12-20 14:02:21 +01:00
|
|
|
use trusted_client::TrustedClient;
|
2017-07-06 14:02:10 +02:00
|
|
|
use types::all::{Error, ServerKeyId, Public};
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2018-02-09 09:32:06 +01:00
|
|
|
use_contract!(acl_storage, "AclStorage", "res/acl_storage.json");
|
|
|
|
|
2017-04-03 17:46:51 +02:00
|
|
|
const ACL_CHECKER_CONTRACT_REGISTRY_NAME: &'static str = "secretstore_acl_checker";
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
/// ACL storage of Secret Store
|
|
|
|
pub trait AclStorage: Send + Sync {
|
|
|
|
/// Check if requestor with `public` key can access document with hash `document`
|
2017-07-06 14:02:10 +02:00
|
|
|
fn check(&self, public: &Public, document: &ServerKeyId) -> Result<bool, Error>;
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 17:46:51 +02:00
|
|
|
/// On-chain ACL storage implementation.
|
|
|
|
pub struct OnChainAclStorage {
|
2017-06-30 10:26:09 +02:00
|
|
|
/// Cached on-chain contract.
|
|
|
|
contract: Mutex<CachedContract>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cached on-chain ACL storage contract.
|
|
|
|
struct CachedContract {
|
2017-04-03 17:46:51 +02:00
|
|
|
/// Blockchain client.
|
2017-12-20 14:02:21 +01:00
|
|
|
client: TrustedClient,
|
2017-06-30 10:26:09 +02:00
|
|
|
/// Contract address.
|
|
|
|
contract_addr: Option<Address>,
|
|
|
|
/// Contract at given address.
|
2018-02-09 09:32:06 +01:00
|
|
|
contract: acl_storage::AclStorage,
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-27 14:48:07 +02:00
|
|
|
/// Dummy ACL storage implementation (check always passed).
|
2017-08-16 08:40:00 +02:00
|
|
|
#[derive(Default, Debug)]
|
2017-07-27 14:48:07 +02:00
|
|
|
pub struct DummyAclStorage {
|
|
|
|
prohibited: RwLock<HashMap<Public, HashSet<ServerKeyId>>>,
|
|
|
|
}
|
|
|
|
|
2017-04-03 17:46:51 +02:00
|
|
|
impl OnChainAclStorage {
|
2017-12-20 14:02:21 +01:00
|
|
|
pub fn new(trusted_client: TrustedClient) -> Result<Arc<Self>, Error> {
|
|
|
|
let client = trusted_client.get_untrusted();
|
2017-07-19 10:35:17 +02:00
|
|
|
let acl_storage = Arc::new(OnChainAclStorage {
|
2017-12-20 14:02:21 +01:00
|
|
|
contract: Mutex::new(CachedContract::new(trusted_client)),
|
2017-07-19 10:35:17 +02:00
|
|
|
});
|
2017-12-20 14:02:21 +01:00
|
|
|
client
|
|
|
|
.ok_or(Error::Internal("Constructing OnChainAclStorage without active Client".into()))?
|
|
|
|
.add_notify(acl_storage.clone());
|
|
|
|
Ok(acl_storage)
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 17:46:51 +02:00
|
|
|
impl AclStorage for OnChainAclStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
fn check(&self, public: &Public, document: &ServerKeyId) -> Result<bool, Error> {
|
2017-06-30 10:26:09 +02:00
|
|
|
self.contract.lock().check(public, document)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:35:17 +02:00
|
|
|
impl ChainNotify for OnChainAclStorage {
|
2017-07-20 12:28:31 +02:00
|
|
|
fn new_blocks(&self, _imported: Vec<H256>, _invalid: Vec<H256>, enacted: Vec<H256>, retracted: Vec<H256>, _sealed: Vec<H256>, _proposed: Vec<Bytes>, _duration: u64) {
|
|
|
|
if !enacted.is_empty() || !retracted.is_empty() {
|
|
|
|
self.contract.lock().update()
|
|
|
|
}
|
2017-07-19 10:35:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 10:26:09 +02:00
|
|
|
impl CachedContract {
|
2017-12-20 14:02:21 +01:00
|
|
|
pub fn new(client: TrustedClient) -> Self {
|
2017-06-30 10:26:09 +02:00
|
|
|
CachedContract {
|
2018-02-09 09:32:06 +01:00
|
|
|
client,
|
2017-06-30 10:26:09 +02:00
|
|
|
contract_addr: None,
|
2018-02-09 09:32:06 +01:00
|
|
|
contract: acl_storage::AclStorage::default(),
|
2017-06-30 10:26:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:35:17 +02:00
|
|
|
pub fn update(&mut self) {
|
2017-12-20 14:02:21 +01:00
|
|
|
if let Some(client) = self.client.get() {
|
2018-02-12 18:02:48 +01:00
|
|
|
match client.registry_address(ACL_CHECKER_CONTRACT_REGISTRY_NAME.to_owned(), BlockId::Latest) {
|
2018-02-09 09:32:06 +01:00
|
|
|
Some(new_contract_addr) if Some(new_contract_addr).as_ref() != self.contract_addr.as_ref() => {
|
|
|
|
trace!(target: "secretstore", "Configuring for ACL checker contract from {}", new_contract_addr);
|
|
|
|
self.contract_addr = Some(new_contract_addr);
|
|
|
|
},
|
|
|
|
Some(_) | None => ()
|
2017-06-30 10:26:09 +02:00
|
|
|
}
|
2017-04-03 17:46:51 +02:00
|
|
|
}
|
2017-07-19 10:35:17 +02:00
|
|
|
}
|
2017-06-30 10:26:09 +02:00
|
|
|
|
2017-07-19 10:35:17 +02:00
|
|
|
pub fn check(&mut self, public: &Public, document: &ServerKeyId) -> Result<bool, Error> {
|
2017-12-20 14:02:21 +01:00
|
|
|
if let Some(client) = self.client.get() {
|
|
|
|
// call contract to check accesss
|
2018-02-09 09:32:06 +01:00
|
|
|
match self.contract_addr {
|
|
|
|
Some(contract_address) => {
|
2017-12-20 14:02:21 +01:00
|
|
|
let address = public_to_address(&public);
|
2018-02-09 09:32:06 +01:00
|
|
|
let do_call = |data| client.call_contract(BlockId::Latest, contract_address, data);
|
|
|
|
self.contract.functions()
|
|
|
|
.check_permissions()
|
|
|
|
.call(address, document.clone(), &do_call)
|
|
|
|
.map_err(|e| Error::Internal(e.to_string()))
|
2017-12-20 14:02:21 +01:00
|
|
|
},
|
|
|
|
None => Err(Error::Internal("ACL checker contract is not configured".to_owned())),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::Internal("Calling ACL contract without trusted blockchain client".into()))
|
2017-04-03 17:46:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 14:48:07 +02:00
|
|
|
impl DummyAclStorage {
|
2017-08-16 08:40:00 +02:00
|
|
|
/// Prohibit given requestor access to given documents
|
2017-07-27 14:48:07 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn prohibit(&self, public: Public, document: ServerKeyId) {
|
|
|
|
self.prohibited.write()
|
|
|
|
.entry(public)
|
|
|
|
.or_insert_with(Default::default)
|
|
|
|
.insert(document);
|
2017-04-03 17:46:51 +02:00
|
|
|
}
|
2017-07-27 14:48:07 +02:00
|
|
|
}
|
2017-04-03 17:46:51 +02:00
|
|
|
|
2017-07-27 14:48:07 +02:00
|
|
|
impl AclStorage for DummyAclStorage {
|
|
|
|
fn check(&self, public: &Public, document: &ServerKeyId) -> Result<bool, Error> {
|
|
|
|
Ok(self.prohibited.read()
|
|
|
|
.get(public)
|
|
|
|
.map(|docs| !docs.contains(document))
|
|
|
|
.unwrap_or(true))
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|