From acb4dc9bc4bdc1d51e2e28b0d51e71777ad9fd0e Mon Sep 17 00:00:00 2001 From: Charlie Lye Date: Sat, 16 Dec 2023 11:24:41 +0000 Subject: [PATCH] fix: Use lookup instead of resolve to ensure consider /etc/hosts (#3720) Please provide a paragraph or two giving a summary of the change, including relevant motivation and context. # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). --- aztec-up/bin/.aztec-run | 14 ++++++++------ aztec-up/bin/aztec-cli | 2 -- aztec-up/bin/aztec-sandbox | 4 ++-- aztec-up/bin/docker-compose.yml | 15 +++++++++------ yarn-project/aztec-sandbox/src/bin/index.ts | 11 +++++------ yarn-project/cli/src/index.ts | 11 +++++------ 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/aztec-up/bin/.aztec-run b/aztec-up/bin/.aztec-run index 03ed5167ced..9dfe07c41f8 100755 --- a/aztec-up/bin/.aztec-run +++ b/aztec-up/bin/.aztec-run @@ -44,9 +44,9 @@ if [[ -z "${SKIP_NET:-}" && "$UNAME" == "Linux" ]]; then # We're in rootless docker. Probe for the host ip and use that. ip=$(hostname -I | head | tr -d ' ') warn "WARNING: Running within rootless docker. Using $ip as host ip. Ensure listening services are listening on this interface." - DOCKER_HOST_BINDS="$DOCKER_HOST_BINDS --add-host host.docker.internal:$ip" + DOCKER_HOST_BINDS="--add-host host.docker.internal:$ip" else - DOCKER_HOST_BINDS="$DOCKER_HOST_BINDS --add-host host.docker.internal:host-gateway" + DOCKER_HOST_BINDS="--add-host host.docker.internal:host-gateway" fi fi @@ -70,10 +70,12 @@ done DOCKER_ENV="-e HOME=$HOME" for env in ${ENV_VARS_TO_INJECT:-}; do - # First substitute any reference to localhost with our host gateway. - env=${env//localhost/host.docker.internal} - # Inject into container. - DOCKER_ENV+="-e $env:${!env:-} " + if [ -n "${!env:-}" ]; then + # First substitute any reference to localhost with our host gateway. + env=${env//localhost/host.docker.internal} + # Inject into container. + DOCKER_ENV+=" -e $env=${!env:-}" + fi done # Volumes to pass to the container. diff --git a/aztec-up/bin/aztec-cli b/aztec-up/bin/aztec-cli index 7d8b75f4146..6bdbb0473ba 100755 --- a/aztec-up/bin/aztec-cli +++ b/aztec-up/bin/aztec-cli @@ -1,6 +1,4 @@ #!/usr/bin/env bash -# TODO: Make compile command always be wasm. Or put nargo in container. Or probe. -# TODO: Make unbox fail if trying to unbox outside of the cwd. set -euo pipefail export ENV_VARS_TO_INJECT="PXE_URL PRIVATE_KEY DEBUG" diff --git a/aztec-up/bin/aztec-sandbox b/aztec-up/bin/aztec-sandbox index 1558bc49cb2..57d6f2e0f8a 100755 --- a/aztec-up/bin/aztec-sandbox +++ b/aztec-up/bin/aztec-sandbox @@ -5,7 +5,7 @@ set -euo pipefail CMD="docker compose" $CMD &>/dev/null || CMD="docker-compose" -ARGS="-f $HOME/.aztec/bin/docker-compose.yml -p sandbox" +ARGS="-f ./bin/docker-compose.yml -p sandbox" # Function to be executed when SIGINT is received. cleanup() { @@ -16,6 +16,6 @@ cleanup() { trap cleanup SIGINT # Change working dir, so relative volume mounts are in the right place. -cd ~/.aztec +cd $(dirname $0)/.. $CMD $ARGS up --force-recreate --remove-orphans \ No newline at end of file diff --git a/aztec-up/bin/docker-compose.yml b/aztec-up/bin/docker-compose.yml index b861d729d16..a1d1e3e646c 100644 --- a/aztec-up/bin/docker-compose.yml +++ b/aztec-up/bin/docker-compose.yml @@ -6,24 +6,25 @@ services: ' [ -n "$$FORK_URL" ] && ARGS="$$ARGS --fork-url $$FORK_URL"; [ -n "$$FORK_BLOCK_NUMBER" ] && ARGS="$$ARGS --fork-block-number $$FORK_BLOCK_NUMBER"; - echo anvil -p 8545 --host 0.0.0.0 --chain-id 31337 --silent $$ARGS; - anvil -p 8545 --host 0.0.0.0 --chain-id 31337 --silent $$ARGS + echo anvil -p $$ANVIL_PORT --host 0.0.0.0 --chain-id 31337 --silent $$ARGS; + anvil -p $$ANVIL_PORT --host 0.0.0.0 --chain-id 31337 --silent $$ARGS ' ports: - - "${SANDBOX_ANVIL_PORT:-8545}:8545" + - "${ANVIL_PORT:-8545}:${ANVIL_PORT:-8545}" environment: FORK_URL: FORK_BLOCK_NUMBER: + ANVIL_PORT: ${ANVIL_PORT:-8545} aztec: image: "aztecprotocol/aztec-sandbox" ports: - - "${SANDBOX_AZTEC_NODE_PORT:-8079}:8079" - - "${SANDBOX_PXE_PORT:-8080}:8080" + - "${AZTEC_NODE_PORT:-8079}:${AZTEC_NODE_PORT:-8079}" + - "${PXE_PORT:-8080}:${PXE_PORT:-8080}" environment: DEBUG: # Loaded from the user shell if explicitly set HOST_WORKDIR: "${PWD}" # Loaded from the user shell to show log files absolute path in host - ETHEREUM_HOST: http://ethereum:8545 + ETHEREUM_HOST: http://ethereum:${ANVIL_PORT:-8545} CHAIN_ID: 31337 ARCHIVER_POLLING_INTERVAL_MS: 50 P2P_BLOCK_CHECK_INTERVAL_MS: 50 @@ -31,5 +32,7 @@ services: WS_BLOCK_CHECK_INTERVAL_MS: 50 PXE_BLOCK_POLLING_INTERVAL_MS: 50 ARCHIVER_VIEM_POLLING_INTERVAL_MS: 500 + AZTEC_NODE_PORT: ${AZTEC_NODE_PORT:-8079} + PXE_PORT: ${PXE_PORT:-8080} volumes: - ./log:/usr/src/yarn-project/aztec-sandbox/log:rw diff --git a/yarn-project/aztec-sandbox/src/bin/index.ts b/yarn-project/aztec-sandbox/src/bin/index.ts index 95ce182ff8a..6e4de4005a5 100644 --- a/yarn-project/aztec-sandbox/src/bin/index.ts +++ b/yarn-project/aztec-sandbox/src/bin/index.ts @@ -10,7 +10,7 @@ import { NoirCommit } from '@aztec/noir-compiler/versions'; import { BootstrapNode, getP2PConfigEnvVars } from '@aztec/p2p'; import { GrumpkinScalar, PXEService, createPXERpcServer } from '@aztec/pxe'; -import { resolve as dnsResolve } from 'dns'; +import { lookup } from 'dns/promises'; import { readFileSync } from 'fs'; import http from 'http'; import { dirname, resolve } from 'path'; @@ -35,11 +35,10 @@ enum SandboxMode { * If we can successfully resolve 'host.docker.internal', then we are running in a container, and we should treat * localhost as being host.docker.internal. */ -function getLocalhost() { - return new Promise(resolve => - dnsResolve('host.docker.internal', err => (err ? resolve('localhost') : resolve('host.docker.internal'))), - ); -} +const getLocalhost = () => + lookup('host.docker.internal') + .then(() => 'host.docker.internal') + .catch(() => 'localhost'); const LOCALHOST = await getLocalhost(); const { diff --git a/yarn-project/cli/src/index.ts b/yarn-project/cli/src/index.ts index 83d5ebd3f96..35f80bec477 100644 --- a/yarn-project/cli/src/index.ts +++ b/yarn-project/cli/src/index.ts @@ -3,7 +3,7 @@ import { fileURLToPath } from '@aztec/foundation/url'; import { addNoirCompilerCommanderActions } from '@aztec/noir-compiler/cli'; import { Command, Option } from 'commander'; -import { resolve as dnsResolve } from 'dns'; +import { lookup } from 'dns/promises'; import { readFileSync } from 'fs'; import { dirname, resolve } from 'path'; @@ -27,11 +27,10 @@ import { * If we can successfully resolve 'host.docker.internal', then we are running in a container, and we should treat * localhost as being host.docker.internal. */ -function getLocalhost() { - return new Promise(resolve => - dnsResolve('host.docker.internal', err => (err ? resolve('localhost') : resolve('host.docker.internal'))), - ); -} +const getLocalhost = () => + lookup('host.docker.internal') + .then(() => 'host.docker.internal') + .catch(() => 'localhost'); const LOCALHOST = await getLocalhost(); const { ETHEREUM_HOST = `http://${LOCALHOST}:8545`, PRIVATE_KEY, API_KEY } = process.env;