Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Allow CORS requests in Secret Store API (#10584)
Browse files Browse the repository at this point in the history
* allow CORS requests for Secret Store API (#10582)

* secretstore CORS: fix error with unit tests

* secretstore CORS: removed debug log

* secretstore CORS: add missing response's header

* secretstore CORS: switched to jsonrpc-server-utils for CORS validation
  • Loading branch information
Antoine Detante authored and sorpaas committed Apr 20, 2019
1 parent c5fa7aa commit 4cc274e
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 59 deletions.
33 changes: 32 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions parity/cli/mod.rs
Expand Up @@ -623,6 +623,10 @@ usage! {
"--no-secretstore-auto-migrate",
"Do not run servers set change session automatically when servers set changes. This option has no effect when servers set is read from configuration file.",

ARG arg_secretstore_http_cors: (String) = "none", or |c: &Config| c.secretstore.as_ref()?.cors.as_ref().map(|vec| vec.join(",")),
"--secretstore-http-cors=[URL]",
"Specify CORS header for Secret Store HTTP API responses. Special options: \"all\", \"none\".",

ARG arg_secretstore_acl_contract: (Option<String>) = Some("registry".into()), or |c: &Config| c.secretstore.as_ref()?.acl_contract.clone(),
"--secretstore-acl-contract=[SOURCE]",
"Secret Store permissioning contract address source: none, registry (contract address is read from 'secretstore_acl_checker' entry in registry) or address.",
Expand Down Expand Up @@ -1328,6 +1332,7 @@ struct SecretStore {
http_interface: Option<String>,
http_port: Option<u16>,
path: Option<String>,
cors: Option<Vec<String>>
}

#[derive(Default, Debug, PartialEq, Deserialize)]
Expand Down Expand Up @@ -1854,6 +1859,7 @@ mod tests {
arg_secretstore_http_interface: "local".into(),
arg_secretstore_http_port: 8082u16,
arg_secretstore_path: "$HOME/.parity/secretstore".into(),
arg_secretstore_http_cors: "null".into(),

// IPFS
flag_ipfs_api: false,
Expand Down Expand Up @@ -2132,6 +2138,7 @@ mod tests {
http_interface: None,
http_port: Some(8082),
path: None,
cors: None,
}),
private_tx: None,
ipfs: Some(Ipfs {
Expand Down
1 change: 1 addition & 0 deletions parity/cli/tests/config.full.toml
Expand Up @@ -105,6 +105,7 @@ http_port = 8082
interface = "local"
port = 8083
path = "$HOME/.parity/secretstore"
cors = ["null"]

[ipfs]
enable = false
Expand Down
20 changes: 20 additions & 0 deletions parity/configuration.rs
Expand Up @@ -638,6 +638,7 @@ impl Configuration {
http_port: self.args.arg_ports_shift + self.args.arg_secretstore_http_port,
data_path: self.directories().secretstore,
admin_public: self.secretstore_admin_public()?,
cors: self.secretstore_cors()
})
}

Expand Down Expand Up @@ -1058,6 +1059,10 @@ impl Configuration {
self.interface(&self.args.arg_secretstore_http_interface)
}

fn secretstore_cors(&self) -> Option<Vec<String>> {
Self::cors(self.args.arg_secretstore_http_cors.as_ref())
}

fn secretstore_self_secret(&self) -> Result<Option<NodeSecretKey>, String> {
match self.args.arg_secretstore_secret {
Some(ref s) if s.len() == 64 => Ok(Some(NodeSecretKey::Plain(s.parse()
Expand Down Expand Up @@ -1969,4 +1974,19 @@ mod tests {
_ => panic!("Should be Cmd::Run"),
}
}

#[test]
fn should_parse_secretstore_cors() {
// given

// when
let conf0 = parse(&["parity"]);
let conf1 = parse(&["parity", "--secretstore-http-cors", "*"]);
let conf2 = parse(&["parity", "--secretstore-http-cors", "http://parity.io,http://something.io"]);

// then
assert_eq!(conf0.secretstore_cors(), Some(vec![]));
assert_eq!(conf1.secretstore_cors(), None);
assert_eq!(conf2.secretstore_cors(), Some(vec!["http://parity.io".into(),"http://something.io".into()]));
}
}
4 changes: 4 additions & 0 deletions parity/secretstore.rs
Expand Up @@ -84,6 +84,8 @@ pub struct Configuration {
pub data_path: String,
/// Administrator public key.
pub admin_public: Option<Public>,
// Allowed CORS domains
pub cors: Option<Vec<String>>,
}

/// Secret store dependencies
Expand Down Expand Up @@ -195,6 +197,7 @@ mod server {
admin_public: conf.admin_public,
auto_migrate_enabled: conf.auto_migrate_enabled,
},
cors: conf.cors
};

cconf.cluster_config.nodes.insert(self_secret.public().clone(), cconf.cluster_config.listener_address.clone());
Expand Down Expand Up @@ -234,6 +237,7 @@ impl Default for Configuration {
http_interface: "127.0.0.1".to_owned(),
http_port: 8082,
data_path: replace_home(&data_dir, "$BASE/secretstore"),
cors: Some(vec![]),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions secret-store/Cargo.toml
Expand Up @@ -36,6 +36,7 @@ tokio = "~0.1.11"
tokio-io = "0.1"
tokio-service = "0.1"
url = "1.0"
jsonrpc-server-utils = "11.0"

[dev-dependencies]
env_logger = "0.5"
Expand Down
3 changes: 2 additions & 1 deletion secret-store/src/lib.rs
Expand Up @@ -37,6 +37,7 @@ extern crate tokio;
extern crate tokio_io;
extern crate tokio_service;
extern crate url;
extern crate jsonrpc_server_utils;

#[macro_use]
extern crate ethabi_derive;
Expand Down Expand Up @@ -107,7 +108,7 @@ pub fn start(client: Arc<Client>, sync: Arc<SyncProvider>, miner: Arc<Miner>, se

// prepare HTTP listener
let http_listener = match config.listener_address {
Some(listener_address) => Some(listener::http_listener::KeyServerHttpListener::start(listener_address, Arc::downgrade(&key_server), executor)?),
Some(listener_address) => Some(listener::http_listener::KeyServerHttpListener::start(listener_address, config.cors, Arc::downgrade(&key_server), executor)?),
None => None,
};

Expand Down

0 comments on commit 4cc274e

Please sign in to comment.