如何快速上手openeuler-intelligence-sandbox?5分钟搭建你的安全代码执行环境
如何快速上手openeuler-intelligence-sandbox5分钟搭建你的安全代码执行环境【免费下载链接】openeuler-intelligence-sandboxCode execution service for openEuler Intelligence supported multiple programming languages项目地址: https://gitcode.com/openeuler/openeuler-intelligence-sandbox前往项目官网免费下载https://ar.openeuler.org/ar/openeuler-intelligence-sandbox是一个基于FastAPI构建的多语言代码执行沙箱服务专为openEuler Intelligence平台设计。这个强大的安全代码执行环境支持Python、JavaScript和Bash等多种编程语言提供两级安全隔离机制让开发者能够在安全可控的环境中运行用户提交的代码。 快速入门指南第一步环境准备与安装在开始之前确保你的系统已安装Python 3.8。通过以下命令克隆项目仓库并安装依赖git clone https://gitcode.com/openeuler/openeuler-intelligence-sandbox cd openeuler-intelligence-sandbox pip install -r requirements.txt第二步启动服务进入应用目录并启动服务cd app python start_service.py服务将在http://localhost:8000启动。你可以通过以下方式验证服务是否正常运行curl http://localhost:8000/health第三步访问API文档服务启动后可以通过以下地址访问完整的API文档Swagger UI界面http://localhost:8000/docsReDoc文档http://localhost:8000/redoc 核心功能详解多语言代码执行支持openeuler-intelligence-sandbox支持多种编程语言的代码执行Python支持Python 3.x语法包含标准库的有限子集JavaScript支持Node.js环境下的JavaScript代码执行Bash支持基本的Shell命令和脚本执行两级安全隔离机制项目提供两种安全等级满足不同场景的需求 低安全等级LOW使用本地安全加固的exec执行器最大并发10个任务适用场景相对受信任的用户代码配置位置app/executor_manager.py 高安全等级HIGH使用完全隔离的容器执行器最大并发3个任务适用场景不受信任的用户代码配置位置app/executor/container_executor.py智能队列管理系统项目内置了高效的队列管理系统具有以下特点优先级队列支持任务优先级设置按安全等级分队列不同安全等级的任务独立处理并发控制可配置的并发任务限制任务调度基于APScheduler的自动任务处理 使用示例提交代码执行任务使用以下API接口提交代码执行任务import requests import json url http://localhost:8000/execute payload { code: print(Hello, openeuler-intelligence-sandbox!), code_type: python, user_info: { user_id: user123, username: 测试用户, permissions: [execute] }, security_level: low, timeout_seconds: 30, memory_limit_mb: 128, cpu_limit: 0.5 } response requests.post(url, jsonpayload) result response.json() print(f任务ID: {result[data][task_id]})获取任务状态和结果提交任务后可以通过以下API获取执行状态和结果# 获取任务状态 curl http://localhost:8000/task/{task_id}/status # 获取任务结果 curl http://localhost:8000/task/{task_id}/result # 取消任务 curl -X DELETE http://localhost:8000/task/{task_id}⚙️ 配置与定制执行器配置调整你可以在 app/executor_manager.py 中调整各安全等级的配置self.default_configs { SecurityLevel.LOW: ExecutorConfig( max_concurrent_tasks10, default_timeout30, resource_limits{memory: 512Mi, cpu: 1} ), SecurityLevel.HIGH: ExecutorConfig( max_concurrent_tasks3, default_timeout60, resource_limits{memory: 1Gi, cpu: 2} ) }安全加固配置项目的安全加固设计包括多个层次代码级别安全控制模块导入限制内置函数限制文件操作禁用系统级别资源限制内存限制64MB可配置CPU限制执行时间最大10秒文件系统限制进程隔离与控制新进程组创建进程数量限制环境变量清理详细配置位于app/executor/secure_exec_executor.py️ 扩展与开发添加新的代码类型支持要添加新的编程语言支持按照以下步骤操作在 app/entities.py 的CodeType枚举中添加新类型在相应执行器中添加支持配置更新容器镜像映射如果需要创建自定义执行器你可以继承BaseExecutor类创建自定义执行器from app.executor.base import BaseExecutor class CustomExecutor(BaseExecutor): async def prepare_environment(self, request): # 实现环境准备逻辑 pass async def apply_security_constraints(self, request, env_info): # 实现安全约束逻辑 pass async def execute_code(self, request, env_info, security_config): # 实现代码执行逻辑 pass async def cleanup_environment(self, env_info): # 实现环境清理逻辑 pass 监控与管理系统状态监控openeuler-intelligence-sandbox提供了完整的监控接口# 健康检查 curl http://localhost:8000/health # 队列信息 curl http://localhost:8000/queues/info # 执行器状态 curl http://localhost:8000/executors/status # 系统整体状态 curl http://localhost:8000/system/status日志配置服务使用Python标准logging模块日志级别默认为INFO。你可以通过修改 app/service.py 中的日志配置来调整logging.basicConfig( levellogging.DEBUG, # 调整日志级别 format%(asctime)s - %(name)s - %(levelname)s - %(message)s ) 测试与验证运行测试任务使用内置的调试接口提交测试任务curl -X POST http://localhost:8000/debug/submit-test-task?security_levellow单元测试运行完整的测试套件cd tests python run_all_tests.py或者使用pytestpytest tests/ 安全注意事项生产环境部署建议启用HTTPS在生产环境中启用SSL/TLS加密限制CORS配置具体的允许域名监控集成集成Prometheus、Grafana等监控工具日志管理使用结构化日志和日志聚合工具容器化部署使用Docker进行容器化部署负载均衡在多实例前使用负载均衡器安全加固建议定期更新保持依赖库和安全补丁的更新资源限制根据实际需求调整资源限制配置访问控制实现基于角色的访问控制审计日志记录所有代码执行操作 故障排除常见问题解决任务执行超时检查超时配置调整timeout_seconds参数查看资源使用情况使用监控接口检查系统状态容器执行器无法使用检查Kubernetes配置确保配置正确验证服务账户权限确保有足够的权限队列积压调整并发配置增加max_concurrent_tasks检查执行器状态确保执行器正常运行日志查看重要的日志信息会输出到控制台包括任务提交和完成状态执行器启动和配置信息错误和异常信息 学习资源项目结构概览openeuler-intelligence-sandbox/ ├── app/ │ ├── __init__.py │ ├── constant.py # 常量定义 │ ├── entities.py # 数据模型定义 │ ├── executor/ │ │ ├── base.py # 执行器基类 │ │ ├── container_executor.py # 容器执行器 │ │ ├── secure_exec_executor.py # 安全执行器 │ │ └── ... │ ├── executor_manager.py # 执行器管理器 │ ├── queue_manager.py # 队列管理器 │ └── service.py # 主服务 ├── tests/ # 测试文件 ├── requirements.txt # 依赖列表 └── README.md # 项目文档进一步学习查看完整的API文档访问http://localhost:8000/docs阅读源代码注释深入了解实现细节参与社区讨论了解最佳实践和最新更新通过这份快速上手指南你现在应该能够快速部署和使用openeuler-intelligence-sandbox了。这个强大的安全代码执行环境将为你的应用提供可靠的多语言代码执行能力同时确保系统的安全性和稳定性。【免费下载链接】openeuler-intelligence-sandboxCode execution service for openEuler Intelligence supported multiple programming languages项目地址: https://gitcode.com/openeuler/openeuler-intelligence-sandbox创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考