-
Notifications
You must be signed in to change notification settings - Fork 151
Add fleet mitm-proxy example #432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
norman465
wants to merge
1
commit into
IBM:main
Choose a base branch
from
norman465:mitm_proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | awk -F- '{print $1}') | ||
|
|
||
| PREHOOK=$(cat <<'OUTER' | ||
| #!/usr/bin/env bash | ||
| set -Eeuo pipefail | ||
|
|
||
| ### ===== User-tunable variables ===== | ||
|
|
||
| export NETWORK_NAME="podman" | ||
| export SUBNET_CIDR="10.88.0.0/16" | ||
| export GATEWAY_IP="10.88.0.1" | ||
|
|
||
| export MITM_IMAGE="docker.io/mitmproxy/mitmproxy:latest" | ||
| export MITM_PORT="8080" | ||
| export MITM_CONTAINER="https-proxy" | ||
|
|
||
| export WORKDIR="$PWD/podman-transparent-proxy-lab" | ||
|
|
||
| ### ===== Derived variables ===== | ||
|
|
||
| MITM_DIR="$WORKDIR/mitmproxy" | ||
| CERTS_DIR="$WORKDIR/certs" | ||
|
|
||
| mkdir -p "$MITM_DIR" "$CERTS_DIR" | ||
|
|
||
| echo "==> Checking dependencies" | ||
| command -v podman >/dev/null | ||
| command -v sudo >/dev/null | ||
| command -v iptables >/dev/null | ||
|
|
||
| echo "==> Creating Podman network if needed" | ||
| if ! podman network exists "${NETWORK_NAME}"; then | ||
| podman network create \ | ||
| --subnet "${SUBNET_CIDR}" \ | ||
| --gateway "${GATEWAY_IP}" \ | ||
| "${NETWORK_NAME}" | ||
| fi | ||
|
|
||
| echo "==> Removing old containers if present" | ||
| podman rm -f "${MITM_CONTAINER}" 2>/dev/null || true | ||
|
|
||
| echo "==> Starting mitmproxy HTTPS transparent proxy" | ||
| podman run -d \ | ||
| --name "${MITM_CONTAINER}" \ | ||
| --network host \ | ||
| -v "${MITM_DIR}:/home/mitmproxy/.mitmproxy:Z" \ | ||
| "${MITM_IMAGE}" \ | ||
| mitmdump --mode transparent --showhost --listen-host 0.0.0.0 --listen-port "${MITM_PORT}" --set "block_list=/!~d '(.*example\.com)$'/403" | ||
|
|
||
| echo "==> Waiting for mitmproxy CA files" | ||
| for i in $(seq 1 30); do | ||
| if [[ -f "${MITM_DIR}/mitmproxy-ca-cert.pem" ]]; then | ||
| break | ||
| fi | ||
| sleep 1 | ||
| done | ||
|
|
||
| if [[ ! -f "${MITM_DIR}/mitmproxy-ca-cert.pem" ]]; then | ||
| echo "ERROR: mitmproxy CA cert was not generated" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "==> Installing CA certificate" | ||
| cp "${MITM_DIR}/mitmproxy-ca-cert.pem" "/usr/local/share/ca-certificates/mitmproxy-ca-cert.crt" | ||
|
|
||
| if update-ca-certificates >/dev/null 2>&1; then | ||
| update-ca-certificates | ||
| elif update-ca-trust >/dev/null 2>&1; then | ||
| update-ca-trust | ||
| else | ||
| echo "No CA update tool found" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "==> Enabling IPv4 forwarding on host" | ||
| sudo sysctl -w net.ipv4.ip_forward=1 >/dev/null | ||
|
|
||
| echo "==> Preparing iptables chain" | ||
| sudo iptables -t nat -N MITM_PROXY 2>/dev/null || true | ||
| sudo iptables -t nat -F MITM_PROXY | ||
|
|
||
| sudo iptables -t nat -A MITM_PROXY -d 127.0.0.0/8 -j RETURN | ||
| sudo iptables -t nat -A MITM_PROXY -d ${SUBNET_CIDR} -j RETURN | ||
| sudo iptables -t nat -A MITM_PROXY -p tcp --dport 443 -j REDIRECT --to-ports 8080 | ||
|
|
||
| sudo iptables -t nat -A PREROUTING -s ${SUBNET_CIDR} -p tcp -j MITM_PROXY | ||
| OUTER | ||
| ) | ||
|
|
||
| ibmcloud ce fleet create --name "fleet-${uuid}" \ | ||
| --tasks-state-store fleet-task-store \ | ||
| --image registry.access.redhat.com/ubi10/ubi-minimal \ | ||
| --cpu "2" \ | ||
| --memory "4G" \ | ||
| --tasks-from-local-file run_hook_mitm_https_proxy_commands.jsonl \ | ||
| --max-scale 2 \ | ||
| --retrylimit 0 \ | ||
| --subnetpool-name fleet-subnetpool \ | ||
| --env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}" \ | ||
| --env __CE_INTERNAL_HOSTPATH_MOUNTS="/etc/ssl/certs:/etc/ssl/certs;/usr/local/share/ca-certificates/:/usr/local/share/ca-certificates" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| { "command":"curl", "args": ["--silent", "--write-out", "%{url} %{http_code}", "--output", "/dev/null", "https://example.com"]} | ||
|
norman465 marked this conversation as resolved.
|
||
| { "command":"curl", "args": ["--silent", "--write-out", "%{url} %{http_code}", "--output", "/dev/null", "https://google.com"]} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.