Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
502659cc
Commit
502659cc
authored
Jul 08, 2016
by
J. Cliff Dyer
Committed by
J. Cliff Dyer
Jul 16, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update loading from settings.
parent
e88c7120
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
130 additions
and
12 deletions
+130
-12
codejail/django_integration.py
+3
-11
codejail/integration.py
+68
-0
codejail/tests/test_integration.py
+50
-0
codejail/tests/test_jail_code.py
+9
-1
No files found.
codejail/django_integration.py
View file @
502659cc
...
@@ -4,10 +4,10 @@ Code to glue codejail into a Django environment.
...
@@ -4,10 +4,10 @@ Code to glue codejail into a Django environment.
"""
"""
from
django.conf
import
settings
as
django_settings
from
django.core.exceptions
import
MiddlewareNotUsed
from
django.core.exceptions
import
MiddlewareNotUsed
from
django.conf
import
settings
import
codejail.limit
s
from
.integration
import
configure_from_setting
s
class
ConfigureCodeJailMiddleware
(
object
):
class
ConfigureCodeJailMiddleware
(
object
):
...
@@ -20,13 +20,5 @@ class ConfigureCodeJailMiddleware(object):
...
@@ -20,13 +20,5 @@ class ConfigureCodeJailMiddleware(object):
"""
"""
def
__init__
(
self
):
def
__init__
(
self
):
python_bin
=
settings
.
CODE_JAIL
.
get
(
'python_bin'
)
configure_from_settings
(
django_settings
)
if
python_bin
:
user
=
settings
.
CODE_JAIL
[
'user'
]
codejail
.
jail_code
.
configure
(
"python"
,
python_bin
,
user
=
user
)
limits
=
settings
.
CODE_JAIL
.
get
(
'limits'
,
{})
for
name
,
value
in
limits
.
items
():
codejail
.
limits
.
set_limit
(
name
,
value
)
raise
MiddlewareNotUsed
raise
MiddlewareNotUsed
codejail/integration.py
0 → 100644
View file @
502659cc
"""
Integrate with application settings files.
"""
from
__future__
import
absolute_import
from
.
import
jail
from
.
import
limits
def
configure_from_settings
(
settings
):
"""
Configure a set of code jails and limits from a django settings file of the
form:
import codejail.languages
CODE_JAIL = {
'jails': [
{
'command': 'python',
'bin_path': '/edx/app/edxapp/venvs/edxapp-sandbox/bin/python',
'user': 'sandbox',
'lang': codejail.languages.python2,
},
{
'command': 'jail3',
'bin_path': '/edx/app/edxapp/venvs/edxapp-sandbox3/bin/python3',
'user': 'sandbox',
'lang': codejail.languages.python3,
},
],
'limits': {
'CPU': 1,
}
}
Each item in `CODE_JAIL['jails']` is a dict of kwargs for a `codejail.jail.Jail` object.
"""
if
'jails'
in
settings
.
CODE_JAIL
:
for
jail_config
in
settings
.
CODE_JAIL
[
'jails'
]:
jail
.
configure
(
**
jail_config
)
requested_limits
=
settings
.
CODE_JAIL
.
get
(
'limits'
,
{})
for
name
,
value
in
requested_limits
.
items
():
limits
.
set_limit
(
name
,
value
)
else
:
legacy_configure_from_settings
(
settings
)
def
legacy_configure_from_settings
(
settings
):
"""
Configure a "python" code jail and limits from a django settings file where
the settings look like this:
CODE_JAIL = {
"python_bin": "/edx/app/edxapp/venvs/edxapp-sandbox/bin/python",
"user": "sandbox",
"limits": {"CPU": 1},
}
"""
python_bin
=
settings
.
CODE_JAIL
.
get
(
'python_bin'
)
if
python_bin
:
user
=
settings
.
CODE_JAIL
[
'user'
]
jail
.
configure
(
"python"
,
python_bin
,
user
=
user
)
requested_limits
=
settings
.
CODE_JAIL
.
get
(
'limits'
,
{})
for
name
,
value
in
requested_limits
.
items
():
limits
.
set_limit
(
name
,
value
)
codejail/tests/test_integration.py
0 → 100644
View file @
502659cc
"""
Test configuring code jails from settings objects.
"""
from
unittest
import
TestCase
from
codejail
import
django_integration
,
jail
,
languages
class
FakeJailSettings
(
object
):
def
__init__
(
self
,
setting_value
):
self
.
CODE_JAIL
=
setting_value
# pylint: disable=invalid-name
class
TestDjangoIntegration
(
TestCase
):
def
setUp
(
self
):
super
(
TestDjangoIntegration
,
self
)
.
setUp
()
self
.
existing_commands
=
jail
.
COMMANDS
jail
.
COMMANDS
=
{}
def
tearDown
(
self
):
jail
.
COMMANDS
=
self
.
existing_commands
super
(
TestDjangoIntegration
,
self
)
.
tearDown
()
def
test_configure_jail
(
self
):
codejail_setting
=
FakeJailSettings
({
'jails'
:
[
{
'command'
:
'fakey-fakey'
,
'user'
:
'nobody'
,
'bin_path'
:
'/usr/bin/python'
,
'lang'
:
languages
.
python3
,
}
]
})
django_integration
.
configure_from_settings
(
codejail_setting
)
self
.
assertTrue
(
jail
.
is_configured
(
'fakey-fakey'
))
def
test_configure_legacy_jail
(
self
):
codejail_setting
=
FakeJailSettings
({
'python_bin'
:
'/usr/bin/python'
,
'user'
:
'abc'
,
})
django_integration
.
configure_from_settings
(
codejail_setting
)
codejail
=
jail
.
get_codejail
(
'python'
)
self
.
assertEqual
(
codejail
.
command
,
'python'
)
self
.
assertEqual
(
codejail
.
bin_path
,
'/usr/bin/python'
)
self
.
assertEqual
(
codejail
.
user
,
'abc'
)
self
.
assertEqual
(
codejail
.
lang
,
languages
.
python2
)
codejail/tests/test_jail_code.py
View file @
502659cc
...
@@ -153,6 +153,14 @@ class TestFeatures(JailCodeMixin, unittest.TestCase):
...
@@ -153,6 +153,14 @@ class TestFeatures(JailCodeMixin, unittest.TestCase):
self
.
assertEqual
(
res
.
stdout
,
'Look: Hello there.
\n\n
'
)
self
.
assertEqual
(
res
.
stdout
,
'Look: Hello there.
\n\n
'
)
def
test_directories_are_copied
(
self
):
def
test_directories_are_copied
(
self
):
# The existence of pyc files makes the test dependent on whether the
# code in pylib/ has been executed or not. Removing them makes the
# test more deterministic.
pyc
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'./pylib/module.pyc'
)
if
os
.
path
.
exists
(
pyc
):
os
.
unlink
(
pyc
)
res
=
jailpy
(
res
=
jailpy
(
code
=
"""
\
code
=
"""
\
import os
import os
...
@@ -167,7 +175,7 @@ class TestFeatures(JailCodeMixin, unittest.TestCase):
...
@@ -167,7 +175,7 @@ class TestFeatures(JailCodeMixin, unittest.TestCase):
self
.
assertResultOk
(
res
)
self
.
assertResultOk
(
res
)
self
.
assertEqual
(
res
.
stdout
,
textwrap
.
dedent
(
"""
\
self
.
assertEqual
(
res
.
stdout
,
textwrap
.
dedent
(
"""
\
('.', ['pylib', 'tmp'], ['hello.txt', 'jailed_code'])
('.', ['pylib', 'tmp'], ['hello.txt', 'jailed_code'])
('./pylib', [], ['module.py'
, 'module.pyc'
])
('./pylib', [], ['module.py'])
('./tmp', [], [])
('./tmp', [], [])
"""
))
"""
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment