Skip to content

Commit 6086bf1

Browse files
committed
bug fixes.
1 parent d6ea23e commit 6086bf1

5 files changed

Lines changed: 176 additions & 4 deletions

File tree

src/Cli/Commands/Secrets/EditCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function execute(): int
5656
{
5757
$configPath = $this->input->getOption( 'config', 'config' );
5858
$env = $this->input->getOption( 'env' );
59-
$editor = $this->input->getOption( 'editor' ) ?? $_ENV['EDITOR'] ?? 'vi';
59+
$editor = $this->input->getOption( 'editor' ) ?? getenv( 'EDITOR' ) ?: 'vi';
6060

6161
// Determine paths based on environment
6262
if( $env )
@@ -94,7 +94,7 @@ public function execute(): int
9494
}
9595
}
9696

97-
$key = $this->secretManager->generateKey( $keyPath );
97+
$this->secretManager->generateKey( $keyPath );
9898
$this->output->success( "Generated new key at: {$keyPath}" );
9999
$this->output->warning( "IMPORTANT: Add {$keyPath} to .gitignore!" );
100100
}

src/Cli/Commands/Secrets/Key/GenerateCommand.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ public function execute(): int
8181
{
8282
$keyPath = $configPath . '/master.key';
8383
$keyName = 'master key';
84+
85+
// Ensure directory exists
86+
$dir = dirname( $keyPath );
87+
if( !is_dir( $dir ) )
88+
{
89+
if( !mkdir( $dir, 0755, true ) )
90+
{
91+
$this->output->error( "Failed to create directory: {$dir}" );
92+
return 1;
93+
}
94+
}
8495
}
8596

8697
// Check if key already exists
@@ -137,7 +148,14 @@ public function execute(): int
137148
) . '_KEY';
138149
$this->output->newLine();
139150
$this->output->info( "Alternative: Set the key as an environment variable:" );
140-
$this->output->write( "export {$envVar}={$key}" );
151+
if( $show )
152+
{
153+
$this->output->write( "export {$envVar}={$key}" );
154+
}
155+
else
156+
{
157+
$this->output->write( "export {$envVar}=<KEY_FROM_{$keyPath}>" );
158+
}
141159
}
142160
catch( \Exception $e )
143161
{

src/Cli/Commands/Secrets/ShowCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ private function checkEnvironmentKey( string $keyPath ): bool
163163
str_replace( ['/', '.', '-'], '_', basename( $keyPath, '.key' ) )
164164
) . '_KEY';
165165

166-
return isset( $_ENV[$envKey] );
166+
return getenv( $envKey ) !== false;
167167
}
168168
}

tests/Cli/Commands/Secrets/Key/GenerateCommandTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,111 @@ public function testExecuteGeneratesEnvironmentKey(): void
121121
$this->assertStringContainsString( "Generated production environment key at: {$keyPath}", $outputContent );
122122
}
123123

