v7.0.0-beta.7 release

This commit is contained in:
HeuJung committed 2026-05-15 19:24:41 +09:00
1 parent c186d9a9c2
commit 0e7fa04b03
36 files changed
+2625 -83

No files matched your search

@@ -0,0 +1,545 @@
<?php
namespace Tests\Feature\Upgrade;
use App\Extension\UpgradeContext;
use App\Upgrades\Data\V7_0_0_beta_7\Migrations\FinalizeOrphanedInstallerRuntime;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
/**
* beta.7 잔존 인스톨러 runtime.php 자동 finalize 회귀 가드 (이슈 #371).
*
* axis 전수:
* 1. runtime.php 존재 + .env 의 INSTALLER_COMPLETED 부재 + state.json 존재
* → .env 머지 + runtime.php 삭제 + state.json 삭제
* 2. runtime.php 존재 + INSTALLER_COMPLETED 부재 + state.json 부재 (멱등)
* → .env 머지 + runtime.php 삭제 + state.json unlink 미호출
* 3. runtime.php 부재 → no-op
* 4. runtime.php 존재 + .env 의 INSTALLER_COMPLETED=true → 보존 + warn
* 5. runtime.php 존재 + .env 부재 + .env.example 만 존재 → .env.example 기반 신규 생성
* 6. runtime.php 형식 불일치 (배열 아님) → runtime.php 보존, skip
* 7. DELETE_INSTALLER_AFTER_COMPLETE 가 false → state.json 보존
*/
class Beta7FinalizeOrphanedInstallerRuntimeTest extends TestCase
{
private string $runtimePath;
private string $envPath;
private string $envExamplePath;
private string $stateJsonPath;
private ?string $originalEnvContent = null;
private bool $originalEnvExisted = false;
private bool $originalStateExisted = false;
private ?string $originalStateContent = null;
private bool $originalRuntimeDirExisted = false;
/**
* @var array<string, string|null> axis 12 가 putenv / $_ENV / $_SERVER 를
* 덮어쓰므로 후속 테스트 / 다음 테스트 파일의 DB 자격증명이 오염되지
* 않도록 tearDown 에서 복원할 ENV 키별 원본 스냅샷.
*/
private array $originalEnvSnapshot = [];
private const ENV_KEYS_FOR_SNAPSHOT = [
'DB_WRITE_HOST', 'DB_WRITE_PORT', 'DB_WRITE_DATABASE', 'DB_WRITE_USERNAME', 'DB_WRITE_PASSWORD',
'DB_READ_HOST', 'DB_READ_PORT', 'DB_READ_DATABASE', 'DB_READ_USERNAME', 'DB_READ_PASSWORD',
'DB_PREFIX', 'APP_KEY',
];
protected function setUp(): void
{
parent::setUp();
$this->runtimePath = base_path('storage/installer/runtime.php');
$this->envPath = base_path('.env');
$this->envExamplePath = base_path('.env.example');
$this->stateJsonPath = base_path('storage/installer-state.json');
// 실제 운영 .env 와 state.json 보존
if (is_file($this->envPath)) {
$this->originalEnvExisted = true;
$this->originalEnvContent = file_get_contents($this->envPath);
}
if (is_file($this->stateJsonPath)) {
$this->originalStateExisted = true;
$this->originalStateContent = file_get_contents($this->stateJsonPath);
}
$this->originalRuntimeDirExisted = is_dir(dirname($this->runtimePath));
// process ENV 원본 스냅샷 — axis 12 가 putenv() 로 자격증명을 갱신하므로
// tearDown 에서 정확히 원본 상태로 복원 (다음 테스트 / 파일 오염 차단)
foreach (self::ENV_KEYS_FOR_SNAPSHOT as $key) {
$value = getenv($key);
$this->originalEnvSnapshot[$key] = $value === false ? null : $value;
}
// installer-state.php 등의 DELETE_INSTALLER_AFTER_COMPLETE 상수가 다른 테스트로
// 이미 정의되어 있을 수 있다. 본 테스트는 그 상수를 신뢰하지 않고 명시 시나리오마다
// 자체 시그널을 사용하므로 추가 정의 불필요.
// DataMigration 은 autoload 대상이 아님 — 동적 require
require_once base_path('upgrades/data/7.0.0-beta.7/migrations/01_FinalizeOrphanedInstallerRuntime.php');
$this->cleanupArtifacts();
}
protected function tearDown(): void
{
$this->cleanupArtifacts();
// .env 복원
if ($this->originalEnvExisted && $this->originalEnvContent !== null) {
file_put_contents($this->envPath, $this->originalEnvContent);
} elseif (! $this->originalEnvExisted && is_file($this->envPath)) {
@unlink($this->envPath);
}
// state.json 복원
if ($this->originalStateExisted && $this->originalStateContent !== null) {
file_put_contents($this->stateJsonPath, $this->originalStateContent);
} elseif (! $this->originalStateExisted && is_file($this->stateJsonPath)) {
@unlink($this->stateJsonPath);
}
// 본 테스트가 생성한 runtime 디렉토리 정리 (원래 없었을 때만)
if (! $this->originalRuntimeDirExisted && is_dir(dirname($this->runtimePath))) {
@rmdir(dirname($this->runtimePath));
}
// process ENV 복원 — axis 12 가 putenv() 로 자격증명을 갱신했어도
// 다음 테스트 / 파일이 정확히 setUp 직전 상태에서 시작하도록 보장.
foreach ($this->originalEnvSnapshot as $key => $value) {
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
} else {
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
}
parent::tearDown();
}
private function cleanupArtifacts(): void
{
if (is_file($this->runtimePath)) {
@unlink($this->runtimePath);
}
}
private function context(): UpgradeContext
{
return new UpgradeContext('7.0.0-beta.6', '7.0.0-beta.7', '7.0.0-beta.7');
}
private function writeRuntime(array $data): void
{
$dir = dirname($this->runtimePath);
if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}
$php = "<?php\n\nreturn ".var_export($data, true).";\n";
file_put_contents($this->runtimePath, $php);
}
private function sampleRuntime(): array
{
return [
'db' => [
'write' => [
'host' => '127.0.0.1',
'port' => '3306',
'database' => 'g7_test_db',
'username' => 'test_user',
'password' => 'test_pass_42',
],
'prefix' => 'g7_',
],
'app' => [
'key' => 'base64:'.base64_encode(random_bytes(32)),
],
'created_at' => date('c'),
];
}
private function writeEnv(string $content): void
{
file_put_contents($this->envPath, $content);
}
// -----------------------------------------------------------------------
// axis 1: 정상 finalize — .env 머지 + runtime 삭제 + state.json 삭제
// -----------------------------------------------------------------------
public function test_finalizes_orphaned_runtime_and_merges_env(): void
{
$runtime = $this->sampleRuntime();
$this->writeRuntime($runtime);
$this->writeEnv("APP_ENV=production\nDB_WRITE_HOST=127.0.0.1\nDB_WRITE_PASSWORD=\nAPP_KEY=\n");
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$this->assertFileDoesNotExist($this->runtimePath, 'runtime.php 가 삭제되어야 함');
$envContent = file_get_contents($this->envPath);
$this->assertStringContainsString('DB_WRITE_DATABASE=g7_test_db', $envContent);
$this->assertStringContainsString('DB_WRITE_USERNAME=test_user', $envContent);
$this->assertStringContainsString('DB_WRITE_PASSWORD="test_pass_42"', $envContent);
$this->assertStringContainsString('APP_KEY=base64:', $envContent);
$this->assertStringContainsString('INSTALLER_COMPLETED=true', $envContent);
}
// -----------------------------------------------------------------------
// axis 2: state.json 부재 시에도 멱등 (.env 머지 + runtime 삭제)
// -----------------------------------------------------------------------
public function test_finalizes_when_state_json_missing(): void
{
$this->writeRuntime($this->sampleRuntime());
$this->writeEnv("APP_ENV=production\nDB_WRITE_HOST=127.0.0.1\n");
if (is_file($this->stateJsonPath)) {
@unlink($this->stateJsonPath);
}
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$this->assertFileDoesNotExist($this->runtimePath);
$envContent = file_get_contents($this->envPath);
$this->assertStringContainsString('INSTALLER_COMPLETED=true', $envContent);
// state.json 은 부재 상태 유지 — 비정상 종료 아님
$this->assertFileDoesNotExist($this->stateJsonPath);
}
// -----------------------------------------------------------------------
// axis 3: runtime.php 부재 → no-op
// -----------------------------------------------------------------------
public function test_noop_when_runtime_absent(): void
{
if (is_file($this->runtimePath)) {
@unlink($this->runtimePath);
}
$originalEnv = "APP_ENV=production\nDB_WRITE_HOST=127.0.0.1\n";
$this->writeEnv($originalEnv);
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
// .env 가 변경되지 않아야 함 — 멱등 no-op
$this->assertSame($originalEnv, file_get_contents($this->envPath));
}
// -----------------------------------------------------------------------
// axis 4: .env 의 INSTALLER_COMPLETED=true + runtime 잔존 → 보존 + warn
// -----------------------------------------------------------------------
public function test_preserves_runtime_when_env_marked_completed(): void
{
$runtime = $this->sampleRuntime();
$this->writeRuntime($runtime);
$envContent = "APP_ENV=production\nINSTALLER_COMPLETED=true\n";
$this->writeEnv($envContent);
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
// runtime 보존 — 자동 삭제 금지
$this->assertFileExists($this->runtimePath);
// .env 변경 없음
$this->assertSame($envContent, file_get_contents($this->envPath));
}
// -----------------------------------------------------------------------
// axis 5: .env 부재 + .env.example 만 존재 → .env.example 기반 신규 생성
// -----------------------------------------------------------------------
public function test_creates_env_from_example_when_env_absent(): void
{
if (! is_file($this->envExamplePath)) {
$this->markTestSkipped('.env.example 이 프로젝트 루트에 없음 — 테스트 건너뜀');
}
$this->writeRuntime($this->sampleRuntime());
if (is_file($this->envPath)) {
@unlink($this->envPath);
}
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$this->assertFileExists($this->envPath, '.env 가 .env.example 기반으로 생성되어야 함');
$envContent = file_get_contents($this->envPath);
$this->assertStringContainsString('DB_WRITE_DATABASE=g7_test_db', $envContent);
$this->assertStringContainsString('INSTALLER_COMPLETED=true', $envContent);
$this->assertFileDoesNotExist($this->runtimePath);
}
// -----------------------------------------------------------------------
// axis 6: runtime.php 가 배열 아닌 형식 → 보존, skip
// -----------------------------------------------------------------------
public function test_preserves_runtime_when_format_invalid(): void
{
$dir = dirname($this->runtimePath);
if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}
// 배열을 반환하지 않는 PHP — null 반환
file_put_contents($this->runtimePath, "<?php\n\nreturn null;\n");
$originalEnv = "APP_ENV=production\n";
$this->writeEnv($originalEnv);
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$this->assertFileExists($this->runtimePath, '형식 불일치 runtime.php 는 보존되어야 함');
$this->assertSame($originalEnv, file_get_contents($this->envPath));
}
// -----------------------------------------------------------------------
// axis 7: .env 의 truthy 변형 ("1", quoted "true") 도 보존 트리거
// -----------------------------------------------------------------------
public function test_preserves_runtime_for_truthy_variants(): void
{
$runtime = $this->sampleRuntime();
$variants = [
'INSTALLER_COMPLETED=1',
'INSTALLER_COMPLETED="true"',
'INSTALLER_COMPLETED=yes',
];
foreach ($variants as $line) {
$this->writeRuntime($runtime);
$envContent = "APP_ENV=production\n{$line}\n";
$this->writeEnv($envContent);
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$this->assertFileExists(
$this->runtimePath,
sprintf('변형 %s 에서 runtime.php 가 보존되어야 함', $line)
);
$this->assertSame(
$envContent,
file_get_contents($this->envPath),
sprintf('변형 %s 에서 .env 가 변경되지 않아야 함', $line)
);
// 다음 반복을 위한 정리
@unlink($this->runtimePath);
}
}
// -----------------------------------------------------------------------
// axis 8: 기존 .env 의 권한/그룹 보존 — CLI 사용자와 PHP-FPM 사용자가 다른 환경
// (자체 구축 서버: jjh:www-data 0640)
// -----------------------------------------------------------------------
public function test_preserves_env_mode_when_env_existed(): void
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Windows: POSIX 권한 시멘틱 미지원 — Linux 환경에서만 검증');
}
$this->writeRuntime($this->sampleRuntime());
$this->writeEnv("APP_ENV=production\nAPP_KEY=\n");
// 자체 구축 서버 시뮬레이션 — 그룹 읽기 허용 (PHP-FPM 이 그룹 멤버로 읽음)
chmod($this->envPath, 0640);
$beforeMode = fileperms($this->envPath) & 0777;
$this->assertSame(0640, $beforeMode, 'precondition: .env 권한 0640');
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$afterMode = fileperms($this->envPath) & 0777;
$this->assertSame(
$beforeMode,
$afterMode,
sprintf('기존 .env 권한(%s) 이 머지 후에도 보존되어야 함 — PHP-FPM 이 계속 읽을 수 있도록', decoct($beforeMode))
);
$this->assertFileDoesNotExist($this->runtimePath);
}
// -----------------------------------------------------------------------
// axis 9: 0600 으로 잠긴 .env 도 그대로 보존 — 본 회귀 (#371) 의 원인은 우리가
// 0600 으로 강제 다운그레이드 한 것이었음. 운영자가 의도해서 0600 을 둔
// 경우 (단일 사용자 환경) 도 같은 정책 — 기존 권한이 SSoT
// -----------------------------------------------------------------------
public function test_preserves_strict_env_mode_unchanged(): void
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Windows: POSIX 권한 시멘틱 미지원');
}
$this->writeRuntime($this->sampleRuntime());
$this->writeEnv("APP_ENV=production\n");
chmod($this->envPath, 0600);
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$this->assertSame(
0600,
fileperms($this->envPath) & 0777,
'운영자가 의도한 0600 권한도 그대로 보존'
);
}
// -----------------------------------------------------------------------
// axis 10: .env 가 새로 생성될 때 default 권한이 0640 — 0600 으로 강제 금지
// (#371 회귀 가드 — PHP-FPM 사용자가 그룹으로만 묶인 환경 보호)
// -----------------------------------------------------------------------
public function test_new_env_uses_group_readable_default_permission(): void
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Windows: POSIX 권한 시멘틱 미지원');
}
if (! is_file($this->envExamplePath)) {
$this->markTestSkipped('.env.example 부재');
}
$this->writeRuntime($this->sampleRuntime());
if (is_file($this->envPath)) {
@unlink($this->envPath);
}
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$mode = fileperms($this->envPath) & 0777;
$this->assertSame(
0640,
$mode,
sprintf(
'.env 신규 생성 시 default 권한은 0640 이어야 함 (PHP-FPM 그룹 읽기 허용). 실제: %s',
decoct($mode)
)
);
}
// -----------------------------------------------------------------------
// axis 11: bootstrap/cache/ 의 그룹을 신규 .env 에 계승 — 자체 구축 서버의
// www-data 그룹 자동 식별. 단일 사용자 환경(카페24)에서는 동일 그룹이
// 유지되므로 회귀 없음
// -----------------------------------------------------------------------
public function test_new_env_inherits_group_from_bootstrap_cache(): void
{
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Windows: POSIX 권한 시멘틱 미지원');
}
if (! is_file($this->envExamplePath)) {
$this->markTestSkipped('.env.example 부재');
}
$bootstrapCache = base_path('bootstrap/cache');
if (! is_dir($bootstrapCache)) {
$this->markTestSkipped('bootstrap/cache 디렉토리 부재');
}
$expectedGid = filegroup($bootstrapCache);
if ($expectedGid === false) {
$this->markTestSkipped('bootstrap/cache filegroup 조회 실패');
}
$this->writeRuntime($this->sampleRuntime());
if (is_file($this->envPath)) {
@unlink($this->envPath);
}
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
$actualGid = filegroup($this->envPath);
// chgrp 가 실패할 수 있는 환경(권한 부족)에서는 그룹이 호출 사용자의 default
// 그룹으로 떨어질 수 있다. 실패해도 머지 자체는 성공해야 하므로 .env 존재만 단언.
$this->assertFileExists($this->envPath);
// 같은 사용자가 bootstrap/cache 의 그룹 멤버인 경우 chgrp 가 성공해 일치해야 함.
// 그렇지 않으면 운영자 수동 보정 안내가 로그에 남는 것까지가 본 axis 의 기대.
if ($actualGid !== $expectedGid) {
$this->markTestSkipped(sprintf(
'chgrp 실패 환경 (실행자가 그룹 %d 멤버 아님) — silent fallback 으로 통과',
$expectedGid
));
}
$this->assertSame($expectedGid, $actualGid, 'bootstrap/cache 그룹이 .env 에 계승되어야 함');
}
// -----------------------------------------------------------------------
// axis 12: 머지 후 부모 프로세스의 process ENV (getenv() / $_ENV / $_SERVER)
// 도 신규 자격증명으로 갱신되어야 함.
//
// 배경 (이슈 #371 후속 회귀):
// beta.4~6 손상 환경에서는 .env 가 .env.example 그대로 (DB_WRITE_USERNAME=root,
// DB_WRITE_PASSWORD= 등) 이고 자격증명은 runtime.php 에 잔존. 부모 프로세스 부팅
// 시점에 Dotenv 가 stale .env 의 ENV (DB_WRITE_USERNAME=root) 를 process ENV
// 에 적재. 마이그레이션이 .env 머지 + runtime.php 삭제 를 수행해도 process ENV
// 는 그대로. 이후 번들 일괄 업데이트 spawn 자식 (BundledExtensionUpdatePrompt 의
// proc_open 5번째 인자 $env = array_merge(getenv(), $_ENV)) 이 부모 stale ENV
// 를 그대로 상속받고, Dotenv::createImmutable() 가 이미 채워진 ENV 를 덮어쓰지
// 않으므로 자식이 stale root/패스워드YES 로 DB 연결 시도 → Access denied.
//
// 본 axis 는 마이그레이션 종료 시점에 getenv()/$_ENV/$_SERVER 가 머지된 신규
// 자격증명을 반영하는지 검증한다. 한시적 보정 — beta.8 이후 환경에서는 .env 가
// 이미 정상이므로 트리거 안 됨.
// -----------------------------------------------------------------------
public function test_refreshes_process_env_with_merged_credentials(): void
{
// .env.example 잔존 상태 시뮬레이션 (DB_WRITE_USERNAME=root, DB_WRITE_PASSWORD=)
$this->writeEnv(
"APP_ENV=production\n"
."DB_WRITE_USERNAME=root\n"
."DB_WRITE_PASSWORD=\n"
."DB_WRITE_DATABASE=laravel\n"
."DB_READ_USERNAME=root\n"
."DB_READ_PASSWORD=\n"
."DB_READ_DATABASE=laravel\n"
);
// 부모 프로세스 ENV 가 stale .env 값으로 채워진 상태 시뮬레이션
putenv('DB_WRITE_USERNAME=root');
putenv('DB_WRITE_PASSWORD=');
putenv('DB_WRITE_DATABASE=laravel');
putenv('DB_READ_USERNAME=root');
putenv('DB_READ_PASSWORD=');
putenv('DB_READ_DATABASE=laravel');
$_ENV['DB_WRITE_USERNAME'] = 'root';
$_ENV['DB_WRITE_PASSWORD'] = '';
$_ENV['DB_WRITE_DATABASE'] = 'laravel';
$_ENV['DB_READ_USERNAME'] = 'root';
$_ENV['DB_READ_PASSWORD'] = '';
$_ENV['DB_READ_DATABASE'] = 'laravel';
$this->writeRuntime($this->sampleRuntime());
(new FinalizeOrphanedInstallerRuntime)->run($this->context());
// 디스크 .env 머지는 기존 axis 1 에서 검증 — 본 axis 는 process ENV 검증
$this->assertSame(
'test_user',
getenv('DB_WRITE_USERNAME'),
'getenv(DB_WRITE_USERNAME) 이 runtime 의 신규 자격증명으로 갱신되어야 함'
);
$this->assertSame(
'test_pass_42',
getenv('DB_WRITE_PASSWORD'),
'getenv(DB_WRITE_PASSWORD) 이 runtime 의 신규 비밀번호로 갱신되어야 함'
);
$this->assertSame(
'g7_test_db',
getenv('DB_WRITE_DATABASE'),
'getenv(DB_WRITE_DATABASE) 이 runtime 의 신규 DB 이름으로 갱신되어야 함'
);
$this->assertSame(
'test_user',
$_ENV['DB_WRITE_USERNAME'] ?? null,
'$_ENV[DB_WRITE_USERNAME] 이 갱신되어야 함 — proc_open ENV 합집합 전파용'
);
$this->assertSame(
'test_pass_42',
$_ENV['DB_WRITE_PASSWORD'] ?? null,
'$_ENV[DB_WRITE_PASSWORD] 도 함께 갱신'
);
// READ 도 동일 정책 — runtime.read 미지정 시 write 값으로 동기화
$this->assertSame('test_user', getenv('DB_READ_USERNAME'));
$this->assertSame('test_pass_42', getenv('DB_READ_PASSWORD'));
}
}
+24
View File
@@ -71,6 +71,9 @@ class UpgradeStepsTest extends TestCase
'2.0.0' => $step_2_0,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -111,6 +114,9 @@ class UpgradeStepsTest extends TestCase
'1.2.0' => $step_1_2,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -139,6 +145,9 @@ class UpgradeStepsTest extends TestCase
'1.1.0' => $callableStep,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -166,6 +175,9 @@ class UpgradeStepsTest extends TestCase
'1.1.0' => $failingStep,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -239,6 +251,9 @@ class UpgradeStepsTest extends TestCase
'1.1.0' => $otherStep,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -270,6 +285,9 @@ class UpgradeStepsTest extends TestCase
'1.0.0-beta.2' => $step_beta2,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -307,6 +325,9 @@ class UpgradeStepsTest extends TestCase
'1.2.0' => $step_1_2,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
@@ -411,6 +432,9 @@ class UpgradeStepsTest extends TestCase
'7.0.0-beta.2' => $step_beta2,
]);
$module->shouldReceive('getIdentifier')->andReturn('test-module');
// ExtensionUpgradeGuardHelper::resolveSinceVersion 가 본 두 메서드를 조회 (a9432939f #347)
$module->shouldReceive('getRequiredCoreVersion')->andReturn(null);
$module->shouldReceive('getVersion')->andReturn('1.0.0');
$manager = $this->createModuleManagerWithMocks();
$method = new \ReflectionMethod($manager, 'runUpgradeSteps');
+69 -2
View File
@@ -57,8 +57,14 @@ class InstallerGuardTest extends TestCase
private function cleanup(): void
{
@unlink(BASE_PATH . '/storage/app/g7_installed');
@unlink(BASE_PATH . '/.env');
$lock = BASE_PATH . '/storage/app/g7_installed';
if (is_file($lock)) {
@unlink($lock);
}
$env = BASE_PATH . '/.env';
if (is_file($env)) {
@unlink($env);
}
}
public function test_no_signals_means_not_completed(): void
@@ -115,4 +121,65 @@ class InstallerGuardTest extends TestCase
$this->assertTrue(installer_is_completed());
}
// ------------------------------------------------------------------------
// finalize 전용 가드 — installer_finalize_is_completed()
//
// 일반 가드와 달리 g7_installed 락 파일은 차단 사유에서 제외되어야 한다
// (complete_flag task 가 락 파일을 먼저 생성한 직후 finalize 가 호출되는
// 설계상의 호출 순서 때문 — 이슈 #371 자가 차단 회귀 가드).
// ------------------------------------------------------------------------
public function test_finalize_guard_passes_with_lock_only(): void
{
// 정상 1회차 finalize 시나리오: 락 파일은 존재하나 .env 머지 아직 안 됨
file_put_contents(BASE_PATH . '/storage/app/g7_installed', '');
$this->assertFalse(
installer_finalize_is_completed(),
'g7_installed 락 파일 단독 존재 시 finalize 차단되면 자가 차단 회귀 (이슈 #371)'
);
}
public function test_finalize_guard_passes_with_no_env_file(): void
{
// .env 자체가 없는 신규 설치 시나리오
$this->assertFalse(installer_finalize_is_completed());
}
public function test_finalize_guard_blocks_when_env_completed_true(): void
{
// 이미 finalize 된 상태 — 멱등 차단
file_put_contents(BASE_PATH . '/.env', "APP_ENV=local\nINSTALLER_COMPLETED=true\n");
$this->assertTrue(installer_finalize_is_completed());
}
public function test_finalize_guard_blocks_with_lock_and_env_completed(): void
{
// 락 + .env true 동시 존재 → 차단 (멱등)
file_put_contents(BASE_PATH . '/storage/app/g7_installed', '');
file_put_contents(BASE_PATH . '/.env', 'INSTALLER_COMPLETED=true' . "\n");
$this->assertTrue(installer_finalize_is_completed());
}
public function test_finalize_guard_passes_when_env_completed_false(): void
{
file_put_contents(BASE_PATH . '/.env', "INSTALLER_COMPLETED=false\n");
$this->assertFalse(installer_finalize_is_completed());
}
public function test_finalize_guard_accepts_quoted_and_alt_truthy_values(): void
{
file_put_contents(BASE_PATH . '/.env', 'INSTALLER_COMPLETED="true"' . "\n");
$this->assertTrue(installer_finalize_is_completed());
file_put_contents(BASE_PATH . '/.env', 'INSTALLER_COMPLETED=1' . "\n");
$this->assertTrue(installer_finalize_is_completed());
file_put_contents(BASE_PATH . '/.env', 'INSTALLER_COMPLETED=yes' . "\n");
$this->assertTrue(installer_finalize_is_completed());
}
}
@@ -73,9 +73,13 @@ class InstallerSecurityHardeningTest extends TestCase
*/
protected function tearDown(): void
{
@unlink(STATE_PATH);
if (is_file(STATE_PATH)) {
@unlink(STATE_PATH);
}
$projectStateFile = dirname(__DIR__, 3) . '/storage/installer-state.json';
@unlink($projectStateFile);
if (is_file($projectStateFile)) {
@unlink($projectStateFile);
}
parent::tearDown();
}
@@ -366,7 +370,7 @@ class InstallerSecurityHardeningTest extends TestCase
'/opt/php/bin/php',
'C:/php/php.exe',
'/nonexistent/path',
// Windows 절대경로 (백슬래시 포함) — 회귀 가드: PO 환경 (Windows 빌트인 서버)
// Windows 절대경로 (백슬래시 포함) — 회귀 가드: Windows 빌트인 서버 환경
'C:\\laragon\\bin\\php\\php-8.3.26-Win32-vs16-x64\\php.exe',
'C:\\php\\php.exe',
'D:\\xampp\\php\\php.exe',
@@ -20,16 +20,44 @@ class InstallerRuntimeServiceProviderTest extends TestCase
private ?string $originalEnv = null;
private string $envPath;
private ?string $originalEnvContent = null;
private bool $originalEnvExisted = false;
/** @var array<string, string|null> */
private array $originalEnvSnapshot = [];
private const ENV_KEYS_FOR_SNAPSHOT = [
'DB_WRITE_HOST', 'DB_WRITE_PORT', 'DB_WRITE_DATABASE', 'DB_WRITE_USERNAME', 'DB_WRITE_PASSWORD',
'DB_READ_HOST', 'DB_READ_PORT', 'DB_READ_DATABASE', 'DB_READ_USERNAME', 'DB_READ_PASSWORD',
'DB_PREFIX', 'APP_KEY',
];
protected function setUp(): void
{
parent::setUp();
$this->runtimePath = base_path('storage/installer/runtime.php');
$this->envPath = base_path('.env');
// 테스트 시작 시 runtime.php 정리 (이전 테스트의 잔재 제거)
if (is_file($this->runtimePath)) {
@unlink($this->runtimePath);
}
// .env 원본 보존
if (is_file($this->envPath)) {
$this->originalEnvExisted = true;
$this->originalEnvContent = file_get_contents($this->envPath);
}
// process ENV 스냅샷 (stale ENV 보정 테스트가 갱신하므로 다음 테스트 격리)
foreach (self::ENV_KEYS_FOR_SNAPSHOT as $key) {
$value = getenv($key);
$this->originalEnvSnapshot[$key] = $value === false ? null : $value;
}
}
protected function tearDown(): void
@@ -49,6 +77,25 @@ class InstallerRuntimeServiceProviderTest extends TestCase
$this->originalEnv = null;
}
// .env 복원
if ($this->originalEnvExisted && $this->originalEnvContent !== null) {
file_put_contents($this->envPath, $this->originalEnvContent);
} elseif (! $this->originalEnvExisted && is_file($this->envPath)) {
@unlink($this->envPath);
}
// process ENV 복원
foreach ($this->originalEnvSnapshot as $key => $value) {
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
} else {
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
}
parent::tearDown();
}
@@ -221,6 +268,161 @@ class InstallerRuntimeServiceProviderTest extends TestCase
$this->assertSame($originalKey, Config::get('app.key'));
}
// -----------------------------------------------------------------------
// beta.7 한시 보정 — beta.4~6 finalize 결함 환경에서 .env 머지 후 spawn 자식의
// stale process ENV (DB_WRITE_USERNAME=root 등) 회귀 차단.
//
// 트리거 조건 (모두 만족 시에만 보정):
// 1. runtime.php 부재 (이미 finalize 완료된 상태)
// 2. .env 존재 + 정합 자격증명 보유 (DB_WRITE_USERNAME 이 root 가 아니고 비어있지 않음)
// 3. process ENV 의 DB_WRITE_USERNAME 이 'root' 또는 빈 값 (stale 시그니처)
// 4. .env 의 INSTALLER_COMPLETED 가 truthy (finalize 성공 신호)
//
// 위 4개를 모두 만족하면 .env 의 DB_* / APP_KEY 를 process ENV (getenv/$_ENV/$_SERVER)
// 에 적용. 정상 환경에서는 #3 (stale 시그니처) 가 false 라 트리거 안 됨.
// -----------------------------------------------------------------------
public function test_recovers_stale_process_env_when_disk_env_is_healthy(): void
{
$this->withProductionEnv();
// 디스크 .env 는 정합 자격증명 + INSTALLER_COMPLETED (finalize 완료 신호)
file_put_contents($this->envPath, implode("\n", [
'APP_ENV=production',
'DB_WRITE_USERNAME=g7_user',
'DB_WRITE_PASSWORD="real_pw"',
'DB_WRITE_DATABASE=g7',
'DB_READ_USERNAME=g7_user',
'DB_READ_PASSWORD="real_pw"',
'DB_READ_DATABASE=g7',
'INSTALLER_COMPLETED=true',
'',
]));
// process ENV 는 stale (Dotenv 가 부팅 시 .env.example fallback 을 적재한 상태)
putenv('DB_WRITE_USERNAME=root');
putenv('DB_WRITE_PASSWORD=');
putenv('DB_READ_USERNAME=root');
putenv('DB_READ_PASSWORD=');
$_ENV['DB_WRITE_USERNAME'] = 'root';
$_ENV['DB_WRITE_PASSWORD'] = '';
$_ENV['DB_READ_USERNAME'] = 'root';
$_ENV['DB_READ_PASSWORD'] = '';
// $_SERVER 도 stale — bootstrap/app.php 의 Env::disablePutenv() 환경에서는
// Dotenv 가 $_ENV/$_SERVER 만 채우므로 셋 다 stale 인 실서버 케이스 재현
$_SERVER['DB_WRITE_USERNAME'] = 'root';
$_SERVER['DB_WRITE_PASSWORD'] = '';
$_SERVER['DB_READ_USERNAME'] = 'root';
$_SERVER['DB_READ_PASSWORD'] = '';
$this->assertFileDoesNotExist($this->runtimePath, 'precondition: runtime.php 부재');
$provider = new InstallerRuntimeServiceProvider($this->app);
$provider->register();
$this->assertSame('g7_user', getenv('DB_WRITE_USERNAME'), 'stale root 가 .env 의 정합 값으로 갱신되어야 함');
$this->assertSame('real_pw', getenv('DB_WRITE_PASSWORD'));
$this->assertSame('g7_user', getenv('DB_READ_USERNAME'));
$this->assertSame('real_pw', getenv('DB_READ_PASSWORD'));
$this->assertSame('g7_user', $_ENV['DB_WRITE_USERNAME'] ?? null);
$this->assertSame('g7_user', $_SERVER['DB_WRITE_USERNAME'] ?? null);
}
public function test_no_recovery_when_process_env_already_healthy(): void
{
$this->withProductionEnv();
file_put_contents($this->envPath, implode("\n", [
'DB_WRITE_USERNAME=g7_user',
'DB_WRITE_PASSWORD="real_pw"',
'INSTALLER_COMPLETED=true',
'',
]));
// 정상 환경 시뮬레이션 — process ENV 가 이미 정합
putenv('DB_WRITE_USERNAME=already_correct');
putenv('DB_WRITE_PASSWORD=already_correct_pw');
$_ENV['DB_WRITE_USERNAME'] = 'already_correct';
$_ENV['DB_WRITE_PASSWORD'] = 'already_correct_pw';
$provider = new InstallerRuntimeServiceProvider($this->app);
$provider->register();
// 보정 트리거 안 됨 — 정합 ENV 보존 (beta.7 보정이 .env 의 g7_user 로 덮어쓰면 안 됨)
$this->assertSame('already_correct', getenv('DB_WRITE_USERNAME'), 'stale 시그니처 아니므로 보정 미발동');
$this->assertSame('already_correct_pw', getenv('DB_WRITE_PASSWORD'));
}
public function test_no_recovery_when_disk_env_also_stale(): void
{
$this->withProductionEnv();
// 디스크 .env 도 stale (finalize 실패 분기 시뮬레이션)
file_put_contents($this->envPath, implode("\n", [
'DB_WRITE_USERNAME=root',
'DB_WRITE_PASSWORD=',
'INSTALLER_COMPLETED=true',
'',
]));
putenv('DB_WRITE_USERNAME=root');
putenv('DB_WRITE_PASSWORD=');
$_ENV['DB_WRITE_USERNAME'] = 'root';
$_ENV['DB_WRITE_PASSWORD'] = '';
$provider = new InstallerRuntimeServiceProvider($this->app);
$provider->register();
// .env 도 stale 이므로 보정 의미 없음 — 빈 값으로 덮어쓰지 않음
$this->assertSame('root', getenv('DB_WRITE_USERNAME'), '.env 도 stale 이면 보정 미발동 (정상 ENV 손상 방지)');
}
public function test_no_recovery_when_installer_not_completed(): void
{
$this->withProductionEnv();
// INSTALLER_COMPLETED 부재 — finalize 미수행 (인스톨러 중간 상태) 시그니처
file_put_contents($this->envPath, implode("\n", [
'DB_WRITE_USERNAME=g7_user',
'DB_WRITE_PASSWORD="real_pw"',
'',
]));
putenv('DB_WRITE_USERNAME=root');
$_ENV['DB_WRITE_USERNAME'] = 'root';
$provider = new InstallerRuntimeServiceProvider($this->app);
$provider->register();
// INSTALLER_COMPLETED 부재 → 보정 미발동 (인스톨러 진행 중일 수 있음, 보수적 보호)
$this->assertSame('root', getenv('DB_WRITE_USERNAME'), 'INSTALLER_COMPLETED 부재 시 보정 미발동');
}
public function test_no_recovery_when_runtime_php_still_present(): void
{
$this->withProductionEnv();
// runtime.php 잔존 — 기존 register() 가 Config 주입을 처리하므로 보정 불필요
$this->writeRuntime([
'db' => ['write' => ['host' => '127.0.0.1', 'username' => 'rt_user', 'password' => 'rt_pw']],
]);
file_put_contents($this->envPath, implode("\n", [
'DB_WRITE_USERNAME=g7_user',
'INSTALLER_COMPLETED=true',
'',
]));
putenv('DB_WRITE_USERNAME=root');
$_ENV['DB_WRITE_USERNAME'] = 'root';
$provider = new InstallerRuntimeServiceProvider($this->app);
$provider->register();
// runtime.php 존재 → 기존 경로 (Config 주입) 가 동작. 본 보정은 트리거 안 됨.
$this->assertSame('root', getenv('DB_WRITE_USERNAME'), 'runtime.php 잔존 시 본 보정 미발동');
}
/**
* @param array<string, mixed> $data
*/
@@ -21,11 +21,24 @@ axes:
env_state: [example_only, partially_filled, fully_finalized]
finalize_call: [success, browser_aborted, retry_after_failure]
rollback_trigger: [none, mid_install, post_finalize]
# 이슈 #371 — finalize 가드 자가 차단 회귀 axis
g7_installed_lock_state: [absent, present_before_finalize]
finalize_guard_signal: [lock_only, env_completed_true_only, lock_and_env_completed_true, neither]
orphaned_recovery_path: [none, upgrade_step_auto_finalize, upgrade_step_warn_manual_review]
# 이슈 #371 — 웹 서버 사용자/그룹 정합 axis (Upgrade Step 의 권한 조정)
cli_vs_phpfpm_user: [same_user, different_user_phpfpm_group_member, different_user_no_group_link]
env_preexisting_mode: [absent, 0600_strict, 0640_group_readable, 0664_group_writable]
bootstrap_cache_owner_group: [phpfpm_user, root_with_phpfpm_group, missing]
# 이슈 #371 — 부모 프로세스 stale ENV 가 spawn 자식에 전파되어
# Access denied 가 발생하던 회귀 차단 axis (한시적 보정 — beta.8 이후 트리거 안 됨)
parent_process_env_state: [matches_runtime, stale_from_env_example_root, stale_partial]
exclusions:
- { runtime_state: absent, env_state: example_only, finalize_call: success, reason: "runtime 부재 시 finalize 는 멱등적 no-op" }
- { runtime_state: present_corrupted, finalize_call: success, reason: "손상된 runtime 은 머지 미수행, runtime 보존" }
- { rollback_trigger: post_finalize, runtime_state: present_with_db_and_key, reason: "finalize 후 rollback 시점에는 runtime.php 가 이미 삭제된 상태" }
- { g7_installed_lock_state: present_before_finalize, finalize_guard_signal: lock_only, finalize_call: success, reason: "이슈 #371 회귀 가드 — 락 단독 시 finalize 통과해야 함 (자가 차단 금지)" }
- { orphaned_recovery_path: upgrade_step_warn_manual_review, env_state: fully_finalized, runtime_state: absent, reason: "정상 finalize 완료 환경은 warn 경로 자체가 트리거되지 않음" }
effects:
- install_step5_sse_does_not_modify_env_file
@@ -88,10 +101,41 @@ effects:
- rollback_complete_flag_removes_g7_installed_marker_when_present
- artisan_serve_workers_restart_at_most_once_after_completion_ui_shown
- non_serve_environments_observe_zero_extra_restart
# 이슈 #371 — finalize 가드 자가 차단 회귀 차단 + 잔존 환경 자동 복구
- finalize_guard_passes_when_only_g7_installed_lock_is_present
- finalize_guard_passes_when_env_lacks_installer_completed_flag
- finalize_guard_blocks_when_env_has_installer_completed_true_idempotent
- finalize_guard_blocks_when_env_has_installer_completed_true_with_quoted_value
- finalize_guard_accepts_alternative_truthy_values_1_and_yes
- upgrade_step_auto_finalizes_orphaned_runtime_when_env_not_marked_completed
- upgrade_step_skips_when_runtime_php_is_absent_idempotent_noop
- upgrade_step_warns_when_env_marked_completed_but_runtime_php_still_exists_no_auto_delete
- upgrade_step_falls_back_to_env_example_when_env_file_is_absent
- upgrade_step_preserves_runtime_php_when_env_write_fails_safe_fallback
- upgrade_step_logs_skip_when_runtime_php_returns_non_array_format_mismatch
- upgrade_step_deletes_state_json_after_merge_when_DELETE_INSTALLER_AFTER_COMPLETE_set
- upgrade_step_preserves_state_json_when_DELETE_INSTALLER_AFTER_COMPLETE_unset
# 이슈 #371 — 웹 서버 권한 자동 조정
- upgrade_step_preserves_existing_env_mode_uid_gid_when_env_existed_before_merge
- upgrade_step_preserves_strict_0600_env_mode_when_owner_intent_present
- upgrade_step_uses_0640_default_permission_for_newly_created_env_not_0600
- upgrade_step_detects_web_server_group_from_bootstrap_cache_for_new_env
- upgrade_step_falls_back_to_storage_logs_when_bootstrap_cache_absent
- upgrade_step_skips_chown_when_lacking_root_privilege_silent_with_log_hint
- upgrade_step_skips_uid_when_bootstrap_cache_owner_is_root_to_avoid_root_owned_env
- upgrade_step_inherits_group_only_when_bootstrap_cache_owner_is_root_but_group_is_not
# 이슈 #371 — 부모 프로세스 process ENV 도 머지된 자격증명으로 갱신하여 spawn 자식 전파 보장
- upgrade_step_refreshes_parent_process_env_with_merged_db_write_credentials
- upgrade_step_refreshes_parent_process_env_with_merged_db_read_credentials
- upgrade_step_skips_env_refresh_when_runtime_credential_keys_are_missing
- upgrade_step_does_not_overwrite_app_key_env_when_runtime_value_lacks_base64_prefix
test_files:
- tests/Unit/Providers/InstallerRuntimeServiceProviderTest.php
- tests/Unit/Installer/InstallerRuntimeHelperTest.php
- tests/Unit/Installer/InstallerGuardTest.php
- tests/Feature/Console/Core/FinalizeOrphanedInstallerRuntimeTest.php
- tests/Feature/Upgrade/Beta7FinalizeOrphanedInstallerRuntimeTest.php
manual_verification:
- scenario: A_artisan_serve
@@ -9,7 +9,7 @@ description: |
비표준 포트 + 일부 브라우저 정책 조합)을 사전 감지하여 운영자에게 명시
안내한다.
본 진단으로는 "설치하기" 버튼 자체를 차단하지 않는다 (PO 결정: 경고만).
본 진단으로는 "설치하기" 버튼 자체를 차단하지 않는다 (정책: 경고만).
검증 시점은 Step 0 1회 한정. 또한 `session.cookie_samesite` 기본값을
Strict → Lax 로 완화하여 차단 케이스 자체를 줄인다 — 진단은 잔여 케이스
대비 안내 역할.