-
Couldn't load subscription status.
- Fork 7
Docker executor #5
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
Draft
colmazia
wants to merge
2
commits into
master
Choose a base branch
from
docker-executor
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| FROM python:3.8-slim-buster | ||
|
|
||
| WORKDIR /executor | ||
| COPY docker-executor /executor | ||
|
|
||
| ENV MAX_EXECUTABLE=8192 | ||
| ENV MAX_DATA_SIZE=512 | ||
|
|
||
| EXPOSE 5000 | ||
|
|
||
| RUN pip3 install -r requirements.txt | ||
|
|
||
| CMD [ "gunicorn", "app:app", "-b", "0.0.0.0:5000"] | ||
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,94 @@ | ||
| from flask import jsonify, Flask, request | ||
| import os | ||
| import shlex | ||
| import subprocess | ||
| import base64 | ||
|
|
||
| # Set environment flag of MAX_EXECUTABLE, MAX_DATA_SIZE | ||
|
|
||
|
|
||
| runtime_version = "google-cloud-function:2.0.3" | ||
| app = Flask(__name__) | ||
RogerKSI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def get_env(env, flag): | ||
| if flag not in env: | ||
| raise Exception(flag + " is missing") | ||
| return int(env[flag]) | ||
|
|
||
|
|
||
| def success(returncode, stdout, stderr, err): | ||
| return ( | ||
| jsonify( | ||
| { | ||
| "returncode": returncode, | ||
| "stdout": stdout, | ||
| "stderr": stderr, | ||
| "err": err, | ||
| "version": runtime_version, | ||
| } | ||
| ), | ||
| 200, | ||
| ) | ||
|
|
||
|
|
||
| def bad_request(err): | ||
| return jsonify({"error": err}), 400 | ||
|
|
||
| @app.route('/', methods=['POST']) | ||
| def execute(): | ||
| """Responds to any HTTP request. | ||
| Args: | ||
| request (flask.Request): HTTP request object. | ||
| Returns: | ||
| The response text or any set of values that can be turned into a | ||
| Response object using | ||
| `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. | ||
| """ | ||
| env = os.environ.copy() | ||
|
|
||
| MAX_EXECUTABLE = get_env(env, "MAX_EXECUTABLE") | ||
| MAX_DATA_SIZE = get_env(env, "MAX_DATA_SIZE") | ||
|
|
||
| request_json = request.get_json(force=True) | ||
| if "executable" not in request_json: | ||
| return bad_request("Missing executable value") | ||
| executable = base64.b64decode(request_json["executable"]) | ||
| if len(executable) > MAX_EXECUTABLE: | ||
| return bad_request("Executable exceeds max size") | ||
| if "calldata" not in request_json: | ||
| return bad_request("Missing calldata value") | ||
| if len(request_json["calldata"]) > MAX_DATA_SIZE: | ||
| return bad_request("Calldata exceeds max size") | ||
| if "timeout" not in request_json: | ||
| return bad_request("Missing timeout value") | ||
| try: | ||
| timeout = int(request_json["timeout"]) | ||
| except ValueError: | ||
| return bad_request("Timeout format invalid") | ||
|
|
||
| path = "/tmp/execute.sh" | ||
| with open(path, "w") as f: | ||
| f.write(executable.decode()) | ||
|
|
||
| os.chmod(path, 0o775) | ||
| try: | ||
| env = os.environ.copy() | ||
| for key, value in request_json.get("env", {}).items(): | ||
| env[key] = value | ||
|
|
||
| proc = subprocess.Popen( | ||
| [path] + shlex.split(request_json["calldata"]), | ||
| env=env, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| ) | ||
|
|
||
| proc.wait(timeout=(timeout / 1000)) | ||
| returncode = proc.returncode | ||
| stdout = proc.stdout.read(MAX_DATA_SIZE).decode() | ||
| stderr = proc.stderr.read(MAX_DATA_SIZE).decode() | ||
| return success(returncode, stdout, stderr, "") | ||
| except OSError: | ||
| return success(126, "", "", "Execution fail") | ||
| except subprocess.TimeoutExpired: | ||
| return success(111, "", "", "Execution time limit exceeded") | ||
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,31 @@ | ||
| aiodns==3.0.0 | ||
| aiohttp==3.8.1 | ||
| aiosignal==1.2.0 | ||
| async-timeout==4.0.2 | ||
| asynctest==0.13.0 | ||
| attrs==21.4.0 | ||
| bech32==1.2.0 | ||
| ccxt==1.87.48 | ||
| certifi==2022.5.18.1 | ||
| cffi==1.15.0 | ||
| charset-normalizer==2.0.12 | ||
| click==8.1.3 | ||
| cryptography==37.0.2 | ||
| Flask==2.1.2 | ||
| frozenlist==1.3.0 | ||
| gunicorn==20.1.0 | ||
| idna==3.3 | ||
| importlib-metadata==4.11.4 | ||
| itsdangerous==2.1.2 | ||
| Jinja2==3.1.2 | ||
| MarkupSafe==2.1.1 | ||
| multidict==6.0.2 | ||
| pycares==4.1.2 | ||
| pycparser==2.21 | ||
| requests==2.28.0 | ||
| typing-extensions==4.4.0 | ||
| urllib3==1.26.9 | ||
| websocket-client==1.3.2 | ||
| Werkzeug==2.1.2 | ||
| yarl==1.7.2 | ||
| zipp==3.8.0 |
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.