-
Notifications
You must be signed in to change notification settings - Fork 808
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
Add tooling for recording and replaying PyPI interactions #609
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ecac4b
Use `rustls-tls-native-roots` feature to support HTTPS proxy from mit…
zanieb c4cb290
Add `offlinepi`
zanieb 5565aef
shellcheck
zanieb 8e437a9
Use a HAR-file
zanieb 795b6f9
Add install instructions
zanieb d397401
Allow injecting certificate for tests (#615)
konstin 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 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 |
---|---|---|
|
@@ -16,3 +16,6 @@ cache-* | |
|
||
# python tmp files | ||
__pycache__ | ||
|
||
scripts/offlinepi/mitmproxy-ca-cert.pem | ||
scripts/offlinepi/responses.dat |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,58 @@ | ||
# offlinepi | ||
|
||
Utilities for managing an offline version of PyPI. | ||
|
||
## Installation | ||
|
||
Installation requires `mitmproxy`. We require unreleased changes, it is recommended to install from GitHub: | ||
|
||
``` | ||
pip install git+https://github.com/mitmproxy/mitmproxy@1fcd0335d59c301d73d1b1ef676ecafcf520ab79 | ||
``` | ||
|
||
## Usage | ||
|
||
Record PyPI responses during a command: | ||
|
||
``` | ||
./offlinepi record <command> | ||
``` | ||
|
||
Replay PyPI responses during a command: | ||
|
||
``` | ||
./offlinepi replay <command> | ||
``` | ||
|
||
### Example | ||
|
||
Record server interactions during Puffin's tests: | ||
|
||
``` | ||
./offlinepi record cargo test --features pypi -- --test-threads=1 | ||
``` | ||
|
||
**Note**: Recording tests without parallelism is helpful for reliable replays. | ||
|
||
Then, run it again using replayed responses: | ||
|
||
``` | ||
./offlinepi replay cargo test --features pypi | ||
``` | ||
|
||
## TLS Certificates | ||
|
||
In order to record HTTPS requests, the certificate generated by mitmproxy must be installed. | ||
See [the mitmproxy certificate documentation](https://docs.mitmproxy.org/stable/concepts-certificates/) for details. | ||
|
||
## Implementation | ||
|
||
[mitmproxy](https://mitmproxy.org/) is used to record and replay responses. | ||
|
||
The proxy is temporarily created for the execution of the provided command. | ||
|
||
The command _must_ respect the `HTTP_PROXY` and `HTTPS_PROXY` environment variables. | ||
|
||
Response recording is limited to `pypi.org` and `files.pythonhosted.org`. | ||
|
||
Responses are written to `responses.dat` in the `offlinepi` project root. |
This file contains 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,50 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Run a command, recording or replaying interaction with the PyPI server. | ||
# | ||
# Usage: | ||
# | ||
# offlinepi <record|replay> <command> | ||
# | ||
|
||
projectroot=$(realpath "$(dirname "$0")") | ||
responsefile=$projectroot/responses.har | ||
|
||
mode=$1 | ||
shift | ||
|
||
if [ -z "$mode" ]; then | ||
echo 'A mode must be provided e.g. `offlinepi record ...`' | ||
exit 1 | ||
fi | ||
|
||
if [[ "${mode}" != @(record|replay) ]]; then | ||
echo "Invalid mode \"$mode\"; expected either \"record\" or \"replay\"." | ||
exit 1 | ||
fi | ||
|
||
if $projectroot/offlinepi-healthcheck; then | ||
echo "Proxy is already running at localhost:8080" | ||
echo "Aborted!" | ||
exit 1 | ||
fi | ||
|
||
echo "Starting proxy server to $mode responses..." | ||
$projectroot/offlinepi-$mode $responsefile& | ||
PROXY_PID=$! | ||
|
||
if ! $projectroot/offlinepi-wait $PROXY_PID; then | ||
echo "Server failed to start!" | ||
echo "Aborted!" | ||
$projectroot/offlinepi-stop $PROXY_PID | ||
exit 1 | ||
fi | ||
|
||
export HTTP_PROXY=http://localhost:8080 | ||
export HTTPS_PROXY=https://localhost:8080 | ||
|
||
echo "Running provided command..." | ||
"$@" | ||
|
||
echo "Stopping proxy server..." | ||
$projectroot/offlinepi-stop $PROXY_PID |
This file contains 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,12 @@ | ||
#!/usr/bin/env sh | ||
# | ||
# Checks if the proxy is running. | ||
# | ||
# Usage: | ||
# | ||
# offlinepi-healthcheck | ||
|
||
exec curl --output /dev/null --silent --head --fail --proxy 127.0.0.1:8080 http://mitm.it | ||
|
||
# TODO(zanieb): We could consider looking at the response to determine if a _different_ proxy is being used. | ||
# TODO(zanieb): This could take a configurable host and port |
This file contains 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,36 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Start a proxy that records client server interactions to a file. | ||
# | ||
# Usage: | ||
# | ||
# offlinepi-record <path> | ||
|
||
path=$1 | ||
shift | ||
|
||
if [ -z "$path" ]; then | ||
echo 'A recording path must be provided.' | ||
exit 1 | ||
fi | ||
|
||
if [ -n "$*" ]; then | ||
echo "Unexpected extra arguments: $*" | ||
exit 1 | ||
fi | ||
|
||
# N.B. Additional options must be added _before_ the filter string | ||
exec mitmdump \ | ||
--set stream_large_bodies=1000m \ | ||
--set hardump="$path" \ | ||
"~d pypi.org|files.pythonhosted.org|mitm.it" | ||
|
||
# stream_large_bodies: must be set to a large value or large responses will not be recorded | ||
# resulting in an unexpected file endings during replays | ||
# hardump: we use a HAR file instead of the binary format (-w <path>) so it the output is | ||
# human readable | ||
# ~d: only interactions with package index domains should be recorded | ||
# we also allow `mitm.it` so healthchecks succeed when replaying | ||
|
||
# Helpful notes for development | ||
# --flow-detail <0-4> can be used to adjust the amount information displayed about traffic |
This file contains 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,30 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Start a proxy that replays server responses from a recording. | ||
# Unknown responses will result in a 500. | ||
# Each response can only be replayed once or it will be treated as unknown. | ||
# | ||
# Usage: | ||
# | ||
# offlinepi-start-replay <path> | ||
|
||
path=$1 | ||
shift | ||
|
||
if [ -z "$path" ]; then | ||
echo 'A recording path must be provided.' | ||
exit 1 | ||
fi | ||
|
||
if [ -n "$*" ]; then | ||
echo "Unexpected extra arguments: $*" | ||
exit 1 | ||
fi | ||
|
||
exec mitmdump --server-replay "$path" \ | ||
--flow-detail 3 \ | ||
--server-replay-extra 500 \ | ||
--set connection_strategy=lazy | ||
|
||
# server-replay-extra: configures behavior when a response is unknown. | ||
# connection_stategy: lazy is required to replay offline |
This file contains 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,24 @@ | ||
#!/usr/bin/env sh | ||
# | ||
# Stops the proxy at the given PID. | ||
# | ||
# Usage: | ||
# | ||
# offlinepi-stop <pid> | ||
|
||
pid=$1 | ||
shift | ||
|
||
if [ -z "$pid" ]; then | ||
echo 'A PID must be provided.' | ||
exit 1 | ||
fi | ||
|
||
if [ -n "$*" ]; then | ||
echo "Unexpected extra arguments: $*" | ||
exit 1 | ||
fi | ||
|
||
kill "$pid" 2> /dev/null | ||
wait "$pid" 2> /dev/null | ||
echo "Done!" |
This file contains 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,32 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Waits for the proxy to be ready. | ||
# | ||
# Usage: | ||
# | ||
# offlinepi-wait-ready <pid> | ||
|
||
projectroot=$(realpath "$(dirname "$0")") | ||
healthcheck="$projectroot/offlinepi-healthcheck" | ||
|
||
pid=$1 | ||
shift | ||
|
||
if [ -z "$pid" ]; then | ||
echo 'A PID must be provided.' | ||
exit 1 | ||
fi | ||
|
||
if [ -n "$*" ]; then | ||
echo "Unexpected extra arguments: $*" | ||
exit 1 | ||
fi | ||
|
||
|
||
# Wait until the server is ready | ||
until $healthcheck; do | ||
if ! kill -0 "$pid" 2> /dev/null; then | ||
exit 1 | ||
fi | ||
sleep 1 | ||
done |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is required because I registered the cert with my system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested in seanmonstar/reqwest#1554
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pros / cons at https://github.com/rustls/rustls-native-certs#should-i-use-this-or-webpki-roots
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related encode/httpx#302