Skip to content

Commit afb2fe9

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

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-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: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,92 @@
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 Filesystem $filesystem;
13+
14+
protected function setUp(): void
15+
{
16+
$this->filesystem = new Filesystem();
17+
}
18+
19+
protected function tearDown(): void
20+
{
21+
parent::tearDown();
22+
23+
$this->filesystem->remove(__DIR__ . '/config');
24+
}
25+
726
public function testKernelSecretGenerated(): void
827
{
28+
$this->setUpDefaultContainer();
29+
30+
$logs = $this->container->logs();
31+
32+
$this->assertStringContainsString('Generated a new kernel secret', $logs);
33+
934
$this->assertContainerFileExists(
1035
'/srv/config/secrets/kernel_secret',
1136
'A kernel_secret file must be generated.',
1237
);
1338
}
39+
40+
public function testKernelSecretNotRegeneratedOnRestart(): void
41+
{
42+
$this->filesystem->mkdir(__DIR__ . '/config/secrets');
43+
$this->filesystem->chmod(__DIR__ . '/config', 0777, recursive: true);
44+
45+
// Generate kernel secret first
46+
$this->container = (new GenericContainer('dirigent-standalone'))
47+
->withMount(__DIR__ . '/config', '/srv/config')
48+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
49+
->withWait(new WaitForLog('ready to handle connections'))
50+
->start();
51+
52+
$initialSecret = $this->filesystem->readFile(__DIR__ . '/config/secrets/kernel_secret');
53+
54+
$this->container->restart();
55+
56+
$logs = $this->container->logs();
57+
58+
$this->assertStringContainsString('Kernel secret exists', $logs);
59+
60+
$secret = $this->filesystem->readFile(__DIR__ . '/config/secrets/kernel_secret');
61+
62+
$this->assertSame($initialSecret, $secret, 'The kernel_secret file must not be changed if it already exists.');
63+
}
64+
65+
public static function kernelSecretEnvVarProvider(): array
66+
{
67+
return [
68+
['KERNEL_SECRET', 'fernando'],
69+
['KERNEL_SECRET_FILE', '/srv/config/secrets/kernel_secret'],
70+
];
71+
}
72+
73+
#[DataProvider('kernelSecretEnvVarProvider')]
74+
public function testKernelSecretNotGeneratedIfEnvVarExists(string $varName, string $varValue): void
75+
{
76+
$this->filesystem->mkdir(__DIR__ . '/config/secrets');
77+
$this->filesystem->chmod(__DIR__ . '/config', 0777, recursive: true);
78+
79+
$this->container = (new GenericContainer('dirigent-standalone'))
80+
->withMount(__DIR__ . '/config', '/srv/config')
81+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
82+
->withEnvironment([$varName => $varValue])
83+
->withWait(new WaitForLog('ready to handle connections'))
84+
->start();
85+
86+
$logs = $this->container->logs();
87+
88+
$this->assertStringContainsString('Kernel secret is defined as an environment variable', $logs);
89+
90+
$kernelSecretExists = $this->filesystem->exists(__DIR__ . '/config/secrets/kernel_secret');
91+
$this->assertFalse($kernelSecretExists, 'The kernel_secret file must not be generated if the kernel secret is defined through an environment variable.');
92+
}
1493
}

0 commit comments

Comments
 (0)