Flatten certificate response model

This commit is contained in:
Khushboo Verma
2024-02-02 18:35:21 +05:30
parent 4c5e6a5225
commit ccb5183ef9
3 changed files with 51 additions and 31 deletions
+8 -11
View File
@@ -421,18 +421,10 @@ App::get('/v1/health/certificate')
$streamContextParams = stream_context_get_params($sslSocket);
$peerCertificate = $streamContextParams['options']['ssl']['peer_certificate'];
$parsedCertificate = openssl_x509_parse($peerCertificate);
$certificatePayload = openssl_x509_parse($peerCertificate);
$certificatePayload = [
'name' => $parsedCertificate['name'],
'subjectCN' => $parsedCertificate['subject']['CN'],
'issuer' => $parsedCertificate['issuer'],
'validFrom' => $parsedCertificate['validFrom_time_t'],
'validTo' => $parsedCertificate['validTo_time_t'],
'signatureTypeSN' => $parsedCertificate['signatureTypeSN'],
];
$sslExpiration = $parsedCertificate['validTo_time_t'];
$sslExpiration = $certificatePayload['validTo_time_t'];
$status = ($sslExpiration < time()) ? 'fail' : 'pass';
if ($status == 'fail') {
@@ -441,7 +433,12 @@ App::get('/v1/health/certificate')
$response->dynamic(new Document([
'name' => 'certificate',
'payload' => json_encode($certificatePayload),
'certificateName' => $certificatePayload['name'],
'certificateSubjectSN' => $certificatePayload['subject']['CN'],
'certificateIssuerOrganisation' => $certificatePayload['issuer']['O'],
'certificateValidFrom' => $certificatePayload['validFrom_time_t'],
'certificateValidTo' => $certificatePayload['validTo_time_t'],
'certificateSignatureTypeSN' => $certificatePayload['signatureTypeSN'],
]), Response::MODEL_HEALTH_CERTIFICATE);
}, ['response']);
@@ -16,16 +16,41 @@ class HealthCertificate extends Model
'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('certificateName', [
'type' => self::TYPE_STRING,
'description' => 'Certificate name',
'default' => '',
'example' => '/CN=www.google.com',
])
->addRule('certificateSubjectSN', [
'type' => self::TYPE_STRING,
'description' => 'Certificate subject SN',
'default' => 'www.google.com',
'example' => '',
])
->addRule('certificateIssuerOrganisation', [
'type' => self::TYPE_STRING,
'description' => 'Certificate issuer organisation',
'default' => 'Google Trust Services LLC',
'example' => '',
])
->addRule('certificateValidFrom', [
'type' => self::TYPE_STRING,
'description' => 'Certificate valid from',
'default' => '',
'example' => '1704200998',
])
->addRule('certificateValidTo', [
'type' => self::TYPE_STRING,
'description' => 'Certificate valid to',
'default' => '',
'example' => '1711458597',
])
->addRule('certificateSignatureTypeSN', [
'type' => self::TYPE_STRING,
'description' => 'Certificate signature type SN',
'default' => '',
'example' => 'RSA-SHA256',
])
;
}
@@ -436,11 +436,10 @@ class HealthCustomServerTest extends Scope
], $this->getHeaders()), []);
$this->assertEquals(200, $response['headers']['status-code']);
$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);
$this->assertEquals('www.google.com', $response['body']['certificateSubjectSN']);
$this->assertEquals('Google Trust Services LLC', $response['body']['certificateIssuerOrganisation']);
$this->assertIsInt($response['body']['certificateValidFrom']);
$this->assertIsInt($response['body']['certificateValidTo']);
$response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=appwrite.io', array_merge([
'content-type' => 'application/json',
@@ -448,11 +447,10 @@ class HealthCustomServerTest extends Scope
], $this->getHeaders()), []);
$this->assertEquals(200, $response['headers']['status-code']);
$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);
$this->assertEquals('appwrite.io', $response['body']['certificateSubjectSN']);
$this->assertEquals("Let's Encrypt", $response['body']['certificateIssuerOrganisation']);
$this->assertIsInt($response['body']['certificateValidFrom']);
$this->assertIsInt($response['body']['certificateValidTo']);
$response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=https://google.com', array_merge([
'content-type' => 'application/json',