From 6bd6a4f09cd63c15a4f6988415cd46231a55171c Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:19:05 +0530 Subject: [PATCH] Add Health Certificate Response Model --- app/controllers/api/health.php | 8 +-- src/Appwrite/Utopia/Response.php | 3 + .../Response/Model/HealthCertificate.php | 58 +++++++++++++++++++ .../Health/HealthCustomServerTest.php | 10 ++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/Appwrite/Utopia/Response/Model/HealthCertificate.php diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index fe275fdd09..7cb5b72fc7 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -401,7 +401,7 @@ App::get('/v1/health/certificate') ->label('sdk.description', '/docs/references/health/get-certificate.md') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_HEALTH_STATUS) + ->label('sdk.response.model', Response::MODEL_HEALTH_CERTIFICATE) ->param('domain', null, new Multiple([new Domain(), new PublicDomain()]), Multiple::TYPE_STRING, 'Domain name') ->inject('response') ->action(function (string $domain, Response $response) { @@ -418,7 +418,7 @@ App::get('/v1/health/certificate') $certificateInfo = openssl_x509_parse($certificate['options']['ssl']['peer_certificate']); $certificatePayload = [ 'name' => $certificateInfo['name'], - 'subject' => $certificateInfo['subject'], + 'subjectCN' => $certificateInfo['subject']['CN'], 'issuer' => $certificateInfo['issuer'], 'validFrom' => $certificateInfo['validFrom_time_t'], 'validTo' => $certificateInfo['validTo_time_t'], @@ -434,8 +434,8 @@ App::get('/v1/health/certificate') $response->dynamic(new Document([ 'name' => 'certificate', 'status' => $status, - 'ping' => 0 - ]), Response::MODEL_HEALTH_STATUS); + 'payload' => json_encode($certificatePayload), + ]), Response::MODEL_HEALTH_CERTIFICATE); }, ['response']); App::get('/v1/health/queue/certificates') diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 85db23b385..53a77be8a0 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -74,6 +74,7 @@ use Appwrite\Utopia\Response\Model\Token; use Appwrite\Utopia\Response\Model\Webhook; use Appwrite\Utopia\Response\Model\Preferences; use Appwrite\Utopia\Response\Model\HealthAntivirus; +use Appwrite\Utopia\Response\Model\HealthCertificate; use Appwrite\Utopia\Response\Model\HealthQueue; use Appwrite\Utopia\Response\Model\HealthStatus; use Appwrite\Utopia\Response\Model\HealthTime; @@ -272,6 +273,7 @@ class Response extends SwooleResponse public const MODEL_HEALTH_TIME = 'healthTime'; public const MODEL_HEALTH_ANTIVIRUS = 'healthAntivirus'; public const MODEL_HEALTH_STATUS_LIST = 'healthStatusList'; + public const MODEL_HEALTH_CERTIFICATE = 'healthCertificate'; // Console public const MODEL_CONSOLE_VARIABLES = 'consoleVariables'; @@ -413,6 +415,7 @@ class Response extends SwooleResponse ->setModel(new HealthAntivirus()) ->setModel(new HealthQueue()) ->setModel(new HealthStatus()) + ->setModel(new HealthCertificate()) ->setModel(new HealthTime()) ->setModel(new HealthVersion()) ->setModel(new Metric()) diff --git a/src/Appwrite/Utopia/Response/Model/HealthCertificate.php b/src/Appwrite/Utopia/Response/Model/HealthCertificate.php new file mode 100644 index 0000000000..76000a12b3 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/HealthCertificate.php @@ -0,0 +1,58 @@ +addRule('name', [ + 'type' => self::TYPE_STRING, + 'description' => 'Name of the service.', + 'default' => '', + 'example' => 'database', + ]) + ->addRule('payload', [ + 'type' => self::TYPE_JSON, + 'description' => 'Certificate information payload', + 'default' => [], + 'example' => [ + 'name' => '/CN=www.google.com', + 'validFrom' => '1704200998', + 'validTo' => '1711458597', + 'signatureTypeSN' => 'RSA-SHA256', + ], + ]) + ->addRule('status', [ + 'type' => self::TYPE_STRING, + 'description' => 'Service status. Possible values can are: `pass`, `fail`', + 'default' => '', + 'example' => 'pass', + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'Health Certificate'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_HEALTH_CERTIFICATE; + } +} diff --git a/tests/e2e/Services/Health/HealthCustomServerTest.php b/tests/e2e/Services/Health/HealthCustomServerTest.php index 141029823e..58b45527f9 100644 --- a/tests/e2e/Services/Health/HealthCustomServerTest.php +++ b/tests/e2e/Services/Health/HealthCustomServerTest.php @@ -439,6 +439,11 @@ class HealthCustomServerTest extends Scope $this->assertNotEmpty($response['body']['status']); $this->assertIsString($response['body']['status']); $this->assertEquals('pass', $response['body']['status']); + $this->assertNotEmpty($response['body']['payload']); + $payload = json_decode($response['body']['payload']); + $this->assertEquals('www.google.com', $payload->subjectCN); + $this->assertEquals('Google Trust Services LLC', $payload->issuer->O); + $this->assertIsInt($payload->validFrom); $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=appwrite.io', array_merge([ 'content-type' => 'application/json', @@ -449,6 +454,11 @@ class HealthCustomServerTest extends Scope $this->assertNotEmpty($response['body']['status']); $this->assertIsString($response['body']['status']); $this->assertEquals('pass', $response['body']['status']); + $this->assertNotEmpty($response['body']['payload']); + $payload = json_decode($response['body']['payload']); + $this->assertEquals('appwrite.io', $payload->subjectCN); + $this->assertEquals("Let's Encrypt", $payload->issuer->O); + $this->assertIsInt($payload->validFrom); $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=https://google.com', array_merge([ 'content-type' => 'application/json',