PHP code example of symfony / doctrine-dbal-key-management
1. Go to this page and download the library: Download symfony/doctrine-dbal-key-management library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
symfony / doctrine-dbal-key-management example snippets
use Doctrine\DBAL\Types\Type;
use Symfony\Component\KeyManagement\Bridge\DoctrineDbal\EncryptedType;
$type = new EncryptedType(
Type::getTypeRegistry()->get('string'),
$envelopeEncrypter,
'alias/app-key',
'string',
);
Type::getTypeRegistry()->register('app_user_email', $type);
$connection->insert('user', ['email' => '[email protected]'], ['email' => 'app_user_email']);
$email = $connection->convertToPHPValue(
$connection->fetchOne('SELECT email FROM user WHERE id = ?', [$id]),
'app_user_email',
);
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\Id, ORM\GeneratedValue, ORM\Column]
public ?int $id = null;
#[ORM\Column(type: 'app_user_email')]
public string $email;
}
// src/Kernel.php
public function boot(): void
{
parent::boot();
$this->container->get('app.encrypted_types')->register();
}
use Symfony\Component\KeyManagement\Bridge\DoctrineDbal\DataKeyStore;
use Symfony\Component\KeyManagement\StoredEnvelopeEncrypter;
$store = new DataKeyStore($connection, $clients, 'aws', 'alias/app-key', $secret);
$store->createTable();
$encrypter = new StoredEnvelopeEncrypter($store);
$envelope = $encrypter->encrypt('user.email', '[email protected]');
Type::getTypeRegistry()->register('app_user_email', new EncryptedType(
Type::getTypeRegistry()->get('string'),
$encrypter,
'user.email',
'string',
));
$encrypter = new StoredEnvelopeEncrypter($store, new EnvelopeEncrypter($kms));
$type = new EncryptedType(Type::getTypeRegistry()->get('string'), $encrypter, 'alias/app-key', 'string');
final class TenantScopedStore implements RewrappableDataKeyStoreInterface
{
public function __construct(
private DataKeyStore $inner,
private Connection $connection,
private string $tenantId,
) {
}
public function rotate(string $scope): DataKeyHandle
{
$handle = $this->inner->rotate($scope);
$this->connection->update(
'key_management_data_keys',
['tenant_id' => $this->tenantId],
['id' => $handle->reference],
['id' => ParameterType::BINARY],
);
return $handle;
}
// current(), get(), all() and rewrap() delegate to $this->inner
}
final class MigratingEnvelopes implements EnvelopeEncrypterInterface, EnvelopeDecrypterInterface
{
public function __construct(
private EnvelopeEncrypterInterface&EnvelopeDecrypterInterface $target,
private EnvelopeDecrypterInterface $legacy,
) {
}
public function encrypt(string $key, #[\SensitiveParameter] string $plaintext, string $aad = ''): Envelope
{
return $this->target->encrypt($key, $plaintext, $aad);
}
public function decrypt(Envelope $envelope, string $aad = ''): string
{
return str_starts_with($envelope->keyId ?? '', 'arn:aws:kms:')
? $this->legacy->decrypt($envelope, $aad)
: $this->target->decrypt($envelope, $aad);
}
}
$platform = $connection->getDatabasePlatform();
$legacy = new EncryptedType($parent, new EnvelopeEncrypter($awsKms), 'alias/app-key', 'string');
$target = new EncryptedType($parent, $encrypter, 'user.email', 'string');
foreach ($connection->iterateAssociative('SELECT id, email FROM user ORDER BY id') as $row) {
$connection->update(
'user',
['email_next' => $target->convertToDatabaseValue($legacy->convertToPHPValue($row['email'], $platform), $platform)],
['id' => $row['id']],
['email_next' => ParameterType::BINARY],
);
}