在基于安全Docker的沙盒中运行不受信任的代码

epicbox的Python项目详细描述


Epicbox

Build Status

在安全、隔离的Docker中运行不受信任代码的python库 基于沙盒。它用于自动给编程作业评分 在Stepik.org上。

它允许在一次性Docker容器中生成一个进程,发送数据 STDIN,并获得其退出代码和STDUD/STDER输出。非常相似 为了什么^{} 模块可以,但您还可以为流程指定自定义环境 (码头工人) 并限制运行进程的CPU、内存、磁盘和网络使用。

用法

使用 ^{}图像:

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在href中将struts2文本字段的值作为参数传递?   java检查对象属性是否为空的最佳方法是什么?   java通过Maven使用Junit类别运行Cucumber测试   java如何在selenium Webdriver(Mac)中使用Robot类上传多个文件?   如何用python绘制图形或将python转换为java和Matlab?   java Osgi捆绑包更新和ResourceBundle   java使用流api将流<@Nullable T>转换为流<@NonNull T>   java中EXCEL的平台无关连接字符串   JavaFX中的java表   java Jetty线程池和sun。HttpServer会话   JPA存储库bean的java Spring注入无法工作NullPointerException   java从另一个Kubernetes作业触发Kubernetes   我的java netbeans抽奖计划需要帮助吗   泛型中的java有界类型无法扩展另一个有界类型   如果混合使用全局构建和概要文件构建,java cxfcodegenplugin会生成错误代码   封装SQL平台之间差异的java策略?