Skip to content

Commit fd1513a

Browse files
committed
Handle key generation when key is not present in .env
1 parent 46eb15c commit fd1513a

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

system/Commands/Encryption/GenerateKey.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ class GenerateKey extends BaseCommand
6767
public function run(array $params)
6868
{
6969
$prefix = $params['prefix'] ?? CLI::getOption('prefix');
70+
7071
if (in_array($prefix, [null, true], true)) {
7172
$prefix = 'hex2bin';
7273
} elseif (! in_array($prefix, ['hex2bin', 'base64'], true)) {
7374
$prefix = CLI::prompt('Please provide a valid prefix to use.', ['hex2bin', 'base64'], 'required'); // @codeCoverageIgnore
7475
}
7576

7677
$length = $params['length'] ?? CLI::getOption('length');
78+
7779
if (in_array($length, [null, true], true)) {
7880
$length = 32;
7981
}
@@ -127,9 +129,7 @@ protected function setNewEncryptionKey(string $key, array $params): bool
127129

128130
if ($currentKey !== '' && ! $this->confirmOverwrite($params)) {
129131
// Not yet testable since it requires keyboard input
130-
// @codeCoverageIgnoreStart
131-
return false;
132-
// @codeCoverageIgnoreEnd
132+
return false; // @codeCoverageIgnore
133133
}
134134

135135
return $this->writeNewEncryptionKeyToFile($currentKey, $key);
@@ -163,13 +163,18 @@ protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey):
163163
copy($baseEnv, $envFile);
164164
}
165165

166-
$ret = file_put_contents($envFile, preg_replace(
167-
$this->keyPattern($oldKey),
168-
"\nencryption.key = {$newKey}",
169-
file_get_contents($envFile)
170-
));
166+
$envFileContents = (string) file_get_contents($envFile);
167+
$replacementKey = "\nencryption.key = {$newKey}";
171168

172-
return $ret !== false;
169+
if (strpos($envFileContents, 'encryption.key') === false) {
170+
return file_put_contents($envFile, $replacementKey, FILE_APPEND) !== false;
171+
}
172+
173+
return file_put_contents($envFile, preg_replace(
174+
$this->keyPattern($oldKey),
175+
$replacementKey,
176+
$envFileContents
177+
)) !== false;
173178
}
174179

175180
/**

tests/system/Commands/GenerateKeyTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ protected function resetEnvironment()
7474

7575
public function testGenerateKeyShowsEncodedKey()
7676
{
77-
command('key:generate -show');
77+
command('key:generate --show');
7878
$this->assertStringContainsString('hex2bin:', $this->getBuffer());
7979

80-
command('key:generate -prefix base64 -show');
80+
command('key:generate --prefix base64 --show');
8181
$this->assertStringContainsString('base64:', $this->getBuffer());
8282

83-
command('key:generate -prefix hex2bin -show');
83+
command('key:generate --prefix hex2bin --show');
8484
$this->assertStringContainsString('hex2bin:', $this->getBuffer());
8585
}
8686

@@ -95,12 +95,12 @@ public function testGenerateKeyCreatesNewKey()
9595
$this->assertStringContainsString(env('encryption.key'), file_get_contents($this->envPath));
9696
$this->assertStringContainsString('hex2bin:', file_get_contents($this->envPath));
9797

98-
command('key:generate -prefix base64 -force');
98+
command('key:generate --prefix base64 --force');
9999
$this->assertStringContainsString('successfully set.', $this->getBuffer());
100100
$this->assertStringContainsString(env('encryption.key'), file_get_contents($this->envPath));
101101
$this->assertStringContainsString('base64:', file_get_contents($this->envPath));
102102

103-
command('key:generate -prefix hex2bin -force');
103+
command('key:generate --prefix hex2bin --force');
104104
$this->assertStringContainsString('successfully set.', $this->getBuffer());
105105
$this->assertStringContainsString(env('encryption.key'), file_get_contents($this->envPath));
106106
$this->assertStringContainsString('hex2bin:', file_get_contents($this->envPath));
@@ -115,4 +115,17 @@ public function testDefaultShippedEnvIsMissing()
115115
$this->assertStringContainsString('Both default shipped', $this->getBuffer());
116116
$this->assertStringContainsString('Error in setting', $this->getBuffer());
117117
}
118+
119+
/**
120+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6838
121+
*/
122+
public function testKeyGenerateWhenKeyIsMissingInDotEnvFile()
123+
{
124+
file_put_contents($this->envPath, '');
125+
126+
command('key:generate');
127+
128+
$this->assertStringContainsString('Application\'s new encryption key was successfully set.', $this->getBuffer());
129+
$this->assertSame("\nencryption.key = " . env('encryption.key'), file_get_contents($this->envPath));
130+
}
118131
}

0 commit comments

Comments
 (0)