Skip to content

Commit 66d120e

Browse files
committed
Add encryption functionality
1 parent d64aeb4 commit 66d120e

27 files changed

+812
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/.env
22
/compose.override.yaml
33
/config/dirigent.yaml
4+
/config/encryption/
45
/config/packages/dirigent.yaml
56
/storage/
67

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ RUN set -e; \
6161
php83-phar \
6262
php83-session \
6363
php83-simplexml \
64+
php83-sodium \
6465
php83-tokenizer \
6566
php83-xml \
6667
postgresql \

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"ext-ctype": "*",
1212
"ext-curl": "*",
1313
"ext-iconv": "*",
14+
"ext-sodium": "*",
1415
"cebe/markdown": "^1.2",
1516
"composer/composer": "^2.7",
1617
"doctrine/doctrine-bundle": "^2.11",

composer.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/doctrine.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ doctrine:
88

99
profiling_collect_backtrace: '%kernel.debug%'
1010
use_savepoints: true
11+
12+
types:
13+
encrypted_text: CodedMonkey\Dirigent\Doctrine\Type\EncryptedTextType
1114
orm:
1215
auto_generate_proxy_classes: true
1316
enable_lazy_ghost_objects: true

config/packages/doctrine_migrations.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ doctrine_migrations:
44
# as migrations classes should NOT be autoloaded
55
'DoctrineMigrations': '%kernel.project_dir%/migrations'
66
enable_profiler: false
7+
services:
8+
'Doctrine\Migrations\Version\MigrationFactory': CodedMonkey\Dirigent\Doctrine\MigrationFactory

docker/config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
framework:
22
secret: '%env(file:KERNEL_SECRET_FILE)%'
3+
4+
dirigent:
5+
encryption:
6+
private_key: '%env(DECRYPTION_KEY)%'
7+
private_key_path: '%env(DECRYPTION_KEY_FILE)%'
8+
public_key: '%env(ENCRYPTION_KEY)%'
9+
public_key_path: '%env(ENCRYPTION_KEY_FILE)%'

docker/env.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
return [
44
'APP_ENV' => 'prod',
55
'DATABASE_URL' => 'postgresql://dirigent@127.0.0.1:5432/dirigent?serverVersion=16&charset=utf8',
6+
'DECRYPTION_KEY' => '',
7+
'DECRYPTION_KEY_FILE' => '/srv/config/secrets/decryption_key',
68
'DIRIGENT_IMAGE' => '1',
9+
'ENCRYPTION_KEY' => '',
10+
'ENCRYPTION_KEY_FILE' => '/srv/config/secrets/encryption_key',
711
'GITHUB_TOKEN' => '',
812
'KERNEL_SECRET_FILE' => '/srv/config/secrets/kernel_secret',
913
'MAILER_DSN' => 'null://null',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
bin/console encryption:generate-keys --no-ansi --no-interaction
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use CodedMonkey\Dirigent\Encryption\Encryption;
8+
use Doctrine\DBAL\Connection;
9+
use Doctrine\DBAL\Schema\Schema;
10+
use Doctrine\Migrations\AbstractMigration;
11+
use Psr\Log\LoggerInterface;
12+
13+
final class Version20250311205816 extends AbstractMigration
14+
{
15+
public function __construct(
16+
Connection $connection,
17+
LoggerInterface $logger,
18+
private readonly Encryption $encryptionUtility,
19+
) {
20+
parent::__construct($connection, $logger);
21+
}
22+
23+
public function getDescription(): string
24+
{
25+
return 'Encrypt sensitive credentials fields';
26+
}
27+
28+
public function up(Schema $schema): void
29+
{
30+
$this->addSql('ALTER TABLE credentials ALTER username TYPE TEXT');
31+
$this->addSql('ALTER TABLE credentials ALTER password TYPE TEXT');
32+
$this->addSql('ALTER TABLE credentials ALTER token TYPE TEXT');
33+
34+
$credentialsCollection = $this->connection->fetchAllAssociative('SELECT id, username, password, token FROM credentials');
35+
36+
foreach ($credentialsCollection as $credentials) {
37+
if (null !== $credentials['username']) {
38+
$sealedUsername = $this->encryptionUtility->seal($credentials['username']);
39+
$this->addSql('UPDATE credentials SET username = ? WHERE id = ?', [$sealedUsername, $credentials['id']]);
40+
}
41+
42+
if (null !== $credentials['password']) {
43+
$sealedPassword = $this->encryptionUtility->seal($credentials['password']);
44+
$this->addSql('UPDATE credentials SET password = ? WHERE id = ?', [$sealedPassword, $credentials['id']]);
45+
}
46+
47+
if (null !== $credentials['token']) {
48+
$sealedToken = $this->encryptionUtility->seal($credentials['token']);
49+
$this->addSql('UPDATE credentials SET token = ? WHERE id = ?', [$sealedToken, $credentials['id']]);
50+
}
51+
}
52+
}
53+
54+
public function down(Schema $schema): void
55+
{
56+
$credentialsCollection = $this->connection->fetchAllAssociative('SELECT id, username, password, token FROM credentials');
57+
58+
foreach ($credentialsCollection as $credentials) {
59+
if (null !== $credentials['username']) {
60+
$username = $this->encryptionUtility->reveal($credentials['username']);
61+
$this->addSql('UPDATE credentials SET username = ? WHERE id = ?', [$username, $credentials['id']]);
62+
}
63+
64+
if (null !== $credentials['password']) {
65+
$password = $this->encryptionUtility->reveal($credentials['password']);
66+
$this->addSql('UPDATE credentials SET password = ? WHERE id = ?', [$password, $credentials['id']]);
67+
}
68+
69+
if (null !== $credentials['token']) {
70+
$token = $this->encryptionUtility->reveal($credentials['token']);
71+
$this->addSql('UPDATE credentials SET token = ? WHERE id = ?', [$token, $credentials['id']]);
72+
}
73+
}
74+
75+
$this->addSql('ALTER TABLE credentials ALTER username TYPE VARCHAR(255)');
76+
$this->addSql('ALTER TABLE credentials ALTER password TYPE VARCHAR(255)');
77+
$this->addSql('ALTER TABLE credentials ALTER token TYPE VARCHAR(255)');
78+
}
79+
}

0 commit comments

Comments
 (0)