Skip to content

Commit e7c6d19

Browse files
committed
Refactored the signer to support host and port config settings
1 parent 843b110 commit e7c6d19

File tree

8 files changed

+72
-23
lines changed

8 files changed

+72
-23
lines changed

crates/cli/src/docker_init.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use cb_common::{
1414
PBS_ENDPOINT_ENV, PBS_MODULE_NAME, PROXY_DIR_DEFAULT, PROXY_DIR_ENV,
1515
PROXY_DIR_KEYS_DEFAULT, PROXY_DIR_KEYS_ENV, PROXY_DIR_SECRETS_DEFAULT,
1616
PROXY_DIR_SECRETS_ENV, SIGNER_DEFAULT, SIGNER_DIR_KEYS_DEFAULT, SIGNER_DIR_KEYS_ENV,
17-
SIGNER_DIR_SECRETS_DEFAULT, SIGNER_DIR_SECRETS_ENV, SIGNER_JWT_SECRET_ENV, SIGNER_KEYS_ENV,
18-
SIGNER_MODULE_NAME, SIGNER_PORT_ENV, SIGNER_URL_ENV,
17+
SIGNER_DIR_SECRETS_DEFAULT, SIGNER_DIR_SECRETS_ENV, SIGNER_ENDPOINT_ENV,
18+
SIGNER_JWT_SECRET_ENV, SIGNER_KEYS_ENV, SIGNER_MODULE_NAME, SIGNER_URL_ENV,
1919
},
2020
pbs::{BUILDER_API_PATH, GET_STATUS_PATH},
21-
signer::{ProxyStore, SignerLoader},
21+
signer::{ProxyStore, SignerLoader, DEFAULT_SIGNER_PORT},
2222
types::ModuleId,
2323
utils::random_jwt_secret,
2424
};
@@ -73,7 +73,11 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
7373
let mut targets = Vec::new();
7474

