Merge pull request #9018 from appwrite/rules-backwards-compatibility

chore: backwards compatibility for 1.6.x
This commit is contained in:
Steven Nguyen 2024-11-22 09:53:09 -08:00 committed by GitHub
commit ad235610da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 10 deletions

View file

@ -328,7 +328,8 @@ App::post('/v1/functions')
if (!empty($functionsDomain)) { if (!empty($functionsDomain)) {
$routeSubdomain = ID::unique(); $routeSubdomain = ID::unique();
$domain = "{$routeSubdomain}.{$functionsDomain}"; $domain = "{$routeSubdomain}.{$functionsDomain}";
$ruleId = md5($domain); // TODO: @christyjacob remove once we migrate the rules in 1.7.x
$ruleId = version_compare(APP_VERSION_STABLE, '1.7.0', '<') ? ID::unique() : md5($domain);
$rule = Authorization::skip( $rule = Authorization::skip(
fn () => $dbForConsole->createDocument('rules', new Document([ fn () => $dbForConsole->createDocument('rules', new Document([

View file

@ -11,6 +11,7 @@ use Utopia\App;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Exception\Query as QueryException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query; use Utopia\Database\Query;
use Utopia\Database\Validator\Query\Cursor; use Utopia\Database\Validator\Query\Cursor;
use Utopia\Database\Validator\UID; use Utopia\Database\Validator\UID;
@ -59,8 +60,16 @@ App::post('/v1/proxy/rules')
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please pick another one.'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please pick another one.');
} }
$ruleId = md5($domain); // TODO: @christyjacob remove once we migrate the rules in 1.7.x
$document = $dbForConsole->getDocument('rules', $ruleId); if (version_compare(APP_VERSION_STABLE, '1.7.0', '<')) {
$document = $dbForConsole->findOne('rules', [
Query::equal('domain', [$domain]),
]);
} else {
$ruleId = md5($domain);
$document = $dbForConsole->getDocument('rules', $ruleId);
}
if (!$document->isEmpty()) { if (!$document->isEmpty()) {
if ($document->getAttribute('projectId') === $project->getId()) { if ($document->getAttribute('projectId') === $project->getId()) {
@ -101,7 +110,9 @@ App::post('/v1/proxy/rules')
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.');
} }
$ruleId = md5($domain->get()); // TODO: @christyjacob remove once we migrate the rules in 1.7.x
$ruleId = version_compare(APP_VERSION_STABLE, '1.7.0', '<') ? ID::unique() : md5($domain->get());
$rule = new Document([ $rule = new Document([
'$id' => $ruleId, '$id' => $ruleId,
'projectId' => $project->getId(), 'projectId' => $project->getId(),

View file

@ -29,6 +29,7 @@ use Utopia\Database\Database;
use Utopia\Database\DateTime; use Utopia\Database\DateTime;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
use Utopia\Domains\Domain; use Utopia\Domains\Domain;
use Utopia\DSN\DSN; use Utopia\DSN\DSN;
@ -51,7 +52,17 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo
$host = $request->getHostname() ?? ''; $host = $request->getHostname() ?? '';
$route = Authorization::skip(fn () => $dbForConsole->getDocument('rules', md5($host))); // TODO: @christyjacob remove once we migrate the rules in 1.7.x
if (version_compare(APP_VERSION_STABLE, '1.7.0', '<')) {
$route = Authorization::skip(
fn () => $dbForConsole->find('rules', [
Query::equal('domain', [$host]),
Query::limit(1)
])
)[0] ?? new Document();
} else {
$route = Authorization::skip(fn () => $dbForConsole->getDocument('rules', md5($host)));
}
if ($route->isEmpty()) { if ($route->isEmpty()) {
if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) { if ($host === System::getEnv('_APP_DOMAIN_FUNCTIONS', '')) {
@ -512,18 +523,31 @@ App::init()
if (!empty($envDomain) && $envDomain !== 'localhost') { if (!empty($envDomain) && $envDomain !== 'localhost') {
$mainDomain = $envDomain; $mainDomain = $envDomain;
} else { } else {
$domainDocument = $dbForConsole->getDocument('rules', md5($envDomain)); // TODO: @christyjacob remove once we migrate the rules in 1.7.x
if (version_compare(APP_VERSION_STABLE, '1.7.0', '<')) {
$domainDocument = $dbForConsole->findOne('rules', [Query::orderAsc('$id')]);
} else {
$domainDocument = $dbForConsole->getDocument('rules', md5($envDomain));
}
$mainDomain = !$domainDocument->isEmpty() ? $domainDocument->getAttribute('domain') : $domain->get(); $mainDomain = !$domainDocument->isEmpty() ? $domainDocument->getAttribute('domain') : $domain->get();
} }
if ($mainDomain !== $domain->get()) { if ($mainDomain !== $domain->get()) {
Console::warning($domain->get() . ' is not a main domain. Skipping SSL certificate generation.'); Console::warning($domain->get() . ' is not a main domain. Skipping SSL certificate generation.');
} else { } else {
$domainDocument = $dbForConsole->getDocument('rules', md5($domain->get())); // TODO: @christyjacob remove once we migrate the rules in 1.7.x
if (version_compare(APP_VERSION_STABLE, '1.7.0', '<')) {
$domainDocument = $dbForConsole->findOne('rules', [
Query::equal('domain', [$domain->get()])
]);
} else {
$domainDocument = $dbForConsole->getDocument('rules', md5($domain->get()));
}
if ($domainDocument->isEmpty()) { if ($domainDocument->isEmpty()) {
$domainDocument = new Document([ $domainDocument = new Document([
'$id' => md5($domain->get()), // TODO: @christyjacob remove once we migrate the rules in 1.7.x
'$id' => version_compare(APP_VERSION_STABLE, '1.7.0', '<') ? ID::unique() : md5($domain->get()),
'domain' => $domain->get(), 'domain' => $domain->get(),
'resourceType' => 'api', 'resourceType' => 'api',
'status' => 'verifying', 'status' => 'verifying',

View file

@ -477,8 +477,14 @@ class Certificates extends Action
*/ */
private function updateDomainDocuments(string $certificateId, string $domain, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions): void private function updateDomainDocuments(string $certificateId, string $domain, bool $success, Database $dbForConsole, Event $queueForEvents, Func $queueForFunctions): void
{ {
// TODO: @christyjacob remove once we migrate the rules in 1.7.x
$rule = $dbForConsole->getDocument('rules', md5($domain)); if (version_compare(APP_VERSION_STABLE, '1.7.0', '<')) {
$rule = $dbForConsole->findOne('rules', [
Query::equal('domain', [$domain]),
]);
} else {
$rule = $dbForConsole->getDocument('rules', md5($domain));
}
if (!$rule->isEmpty()) { if (!$rule->isEmpty()) {
$rule->setAttribute('certificateId', $certificateId); $rule->setAttribute('certificateId', $certificateId);