Skip to content

Commit db8a0eb

Browse files
committed
improve UX in key:rotate command
1 parent 8ad03eb commit db8a0eb

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

system/Commands/Encryption/RotateKey.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ protected function execute(array $arguments, array $options): int
103103

104104
if ($options['force'] === false) {
105105
if ($this->isInteractive()) {
106-
CLI::error('Key rotation cancelled.');
107-
} else {
108-
CLI::error('Key rotation aborted.');
109-
CLI::error('If you want, use the "--force" option to force the rotation.');
106+
CLI::write('Key rotation cancelled.', 'yellow');
107+
108+
return EXIT_SUCCESS;
110109
}
111110

111+
CLI::error('Key rotation aborted: pass --force to rotate the encryption key in non-interactive mode.');
112+
112113
return EXIT_ERROR;
113114
}
114115

@@ -133,8 +134,10 @@ protected function execute(array $arguments, array $options): int
133134
// Write previousKeys first. If the subsequent `key:generate` call fails,
134135
// the worst case is a stale-but-still-decryptable `.env` (the rotated-out
135136
// key is preserved on disk).
136-
if (! $this->writePreviousKeys($previousKeys)) {
137-
CLI::error('Error in writing `encryption.previousKeys` to `.env` file.');
137+
$envFile = ((new Paths())->envDirectory ?? ROOTPATH) . '.env'; // @phpstan-ignore nullCoalesce.property
138+
139+
if (! $this->writePreviousKeys($previousKeys, $envFile)) {
140+
CLI::error(sprintf('Failed to write `encryption.previousKeys` to %s.', realpath($envFile) ?: $envFile));
138141

139142
return EXIT_ERROR;
140143
}
@@ -225,10 +228,8 @@ private function mergePreviousKeys(string $currentKey, array $existing, int $kee
225228
*
226229
* @param list<string> $previousKeys
227230
*/
228-
private function writePreviousKeys(array $previousKeys): bool
231+
private function writePreviousKeys(array $previousKeys, string $envFile): bool
229232
{
230-
$envFile = ((new Paths())->envDirectory ?? ROOTPATH) . '.env'; // @phpstan-ignore nullCoalesce.property
231-
232233
if (! is_file($envFile)) {
233234
return false; // @codeCoverageIgnore
234235
}

tests/system/Commands/Encryption/RotateKeyTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function testRotateErrorsWhenNoCurrentKey(): void
229229
$this->assertStringNotContainsString('encryption.previousKeys', (string) file_get_contents($this->envPath));
230230
}
231231

232-
public function testRotateAbortsWhenOverwritePromptIsDeclined(): void
232+
public function testRotateCancelsWhenOverwritePromptIsDeclined(): void
233233
{
234234
$this->seedEnv(self::SEED_KEY);
235235

@@ -274,7 +274,7 @@ public function testRotateOverwritesWhenOverwritePromptIsConfirmed(): void
274274
$this->assertSame(self::SEED_KEY, env('encryption.previousKeys'));
275275
}
276276

277-
public function testRotateAbortsNonInteractivelyAndHintsAboutForceFlag(): void
277+
public function testRotateAbortsNonInteractively(): void
278278
{
279279
$this->seedEnv(self::SEED_KEY);
280280

@@ -283,8 +283,7 @@ public function testRotateAbortsNonInteractivelyAndHintsAboutForceFlag(): void
283283
$this->assertSame(
284284
<<<'EOT'
285285
286-
Key rotation aborted.
287-
If you want, use the "--force" option to force the rotation.
286+
Key rotation aborted: pass --force to rotate the encryption key in non-interactive mode.
288287

289288
EOT,
290289
$this->getUndecoratedBuffer(),
@@ -525,11 +524,14 @@ public function testRotateErrorsWhenEnvFileIsNotWritable(): void
525524
command('key:rotate --force');
526525

527526
$this->assertSame(
528-
<<<'EOT'
527+
sprintf(
528+
<<<'EOT'
529529
530-
Error in writing `encryption.previousKeys` to `.env` file.
530+
Failed to write `encryption.previousKeys` to %s.
531531

532-
EOT,
532+
EOT,
533+
$this->envPath,
534+
),
533535
$this->getUndecoratedBuffer(),
534536
);
535537
$this->assertSame(self::SEED_KEY, env('encryption.key'));

0 commit comments

Comments
 (0)