Skip to content

Commit

Permalink
Automate ngrok url mapping for controller, agent services
Browse files Browse the repository at this point in the history
Signed-off-by: Emiliano Suñé <[email protected]>
  • Loading branch information
esune committed Feb 7, 2023
1 parent 0c4fa64 commit 768c884
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ proxy-data/

# General
.env
*ngrok.json

# Visual Studio Code
.vscode
Expand Down
25 changes: 25 additions & 0 deletions docker/docker-compose-ngrok.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3"
services:
controller-ngrok:
image: wernight/ngrok
environment:
- CONTROLLER_SERVICE_PORT=5000
ports:
- 4056:4040
command: ngrok http controller:5000 --log stdout
networks:
- vc_auth

aca-py-ngrok:
image: wernight/ngrok
environment:
- AGENT_HTTP_PORT=${AGENT_HTTP_PORT}
ports:
- 4059:4040
command: ngrok http aca-py:${AGENT_HTTP_PORT} --log stdout
networks:
- vc_auth

networks:
vc_auth:
driver: bridge
23 changes: 0 additions & 23 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ services:
depends_on:
controller-db:
condition: service_started
controller-ngrok:
condition: service_started
environment:
- DB_HOST=${MONGODB_HOST}
- DB_PORT=${MONGODB_PORT}
Expand Down Expand Up @@ -77,26 +75,6 @@ services:
networks:
- vc_auth

controller-ngrok:
image: wernight/ngrok
environment:
- CONTROLLER_SERVICE_PORT=5000
ports:
- 4056:4040
command: ngrok http controller:5000 --log stdout
networks:
- vc_auth

aca-py-ngrok:
image: wernight/ngrok
environment:
- AGENT_HTTP_PORT=${AGENT_HTTP_PORT}
ports:
- 4059:4040
command: ngrok http aca-py:${AGENT_HTTP_PORT} --log stdout
networks:
- vc_auth

