Skip to content

Commit 5d9df40

Browse files
apollo_network: Sensitive secret_key (#10399)
1 parent 827442d commit 5d9df40

File tree

7 files changed

+35
-12
lines changed

7 files changed

+35
-12
lines changed

crates/apollo_config/src/converters.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ use serde::de::Error;
3333
use serde::{Deserialize, Deserializer, Serialize};
3434
use url::Url;
3535

36+
use crate::secrets::Sensitive;
37+
3638
/// Deserializes milliseconds to duration object.
3739
pub fn deserialize_milliseconds_to_duration<'de, D>(de: D) -> Result<Duration, D::Error>
3840
where
@@ -310,3 +312,14 @@ where
310312
}
311313
Ok(Some(output))
312314
}
315+
316+
/// Deserializes a sensitive `Vec<u8>` from hex string structure.
317+
pub fn deserialize_optional_sensitive_vec_u8<'de, D>(
318+
de: D,
319+
) -> Result<Option<Sensitive<Vec<u8>>>, D::Error>
320+
where
321+
D: Deserializer<'de>,
322+
{
323+
let optional_vec = deserialize_optional_vec_u8(de)?;
324+
Ok(optional_vec.map(Sensitive::new))
325+
}

crates/apollo_config/src/validators.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::Path;
44

55
use validator::{Validate, ValidationError, ValidationErrors, ValidationErrorsKind};
66

7+
use crate::secrets::Sensitive;
78
use crate::ConfigError;
89

910
/// Custom validation for ASCII string.
@@ -34,6 +35,13 @@ pub fn validate_vec_u256(vec: &[u8]) -> Result<(), ValidationError> {
3435
Ok(())
3536
}
3637

38+
/// Validates a sensitive `Vec<u8>` to ensure it's 32 bytes.
39+
pub fn validate_optional_sensitive_vec_u256(
40+
secret_key: &Sensitive<Vec<u8>>,
41+
) -> Result<(), ValidationError> {
42+
validate_vec_u256(secret_key.as_ref())
43+
}
44+
3745
/// Struct for parsing a validation error.
3846
#[derive(Debug)]
3947
pub struct ParsedValidationError {

crates/apollo_network/src/bin/broadcast_network_stress_test_node/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async fn main() {
186186

187187
let mut network_config = NetworkConfig {
188188
port: args.p2p_port,
189-
secret_key: Some(peer_private_key.to_vec()),
189+
secret_key: Some(peer_private_key.to_vec().into()),
190190
..Default::default()
191191
};
192192
if let Some(peer) = &args.bootstrap {

crates/apollo_network/src/bin/broadcast_network_stress_test_node/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl TestConfig {
8383
let _ = TestConfig {
8484
network_config: NetworkConfig {
8585
port: 10000,
86-
secret_key: Some(secret_key),
86+
secret_key: Some(secret_key.into()),
8787
..Default::default()
8888
},
8989
..Default::default()

crates/apollo_network/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::time::Duration;
2525

2626
use apollo_config::converters::{
2727
deserialize_comma_separated_str,
28-
deserialize_optional_vec_u8,
28+
deserialize_optional_sensitive_vec_u8,
2929
deserialize_seconds_to_duration,
3030
serialize_optional_comma_separated,
3131
serialize_optional_vec_u8,
@@ -36,7 +36,8 @@ use apollo_config::dumping::{
3636
ser_param,
3737
SerializeConfig,
3838
};
39-
use apollo_config::validators::validate_vec_u256;
39+
use apollo_config::secrets::Sensitive;
40+
use apollo_config::validators::validate_optional_sensitive_vec_u256;
4041
use apollo_config::{ParamPath, ParamPrivacyInput, SerializedParam};
4142
use discovery::DiscoveryConfig;
4243
use libp2p::swarm::dial_opts::DialOpts;
@@ -61,9 +62,9 @@ pub struct NetworkConfig {
6162
#[serde(deserialize_with = "deserialize_comma_separated_str")]
6263
#[validate(custom(function = "validate_bootstrap_peer_multiaddr_list"))]
6364
pub bootstrap_peer_multiaddr: Option<Vec<Multiaddr>>,
64-
#[validate(custom(function = "validate_vec_u256"))]
65-
#[serde(deserialize_with = "deserialize_optional_vec_u8")]
66-
pub secret_key: Option<Vec<u8>>,
65+
#[validate(custom(function = "validate_optional_sensitive_vec_u256"))]
66+
#[serde(deserialize_with = "deserialize_optional_sensitive_vec_u8")]
67+
pub secret_key: Option<Sensitive<Vec<u8>>>,
6768
pub advertised_multiaddr: Option<Multiaddr>,
6869
pub chain_id: ChainId,
6970
pub discovery_config: DiscoveryConfig,
@@ -140,7 +141,7 @@ impl SerializeConfig for NetworkConfig {
140141
));
141142
config.extend([ser_param(
142143
"secret_key",
143-
&serialize_optional_vec_u8(&self.secret_key),
144+
&serialize_optional_vec_u8(&self.secret_key.as_ref().map(|s| s.as_ref().clone())),
144145
"The secret key used for building the peer id. If it's an empty string a random one \
145146
will be used.",
146147
ParamPrivacyInput::Private,

crates/apollo_network/src/network_manager/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,10 @@ impl NetworkManager {
716716
debug!("Creating swarm with listen address: {listen_address:?}");
717717

718718
let key_pair = match secret_key {
719-
Some(secret_key) => {
720-
Keypair::ed25519_from_bytes(secret_key).expect("Error while parsing secret key")
721-
}
719+
Some(secret_key) => Keypair::ed25519_from_bytes(secret_key.as_ref().clone())
720+
.expect("Error while parsing secret key"), // TODO(victork): make sure we're
721+
// allowed to expose the secret key
722+
// here
722723
None => Keypair::generate_ed25519(),
723724
};
724725
let mut swarm = SwarmBuilder::with_existing_identity(key_pair)

crates/apollo_network/src/network_manager/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn create_connected_network_configs(ports: Vec<u16>) -> Vec<NetworkConfig> {
184184
.map(|(port, private_key)| NetworkConfig {
185185
port,
186186
bootstrap_peer_multiaddr: Some(nodes_addresses.clone()),
187-
secret_key: Some(private_key.to_vec()),
187+
secret_key: Some(private_key.to_vec().into()),
188188
..Default::default()
189189
})
190190
.collect()

0 commit comments

Comments
 (0)