diff --git a/AGENTS.md b/AGENTS.md index 157e4171..233906eb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -149,6 +149,6 @@ - 隧道由 `scripts/keep-upstream-tunnel.ps1` 自动重连,并通过 `scripts/install-upstream-tunnel-task.ps1 -SourceKey <来源标识>` 安装。数据库凭据不得作为隧道参数;每台候选中继开发机使用独立 SSH 私钥,不得多人共享私钥。 - 每个来源最多安排 1 台主中继和 1 台备用中继;同一来源同一时刻只允许一个远端监听成功,备用机低频重试。隧道中断时页面继续读取该来源旧快照,禁止绕过缓存直接重试上游。 - 系统数据库的启动连接只允许存在于服务器私有 `runtime/.env`;超级管理员和普通管理员可在“数据库管理”中修改,但保存前必须验证 TLS、系统必需表和用户数据,并在持久化 `storage` 中生成私有备份。 -- 云数据库连接与系统数据库配置必须分开。显式开启 `UPSTREAM_STORED_CONFIG_ENABLED` 后,云数据库密码使用当前 `APP_KEY` 加密保存到系统数据库的 `system_database_connections`;真实值仍不得进入代码、提交、PR、共享文档或日志。共享系统库的开发环境禁止复制生产 `APP_KEY`,无法解密共享密码时只允许通过本机私有 `DB_<来源>_PASSWORD` 环境变量覆盖,不得用开发机密钥改写共享密码。中继开发机只保留已收紧 ACL 的隧道端点配置,不保存数据库密码。 +- 云数据库连接与系统数据库配置必须分开。显式开启 `UPSTREAM_STORED_CONFIG_ENABLED` 后,云数据库密码使用当前 `APP_KEY` 加密保存到系统数据库的 `system_database_connections`;真实值仍不得进入代码、提交、PR、共享文档或日志。共享系统库的开发环境禁止复制生产 `APP_KEY`,本机直连测试必须显式开启 `UPSTREAM_LOCAL_CONFIG_ENABLED`,并通过私有 `DB_<来源>_*` 环境变量提供完整连接,不得用开发机密钥改写共享密码。中继开发机只保留已收紧 ACL 的隧道端点配置,不保存数据库密码。 详细 SOP 见 `docs/开发与部署SOP.md`。 diff --git a/app/Http/Controllers/SystemSettings/Databases.php b/app/Http/Controllers/SystemSettings/Databases.php index 3106ac47..944a5a36 100644 --- a/app/Http/Controllers/SystemSettings/Databases.php +++ b/app/Http/Controllers/SystemSettings/Databases.php @@ -333,13 +333,12 @@ class Databases extends Controller private function odbcConfiguration(DatabaseConnection $connection): array { - $password = $connection->source_key - ? app(UpstreamSourceRegistry::class)->value( - $connection->source_key, - 'connection.password', - '' - ) - : $connection->password; + $registry = app(UpstreamSourceRegistry::class); + $source = $connection->source_key; + $value = fn (string $path, mixed $default) => $source + ? $registry->value($source, 'connection.' . $path, $default) + : $default; + $password = $value('password', $source ? '' : $connection->password); if (! is_string($password) || $password === '') { throw new DecryptException( @@ -348,15 +347,15 @@ class Databases extends Controller } return [ - 'host' => $connection->host, - 'port' => $connection->port, - 'database' => $connection->database_name, - 'username' => $connection->username, + 'host' => $value('host', $connection->host), + 'port' => $value('port', $connection->port), + 'database' => $value('database', $connection->database_name), + 'username' => $value('username', $connection->username), 'password' => $password, - 'odbc_driver' => $connection->odbc_driver, - 'tds_version' => $connection->tds_version, - 'connect_timeout' => $connection->connect_timeout, - 'query_timeout' => $connection->query_timeout, + 'odbc_driver' => $value('odbc_driver', $connection->odbc_driver), + 'tds_version' => $value('tds_version', $connection->tds_version), + 'connect_timeout' => $value('connect_timeout', $connection->connect_timeout), + 'query_timeout' => $value('query_timeout', $connection->query_timeout), ]; } diff --git a/config/upstreams.php b/config/upstreams.php index 916f13c3..9a1ad57f 100644 --- a/config/upstreams.php +++ b/config/upstreams.php @@ -21,6 +21,11 @@ return [ 'enabled' => filter_var(env('UPSTREAM_STORED_CONFIG_ENABLED', false), FILTER_VALIDATE_BOOL), ], + 'local_configuration' => [ + // 开发机显式开启后完全使用本机私有来源配置,不读取共享密码。 + 'enabled' => filter_var(env('UPSTREAM_LOCAL_CONFIG_ENABLED', false), FILTER_VALIDATE_BOOL), + ], + 'sources' => [ 'jiansun' => [ 'label' => 'JianSun 旧业务库', diff --git a/docs/开发与部署SOP.md b/docs/开发与部署SOP.md index f7f191bf..c5a3b64e 100644 --- a/docs/开发与部署SOP.md +++ b/docs/开发与部署SOP.md @@ -196,6 +196,7 @@ php artisan upstream:snapshots:refresh ```dotenv UPSTREAM_STORED_CONFIG_ENABLED=true +UPSTREAM_LOCAL_CONFIG_ENABLED=false JIANSUN_ENABLED=false JIANSUN_ALLOWED_SCOPES= DB_JIANSUN_HOST= @@ -219,7 +220,7 @@ JIANSUN_SLOW_QUERY_MS=5000 JIANSUN_SLOW_QUERY_COOLDOWN=300 ``` -开发机连接共享系统库时,如果云数据库密码由其他环境的 `APP_KEY` 加密,应用会改用本机私有 `DB_JIANSUN_PASSWORD` 或 `DB_OPERATIONS_REPLICA_PASSWORD`。每位开发者只在自己的 `runtime/.env` 保存真实密码,不在网页中覆盖共享密码;地址、端口、数据库名、账号和允许范围仍由系统库统一维护。生产 `APP_KEY` 和各开发机私有配置不得进入 Git、聊天、日志或截图。 +开发机需要直接使用数据库账号密码测试时,在本机私有 `runtime/.env` 设置 `UPSTREAM_LOCAL_CONFIG_ENABLED=true`,并完整填写 `DB_JIANSUN_*` 与 `DB_OPERATIONS_REPLICA_*`。开启后,该开发环境的上游连接、开关和允许范围全部使用本机配置,不读取共享密码;系统数据库仍只共享用户和系统设置。每位开发者不得在网页中覆盖共享密码,生产 `APP_KEY` 和各开发机私有配置不得进入 Git、聊天、日志或截图。 `JIANSUN_ALLOWED_SCOPES` 支持逗号分隔的精确范围和 `prefix.*`。首次上线先开放完成探测的小表范围,例如 `invoice-deductions.*`;约 280 万行核心表对应范围必须单独验证,未验证前不得填写 `*`。 diff --git a/modules/EcommerceFinance/Services/UpstreamSourceRegistry.php b/modules/EcommerceFinance/Services/UpstreamSourceRegistry.php index e6926e77..93e69343 100644 --- a/modules/EcommerceFinance/Services/UpstreamSourceRegistry.php +++ b/modules/EcommerceFinance/Services/UpstreamSourceRegistry.php @@ -51,6 +51,10 @@ class UpstreamSourceRegistry $key = $definition['config'] . ($suffix === '' ? '' : '.' . $suffix); $configured = config($key, $default); + if ($this->usesLocalConfiguration()) { + return $configured; + } + return $this->storedConfiguration->value($definition['key'], $suffix, $configured); } @@ -58,4 +62,10 @@ class UpstreamSourceRegistry { return (bool) $this->value($source, 'enabled', false); } + + private function usesLocalConfiguration(): bool + { + return app()->environment(['local', 'development', 'testing']) + && (bool) config('upstreams.local_configuration.enabled', false); + } } diff --git a/tests/Feature/SystemSettings/UpstreamDatabasesTest.php b/tests/Feature/SystemSettings/UpstreamDatabasesTest.php index 6834e7ff..20096316 100644 --- a/tests/Feature/SystemSettings/UpstreamDatabasesTest.php +++ b/tests/Feature/SystemSettings/UpstreamDatabasesTest.php @@ -90,16 +90,24 @@ class UpstreamDatabasesTest extends FeatureTestCase 'encrypter', new Encrypter(str_repeat('x', 32), (string) config('app.cipher')) ); - config(['jiansun.connection.password' => 'local-secret']); + config([ + 'upstreams.local_configuration.enabled' => true, + 'jiansun.connection.host' => 'local-db.test', + 'jiansun.connection.database' => 'local_database', + 'jiansun.connection.username' => 'local_user', + 'jiansun.connection.password' => 'local-secret', + 'jiansun.connection.port' => 11433, + ]); + $sources = new UpstreamSourceRegistry(new UpstreamStoredConfiguration()); $this->assertSame( 'local-secret', - (new UpstreamStoredConfiguration())->value( - 'jiansun', - 'connection.password', - config('jiansun.connection.password') - ) + $sources->value('jiansun', 'connection.password') ); + $this->assertSame('local-db.test', $sources->value('jiansun', 'connection.host')); + $this->assertSame('local_database', $sources->value('jiansun', 'connection.database')); + $this->assertSame('local_user', $sources->value('jiansun', 'connection.username')); + $this->assertSame(11433, $sources->value('jiansun', 'connection.port')); } finally { $this->app->instance('encrypter', $originalEncrypter); }