Commit bbfd376d by Adam Palay

create proxy process on startup (TNL-1226)

add startup function
parent 6b17c33a
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
Code to glue codejail into a Django environment. Code to glue codejail into a Django environment.
""" """
from django.core.exceptions import MiddlewareNotUsed from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings from django.conf import settings
...@@ -29,4 +28,7 @@ class ConfigureCodeJailMiddleware(object): ...@@ -29,4 +28,7 @@ class ConfigureCodeJailMiddleware(object):
for name, value in limits.items(): for name, value in limits.items():
codejail.jail_code.set_limit(name, value) codejail.jail_code.set_limit(name, value)
# if using a proxy, start it up now
codjail.jail_code.startup()
raise MiddlewareNotUsed raise MiddlewareNotUsed
...@@ -7,7 +7,7 @@ import resource ...@@ -7,7 +7,7 @@ import resource
import shutil import shutil
import sys import sys
from .proxy import run_subprocess_through_proxy from .proxy import run_subprocess_through_proxy, get_proxy
from .subproc import run_subprocess from .subproc import run_subprocess
from .util import temp_directory from .util import temp_directory
...@@ -220,12 +220,7 @@ def jail_code(command, code=None, files=None, extra_files=None, argv=None, ...@@ -220,12 +220,7 @@ def jail_code(command, code=None, files=None, extra_files=None, argv=None,
# Add the code-specific command line pieces. # Add the code-specific command line pieces.
cmd.extend(argv) cmd.extend(argv)
# Use the configuration and maybe an environment variable to determine if use_proxy():
# whether to use a proxy process.
use_proxy = LIMITS["PROXY"]
if use_proxy is None:
use_proxy = int(os.environ.get("CODEJAIL_PROXY", "0"))
if use_proxy:
run_subprocess_fn = run_subprocess_through_proxy run_subprocess_fn = run_subprocess_through_proxy
else: else:
run_subprocess_fn = run_subprocess run_subprocess_fn = run_subprocess
...@@ -284,3 +279,23 @@ def create_rlimits(): ...@@ -284,3 +279,23 @@ def create_rlimits():
rlimits.append((resource.RLIMIT_FSIZE, (fsize, fsize))) rlimits.append((resource.RLIMIT_FSIZE, (fsize, fsize)))
return rlimits return rlimits
def startup():
"""
To be run when whatever process is running codejail starts up.
If we're using a proxy process, start it. Otherwise, noop.
"""
if use_proxy():
return get_proxy()
def use_proxy():
"""
Use the configuration and maybe an environment variable to determine
whether to use a proxy process.
"""
use_it = LIMITS["PROXY"]
if use_it is None:
use_it = bool(int(os.environ.get("CODEJAIL_PROXY", "0")))
return use_it
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment