在基于安全Docker的沙盒中运行不受信任的代码
epicbox的Python项目详细描述
Epicbox
在安全、隔离的Docker中运行不受信任代码的python库 基于沙盒。它用于自动给编程作业评分 在Stepik.org上。
它允许在一次性Docker容器中生成一个进程,发送数据
STDIN,并获得其退出代码和STDUD/STDER输出。非常相似
为了什么^{
用法
使用
^{
importepicboxepicbox.configure(profiles=[epicbox.Profile('python','python:3.6.5-alpine')])files=[{'name':'main.py','content':b'print(42)'}]limits={'cputime':1,'memory':64}result=epicbox.run('python','python3 main.py',files=files,limits=limits)
result
值是:
{'exit_code':0,'stdout':b'42\n','stderr':b'','duration':0.143358,'timeout':False,'oom_killed':False}
可用限制选项
可用的限制选项和默认值:
DEFAULT_LIMITS = {
# CPU time in seconds, None for unlimited
'cputime': 1,
# Real time in seconds, None for unlimited
'realtime': 5,
# Memory in megabytes, None for unlimited
'memory': 64,
# limit the max processes the sandbox can have
# -1 or None for unlimited(default)
'processes': -1,
}
高级使用
<^ >一个更先进的使用例子^ {< CD4>}是编译一个C++程序,然后 在不同的输入数据上运行多次。在这个例子中epicbox
将
在专用的Docker Swarm上运行容器
群集而不是本地安装的Docker引擎:importepicboxPROFILES={'gcc_compile':{'docker_image':'stepik/epicbox-gcc:6.3.0','user':'root',},'gcc_run':{'docker_image':'stepik/epicbox-gcc:6.3.0',# It's safer to run untrusted code as a non-root user (even in a container)'user':'sandbox','read_only':True,'network_disabled':False,},}epicbox.configure(profiles=PROFILES,docker_url='tcp://1.2.3.4:2375')untrusted_code=b"""// C++ program#include <iostream>int main() { int a, b; std::cin >> a >> b; std::cout << a + b << std::endl;}"""# A working directory allows to preserve files created in a one-time container# and access them from another one. Internally it is a temporary Docker volume.withepicbox.working_directory()asworkdir:epicbox.run('gcc_compile','g++ -pipe -O2 -static -o main main.cpp',files=[{'name':'main.cpp','content':untrusted_code}],workdir=workdir)epicbox.run('gcc_run','./main',stdin='2 2',limits={'cputime':1,'memory':64},workdir=workdir)# {'exit_code': 0, 'stdout': b'4\n', 'stderr': b'', 'duration': 0.095318, 'timeout': False, 'oom_killed': False}epicbox.run('gcc_run','./main',stdin='14 5',limits={'cputime':1,'memory':64},workdir=workdir)# {'exit_code': 0, 'stdout': b'19\n', 'stderr': b'', 'duration': 0.10285, 'timeout': False, 'oom_killed': False}
安装
epicbox
可以通过运行pip install epicbox
来安装。它在Python3.4+上进行了测试
Docker 1.12+。
您还可以检查epicbox-images 包含Docker图像的存储库,用于自动分级编程 关于Stepik.org的作业。
贡献
我们欢迎您的贡献,我们非常感谢您的贡献! 更多详细信息请参见CONTRIBUTING。