Skip to content

Commit aa9a882

Browse files
committed
require http and metrics for respective flags (#4674)
## Issue Addressed following discussion on #4639 (comment) this PR makes the `http` and `metrics` sub-flags to require those main flags enabled
1 parent dfa39c3 commit aa9a882

File tree

4 files changed

+90
-64
lines changed

4 files changed

+90
-64
lines changed

beacon_node/src/cli.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{App, Arg};
1+
use clap::{App, Arg, ArgGroup};
22
use strum::VariantNames;
33
use types::ProgressiveBalancesMode;
44

@@ -355,22 +355,25 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
355355
.arg(
356356
Arg::with_name("http-address")
357357
.long("http-address")
358+
.requires("enable_http")
358359
.value_name("ADDRESS")
359360
.help("Set the listen address for the RESTful HTTP API server.")
360-
.default_value("127.0.0.1")
361+
.default_value_if("enable_http", None, "127.0.0.1")
361362
.takes_value(true),
362363
)
363364
.arg(
364365
Arg::with_name("http-port")
365366
.long("http-port")
367+
.requires("enable_http")
366368
.value_name("PORT")
367369
.help("Set the listen TCP port for the RESTful HTTP API server.")
368-
.default_value("5052")
370+
.default_value_if("enable_http", None, "5052")
369371
.takes_value(true),
370372
)
371373
.arg(
372374
Arg::with_name("http-allow-origin")
373375
.long("http-allow-origin")
376+
.requires("enable_http")
374377
.value_name("ORIGIN")
375378
.help("Set the value of the Access-Control-Allow-Origin response HTTP header. \
376379
Use * to allow any origin (not recommended in production). \
@@ -381,11 +384,13 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
381384
.arg(
382385
Arg::with_name("http-disable-legacy-spec")
383386
.long("http-disable-legacy-spec")
387+
.requires("enable_http")
384388
.hidden(true)
385389
)
386390
.arg(
387391
Arg::with_name("http-spec-fork")
388392
.long("http-spec-fork")
393+
.requires("enable_http")
389394
.value_name("FORK")
390395
.help("Serve the spec for a specific hard fork on /eth/v1/config/spec. It should \
391396
not be necessary to set this flag.")
@@ -403,52 +408,58 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
403408
.arg(
404409
Arg::with_name("http-tls-cert")
405410
.long("http-tls-cert")
411+
.requires("enable_http")
406412
.help("The path of the certificate to be used when serving the HTTP API server \
407413
over TLS.")
408414
.takes_value(true)
409415
)
410416
.arg(
411417
Arg::with_name("http-tls-key")
412418
.long("http-tls-key")
419+
.requires("enable_http")
413420
.help("The path of the private key to be used when serving the HTTP API server \
414421
over TLS. Must not be password-protected.")
415422
.takes_value(true)
416423
)
417424
.arg(
418425
Arg::with_name("http-allow-sync-stalled")
419426
.long("http-allow-sync-stalled")
427+
.requires("enable_http")
420428
.help("Forces the HTTP to indicate that the node is synced when sync is actually \
421429
stalled. This is useful for very small testnets. TESTING ONLY. DO NOT USE ON \
422430
MAINNET.")
423431
)
424432
.arg(
425433
Arg::with_name("http-sse-capacity-multiplier")
426434
.long("http-sse-capacity-multiplier")
435+
.requires("enable_http")
427436
.takes_value(true)
428-
.default_value("1")
437+
.default_value_if("enable_http", None, "1")
429438
.value_name("N")
430439
.help("Multiplier to apply to the length of HTTP server-sent-event (SSE) channels. \
431440
Increasing this value can prevent messages from being dropped.")
432441
)
433442
.arg(
434443
Arg::with_name("http-duplicate-block-status")
435444
.long("http-duplicate-block-status")
445+
.requires("enable_http")
436446
.takes_value(true)
437-
.default_value("202")
447+
.default_value_if("enable_http", None, "202")
438448
.value_name("STATUS_CODE")
439449
.help("Status code to send when a block that is already known is POSTed to the \
440450
HTTP API.")
441451
)
442452
.arg(
443453
Arg::with_name("http-enable-beacon-processor")
444454
.long("http-enable-beacon-processor")
455+
.requires("enable_http")
445456
.value_name("BOOLEAN")
446457
.help("The beacon processor is a scheduler which provides quality-of-service and \
447458
DoS protection. When set to \"true\", HTTP API requests will be queued and scheduled \
448459
alongside other tasks. When set to \"false\", HTTP API responses will be executed \
449460
immediately.")
450461
.takes_value(true)
451-
.default_value("true")
462+
.default_value_if("enable_http", None, "true")
452463
)
453464
/* Prometheus metrics HTTP server related arguments */
454465
.arg(
@@ -461,22 +472,25 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
461472
Arg::with_name("metrics-address")
462473
.long("metrics-address")
463474
.value_name("ADDRESS")
475+
.requires("metrics")
464476
.help("Set the listen address for the Prometheus metrics HTTP server.")
465-
.default_value("127.0.0.1")
477+
.default_value_if("metrics", None, "127.0.0.1")
466478
.takes_value(true),
467479
)
468480
.arg(
469481
Arg::with_name("metrics-port")
470482
.long("metrics-port")
483+
.requires("metrics")
471484
.value_name("PORT")
472485
.help("Set the listen TCP port for the Prometheus metrics HTTP server.")
473-
.default_value("5054")
486+
.default_value_if("metrics", None, "5054")
474487
.takes_value(true),
475488
)
476489
.arg(
477490
Arg::with_name("metrics-allow-origin")
478491
.long("metrics-allow-origin")
479492
.value_name("ORIGIN")
493+
.requires("metrics")
480494
.help("Set the value of the Access-Control-Allow-Origin response HTTP header. \
481495
Use * to allow any origin (not recommended in production). \
482496
If no value is supplied, the CORS allowed origin is set to the listen \
@@ -1259,4 +1273,5 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
12591273
.default_value("64")
12601274
.takes_value(true)
12611275
)
1276+
.group(ArgGroup::with_name("enable_http").args(&["http", "gui", "staking"]))
12621277
}

beacon_node/src/config.rs

+51-51
Original file line numberDiff line numberDiff line change
@@ -94,69 +94,69 @@ pub fn get_config<E: EthSpec>(
9494
* Http API server
9595
*/
9696

97-
if cli_args.is_present("http") {
97+
if cli_args.is_present("enable_http") {
9898
client_config.http_api.enabled = true;
99-
}
10099

101-
if let Some(address) = cli_args.value_of("http-address") {
102-
client_config.http_api.listen_addr = address
103-
.parse::<IpAddr>()
104-
.map_err(|_| "http-address is not a valid IP address.")?;
105-
}
100+
if let Some(address) = cli_args.value_of("http-address") {
101+
client_config.http_api.listen_addr = address
102+
.parse::<IpAddr>()
103+
.map_err(|_| "http-address is not a valid IP address.")?;
104+
}
106105

107-
if let Some(port) = cli_args.value_of("http-port") {
108-
client_config.http_api.listen_port = port
109-
.parse::<u16>()
110-
.map_err(|_| "http-port is not a valid u16.")?;
111-
}
106+
if let Some(port) = cli_args.value_of("http-port") {
107+
client_config.http_api.listen_port = port
108+
.parse::<u16>()
109+
.map_err(|_| "http-port is not a valid u16.")?;
110+
}
112111

113-
if let Some(allow_origin) = cli_args.value_of("http-allow-origin") {
114-
// Pre-validate the config value to give feedback to the user on node startup, instead of
115-
// as late as when the first API response is produced.
116-
hyper::header::HeaderValue::from_str(allow_origin)
117-
.map_err(|_| "Invalid allow-origin value")?;
112+
if let Some(allow_origin) = cli_args.value_of("http-allow-origin") {
113+
// Pre-validate the config value to give feedback to the user on node startup, instead of
114+
// as late as when the first API response is produced.
115+
hyper::header::HeaderValue::from_str(allow_origin)
116+
.map_err(|_| "Invalid allow-origin value")?;
118117

119-
client_config.http_api.allow_origin = Some(allow_origin.to_string());
120-
}
118+
client_config.http_api.allow_origin = Some(allow_origin.to_string());
119+
}
121120

122-
if cli_args.is_present("http-disable-legacy-spec") {
123-
warn!(
124-
log,
125-
"The flag --http-disable-legacy-spec is deprecated and will be removed"
126-
);
127-
}
121+
if cli_args.is_present("http-disable-legacy-spec") {
122+
warn!(
123+
log,
124+
"The flag --http-disable-legacy-spec is deprecated and will be removed"
125+
);
126+
}
128127

129-
if let Some(fork_name) = clap_utils::parse_optional(cli_args, "http-spec-fork")? {
130-
client_config.http_api.spec_fork_name = Some(fork_name);
131-
}
128+
if let Some(fork_name) = clap_utils::parse_optional(cli_args, "http-spec-fork")? {
129+
client_config.http_api.spec_fork_name = Some(fork_name);
130+
}
132131

133-
if cli_args.is_present("http-enable-tls") {
134-
client_config.http_api.tls_config = Some(TlsConfig {
135-
cert: cli_args
136-
.value_of("http-tls-cert")
137-
.ok_or("--http-tls-cert was not provided.")?
138-
.parse::<PathBuf>()
139-
.map_err(|_| "http-tls-cert is not a valid path name.")?,
140-
key: cli_args
141-
.value_of("http-tls-key")
142-
.ok_or("--http-tls-key was not provided.")?
143-
.parse::<PathBuf>()
144-
.map_err(|_| "http-tls-key is not a valid path name.")?,
145-
});
146-
}
132+
if cli_args.is_present("http-enable-tls") {
133+
client_config.http_api.tls_config = Some(TlsConfig {
134+
cert: cli_args
135+
.value_of("http-tls-cert")
136+
.ok_or("--http-tls-cert was not provided.")?
137+
.parse::<PathBuf>()
138+
.map_err(|_| "http-tls-cert is not a valid path name.")?,
139+
key: cli_args
140+
.value_of("http-tls-key")
141+
.ok_or("--http-tls-key was not provided.")?
142+
.parse::<PathBuf>()
143+
.map_err(|_| "http-tls-key is not a valid path name.")?,
144+
});
145+
}
147146

148-
if cli_args.is_present("http-allow-sync-stalled") {
149-
client_config.http_api.allow_sync_stalled = true;
150-
}
147+
if cli_args.is_present("http-allow-sync-stalled") {
148+
client_config.http_api.allow_sync_stalled = true;
149+
}
151150

152-
client_config.http_api.sse_capacity_multiplier =
153-
parse_required(cli_args, "http-sse-capacity-multiplier")?;
151+
client_config.http_api.sse_capacity_multiplier =
152+
parse_required(cli_args, "http-sse-capacity-multiplier")?;
154153

155-
client_config.http_api.enable_beacon_processor =
156-
parse_required(cli_args, "http-enable-beacon-processor")?;
154+
client_config.http_api.enable_beacon_processor =
155+
parse_required(cli_args, "http-enable-beacon-processor")?;
157156

158-
client_config.http_api.duplicate_block_status_code =
159-
parse_required(cli_args, "http-duplicate-block-status")?;
157+
client_config.http_api.duplicate_block_status_code =
158+
parse_required(cli_args, "http-duplicate-block-status")?;
159+
}
160160

161161
if let Some(cache_size) = clap_utils::parse_optional(cli_args, "shuffling-cache-size")? {
162162
client_config.chain.shuffling_cache_size = cache_size;

lighthouse/tests/beacon_node.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@ fn http_flag() {
14661466
fn http_address_flag() {
14671467
let addr = "127.0.0.99".parse::<IpAddr>().unwrap();
14681468
CommandLineTest::new()
1469+
.flag("http", None)
14691470
.flag("http-address", Some("127.0.0.99"))
14701471
.run_with_zero_port()
14711472
.with_config(|config| assert_eq!(config.http_api.listen_addr, addr));
@@ -1474,6 +1475,7 @@ fn http_address_flag() {
14741475
fn http_address_ipv6_flag() {
14751476
let addr = "::1".parse::<IpAddr>().unwrap();
14761477
CommandLineTest::new()
1478+
.flag("http", None)
14771479
.flag("http-address", Some("::1"))
14781480
.run_with_zero_port()
14791481
.with_config(|config| assert_eq!(config.http_api.listen_addr, addr));
@@ -1483,6 +1485,7 @@ fn http_port_flag() {
14831485
let port1 = unused_tcp4_port().expect("Unable to find unused port.");
14841486
let port2 = unused_tcp4_port().expect("Unable to find unused port.");
14851487
CommandLineTest::new()
1488+
.flag("http", None)
14861489
.flag("http-port", Some(port1.to_string().as_str()))
14871490
.flag("port", Some(port2.to_string().as_str()))
14881491
.run()
@@ -2446,6 +2449,7 @@ fn http_sse_capacity_multiplier_default() {
24462449
#[test]
24472450
fn http_sse_capacity_multiplier_override() {
24482451
CommandLineTest::new()
2452+
.flag("http", None)
24492453
.flag("http-sse-capacity-multiplier", Some("10"))
24502454
.run_with_zero_port()
24512455
.with_config(|config| assert_eq!(config.http_api.sse_capacity_multiplier, 10));
@@ -2463,6 +2467,7 @@ fn http_duplicate_block_status_default() {
24632467
#[test]
24642468
fn http_duplicate_block_status_override() {
24652469
CommandLineTest::new()
2470+
.flag("http", None)
24662471
.flag("http-duplicate-block-status", Some("301"))
24672472
.run_with_zero_port()
24682473
.with_config(|config| {

validator_client/src/cli.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
170170
.arg(
171171
Arg::with_name("http-address")
172172
.long("http-address")
173+
.requires("http")
173174
.value_name("ADDRESS")
174175
.help("Set the address for the HTTP address. The HTTP server is not encrypted \
175176
and therefore it is unsafe to publish on a public network. When this \
@@ -189,14 +190,16 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
189190
.arg(
190191
Arg::with_name("http-port")
191192
.long("http-port")
193+
.requires("http")
192194
.value_name("PORT")
193195
.help("Set the listen TCP port for the RESTful HTTP API server.")
194-
.default_value("5062")
196+
.default_value_if("http", None, "5062")
195197
.takes_value(true),
196198
)
197199
.arg(
198200
Arg::with_name("http-allow-origin")
199201
.long("http-allow-origin")
202+
.requires("http")
200203
.value_name("ORIGIN")
201204
.help("Set the value of the Access-Control-Allow-Origin response HTTP header. \
202205
Use * to allow any origin (not recommended in production). \
@@ -207,21 +210,21 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
207210
.arg(
208211
Arg::with_name("http-allow-keystore-export")
209212
.long("http-allow-keystore-export")
213+
.requires("http")
210214
.help("If present, allow access to the DELETE /lighthouse/keystores HTTP \
211215
API method, which allows exporting keystores and passwords to HTTP API \
212216
consumers who have access to the API token. This method is useful for \
213217
exporting validators, however it should be used with caution since it \
214218
exposes private key data to authorized users.")
215-
.required(false)
216219
.takes_value(false),
217220
)
218221
.arg(
219222
Arg::with_name("http-store-passwords-in-secrets-dir")
220223
.long("http-store-passwords-in-secrets-dir")
224+
.requires("http")
221225
.help("If present, any validators created via the HTTP will have keystore \
222226
passwords stored in the secrets-dir rather than the validator \
223227
definitions file.")
224-
.required(false)
225228
.takes_value(false),
226229
)
227230
/* Prometheus metrics HTTP server related arguments */
@@ -234,22 +237,25 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
234237
.arg(
235238
Arg::with_name("metrics-address")
236239
.long("metrics-address")
240+
.requires("metrics")
237241
.value_name("ADDRESS")
238242
.help("Set the listen address for the Prometheus metrics HTTP server.")
239-
.default_value("127.0.0.1")
243+
.default_value_if("metrics", None, "127.0.0.1")
240244
.takes_value(true),
241245
)
242246
.arg(
243247
Arg::with_name("metrics-port")
244248
.long("metrics-port")
249+
.requires("metrics")
245250
.value_name("PORT")
246251
.help("Set the listen TCP port for the Prometheus metrics HTTP server.")
247-
.default_value("5064")
252+
.default_value_if("metrics", None, "5064")
248253
.takes_value(true),
249254
)
250255
.arg(
251256
Arg::with_name("metrics-allow-origin")
252257
.long("metrics-allow-origin")
258+
.requires("metrics")
253259
.value_name("ORIGIN")
254260
.help("Set the value of the Access-Control-Allow-Origin response HTTP header. \
255261
Use * to allow any origin (not recommended in production). \

0 commit comments

Comments
 (0)