Skip to content

Commit cc10e54

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 cc10e54

File tree

6 files changed

+112
-4
lines changed

6 files changed

+112
-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: 9 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
@@ -52,4 +52,11 @@ protected function assertContainerFileExists(string $path, ?string $message = nu
5252
$this->fail($message);
5353
}
5454
}
55+
56+
protected function assertContainerLogsContain(string $needle, string $message = ''): void
57+
{
58+
$logs = $this->container->logs();
59+
60+
$this->assertStringContainsString($needle, $logs, $message);
61+
}
5562
}

tests/Docker/Standalone/InitTest.php

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

0 commit comments

Comments
 (0)