Improve error logs

This commit is contained in:
Matej Bačo committed 2023-03-08 20:50:51 +01:00
1 parent 0faaf32e51
commit 83370c21f7
10 files changed
+36 -22

No files matched your search

+2 -2
View File
@@ -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=
+5 -5
View File
@@ -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],
],
+5
View File
@@ -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.',
+2 -1
View File
@@ -49,7 +49,8 @@ $admins = [
'functions.write',
'execution.read',
'execution.write',
'rules.read'
'rules.read',
'rules.write'
];
return [
+1 -1
View File
@@ -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' ]),
]))
+4 -4
View File
@@ -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();
+11 -4
View File
@@ -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);
}
}
+1
View File
@@ -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';
+4 -4
View File
@@ -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',
])
;
}