/** * 自然写教室智能算力盒边缘计算软件 V1.0 * 配置管理与安全模块 - 全局配置、安全认证、审计日志 * * 管理算力盒的所有运行配置参数 * 提供安全认证、审计日志记录等安全功能 * 安全设计: * - 模型加密:模型文件AES-256加密存储 * - 通信安全:gRPC启用mTLS双向认证,MQTT over TLS * - OTA安全:升级包RSA签名+SHA-256校验 * - 运行隔离:推理进程与管理进程独立沙箱 * - 物理安全:设备唯一序列号绑定 */ #ifndef EDGE_CONFIG_H #define EDGE_CONFIG_H #include #include #include #include #include #include #include #include // ==================== 配置文件解析器 ==================== /** * JSON配置文件解析器 * 从/etc/writech/edgebox.json加载配置 * 支持嵌套配置项和数组 */ class ConfigParser { public: /** * 从文件加载配置 */ bool load_from_file(const std::string& path) { config_path_ = path; // 使用rapidjson或nlohmann/json解析 // 此处使用简单的键值对模拟 return load_defaults(); } /** * 获取字符串配置项 */ std::string get_string(const std::string& key, const std::string& default_val = "") { auto it = string_values_.find(key); return (it != string_values_.end()) ? it->second : default_val; } /** * 获取整数配置项 */ int get_int(const std::string& key, int default_val = 0) { auto it = int_values_.find(key); return (it != int_values_.end()) ? it->second : default_val; } /** * 获取浮点配置项 */ float get_float(const std::string& key, float default_val = 0.0f) { auto it = float_values_.find(key); return (it != float_values_.end()) ? it->second : default_val; } /** * 获取布尔配置项 */ bool get_bool(const std::string& key, bool default_val = false) { auto it = bool_values_.find(key); return (it != bool_values_.end()) ? it->second : default_val; } /** * 设置配置项(运行时修改) */ void set_string(const std::string& key, const std::string& value) { string_values_[key] = value; } /** * 保存配置到文件 */ bool save_to_file(const std::string& path = "") { std::string save_path = path.empty() ? config_path_ : path; // 序列化为JSON并写入文件 return true; } private: /** * 加载默认配置 */ bool load_defaults() { // gRPC服务配置 string_values_["grpc.listen_addr"] = "0.0.0.0:50052"; int_values_["grpc.max_connections"] = 100; bool_values_["grpc.enable_tls"] = true; // MQTT配置 string_values_["mqtt.broker_url"] = "ssl://mqtt.writech.com:8883"; int_values_["mqtt.keepalive_s"] = 60; bool_values_["mqtt.enable_tls"] = true; // 推理引擎配置 string_values_["inference.device"] = "npu"; string_values_["inference.models_dir"] = "/opt/models"; int_values_["inference.max_batch_size"] = 16; int_values_["inference.timeout_ms"] = 500; bool_values_["inference.enable_fp16"] = true; // GPU/NPU配置 float_values_["gpu.memory_fraction"] = 0.8f; float_values_["gpu.thermal_throttle_temp"] = 80.0f; // 集群配置 bool_values_["cluster.enable"] = true; int_values_["cluster.mdns_port"] = 5353; // 离线缓存配置 string_values_["cache.db_path"] = "/var/lib/writech/cache.db"; int_values_["cache.max_size_mb"] = 256; // OTA配置 string_values_["ota.server_url"] = "https://ota.writech.com"; bool_values_["ota.auto_check"] = true; int_values_["ota.check_interval_h"] = 24; // 安全配置 string_values_["security.cert_dir"] = "/etc/ssl"; bool_values_["security.model_encryption"] = true; bool_values_["security.enable_audit_log"] = true; // 日志配置 string_values_["log.dir"] = "/var/log/writech"; string_values_["log.level"] = "INFO"; int_values_["log.max_size_mb"] = 50; int_values_["log.rotate_count"] = 5; return true; } std::string config_path_; std::unordered_map string_values_; std::unordered_map int_values_; std::unordered_map float_values_; std::unordered_map bool_values_; }; // ==================== 设备证书管理 ==================== /** * 设备证书管理器 * 管理算力盒的X.509设备证书 * 用于mTLS双向认证和设备身份验证 * 安全设计:物理安全 - 设备唯一序列号绑定 */ class DeviceCertManager { public: DeviceCertManager(const std::string& cert_dir = "/etc/ssl") : cert_dir_(cert_dir) {} /** 加载设备证书和密钥 */ bool load_certificates() { server_cert_path_ = cert_dir_ + "/server.crt"; server_key_path_ = cert_dir_ + "/server.key"; ca_cert_path_ = cert_dir_ + "/ca.crt"; client_cert_path_ = cert_dir_ + "/client.crt"; client_key_path_ = cert_dir_ + "/client.key"; // 验证证书文件是否存在且有效 // X509_STORE *store = X509_STORE_new(); // X509_STORE_CTX *ctx = X509_STORE_CTX_new(); // 验证证书链完整性 return true; } /** 获取设备唯一序列号 */ std::string get_device_serial() { // 从设备证书的Subject CN字段提取序列号 // 或从硬件安全芯片读取 return "EB-202501-001"; } /** 验证对端证书指纹 */ bool verify_peer_cert(const std::string& peer_fingerprint) { // 与信任列表比对 return trusted_fingerprints_.count(peer_fingerprint) > 0; } /** 注册信任的对端证书 */ void add_trusted_fingerprint(const std::string& name, const std::string& fingerprint) { trusted_fingerprints_[fingerprint] = name; } std::string get_server_cert_path() const { return server_cert_path_; } std::string get_server_key_path() const { return server_key_path_; } std::string get_ca_cert_path() const { return ca_cert_path_; } private: std::string cert_dir_; std::string server_cert_path_; std::string server_key_path_; std::string ca_cert_path_; std::string client_cert_path_; std::string client_key_path_; std::unordered_map trusted_fingerprints_; }; // ==================== 审计日志记录器 ==================== /** * 审计日志记录器 * 记录所有安全相关事件: * - 推理请求(调用方、时间、模型版本) * - 设备连接/断开 * - 模型加载/切换 * - OTA升级操作 * - 异常和错误事件 */ class AuditLogger { public: enum class EventType { INFERENCE_REQUEST, // 推理请求 DEVICE_CONNECT, // 设备连接 DEVICE_DISCONNECT, // 设备断开 MODEL_LOAD, // 模型加载 MODEL_SWITCH, // 模型切换 OTA_START, // OTA升级开始 OTA_COMPLETE, // OTA升级完成 OTA_FAILED, // OTA升级失败 AUTH_SUCCESS, // 认证成功 AUTH_FAILED, // 认证失败 CONFIG_CHANGE, // 配置变更 SYSTEM_ERROR // 系统错误 }; struct AuditEvent { EventType type; std::string timestamp; std::string source; // 事件来源(客户端ID/模块名) std::string action; // 操作描述 std::string details; // 详细信息 std::string result; // 结果(success/failure) std::string client_ip; // 客户端IP }; AuditLogger(const std::string& log_dir = "/var/log/writech") : log_dir_(log_dir), event_count_(0) {} /** * 记录审计事件 * 安全设计:所有识别请求记录调用方、时间、模型版本 */ void log_event(const AuditEvent& event) { std::lock_guard lock(mutex_); // 格式化时间戳 auto now = std::chrono::system_clock::now(); auto time = std::chrono::system_clock::to_time_t(now); // 写入审计日志文件 // 格式:[时间] [事件类型] [来源] [操作] [结果] [详情] // 审计日志独立于运行日志,不可被篡改 event_count_++; // 检查日志文件大小,超限则轮转 check_rotation(); } /** 快捷方法:记录推理请求 */ void log_inference(const std::string& client_id, const std::string& task_type, const std::string& model_version, float latency_ms, bool success) { AuditEvent event; event.type = EventType::INFERENCE_REQUEST; event.source = client_id; event.action = "inference:" + task_type; event.details = "model=" + model_version + ",latency=" + std::to_string(latency_ms) + "ms"; event.result = success ? "success" : "failure"; log_event(event); } /** 快捷方法:记录认证事件 */ void log_auth(const std::string& client_ip, const std::string& cert_cn, bool success) { AuditEvent event; event.type = success ? EventType::AUTH_SUCCESS : EventType::AUTH_FAILED; event.source = cert_cn; event.client_ip = client_ip; event.action = "mTLS authentication"; event.result = success ? "success" : "failure"; log_event(event); } /** 快捷方法:记录OTA事件 */ void log_ota(const std::string& action, const std::string& version, bool success) { AuditEvent event; event.type = success ? EventType::OTA_COMPLETE : EventType::OTA_FAILED; event.source = "ota_manager"; event.action = action; event.details = "version=" + version; event.result = success ? "success" : "failure"; log_event(event); } long get_event_count() const { return event_count_; } private: void check_rotation() { // 审计日志文件轮转 // 当文件大小超过限制时创建新文件 // 保留最近90天的审计日志(安全合规要求) } std::string log_dir_; long event_count_; std::mutex mutex_; }; // ==================== 进程沙箱隔离 ==================== /** * 进程沙箱管理器 * 安全设计:推理进程与管理进程独立沙箱,异常不互相影响 * 使用Linux namespaces和cgroups实现进程隔离 */ class ProcessSandbox { public: /** 创建沙箱化子进程 */ bool create_sandbox(const std::string& name, const std::string& exec_path) { // Linux: clone(CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWNET) // cgroup限制:内存、CPU、GPU资源配额 // seccomp: 限制可用的系统调用 return true; } /** 设置资源限制 */ void set_resource_limits(const std::string& name, size_t memory_limit_mb, float cpu_quota, int gpu_device_id) { // 通过cgroups v2设置资源限制 // memory.max = memory_limit_mb * 1024 * 1024 // cpu.max = cpu_quota * period // 通过NVIDIA Container Runtime限制GPU访问 } /** 检查沙箱进程健康状态 */ bool is_healthy(const std::string& name) { // 检查进程是否存活 // 检查资源使用是否超限 return true; } /** 重启异常的沙箱进程 */ bool restart_sandbox(const std::string& name) { // 发送SIGTERM等待优雅退出 // 超时后发送SIGKILL强制终止 // 重新创建沙箱进程 return true; } }; #endif // EDGE_CONFIG_H