Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Commit c3a6d99

Browse files
committed
feat(node): add possibility to specify the secret key via environment variable
1 parent d4e16b3 commit c3a6d99

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ hexdump -s32 -e '32/1 "%02x" "\n"' ./key
5858
### Secret key
5959

6060
Secret key is a hexadecimal string of size 32 bytes. It can be specified via
61-
`--secret-key` argument. Any random string will fit but note that only strong
62-
random generators should be used to generate a secret key. Here are some
63-
examples how you can do it in the terminal:
61+
`TOX_SECRET_KEY` environment variable. Any random string will fit but note that
62+
only strong random generators should be used to generate a secret key. Here are
63+
some examples how you can do it in the terminal:
6464

6565
```sh
6666
openssl rand -hex 32

src/cli_config.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::net::{SocketAddr, ToSocketAddrs};
22
use std::num::ParseIntError;
33
use std::str::FromStr;
44

5-
use clap::{App, AppSettings, Arg, ArgGroup};
5+
use clap::{App, AppSettings, Arg};
66
use hex::FromHex;
77
use itertools::Itertools;
88
use regex::Regex;
@@ -63,6 +63,10 @@ pub struct CliConfig {
6363
pub tcp_addrs: Vec<SocketAddr>,
6464
/// DHT SecretKey
6565
pub sk: Option<SecretKey>,
66+
/// True if the SecretKey was passed as an argument instead of environment
67+
/// variable. Necessary to print a warning since the logger backend is not
68+
/// initialized when we parse arguments.
69+
pub sk_passed_as_arg: bool,
6670
/// Path to the file where DHT keys are stored.
6771
pub keys_file: Option<String>,
6872
/// List of bootstrap nodes.
@@ -98,19 +102,24 @@ pub fn cli_parse() -> CliConfig {
98102
.takes_value(true)
99103
.use_delimiter(true)
100104
.required_unless("udp-address"))
101-
.group(ArgGroup::with_name("credentials")
102-
.args(&["secret-key", "keys-file"])
103-
.required(true))
104105
.arg(Arg::with_name("secret-key")
105106
.short("s")
106107
.long("secret-key")
107-
.help("DHT secret key")
108-
.takes_value(true))
108+
.help("DHT secret key. Note that you should not pass the key via \
109+
arguments due to security reasons. Use this argument for \
110+
test purposes only. In the real world use the environment \
111+
variable instead")
112+
.takes_value(true)
113+
.conflicts_with("keys-file")
114+
.env("TOX_SECRET_KEY")
115+
.hidden(true))
109116
.arg(Arg::with_name("keys-file")
110117
.short("k")
111118
.long("keys-file")
112119
.help("Path to the file where DHT keys are stored")
113-
.takes_value(true))
120+
.takes_value(true)
121+
.required_unless("secret-key")
122+
.conflicts_with("secret-key"))
114123
.arg(Arg::with_name("bootstrap-node")
115124
.short("b")
116125
.long("bootstrap-node")
@@ -174,6 +183,8 @@ pub fn cli_parse() -> CliConfig {
174183
SecretKey::from_slice(&sk_bytes).expect("Invalid DHT secret key")
175184
});
176185

186+
let sk_passed_as_arg = matches.occurrences_of("secret-key") > 0;
187+
177188
let keys_file = matches.value_of("keys-file").map(|s| s.to_owned());
178189

179190
let bootstrap_nodes = matches
@@ -208,6 +219,7 @@ pub fn cli_parse() -> CliConfig {
208219
udp_addr,
209220
tcp_addrs,
210221
sk,
222+
sk_passed_as_arg,
211223
keys_file,
212224
bootstrap_nodes,
213225
threads_config,

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ fn main() {
333333
} else {
334334
panic!("Neither secret key nor keys file is specified")
335335
};
336+
if cli_config.sk_passed_as_arg {
337+
warn!("You should not pass the secret key via arguments due to \
338+
security reasons. Use the environment variable instead");
339+
}
340+
336341
info!("DHT public key: {}", hex::encode(dht_pk.as_ref()).to_uppercase());
337342

338343
let (tcp_onion, udp_onion) = create_onion_streams();

0 commit comments

Comments
 (0)