Skip to content

Commit 7ce2154

Browse files
authored
feat: Add an option to fetch the root key, for testing or local development. (#271)
* Add an option to fetch the root key, for testing or local development. Signed-off-by: John Plevyak <jplevyak@gmail.com>
1 parent 4bf6f1d commit 7ce2154

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

icx-proxy/src/main.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ pub(crate) struct Opts {
8484
#[clap(long)]
8585
debug: bool,
8686

87+
/// Whether or not to fetch the root key from the replica back end. Do not use this when
88+
/// talking to the Internet Computer blockchain mainnet as it is unsecure.
89+
#[clap(long)]
90+
fetch_root_key: bool,
91+
8792
/// A map of domain names to canister IDs.
8893
/// Format: domain.name:canister-id
8994
#[clap(long)]
@@ -419,13 +424,21 @@ fn not_found() -> Result<Response<Body>, Box<dyn Error>> {
419424
.body("Not found".into())?)
420425
}
421426

427+
fn unable_to_fetch_root_key() -> Result<Response<Body>, Box<dyn Error>> {
428+
Ok(Response::builder()
429+
.status(StatusCode::INTERNAL_SERVER_ERROR)
430+
.body("Unable to fetch root key".into())?)
431+
}
432+
433+
#[allow(clippy::too_many_arguments)]
422434
async fn handle_request(
423435
ip_addr: IpAddr,
424436
request: Request<Body>,
425437
replica_url: String,
426438
proxy_url: Option<String>,
427439
dns_canister_config: Arc<DnsCanisterConfig>,
428440
logger: slog::Logger,
441+
fetch_root_key: bool,
429442
debug: bool,
430443
) -> Result<Response<Body>, Infallible> {
431444
let request_uri_path = request.uri().path();
@@ -459,8 +472,11 @@ async fn handle_request(
459472
.build()
460473
.expect("Could not create agent..."),
461474
);
462-
463-
forward_request(request, agent, dns_canister_config.as_ref(), logger.clone()).await
475+
if fetch_root_key && agent.fetch_root_key().await.is_err() {
476+
unable_to_fetch_root_key()
477+
} else {
478+
forward_request(request, agent, dns_canister_config.as_ref(), logger.clone()).await
479+
}
464480
} {
465481
Err(err) => {
466482
slog::warn!(logger, "Internal Error during request:\n{:#?}", err);
@@ -491,6 +507,7 @@ fn main() -> Result<(), Box<dyn Error>> {
491507
let counter = AtomicUsize::new(0);
492508
let debug = opts.debug;
493509
let proxy_url = opts.proxy.clone();
510+
let fetch_root_key = opts.fetch_root_key;
494511

495512
let service = make_service_fn(|socket: &hyper::server::conn::AddrStream| {
496513
let ip_addr = socket.remote_addr();
@@ -520,6 +537,7 @@ fn main() -> Result<(), Box<dyn Error>> {
520537
proxy_url.clone(),
521538
dns_canister_config,
522539
logger,
540+
fetch_root_key,
523541
debug,
524542
)
525543
}))

0 commit comments

Comments
 (0)