From 83370c21f7e8030022b34bcd00bae99976fcff84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 8 Mar 2023 20:50:51 +0100 Subject: [PATCH] Improve error logs --- .env | 4 ++-- app/config/collections.php | 10 +++++----- app/config/errors.php | 5 +++++ app/config/roles.php | 3 ++- app/console | 2 +- app/controllers/api/functions.php | 2 +- app/controllers/api/proxy.php | 8 ++++---- app/workers/certificates.php | 15 +++++++++++---- src/Appwrite/Extend/Exception.php | 1 + src/Appwrite/Utopia/Response/Model/Rule.php | 8 ++++---- 10 files changed, 36 insertions(+), 22 deletions(-) diff --git a/.env b/.env index 43c198a683..597910aaf9 100644 --- a/.env +++ b/.env @@ -13,10 +13,10 @@ _APP_SYSTEM_RESPONSE_FORMAT= _APP_OPTIONS_ABUSE=disabled _APP_OPTIONS_FORCE_HTTPS=disabled _APP_OPENSSL_KEY_V1=your-secret-key -_APP_DOMAIN=localhost +_APP_DOMAIN=wise-crews-talk-86-49-225-170.loca.lt _APP_DOMAIN_FUNCTIONS=functions.localhost _APP_DOMAIN_DO_TOKEN= -_APP_DOMAIN_TARGET=localhost +_APP_DOMAIN_TARGET=wise-crews-talk-86-49-225-170.loca.lt _APP_REDIS_HOST=redis _APP_REDIS_PORT=6379 _APP_REDIS_PASS= diff --git a/app/config/collections.php b/app/config/collections.php index 8a2bd5f8cd..8c71c38786 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -841,10 +841,10 @@ $collections = [ 'filters' => [], ], [ - '$id' => ID::custom('verification'), - 'type' => Database::VAR_BOOLEAN, + '$id' => ID::custom('status'), + 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 0, + 'size' => Database::LENGTH_KEY, 'signed' => true, 'required' => false, 'default' => null, @@ -911,9 +911,9 @@ $collections = [ 'orders' => [Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_verification'), + '$id' => ID::custom('_key_status'), 'type' => Database::INDEX_KEY, - 'attributes' => ['verification'], + 'attributes' => ['status'], 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], diff --git a/app/config/errors.php b/app/config/errors.php index 80a33c4566..bd0bd27995 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -529,6 +529,11 @@ return [ 'description' => 'With resourceType you provided, the resourceId is required.', 'code' => 400, ], + Exception::RULE_CONFIGURATION_MISSING => [ + 'name' => Exception::RULE_CONFIGURATION_MISSING, + 'description' => 'Configuration for _APP_DOMAIN_TARGET missing proper domain.', + 'code' => 403, + ], Exception::RULE_RESOURCE_ID_NOT_FOUND => [ 'name' => Exception::RULE_RESOURCE_ID_NOT_FOUND, 'description' => 'Resource could not be found. Check resourceId and resourceType.', diff --git a/app/config/roles.php b/app/config/roles.php index 864547563c..80219499ef 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -49,7 +49,8 @@ $admins = [ 'functions.write', 'execution.read', 'execution.write', - 'rules.read' + 'rules.read', + 'rules.write' ]; return [ diff --git a/app/console b/app/console index e168088cc8..85ee6ff5da 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit e168088cc806038fbef844bd760da159f7b2b1cb +Subproject commit 85ee6ff5da76554788fc9fdb9bc65d943bef434c diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 17806d2dc1..744b8e409f 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -122,7 +122,7 @@ App::post('/v1/functions') 'resourceId' => $function->getId(), 'resourceInternalId' => $function->getInternalId(), 'redirect' => '', - 'verification' => true, + 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [ $domain, $ruleId, $function->getId(), 'function' ]), ])) diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index 446a11b068..72ab81c67d 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -56,7 +56,7 @@ App::post('/v1/proxy/rules') $target = new Domain(App::getEnv('_APP_DOMAIN_TARGET', '')); if (!$target->isKnown() || $target->isTest()) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Unreachable CNAME target (' . $target->get() . '), please use a domain with a public suffix.'); + throw new Exception(Exception::RULE_CONFIGURATION_MISSING); } $resourceInternalId = ''; @@ -87,7 +87,7 @@ App::post('/v1/proxy/rules') 'resourceId' => $resourceId, 'resourceInternalId' => $resourceInternalId, 'redirect' => $redirect, - 'verification' => false, + 'status' => 'created', 'certificateId' => '', 'search' => implode(' ', [ $domain, $ruleId, $resourceId, $resourceType, $redirect ]), ])); @@ -200,7 +200,7 @@ App::delete('/v1/proxy/rules/:ruleId') } if (!$dbForConsole->deleteDocument('rules', $rule->getId())) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove rule from DB'); + throw new Exception(Exception::RULE_CONFIGURATION_MISSING); } // TODO: Ensure certificate is deleted. Previously DELETE_TYPE_CERTIFICATES @@ -250,7 +250,7 @@ App::patch('/v1/proxy/rules/:ruleId/verification') throw new Exception(Exception::RULE_VERIFICATION_FAILED); } - $dbForConsole->updateDocument('rules', $rule->getId(), $rule->setAttribute('verification', true)); + $dbForConsole->updateDocument('rules', $rule->getId(), $rule->setAttribute('status', 'verifying')); // Issue a TLS certificate when domain is verified $event = new Certificate(); diff --git a/app/workers/certificates.php b/app/workers/certificates.php index 0b8933026f..5f0b11da80 100644 --- a/app/workers/certificates.php +++ b/app/workers/certificates.php @@ -84,6 +84,8 @@ class CertificatesV1 extends Worker $certificate->setAttribute('domain', $domain->get()); } + $success = false; + try { // Email for alerts is required by LetsEncrypt $email = App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS'); @@ -123,6 +125,8 @@ class CertificatesV1 extends Worker $certificate->setAttribute('renewDate', $this->getRenewDate($domain->get())); $certificate->setAttribute('attempts', 0); $certificate->setAttribute('issueDate', DateTime::now()); + + $success = true; } catch (Throwable $e) { // Set exception as log in certificate document $certificate->setAttribute('log', $e->getMessage()); @@ -141,7 +145,7 @@ class CertificatesV1 extends Worker $certificate->setAttribute('updated', DateTime::now()); // Save all changes we made to certificate document into database - $this->saveCertificateDocument($domain->get(), $certificate); + $this->saveCertificateDocument($domain->get(), $certificate, $success); } } @@ -154,10 +158,11 @@ class CertificatesV1 extends Worker * * @param string $domain Domain name that certificate is for * @param Document $certificate Certificate document that we need to save + * @param bool $success Was certificate generation successful? * * @return void */ - private function saveCertificateDocument(string $domain, Document $certificate): void + private function saveCertificateDocument(string $domain, Document $certificate, bool $success): void { // Check if update or insert required $certificateDocument = $this->dbForConsole->findOne('certificates', [Query::equal('domain', [$domain])]); @@ -171,7 +176,7 @@ class CertificatesV1 extends Worker } $certificateId = $certificate->getId(); - $this->updateDomainDocuments($certificateId, $domain); + $this->updateDomainDocuments($certificateId, $domain, $success); } /** @@ -395,10 +400,11 @@ class CertificatesV1 extends Worker * * @param string $certificateId ID of a new or updated certificate document * @param string $domain Domain that is affected by new certificate + * @param bool $success Was certificate generation successful? * * @return void */ - private function updateDomainDocuments(string $certificateId, string $domain): void + private function updateDomainDocuments(string $certificateId, string $domain, bool $success): void { $rule = $this->dbForConsole->findOne('rules', [ Query::equal('domain', [$domain]), @@ -406,6 +412,7 @@ class CertificatesV1 extends Worker if(!$rule->isEmpty()) { $rule->setAttribute('certificateId', $certificateId); + $rule->setAttribute('status', $success ? 'verified' : 'failed'); $this->dbForConsole->updateDocument('rules', $rule->getId(), $rule); } } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 38559fb40b..f77dc50e29 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -168,6 +168,7 @@ class Exception extends \Exception public const ROUTER_INVALID_TYPE = 'router_unknown_type'; /** Proxy */ + public const RULE_CONFIGURATION_MISSING = 'rule_configuration_missing'; public const RULE_RESOURCE_ID_MISSING = 'rule_resource_id_missing'; public const RULE_RESOURCE_ID_NOT_FOUND = 'rule_resource_id_not_found'; public const RULE_NOT_FOUND = 'rule_not_found'; diff --git a/src/Appwrite/Utopia/Response/Model/Rule.php b/src/Appwrite/Utopia/Response/Model/Rule.php index c47fdcd585..29ab75a87c 100644 --- a/src/Appwrite/Utopia/Response/Model/Rule.php +++ b/src/Appwrite/Utopia/Response/Model/Rule.php @@ -54,11 +54,11 @@ class Rule extends Model 'default' => '', 'example' => 'https://appwrite.io/', ]) - ->addRule('verification', [ - 'type' => self::TYPE_BOOLEAN, - 'description' => 'Domain verification status.', + ->addRule('status', [ + 'type' => self::TYPE_STRING, + 'description' => 'Domain verification status. Possible values are "created", "verifying", "verified" and "failed"', 'default' => false, - 'example' => true, + 'example' => 'verified', ]) ; }