Skip to content

Commit d513359

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 d513359

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-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: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,90 @@
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');
43+
44+
// Generate kernel secret first
45+
$this->container = (new GenericContainer('dirigent-standalone'))
46+
->withMount(__DIR__ . '/config', '/srv/config')
47+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
48+
->withWait(new WaitForLog('ready to handle connections'))
49+
->start();
50+
51+
$initialSecret = $this->filesystem->readFile(__DIR__ . '/config/secrets/kernel_secret');
52+
53+
$this->container->restart();
54+
55+
$logs = $this->container->logs();
56+
57+
$this->assertStringContainsString('Kernel secret exists', $logs);
58+
59+
$secret = $this->filesystem->readFile(__DIR__ . '/config/secrets/kernel_secret');
60+
61+
$this->assertSame($initialSecret, $secret, 'The kernel_secret file must not be changed if it already exists.');
62+
}
63+
64+
public static function kernelSecretEnvVarProvider(): array
65+
{
66+
return [
67+
['KERNEL_SECRET', 'fernando'],
68+
['KERNEL_SECRET_FILE', '/srv/config/secrets/kernel_secret'],
69+
];
70+
}
71+
72+
#[DataProvider('kernelSecretEnvVarProvider')]
73+
public function testKernelSecretNotGeneratedIfEnvVarExists(string $varName, string $varValue): void
74+
{
75+
$this->filesystem->mkdir(__DIR__ . '/config');
76+
77+
$this->container = (new GenericContainer('dirigent-standalone'))
78+
->withMount(__DIR__ . '/config', '/srv/config')
79+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
80+
->withEnvironment([$varName => $varValue])
81+
->withWait(new WaitForLog('ready to handle connections'))
82+
->start();
83+
84+
$logs = $this->container->logs();
85+
86+
$this->assertStringContainsString('Kernel secret is defined as an environment variable', $logs);
87+
88+
$kernelSecretExists = $this->filesystem->exists(__DIR__ . '/config/secrets/kernel_secret');
89+
$this->assertFalse($kernelSecretExists, 'The kernel_secret file must not be generated if the kernel secret is defined through an environment variable.');
90+
}
1491
}

0 commit comments

Comments
 (0)