124+
/**
125+
* Test that config directory is created for master key when missing
126+
*/
127+
public function testExecuteCreatesMasterKeyDirectory(): void
128+
{
129+
// Use a non-existent config path
130+
$nonExistentPath = sys_get_temp_dir() . '/test_config_' . uniqid() . '/config';
131+
132+
// Create input with options pointing to non-existent path
133+
$input = new Input( [ '--config=' . $nonExistentPath ] );
134+
$input->parse( $this->command );
135+
136+
// Create output
137+
$output = new Output( false );
138+
139+
$this->command->setInput( $input );
140+
$this->command->setOutput( $output );
141+
142+
// Capture output
143+
ob_start();
144+
$result = $this->command->execute();
145+
$outputContent = ob_get_clean();
146+
147+
// Execute should succeed
148+
$this->assertEquals( 0, $result );
149+
150+
// Directory should be created
151+
$this->assertDirectoryExists( $nonExistentPath );
152+
153+
// Key file should exist
154+
$keyPath = $nonExistentPath . '/master.key';
155+
$this->assertFileExists( $keyPath );
156+
157+
// Check output contains success message
158+
$this->assertStringContainsString( "Generated master key at: {$keyPath}", $outputContent );
159+
160+
// Clean up
161+
unlink( $keyPath );
162+
rmdir( $nonExistentPath );
163+
rmdir( dirname( $nonExistentPath ) );
164+
}
165+
166+
/**
167+
* Test that key is only shown when --show flag is used
168+
*/
169+
public function testKeyOnlyShownWithShowFlag(): void
170+
{
171+
// Test WITHOUT --show flag (default)
172+
$input = new Input( [ '--config=' . $this->testConfigPath ] );
173+
$input->parse( $this->command );
174+
175+
$output = new Output( false );
176+
177+
$this->command->setInput( $input );
178+
$this->command->setOutput( $output );
179+
180+
ob_start();
181+
$result = $this->command->execute();
182+
$outputContentNoShow = ob_get_clean();
183+
184+
$this->assertEquals( 0, $result );
185+
186+
$keyPath = $this->testConfigPath . '/master.key';
187+
$this->assertFileExists( $keyPath );
188+
189+
// Read the actual key
190+
$actualKey = file_get_contents( $keyPath );
191+
192+
// Key should NOT be in the output (except in the placeholder)
193+
$this->assertStringNotContainsString( "export NEURON_MASTER_KEY={$actualKey}", $outputContentNoShow );
194+
$this->assertStringContainsString( "export NEURON_MASTER_KEY=<KEY_FROM_{$keyPath}>", $outputContentNoShow );
195+
$this->assertStringNotContainsString( "Generated Key", $outputContentNoShow );
196+
197+
// Clean up before second test
198+
unlink( $keyPath );
199+
200+
// Test WITH --show flag
201+
$input2 = new Input( [
202+
'--config=' . $this->testConfigPath,
203+
'--show'
204+
] );
205+
$input2->parse( $this->command );
206+
207+
$output2 = new Output( false );
208+
209+
$this->command->setInput( $input2 );
210+
$this->command->setOutput( $output2 );
211+
212+
ob_start();
213+
$result2 = $this->command->execute();
214+
$outputContentWithShow = ob_get_clean();
215+
216+
$this->assertEquals( 0, $result2 );
217+
218+
// Read the new key
219+
$actualKey2 = file_get_contents( $keyPath );
220+
221+
// Key SHOULD be in the output
222+
$this->assertStringContainsString( "export NEURON_MASTER_KEY={$actualKey2}", $outputContentWithShow );
223+
$this->assertStringNotContainsString( "<KEY_FROM_", $outputContentWithShow );
224+
$this->assertStringContainsString( "Generated Key", $outputContentWithShow );
225+
$this->assertStringContainsString( $actualKey2, $outputContentWithShow );
226+
$this->assertStringContainsString( "This key is shown only once", $outputContentWithShow );
227+
}
228+
124229
/**
125230
* Test error when key already exists without force
126231
*/

tests/Cli/Commands/Secrets/ShowCommandTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,55 @@ public function testExecuteProductionConfirmation(): void
233233
$this->assertStringContainsString( 'production_secret', $outputContent3 );
234234
}
235235

236+
/**
237+
* Test showing secrets using environment variable for key
238+
*/
239+
public function testExecuteWithEnvironmentVariable(): void
240+
{
241+
// Create test secrets
242+
$keyPath = $this->testConfigPath . '/master.key';
243+
$credentialsPath = $this->testConfigPath . '/secrets.yml.enc';
244+
245+
// Generate a key and store it
246+
$key = $this->secretManager->generateKey( $keyPath );
247+
248+
// Create encrypted secrets
249+
$tempPlaintextPath = $this->testConfigPath . '/temp_plaintext.yml';
250+
$testData = "database:\n host: localhost\n password: env_secret";
251+
file_put_contents( $tempPlaintextPath, $testData );
252+
$this->secretManager->encrypt( $tempPlaintextPath, $credentialsPath, $keyPath );
253+
unlink( $tempPlaintextPath );
254+
255+
// Read the key value and delete the key file
256+
$keyValue = trim( file_get_contents( $keyPath ) );
257+
unlink( $keyPath );
258+
259+
// Set the key as an environment variable
260+
putenv( "NEURON_MASTER_KEY={$keyValue}" );
261+
262+
// Create input and output
263+
$input = new Input( [ '--config=' . $this->testConfigPath ] );
264+
$input->parse( $this->command );
265+
266+
$output = new Output( false );
267+
268+
$this->command->setInput( $input );
269+
$this->command->setOutput( $output );
270+
271+
// Capture output
272+
ob_start();
273+
$result = $this->command->execute();
274+
$outputContent = ob_get_clean();
275+
276+
// Should succeed using the environment variable
277+
$this->assertEquals( 0, $result );
278+
$this->assertStringContainsString( 'Base Secrets', $outputContent );
279+
$this->assertStringContainsString( 'env_secret', $outputContent );
280+
281+
// Clean up environment variable
282+
putenv( "NEURON_MASTER_KEY" );
283+
}
284+
236285
/**
237286
* Test error when secrets file not found
238287
*/

0 commit comments

Comments
 (0)