mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
set instance filters in workers
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Appwrite\Resque;
|
||||
|
||||
use Appwrite\OpenSSL\OpenSSL;
|
||||
use Utopia\App;
|
||||
use Utopia\Cache\Cache;
|
||||
use Utopia\Cache\Adapter\Redis as RedisCache;
|
||||
@@ -203,6 +204,64 @@ abstract class Worker
|
||||
$cache = new Cache(new RedisCache($register->get('cache')));
|
||||
$database = new Database(new MariaDB($register->get('db')), $cache);
|
||||
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
||||
|
||||
if($namespace != "_console" && !empty($projectId)) {
|
||||
$database->setNamespace("_console");
|
||||
$project = $database->getDocument('projects', $projectId);
|
||||
if($project->isEmpty()) {
|
||||
throw new Exception("Project not found: {$projectId}");
|
||||
}
|
||||
$secrets = $project->getAttribute('databaseSecrets');
|
||||
$displacement = $project->getAttribute('databaseSecretsDisplacement', 0);
|
||||
$version = $displacement + \count($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');
|
||||
$iv = OpenSSL::randomPseudoBytes(OpenSSL::cipherIVLength(OpenSSL::CIPHER_AES_128_GCM));
|
||||
$tag = null;
|
||||
return 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' => '1',
|
||||
]);
|
||||
},
|
||||
'decode' => function($value) use($secrets, $displacement) {
|
||||
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];
|
||||
|
||||
return OpenSSL::decrypt($value['data'], $value['method'], $key, 0, hex2bin($value['iv']), hex2bin($value['tag']));
|
||||
}
|
||||
];
|
||||
|
||||
$database = new Database(new MariaDB($register->get('db')), $cache, $filters);
|
||||
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
||||
}
|
||||
|
||||
$database->setNamespace($namespace); // Main DB
|
||||
|
||||
if ($type === self::DATABASE_CONSOLE && !$database->exists($database->getDefaultDatabase(), 'realtime')) {
|
||||
|
||||
Reference in New Issue
Block a user