diff --git a/app/init.php b/app/init.php index 8eb29fef37..a2f42dae67 100644 --- a/app/init.php +++ b/app/init.php @@ -781,55 +781,58 @@ App::setResource('console', function() { }, []); App::setResource('dbForProject', function($db, $cache, $project) { - $cache = new Cache(new RedisCache($cache)); - $secrets = $project->getAttribute('databaseSecrets'); - $displacement = $project->getAttribute('databaseSecretsDisplacement', 0); - $version = $displacement + \count($secrets); + $filters = []; + if($project->getId() != 'console') { + $secrets = $project->getAttribute('databaseSecrets'); + $displacement = $project->getAttribute('databaseSecretsDisplacement', 0); + $version = $displacement + \count($secrets); + + $filters['encrypt'] = [ + 'encode' => function($value) use($version, $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; + $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'])); } - - $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'])); - } - ]; - + $cache = new Cache(new RedisCache($cache)); $database = new Database(new MariaDB($db), $cache, $filters); $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); $database->setNamespace("_{$project->getId()}");