update project level encryption decryption filter

This commit is contained in:
Damodar Lohani
2022-05-06 09:51:21 +00:00
parent 967c751926
commit 87f8188665
2 changed files with 8 additions and 31 deletions
+1 -4
View File
@@ -101,10 +101,7 @@ App::post('/v1/projects')
'keys' => null,
'domains' => null,
'auths' => $auths,
'storageSecrets' => [\bin2hex(OpenSSL::randomPseudoBytes(128))],
'storageSecretsDisplacement' => 0,
'databaseSecrets' => [\bin2hex(OpenSSL::randomPseudoBytes(128))],
'databaseSecretsDisplacement' => 0,
'databaseSecrets' => [\uniqid() => \bin2hex(OpenSSL::randomPseudoBytes(128))],
'jwtSecret' => \bin2hex(OpenSSL::randomPseudoBytes(128)),
'search' => implode(' ', [$projectId, $name]),
]));
+7 -27
View File
@@ -781,28 +781,13 @@ App::setResource('console', function() {
}, []);
App::setResource('dbForProject', function($db, $cache, $project) {
$filters = [];
if($project->getId() != 'console' && !$project->isEmpty()) {
if(!$project->isEmpty()) {
$secrets = $project->getAttribute('databaseSecrets');
$displacement = $project->getAttribute('databaseSecretsDisplacement', 0);
$version = $displacement + \count($secrets);
$version = array_key_last($secrets);
$filters['encrypt'] = [
'encode' => function($value) use($version, $secrets) {
$key = $secrets[\count($secrets)-1];
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
$tag = null;
$value = json_encode([
'data' => OpenSSL::encrypt($value, OpenSSL::CIPHER_AES_128_GCM, $key, 0, $iv, $tag),
'method' => OpenSSL::CIPHER_AES_128_GCM,
'iv' => \bin2hex($iv),
'tag' => \bin2hex($tag ?? ''),
'version' => $version,
]);
$key = App::getEnv('_APP_OPENSSL_KEY_V1');
$key = $secrets[$version];
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
$tag = null;
return json_encode([
@@ -810,22 +795,17 @@ App::setResource('dbForProject', function($db, $cache, $project) {
'method' => OpenSSL::CIPHER_AES_128_GCM,
'iv' => \bin2hex($iv),
'tag' => \bin2hex($tag ?? ''),
'version' => '1',
'version' => $version,
]);
},
'decode' => function($value) use($secrets, $displacement) {
'decode' => function($value) use($secrets) {
if(is_null($value)) {
return null;
}
$value = json_decode($value, true);
$key = App::getEnv('_APP_OPENSSL_KEY_V'.$value['version']);
$value = OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
$value = json_decode($value, true);
$version = ($value['version'] ?? 1) - $displacement;
$key = $secrets[$version - 1];
$version = $value['version'];
$key = $secrets[$version];
return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
}