aca-py:
image: bcgovimages/aries-cloudagent:py36-1.16-1_1.0.0-rc1
environment:
Expand Down Expand Up @@ -124,7 +102,6 @@ services:
- vc_auth
depends_on:
- wallet-db
- aca-py-ngrok
entrypoint: /bin/bash
command: [
"-c",
Expand Down
48 changes: 44 additions & 4 deletions docker/manage
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ function echoWarning (){
echo -e "${_yellow}${_msg}${_nc}"
}

function echoSuccess (){
_msg=${1}
_green='\e[32m'
_nc='\e[0m' # No Color
echo -e "${_green}${_msg}${_nc}"
}

function echoInfo (){
_msg=${1}
_gray='\e[36m'
_nc='\e[0m' # No Color
echo -e "${_gray}${_msg}${_nc}"
}

function generateKey(){
(
_length=${1:-48}
Expand Down Expand Up @@ -83,8 +97,8 @@ EOF
# Default Settings:
# -----------------------------------------------------------------------------------------------------------------
DEFAULT_CONTAINERS="keycloak keycloak-db controller-db"
ACAPY_CONTAINERS="aca-py wallet-db aca-py-ngrok"
PROD_CONTAINERS="controller controller-ngrok"
ACAPY_CONTAINERS="aca-py wallet-db"
PROD_CONTAINERS="controller"

# -----------------------------------------------------------------------------------------------------------------
# Functions:
Expand Down Expand Up @@ -160,7 +174,7 @@ configureEnvironment() {
export AGENT_HOST="http://aca-py"
export AGENT_NGROK_TUNNEL="http://aca-py-ngrok:4040"
export AGENT_NAME="VC-AuthN Agent"
export AGENT_HTTP_PORT="8030"
export AGENT_HTTP_PORT=${AGENT_HTTP_PORT:-8030}
export AGENT_ADMIN_PORT=${AGENT_ADMIN_PORT:-"8077"}
export AGENT_ADMIN_URL=${AGENT_ADMIN_URL:-http://$AGENT_HOST:$AGENT_ADMIN_PORT}
export AGENT_ENDPOINT=${AGENT_ENDPOINT:-http://$AGENT_HOST:$AGENT_HTTP_PORT}
Expand Down Expand Up @@ -271,11 +285,34 @@ initializeUserPrompts() {
;;
esac
done

read -p "Do you want to use ngrok for your agent and controller [y/n]? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "AGENT_HTTP_PORT=8030" >> .env
# start ngrok containers first so we can grab the URLs
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-vc-authn}" docker compose -f docker-compose-ngrok.yaml up -d --force-recreate

echoInfo "Determining ngrok url for controller service..."
docker run --rm curlimages/curl -L -s http://host.docker.internal:4056/api/tunnels > controller-ngrok.json
NGROK_CONTROLLER_URL=$(docker run --rm -i stedolan/jq < controller-ngrok.json --raw-output '.tunnels | map(select(.name | contains("command_line"))) | .[0] | .public_url')
echo "CONTROLLER_URL=${NGROK_CONTROLLER_URL}" >> .env
echoSuccess "The controller url is: ${NGROK_CONTROLLER_URL}"

echoInfo "Determining ngrok url for agent service..."
docker run --rm curlimages/curl -L -s http://host.docker.internal:4059/api/tunnels > agent-ngrok.json
NGROK_AGENT_URL=$(docker run --rm -i stedolan/jq < agent-ngrok.json --raw-output '.tunnels | map(select(.name | contains("command_line"))) | .[0] | .public_url')
echo "AGENT_ENDPOINT=${NGROK_AGENT_URL}" >> .env
echoSuccess "The agent url is: ${NGROK_AGENT_URL}"

rm *-ngrok.json
fi
}

setDefaultProofConfig() {
# post default proof-request configuration to controller
docker run --network=vc-authn_vc_auth --rm curlimages/curl:latest -X POST "http://controller:5000/api/vc-configs" -H "accept: application/json" -H "X-Api-Key: controller-api-key" -H "Content-Type: application/json-patch+json" -d "{ \"id\": \"test-request-config\", \"subject_identifier\": \"email\", \"configuration\": { \"name\": \"Basic Proof\", \"version\": \"1.0\", \"requested_attributes\": [ { \"name\": \"email\", \"restrictions\": [] }, { \"name\": \"first_name\", \"restrictions\": [] }, { \"name\": \"last_name\", \"restrictions\": [] } ], \"requested_predicates\": [] }}"
docker run --network=vc-authn_vc_auth --rm curlimages/curl -X POST "http://controller:5000/api/vc-configs" -H "accept: application/json" -H "X-Api-Key: controller-api-key" -H "Content-Type: application/json-patch+json" -d "{ \"id\": \"test-request-config\", \"subject_identifier\": \"email\", \"configuration\": { \"name\": \"Basic Proof\", \"version\": \"1.0\", \"requested_attributes\": [ { \"name\": \"email\", \"restrictions\": [] }, { \"name\": \"first_name\", \"restrictions\": [] }, { \"name\": \"last_name\", \"restrictions\": [] } ], \"requested_predicates\": [] }}"
}
# =================================================================================================================

Expand Down Expand Up @@ -319,6 +356,9 @@ stop)
docker-compose stop
;;
rm|down)
# stop ngrok services, if running
docker compose -f docker-compose-ngrok.yaml down

# delete previously saved settings
if [ -f ".env" ] ; then
rm ".env"
Expand Down
25 changes: 4 additions & 21 deletions oidc-controller/api/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,14 @@ class GlobalConfig(BaseSettings):
MONGODB_URL: str = f"mongodb://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}?retryWrites=true&w=majority"

CONTROLLER_URL: str = os.environ.get("CONTROLLER_URL")
# # Get CONTROLLER_URL from env or NGROK.
CONTROLLER_NGROK: str = os.environ.get("CONTROLLER_NGROK")
if not CONTROLLER_URL and CONTROLLER_NGROK:
raw_resp = requests.get(CONTROLLER_NGROK + "/api/tunnels")
resp = json.loads(raw_resp.content)
CONTROLLER_URL = resp["tunnels"][0]["public_url"]
print("loaded CONTROLLER_URL from NGROK_TUNNEL_HOST")
print("CONTROLLER_URL: " + CONTROLLER_URL)

#

ACAPY_AGENT_URL: str = os.environ.get("ACAPY_AGENT_URL")
ACAPY_NGROK_TUNNEL_HOST: str = os.environ.get("ACAPY_NGROK_TUNNEL_HOST")
if not ACAPY_AGENT_URL and not ACAPY_NGROK_TUNNEL_HOST:
# ACAPY_NGROK_TUNNEL_HOST: str = os.environ.get("ACAPY_NGROK_TUNNEL_HOST")
if not ACAPY_AGENT_URL:
print(
"WARNING: neither ACAPY_AGENT_URL or ACAPY_NGROK_TUNNEL_HOST provided, agent will not be accessible"
"WARNING: ACAPY_AGENT_URL was not provided, agent will not be accessible"
)

if not ACAPY_AGENT_URL and ACAPY_NGROK_TUNNEL_HOST:
raw_resp = requests.get(ACAPY_NGROK_TUNNEL_HOST + "/api/tunnels")
resp = json.loads(raw_resp.content)
https_tunnels = [t for t in resp["tunnels"] if t["proto"] == "https"]
ACAPY_AGENT_URL = https_tunnels[0]["public_url"]
print("loaded ACAPY_AGENT_URL from ACAPY_NGROK_TUNNEL_HOST")
print("ACAPY_AGENT_URL: " + str(ACAPY_AGENT_URL))

ACAPY_TENANCY: str = os.environ.get(
"ACAPY_TENANCY", "single"
) # other option is "multi"
Expand Down

0 comments on commit 768c884

Please sign in to comment.