From b5d812207cee5a1d90ca4d9e55881b68eec4e658 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Mon, 29 Jan 2024 14:53:39 +0530 Subject: [PATCH] Renamed variable names --- app/config/errors.php | 12 +++--- app/controllers/api/health.php | 39 +++++++++++-------- docs/references/health/get-certificate.md | 2 +- src/Appwrite/Extend/Exception.php | 4 +- .../Response/Model/HealthCertificate.php | 6 --- .../Health/HealthCustomServerTest.php | 9 ----- 6 files changed, 32 insertions(+), 40 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index c0943d31e5..3ef24723e8 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -795,15 +795,15 @@ return [ 'publish' => false ], - Exception::CERTIFICATE_EXPIRED => [ - 'name' => Exception::CERTIFICATE_EXPIRED, - 'description' => 'Certificate expired.', + Exception::HEALTH_CERTIFICATE_EXPIRED => [ + 'name' => Exception::HEALTH_CERTIFICATE_EXPIRED, + 'description' => 'The SSL certificate for the specified domain has expired and is no longer valid.', 'code' => 404, ], - Exception::INVALID_HOST => [ - 'name' => Exception::INVALID_HOST, - 'description' => 'Host not found.', + Exception::HEALTH_INVALID_HOST => [ + 'name' => Exception::HEALTH_INVALID_HOST, + 'description' => 'Failed to establish a connection to the specified domain. Please verify the domain name and ensure that the server is running and accessible.', 'code' => 404, ], diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index 7cb5b72fc7..04b0efbd42 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -392,7 +392,7 @@ App::get('/v1/health/queue/logs') }, ['response']); App::get('/v1/health/certificate') - ->desc('Get status of certificate for a domain to check whether it is still valid or expired.') + ->desc('Get the SSL certificate for a domain') ->groups(['api', 'health']) ->label('scope', 'health.read') ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) @@ -409,31 +409,38 @@ App::get('/v1/health/certificate') $domain = parse_url($domain, PHP_URL_HOST); } - $get = stream_context_create(array("ssl" => array("capture_peer_cert" => true))); - $read = stream_socket_client("ssl://" . $domain . ":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get); - if (!$read) { - throw new Exception(Exception::INVALID_HOST, 'The domain is not valid.'); + $sslContext = stream_context_create([ + "ssl" => [ + "capture_peer_cert" => true + ] + ]); + $sslSocket = stream_socket_client("ssl://" . $domain . ":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $sslContext); + if (!$sslSocket) { + throw new Exception(Exception::HEALTH_INVALID_HOST); } - $certificate = stream_context_get_params($read); - $certificateInfo = openssl_x509_parse($certificate['options']['ssl']['peer_certificate']); + + $streamContextParams = stream_context_get_params($sslSocket); + $peerCertificate = $streamContextParams['options']['ssl']['peer_certificate']; + $parsedCertificate = openssl_x509_parse($peerCertificate); + $certificatePayload = [ - 'name' => $certificateInfo['name'], - 'subjectCN' => $certificateInfo['subject']['CN'], - 'issuer' => $certificateInfo['issuer'], - 'validFrom' => $certificateInfo['validFrom_time_t'], - 'validTo' => $certificateInfo['validTo_time_t'], - 'signatureTypeSN' => $certificateInfo['signatureTypeSN'], + 'name' => $parsedCertificate['name'], + 'subjectCN' => $parsedCertificate['subject']['CN'], + 'issuer' => $parsedCertificate['issuer'], + 'validFrom' => $parsedCertificate['validFrom_time_t'], + 'validTo' => $parsedCertificate['validTo_time_t'], + 'signatureTypeSN' => $parsedCertificate['signatureTypeSN'], ]; - $sslExpiration = $certificateInfo['validTo_time_t']; + + $sslExpiration = $parsedCertificate['validTo_time_t']; $status = ($sslExpiration < time()) ? 'fail' : 'pass'; if ($status == 'fail') { - throw new Exception(Exception::CERTIFICATE_EXPIRED, 'The certificate of the domain has expired.'); + throw new Exception(Exception::HEALTH_CERTIFICATE_EXPIRED); } $response->dynamic(new Document([ 'name' => 'certificate', - 'status' => $status, 'payload' => json_encode($certificatePayload), ]), Response::MODEL_HEALTH_CERTIFICATE); }, ['response']); diff --git a/docs/references/health/get-certificate.md b/docs/references/health/get-certificate.md index 51154516ff..bf1eeb8384 100644 --- a/docs/references/health/get-certificate.md +++ b/docs/references/health/get-certificate.md @@ -1 +1 @@ -Get status of certificate for a domain to check whether it is still valid or expired. \ No newline at end of file +Get the SSL certificate for a domain \ No newline at end of file diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index f227482835..218a3cb35e 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -239,8 +239,8 @@ class Exception extends \Exception /** Health */ public const QUEUE_SIZE_EXCEEDED = 'queue_size_exceeded'; - public const CERTIFICATE_EXPIRED = 'certificate_expired'; - public const INVALID_HOST = 'invalid_host'; + public const HEALTH_CERTIFICATE_EXPIRED = 'health_certificate_expired'; + public const HEALTH_INVALID_HOST = 'health_invalid_host'; /** Provider */ public const PROVIDER_NOT_FOUND = 'provider_not_found'; diff --git a/src/Appwrite/Utopia/Response/Model/HealthCertificate.php b/src/Appwrite/Utopia/Response/Model/HealthCertificate.php index 76000a12b3..03c544e7b4 100644 --- a/src/Appwrite/Utopia/Response/Model/HealthCertificate.php +++ b/src/Appwrite/Utopia/Response/Model/HealthCertificate.php @@ -27,12 +27,6 @@ class HealthCertificate extends Model 'signatureTypeSN' => 'RSA-SHA256', ], ]) - ->addRule('status', [ - 'type' => self::TYPE_STRING, - 'description' => 'Service status. Possible values can are: `pass`, `fail`', - 'default' => '', - 'example' => 'pass', - ]) ; } diff --git a/tests/e2e/Services/Health/HealthCustomServerTest.php b/tests/e2e/Services/Health/HealthCustomServerTest.php index 58b45527f9..bbfc702d4b 100644 --- a/tests/e2e/Services/Health/HealthCustomServerTest.php +++ b/tests/e2e/Services/Health/HealthCustomServerTest.php @@ -436,9 +436,6 @@ class HealthCustomServerTest extends Scope ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); - $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); @@ -451,9 +448,6 @@ class HealthCustomServerTest extends Scope ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); - $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); @@ -466,9 +460,6 @@ class HealthCustomServerTest extends Scope ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertNotEmpty($response['body']['status']); - $this->assertIsString($response['body']['status']); - $this->assertEquals('pass', $response['body']['status']); /** * Test for FAILURE