Skip to content

Commit 1dff0a5

Browse files
committed
Prevent generating a kernel_secret in the standalone image if it already exists or is defined through an environment variable
1 parent 91d3fe4 commit 1dff0a5

File tree

6 files changed

+101
-4
lines changed

6 files changed

+101
-4
lines changed

docker/config.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
parameters:
2+
kernel_secret: '%env(default:kernel_secret_file:KERNEL_SECRET)%'
3+
kernel_secret_file: '%env(default::file:KERNEL_SECRET_FILE)%'
4+
15
framework:
2-
secret: '%env(file:KERNEL_SECRET_FILE)%'
6+
secret: '%kernel_secret%'

docker/scripts/init/10-kernel-secret.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
set -e
44

5+
if [ ! -z "${KERNEL_SECRET}" ] || [ ! -z "${KERNEL_SECRET_FILE}" ]; then
6+
echo "Kernel secret is defined as an environment variable"
7+
8+
exit 0
9+
fi
10+
511
if [ -f "/srv/config/secrets/kernel_secret" ]; then
612
echo "Kernel secret exists"
13+
14+
exit 0
715
fi
816

917
# Make sure secrets directory exists

tests/Docker/Standalone/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Tests\Docker\Standalone;
4+
5+
abstract class DockerStandaloneIsolatedTestCase extends DockerStandaloneTestCase
6+
{
7+
protected function setUp(): void
8+
{
9+
}
10+
11+
protected function setUpDefaultContainer(): void
12+
{
13+
parent::setUp();
14+
}
15+
}

tests/Docker/Standalone/DockerStandaloneTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
abstract class DockerStandaloneTestCase extends TestCase
1111
{
12-
protected StartedGenericContainer $container;
12+
protected ?StartedGenericContainer $container = null;
1313

1414
protected function setUp(): void
1515
{
@@ -22,7 +22,7 @@ protected function setUp(): void
2222

2323
protected function tearDown(): void
2424
{
25-
$this->container->stop();
25+
$this->container?->stop();
2626
}
2727

2828
protected function assertCommandSuccessful(array $command, ?string $message = null): void

tests/Docker/Standalone/InitTest.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,82 @@
22

33
namespace CodedMonkey\Dirigent\Tests\Docker\Standalone;
44

5-
class InitTest extends DockerStandaloneTestCase
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use Symfony\Component\Filesystem\Filesystem;
7+
use Testcontainers\Container\GenericContainer;
8+
use Testcontainers\Wait\WaitForLog;
9+
10+
class InitTest extends DockerStandaloneIsolatedTestCase
611
{
12+
protected function tearDown(): void
13+
{
14+
parent::tearDown();
15+
16+
(new Filesystem())->remove(__DIR__ . '/config');
17+
}
18+
719
public function testKernelSecretGenerated(): void
820
{
21+
$this->setUpDefaultContainer();
22+
23+
$logs = $this->container->logs();
24+
25+
$this->assertStringContainsString('Generated a new kernel secret', $logs);
26+
927
$this->assertContainerFileExists(
1028
'/srv/config/secrets/kernel_secret',
1129
'A kernel_secret file must be generated.',
1230
);
1331
}
32+
33+
public function testKernelSecretNotGeneratedAgain(): void
34+
{
35+
(new Filesystem())->mkdir(__DIR__ . '/config');
36+
37+
// Generate kernel secret first
38+
$this->container = (new GenericContainer('dirigent-standalone'))
39+
->withMount(__DIR__ . '/config', '/srv/config')
40+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
41+
->withWait(new WaitForLog('ready to handle connections'))
42+
->start();
43+
44+
$initialSecret = (new Filesystem())->readFile(__DIR__ . '/config/secrets/kernel_secret');
45+
46+
$this->container->restart();
47+
48+
$logs = $this->container->logs();
49+
50+
$this->assertStringContainsString('Kernel secret exists', $logs);
51+
52+
$secret = (new Filesystem())->readFile(__DIR__ . '/config/secrets/kernel_secret');
53+
54+
$this->assertSame($initialSecret, $secret, 'The kernel_secret file must not be changed if it already exists.');
55+
}
56+
57+
public static function kernelSecretEnvVarProvider(): array
58+
{
59+
return [
60+
['KERNEL_SECRET', 'fernando'],
61+
['KERNEL_SECRET_FILE', '/srv/config/secrets/kernel_secret'],
62+
];
63+
}
64+
65+
#[DataProvider('kernelSecretEnvVarProvider')]
66+
public function testKernelSecretNotGeneratedIfEnvVarExists(string $varName, string $varValue): void
67+
{
68+
(new Filesystem())->mkdir(__DIR__ . '/config');
69+
70+
$this->container = (new GenericContainer('dirigent-standalone'))
71+
->withMount(__DIR__ . '/config', '/srv/config')
72+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
73+
->withEnvironment([$varName => $varValue])
74+
->withWait(new WaitForLog('ready to handle connections'))
75+
->start();
76+
77+
$logs = $this->container->logs();
78+
79+
$this->assertStringContainsString('Kernel secret is defined as an environment variable', $logs);
80+
81+
$this->assertFalse((new Filesystem())->exists(__DIR__ . '/config/secrets/kernel_secret'), 'The kernel_secret file must not be generated if the kernel secret is defined through an environment variable.');
82+
}
1483
}

0 commit comments

Comments
 (0)