Validate admin_keys before starting scabbard service
@@ -41,7 +41,6 @@ use splinter::error::{InternalError, InvalidStateError};
use splinter::service::validation::ServiceArgValidator;
use splinter::service::{FactoryCreateError, Service, ServiceFactory};
-#[cfg(feature = "service-arg-validation")]
use crate::hex::parse_hex;
use crate::hex::to_hex;
@@ -447,6 +446,22 @@ impl ServiceFactory for ScabbardFactory {
))
})?;
+ for key in admin_keys.iter() {
+ let key_bytes = parse_hex(key).map_err(|_| {
+ FactoryCreateError::InvalidArguments(format!(
+ "{:?} is not a valid hex-formatted public key",
+ key,
+ ))
+ })?;
+
+ if key_bytes.len() != 33 {
+ return Err(FactoryCreateError::InvalidArguments(format!(
+ "{} is not a valid public key: invalid length",
+ key
+ )));
+ }
+ }
+
let coordinator_timeout = args
.get("coordinator_timeout")
.map(|timeout| match timeout.parse::<u64>() {
@@ -683,7 +698,7 @@ mod tests {
}
/// Verify that the scabbard factory produces a valid `Scabbard` instance if the service
- /// arguments are commo seperated instead of json fmt.
+ /// arguments are comma seperated instead of json fmt.
#[test]
fn create_successful_no_json() {
let (_tempdir, factory) = get_factory();
@@ -746,6 +761,19 @@ mod tests {
);
}
+ /// Verify that `Scabbard` creation fails when the `admin_keys` argument isn't specified.
+ #[test]
+ fn create_with_invalid_admin_keys() {
+ let (_tempdir, factory) = get_factory();
+ let mut args = get_mock_args();
+ args.insert("admin_keys".to_string(), "bad_key".to_string());
+
+ assert!(
+ factory.create("".into(), "", "", args).is_err(),
+ "Creating factory with invalid admin keys did not fail"
+ );
+ }
+
/// Verify arg validation returns ok with valid common seperated Args
#[test]
#[cfg(feature = "service-arg-validation")]
