Commit 5d4b61c7 by Ned Batchelder

Better configuration for codejail.

parent 30748a06
"""Django integration for codejail"""
from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
import codejail.jailpy
class ConfigureCodeJailMiddleware(object):
"""Middleware to configure codejail on startup."""
def __init__(self):
python_bin = settings.CODE_JAIL.get('python_bin')
if python_bin:
user = settings.CODE_JAIL['user']
codejail.jailpy.configure(python_bin, user=user)
raise MiddlewareNotUsed
...@@ -7,6 +7,7 @@ import os, os.path ...@@ -7,6 +7,7 @@ import os, os.path
import resource import resource
import shutil import shutil
import subprocess import subprocess
import sys
import threading import threading
import time import time
...@@ -16,21 +17,24 @@ from .util import temp_directory ...@@ -16,21 +17,24 @@ from .util import temp_directory
# Configure the Python command # Configure the Python command
SANDBOX_POSSIBILITIES = [ PYTHON_CMD = None
"~/mitx_all/python-sandbox/bin/python",
"/usr/bin/python-sandbox",
]
for sandbox_python in SANDBOX_POSSIBILITIES: def configure(python_bin, user=None):
sandbox_python = os.path.expanduser(sandbox_python) """Configure the jailpy module."""
if os.path.exists(sandbox_python): global PYTHON_CMD
PYTHON_CMD = [ PYTHON_CMD = []
'sudo', '-u', 'sandbox', if user:
sandbox_python, '-E', PYTHON_CMD.extend(['sudo', '-u', 'sandbox'])
] PYTHON_CMD.extend([python_bin, '-E'])
break
else: def is_configured():
raise Exception("Couldn't find Python sandbox") return bool(PYTHON_CMD)
# By default, look where our current Python is, and maybe there's a
# python-sandbox alongside. Only do this if running in a virtualenv.
if hasattr(sys, 'real_prefix'):
if os.path.isdir(sys.prefix + "-sandbox"):
configure(sys.prefix + "-sandbox/bin/python", "sandbox")
class JailResult(object): class JailResult(object):
...@@ -52,6 +56,9 @@ def jailpy(code, files=None, argv=None, stdin=None): ...@@ -52,6 +56,9 @@ def jailpy(code, files=None, argv=None, stdin=None):
.status: return status of the process: an int, 0 for successful .status: return status of the process: an int, 0 for successful
""" """
if not PYTHON_CMD:
raise Exception("jailpy needs to be configured")
with temp_directory(delete_when_done=True) as tmpdir: with temp_directory(delete_when_done=True) as tmpdir:
# All the supporting files are copied into our directory. # All the supporting files are copied into our directory.
......
...@@ -4,12 +4,17 @@ import textwrap ...@@ -4,12 +4,17 @@ import textwrap
import unittest import unittest
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from codejail.jailpy import jailpy from codejail.jailpy import jailpy, is_configured
dedent = textwrap.dedent dedent = textwrap.dedent
class JailPyHelpers(object): class JailPyHelpers(object):
"""Assert helpers for jailpy tests.""" """Assert helpers for jailpy tests."""
def setUp(self):
super(JailPyHelpers, self).setUp()
if not is_configured():
raise SkipTest
def assertResultOk(self, res): def assertResultOk(self, res):
self.assertEqual(res.stderr, "") self.assertEqual(res.stderr, "")
self.assertEqual(res.status, 0) self.assertEqual(res.status, 0)
......
...@@ -242,6 +242,16 @@ MODULESTORE = { ...@@ -242,6 +242,16 @@ MODULESTORE = {
} }
CONTENTSTORE = None CONTENTSTORE = None
#################### Python sandbox ############################################
CODE_JAIL = {
# Path to a sandboxed Python executable. None means don't bother.
'python_bin': None,
# User to run as in the sandbox.
'user': 'sandbox',
}
############################ SIGNAL HANDLERS ################################ ############################ SIGNAL HANDLERS ################################
# This is imported to register the exception signal handling that logs exceptions # This is imported to register the exception signal handling that logs exceptions
import monitoring.exceptions # noqa import monitoring.exceptions # noqa
...@@ -385,6 +395,7 @@ MIDDLEWARE_CLASSES = ( ...@@ -385,6 +395,7 @@ MIDDLEWARE_CLASSES = (
# 'debug_toolbar.middleware.DebugToolbarMiddleware', # 'debug_toolbar.middleware.DebugToolbarMiddleware',
'django_comment_client.utils.ViewNameMiddleware', 'django_comment_client.utils.ViewNameMiddleware',
'codejail.django_integration.ConfigureCodeJailMiddleware',
) )
############################### Pipeline ####################################### ############################### Pipeline #######################################
......
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