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-08-07 18:54:05 +02:00
|
|
|
use std::sync::{Arc, Weak};
|
2017-04-03 17:46:51 +02:00
|
|
|
use futures::{future, Future};
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use ethkey::public_to_address;
|
2017-08-07 18:54:05 +02:00
|
|
|
use ethcore::client::{Client, BlockChainClient, BlockId, ChainNotify};
|
2017-04-03 17:46:51 +02:00
|
|
|
use native_contracts::SecretStoreAclStorage;
|
2017-08-07 18:54:05 +02:00
|
|
|
use util::{H256, Address, Bytes};
|
2017-07-06 14:02:10 +02:00
|
|
|
use types::all::{Error, ServerKeyId, Public};
|
2017-02-20 16:13:21 +01:00
|
|
|
|
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-08-07 18:54:05 +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-08-07 18:54:05 +02:00
|
|
|
client: Weak<Client>,
|
|
|
|
/// Contract address.
|
|
|
|
contract_addr: Option<Address>,
|
|
|
|
/// Contract at given address.
|
|
|
|
contract: Option<SecretStoreAclStorage>,
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 17:46:51 +02:00
|
|
|
impl OnChainAclStorage {
|
2017-08-07 18:54:05 +02:00
|
|
|
pub fn new(client: &Arc<Client>) -> Arc<Self> {
|
|
|
|
let acl_storage = Arc::new(OnChainAclStorage {
|
|
|
|
contract: Mutex::new(CachedContract::new(client)),
|
|
|
|
});
|
|
|
|
client.add_notify(acl_storage.clone());
|
|
|
|
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-08-07 18:54:05 +02:00
|
|
|
self.contract.lock().check(public, document)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChainNotify for OnChainAclStorage {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CachedContract {
|
|
|
|
pub fn new(client: &Arc<Client>) -> Self {
|
|
|
|
CachedContract {
|
|
|
|
client: Arc::downgrade(client),
|
|
|
|
contract_addr: None,
|
|
|
|
contract: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(&mut self) {
|
|
|
|
if let Some(client) = self.client.upgrade() {
|
|
|
|
let new_contract_addr = client.registry_address(ACL_CHECKER_CONTRACT_REGISTRY_NAME.to_owned());
|
|
|
|
if self.contract_addr.as_ref() != new_contract_addr.as_ref() {
|
|
|
|
self.contract = new_contract_addr.map(|contract_addr| {
|
2017-04-03 17:46:51 +02:00
|
|
|
trace!(target: "secretstore", "Configuring for ACL checker contract from {}", contract_addr);
|
|
|
|
|
2017-08-07 18:54:05 +02:00
|
|
|
SecretStoreAclStorage::new(contract_addr)
|
|
|
|
});
|
|
|
|
|
|
|
|
self.contract_addr = new_contract_addr;
|
|
|
|
}
|
2017-04-03 17:46:51 +02:00
|
|
|
}
|
2017-08-07 18:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check(&mut self, public: &Public, document: &ServerKeyId) -> Result<bool, Error> {
|
|
|
|
match self.contract.as_ref() {
|
|
|
|
Some(contract) => {
|
|
|
|
let address = public_to_address(&public);
|
|
|
|
let do_call = |a, d| future::done(
|
|
|
|
self.client
|
|
|
|
.upgrade()
|
|
|
|
.ok_or("Calling contract without client".into())
|
|
|
|
.and_then(|c| c.call_contract(BlockId::Latest, a, d)));
|
|
|
|
contract.check_permissions(do_call, address, document.clone())
|
|
|
|
.map_err(|err| Error::Internal(err))
|
|
|
|
.wait()
|
|
|
|
},
|
|
|
|
None => Err(Error::Internal("ACL checker contract is not configured".to_owned())),
|
2017-04-03 17:46:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use parking_lot::RwLock;
|
2017-07-06 14:02:10 +02:00
|
|
|
use types::all::{Error, ServerKeyId, Public};
|
2017-04-03 17:46:51 +02:00
|
|
|
use super::AclStorage;
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
/// Dummy ACL storage implementation
|
|
|
|
pub struct DummyAclStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
prohibited: RwLock<HashMap<Public, HashSet<ServerKeyId>>>,
|
2017-04-03 17:46:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DummyAclStorage {
|
|
|
|
#[cfg(test)]
|
|
|
|
/// Prohibit given requestor access to given document
|
2017-07-06 14:02:10 +02:00
|
|
|
pub fn prohibit(&self, public: Public, document: ServerKeyId) {
|
2017-04-03 17:46:51 +02:00
|
|
|
self.prohibited.write()
|
|
|
|
.entry(public)
|
|
|
|
.or_insert_with(Default::default)
|
|
|
|
.insert(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AclStorage for DummyAclStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
fn check(&self, public: &Public, document: &ServerKeyId) -> Result<bool, Error> {
|
2017-04-03 17:46:51 +02:00
|
|
|
Ok(self.prohibited.read()
|
|
|
|
.get(public)
|
|
|
|
.map(|docs| !docs.contains(document))
|
|
|
|
.unwrap_or(true))
|
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|