Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: no plain-text private key in configuration file #1481

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions common/config-parser/src/types/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::{collections::HashMap, ffi::OsStr, io, net::SocketAddr, path::PathBuf};
use std::{
collections::HashMap,
ffi::OsStr,
fs::File,
io::{self, Read as _},
net::SocketAddr,
path::{Path, PathBuf},
};

use clap::builder::{StringValueParser, TypedValueParser, ValueParserFactory};
use serde::Deserialize;
use tentacle_multiaddr::MultiAddr;

use protocol::{
codec::deserialize_256bits_key,
types::{Key256Bits, H160},
};
use protocol::types::{Key256Bits, H160};

use crate::parse_file;

Expand All @@ -24,11 +28,13 @@ pub const DEFAULT_CACHE_SIZE: usize = 100;
pub struct Config {
// crypto
/// `net_privkey` is used for network connection.
#[serde(deserialize_with = "deserialize_256bits_key")]
pub net_privkey: Key256Bits,
#[serde(skip)]
pub net_privkey: Key256Bits,
pub net_privkey_file: PathBuf,
/// `bls_privkey` is used for signing consensus messages.
#[serde(deserialize_with = "deserialize_256bits_key")]
pub bls_privkey: Key256Bits,
#[serde(skip)]
pub bls_privkey: Key256Bits,
pub bls_privkey_file: PathBuf,

// db config
pub data_path: PathBuf,
Expand Down Expand Up @@ -124,9 +130,51 @@ impl TypedValueParser for ConfigValueParser {
);
clap::Error::raw(kind, msg)
})
.and_then(|mut config: Self::Value| {
let privkey_path = dir_path.join(&config.net_privkey_file);
config.net_privkey = load_privkey_from_file(&privkey_path)?;
Ok(config)
})
.and_then(|mut config: Self::Value| {
let privkey_path = dir_path.join(&config.bls_privkey_file);
config.bls_privkey = load_privkey_from_file(&privkey_path)?;
Ok(config)
})
}
}

fn load_privkey_from_file(privkey_path: &Path) -> Result<Key256Bits, clap::Error> {
File::open(privkey_path)
.and_then(|mut f| {
let mut buffer = Vec::new();
f.read_to_end(&mut buffer).map(|_| buffer)
})
.map_err(|err| {
let kind = clap::error::ErrorKind::InvalidValue;
let msg = format!(
"failed to parse private key file {} since {err}",
privkey_path.display()
);
clap::Error::raw(kind, msg)
})
.and_then(|bytes| {
const LEN: usize = 32;
if bytes.len() == LEN {
let mut v = [0u8; 32];
v.copy_from_slice(&bytes);
Ok(Key256Bits::from(v))
} else {
let kind = clap::error::ErrorKind::InvalidValue;
let msg = format!(
"failed to parse private key file {} since its length is {} but expect {LEN}.",
privkey_path.display(),
bytes.len()
);
Err(clap::Error::raw(kind, msg))
}
})
}

#[derive(Clone, Debug, Deserialize)]
pub struct ConfigApi {
pub http_listening_address: Option<SocketAddr>,
Expand Down
Binary file added devtools/chain/bls.key
Binary file not shown.
5 changes: 3 additions & 2 deletions devtools/chain/config.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# crypto
# file paths to private keys.
# net_privkey is used for network, bls_privkey is use for sign consensus messages
# DO NOT USE this private key in any production environment!
net_privkey = "0x37aa0f893d05914a4def0460c0a984d3611546cfb26924d7a7ca6e0db9950a2d"
bls_privkey = "0x4179b05f5ad5bdd46ca98a9e8b435b00a504562dfe02687895edf747ddf5de18"
net_privkey_file = "net.key"
bls_privkey_file = "bls.key"

# db config
data_path = "./devtools/chain/data"
Expand Down
4 changes: 2 additions & 2 deletions devtools/chain/k8s/node_1.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# crypto
net_privkey = "0x37aa0f893d05914a4def0460c0a984d3611546cfb26924d7a7ca6e0db9950a2d"
bls_privkey = "0x4179b05f5ad5bdd46ca98a9e8b435b00a504562dfe02687895edf747ddf5de18"
net_privkey_file = "node_1_net.key"
bls_privkey_file = "node_1_bls.key"

# db config
data_path = "./devtools/chain/data1"
Expand Down
Binary file added devtools/chain/k8s/node_1_bls.key
Binary file not shown.
2 changes: 2 additions & 0 deletions devtools/chain/k8s/node_1_net.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
7ª‰=‘JMï`À©„ÓaFϲi$קÊn¹•
-
Expand Down
4 changes: 2 additions & 2 deletions devtools/chain/k8s/node_2.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# crypto
net_privkey = "0x383fcff8683b8115e31613949be24254b4204ffbe43c227408a76334a2e3fb32"
bls_privkey = "0x422951d5ac7ddbe86cae7d2d4c82af713785b3177043ac6feb50eda7e360b860"
net_privkey_file = "node_2_net.key"
bls_privkey_file = "node_2_bls.key"

# db config
data_path = "./devtools/chain/data2"
Expand Down
1 change: 1 addition & 0 deletions devtools/chain/k8s/node_2_bls.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
B)QÕ¬}Ûèl®}-L‚¯q7…³pC¬oëPí§ã`¸`
1 change: 1 addition & 0 deletions devtools/chain/k8s/node_2_net.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8?Ïøh;ã”›âBT´ Oûä<"t§c4¢ãû2
4 changes: 2 additions & 2 deletions devtools/chain/k8s/node_3.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# crypto
net_privkey = "0x51ce21643b911347c5d5c85c323d9d5421810dc89f46b688720b2715f5e8e936"
bls_privkey = "0x51a04542786ca3bae046d1c7451b6a0745efdcc66c39ede37827172f964d5fdf"
net_privkey_file = "node_3_net.key"
bls_privkey_file = "node_3_bls.key"

# db config
data_path = "./devtools/chain/data3"
Expand Down
1 change: 1 addition & 0 deletions devtools/chain/k8s/node_3_bls.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Q EBxl£ºàFÑÇEjEïÜÆl9íãx'/–M_ß
1 change: 1 addition & 0 deletions devtools/chain/k8s/node_3_net.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QÎ!d;‘GÅÕÈ\2=T!ÈŸF¶ˆr 'õèé6
Expand Down
4 changes: 2 additions & 2 deletions devtools/chain/k8s/node_4.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# crypto
net_privkey = "0x69ff51f4c22f30615f68b88efa740f8f1b9169e88842b83d189748d06f1a948e"
bls_privkey = "0x67fc8772fdcff8140564e9c4ed693fffd0929c68f24529ee2fb2adfbe9c453fe"
net_privkey_file = "node_4_net.key"
bls_privkey_file = "node_4_bls.key"

# db config
data_path = "./devtools/chain/data4"
Expand Down
1 change: 1 addition & 0 deletions devtools/chain/k8s/node_4_bls.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gü‡rýÏødéÄíi?ÿÐ’œhòE)î/²­ûéÄSþ
1 change: 1 addition & 0 deletions devtools/chain/k8s/node_4_net.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iÿQôÂ/0a_h¸Žút‘ièˆB¸=—HÐo”Ž
70 changes: 0 additions & 70 deletions devtools/chain/k8s/sync_nodes/node_5.toml

This file was deleted.

70 changes: 0 additions & 70 deletions devtools/chain/k8s/sync_nodes/node_6.toml

This file was deleted.

70 changes: 0 additions & 70 deletions devtools/chain/k8s/sync_nodes/node_7.toml

This file was deleted.

Loading
Loading