7575
// address for signer API communication
76-
let signer_port = 20000;
76+
let signer_port = if let Some(signer_config) = &cb_config.signer {
77+
signer_config.port
78+
} else {
79+
DEFAULT_SIGNER_PORT
80+
};
7781
let signer_server =
7882
if let Some(SignerConfig { inner: SignerType::Remote { url }, .. }) = &cb_config.signer {
7983
url.to_string()
@@ -334,10 +338,17 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
334338
let mut signer_envs = IndexMap::from([
335339
get_env_val(CONFIG_ENV, CONFIG_DEFAULT),
336340
get_env_same(JWTS_ENV),
337-
get_env_uval(SIGNER_PORT_ENV, signer_port as u64),
338341
]);
339342

340-
let mut ports = vec![];
343+
// Bind the signer API to 0.0.0.0
344+
let container_endpoint =
345+
SocketAddr::from((Ipv4Addr::UNSPECIFIED, signer_config.port));
346+
let (key, val) = get_env_val(SIGNER_ENDPOINT_ENV, &container_endpoint.to_string());
347+
signer_envs.insert(key, val);
348+
349+
let host_endpoint = SocketAddr::from((signer_config.host, signer_config.port));
350+
let mut ports = vec![format!("{}:{}", host_endpoint, signer_config.port)];
351+
warnings.push(format!("cb_signer has an exported port on {}", signer_config.port));
341352

342353
if let Some((key, val)) = chain_spec_env.clone() {
343354
signer_envs.insert(key, val);
@@ -459,13 +470,20 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
459470
let mut signer_envs = IndexMap::from([
460471
get_env_val(CONFIG_ENV, CONFIG_DEFAULT),
461472
get_env_same(JWTS_ENV),
462-
get_env_uval(SIGNER_PORT_ENV, signer_port as u64),
463473
get_env_val(DIRK_CERT_ENV, DIRK_CERT_DEFAULT),
464474
get_env_val(DIRK_KEY_ENV, DIRK_KEY_DEFAULT),
465475
get_env_val(DIRK_DIR_SECRETS_ENV, DIRK_DIR_SECRETS_DEFAULT),
466476
]);
467477

468-
let mut ports = vec![];
478+
// Bind the signer API to 0.0.0.0
479+
let container_endpoint =
480+
SocketAddr::from((Ipv4Addr::UNSPECIFIED, signer_config.port));
481+
let (key, val) = get_env_val(SIGNER_ENDPOINT_ENV, &container_endpoint.to_string());
482+
signer_envs.insert(key, val);
483+
484+
let host_endpoint = SocketAddr::from((signer_config.host, signer_config.port));
485+
let mut ports = vec![format!("{}:{}", host_endpoint, signer_config.port)];
486+
warnings.push(format!("cb_signer has an exported port on {}", signer_config.port));
469487

470488
if let Some((key, val)) = chain_spec_env.clone() {
471489
signer_envs.insert(key, val);

crates/common/src/config/constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub const SIGNER_IMAGE_DEFAULT: &str = "ghcr.io/commit-boost/signer:latest";
3333
pub const SIGNER_MODULE_NAME: &str = "signer";
3434

3535
/// Where the signer module should open the server
36-
pub const SIGNER_PORT_ENV: &str = "CB_SIGNER_PORT";
36+
pub const SIGNER_ENDPOINT_ENV: &str = "CB_SIGNER_ENDPOINT";
3737

3838
/// Comma separated list module_id=jwt_secret
3939
pub const JWTS_ENV: &str = "CB_JWTS";

crates/common/src/config/signer.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
use std::{collections::HashMap, path::PathBuf};
1+
use std::{
2+
collections::HashMap,
3+
net::{Ipv4Addr, SocketAddr},
4+
path::PathBuf,
5+
};
26

37
use eyre::{bail, OptionExt, Result};
48
use serde::{Deserialize, Serialize};
59
use tonic::transport::{Certificate, Identity};
610
use url::Url;
711

812
use super::{
9-
constants::SIGNER_IMAGE_DEFAULT, load_jwt_secrets, utils::load_env_var, CommitBoostConfig,
10-
SIGNER_PORT_ENV,
13+
load_jwt_secrets, load_optional_env_var, utils::load_env_var, CommitBoostConfig,
14+
SIGNER_ENDPOINT_ENV, SIGNER_IMAGE_DEFAULT,
1115
};
1216
use crate::{
1317
config::{DIRK_CA_CERT_ENV, DIRK_CERT_ENV, DIRK_DIR_SECRETS_ENV, DIRK_KEY_ENV},
14-
signer::{ProxyStore, SignerLoader},
18+
signer::{ProxyStore, SignerLoader, DEFAULT_SIGNER_PORT},
1519
types::{Chain, ModuleId},
20+
utils::{default_host, default_u16},
1621
};
1722

1823
#[derive(Debug, Serialize, Deserialize, Clone)]
1924
#[serde(rename_all = "snake_case")]
2025
pub struct SignerConfig {
26+
/// Host address to listen for signer API calls on
27+
#[serde(default = "default_host")]
28+
pub host: Ipv4Addr,
29+
/// Port to listen for signer API calls on
30+
#[serde(default = "default_u16::<DEFAULT_SIGNER_PORT>")]
31+
pub port: u16,
2132
/// Docker image of the module
2233
#[serde(default = "default_signer")]
2334
pub docker_image: String,
@@ -87,7 +98,7 @@ pub struct StartSignerConfig {
8798
pub chain: Chain,
8899
pub loader: Option<SignerLoader>,
89100
pub store: Option<ProxyStore>,
90-
pub server_port: u16,
101+
pub endpoint: SocketAddr,
91102
pub jwts: HashMap<ModuleId, String>,
92103
pub dirk: Option<DirkConfig>,
93104
}
@@ -97,15 +108,25 @@ impl StartSignerConfig {
97108
let config = CommitBoostConfig::from_env_path()?;
98109

99110
let jwts = load_jwt_secrets()?;
100-
let server_port = load_env_var(SIGNER_PORT_ENV)?.parse()?;
111+
112+
// Load the server endpoint first from the env var, then the config, and finally
113+
// the defaults
114+
let endpoint = if let Some(endpoint) = load_optional_env_var(SIGNER_ENDPOINT_ENV) {
115+
endpoint.parse()?
116+
} else {
117+
match config.signer {
118+
Some(ref signer) => SocketAddr::from((signer.host, signer.port)),
119+
None => SocketAddr::from((default_host(), DEFAULT_SIGNER_PORT)),
120+
}
121+
};
101122

102123
let signer = config.signer.ok_or_eyre("Signer config is missing")?.inner;
103124

104125
match signer {
105126
SignerType::Local { loader, store, .. } => Ok(StartSignerConfig {
106127
chain: config.chain,
107128
loader: Some(loader),
108-
server_port,
129+
endpoint,
109130
jwts,
110131
store,
111132
dirk: None,
@@ -133,7 +154,7 @@ impl StartSignerConfig {
133154

134155
Ok(StartSignerConfig {
135156
chain: config.chain,
136-
server_port,
157+
endpoint,
137158
jwts,
138159
loader: None,
139160
store,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const DEFAULT_SIGNER_PORT: u16 = 20000;

crates/common/src/signer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
mod constants;
12
mod loader;
23
mod schemes;
34
mod store;
45
mod types;
56

7+
pub use constants::*;
68
pub use loader::*;
79
pub use schemes::*;
810
pub use store::*;

crates/signer/src/service.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, net::SocketAddr, sync::Arc};
1+
use std::{collections::HashMap, sync::Arc};
22

33
use axum::{
44
extract::{Request, State},
@@ -67,7 +67,7 @@ impl SigningService {
6767
let loaded_consensus = state.manager.read().await.available_consensus_signers();
6868
let loaded_proxies = state.manager.read().await.available_proxy_signers();
6969

70-
info!(version = COMMIT_BOOST_VERSION, commit_hash = COMMIT_BOOST_COMMIT, modules =? module_ids, port =? config.server_port, loaded_consensus, loaded_proxies, "Starting signing service");
70+
info!(version = COMMIT_BOOST_VERSION, commit_hash = COMMIT_BOOST_COMMIT, modules =? module_ids, endpoint =? config.endpoint, loaded_consensus, loaded_proxies, "Starting signing service");
7171

7272
SigningService::init_metrics(config.chain)?;
7373

@@ -81,8 +81,7 @@ impl SigningService {
8181
.route_layer(middleware::from_fn(log_request))
8282
.route(STATUS_PATH, get(handle_status));
8383

84-
let address = SocketAddr::from(([0, 0, 0, 0], config.server_port));
85-
let listener = TcpListener::bind(address).await?;
84+
let listener = TcpListener::bind(config.endpoint).await?;
8685

8786
axum::serve(listener, app).await.wrap_err("signer server exited")
8887
}

docs/docs/get_started/configuration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ We currently support Lighthouse, Prysm, Teku and Lodestar's keystores so it's ea
6565
#### Config:
6666
```toml
6767
[signer]
68+
port = 20000
69+
6870
[signer.local.loader]
6971
format = "lighthouse"
7072
keys_path = "keys"
@@ -111,6 +113,8 @@ We currently support Lighthouse, Prysm, Teku and Lodestar's keystores so it's ea
111113
#### Config:
112114
```toml
113115
[signer]
116+
port = 20000
117+
114118
[signer.local.loader]
115119
format = "teku"
116120
keys_path = "keys"
@@ -133,6 +137,8 @@ We currently support Lighthouse, Prysm, Teku and Lodestar's keystores so it's ea
133137
#### Config:
134138
```toml
135139
[signer]
140+
port = 20000
141+
136142
[signer.local.loader]
137143
format = "lodestar"
138144
keys_path = "keys"
@@ -299,6 +305,8 @@ port = 18550
299305
url = ""
300306

301307
[signer]
308+
port = 20000
309+
302310
[signer.loader]
303311
format = "lighthouse"
304312
keys_path = "/path/to/keys"

docs/docs/get_started/running/binary.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ Modules need some environment variables to work correctly.
2222

2323
### PBS Module
2424
- `CB_BUILDER_URLS`: optional, comma-separated list of urls to `events` modules where to post builder events.
25-
- `CB_PBS_ENDPOINT`: optional, override the endpoint where the PBS module will open the port for the beacon node.
25+
- `CB_PBS_ENDPOINT`: optional, override to specify the `IP:port` endpoint where the PBS module will open the port for the beacon node.
2626
- `CB_MUX_PATH_{ID}`: optional, override where to load mux validator keys for mux with `id=\{ID\}`.
2727

2828
### Signer Module
2929
- `CB_SIGNER_JWT_SECRET`: secret to use for JWT authentication with the Signer module.
30-
- `CB_SIGNER_PORT`: required, port to open the signer server on.
30+
- `CB_SIGNER_ENDPOINT`: optional, override to specify the `IP:port` endpoint to bind the signer server to.
3131
- For loading keys we currently support:
3232
- `CB_SIGNER_LOADER_FILE`: path to a `.json` with plaintext keys (for testing purposes only).
3333
- `CB_SIGNER_LOADER_FORMAT`, `CB_SIGNER_LOADER_KEYS_DIR` and `CB_SIGNER_LOADER_SECRETS_DIR`: paths to the `keys` and `secrets` directories or files (ERC-2335 style keystores, see [Signer config](../configuration/#signer-module) for more info).

0 commit comments

Comments
 (0)