feat(ssl): improve SSL helper

- improve function parameters
- set default validity to 1 year as resources need to be manually restarted to use the new certificates
- use the CA cert to sign certificates
This commit is contained in:
peaklabs-dev
2025-01-31 13:37:34 +01:00
parent fab7300a5f
commit 02475c5232

View File

@@ -11,10 +11,14 @@ class SslHelper
public static function generateSslCertificate( public static function generateSslCertificate(
string $commonName, string $commonName,
array $additionalSans, array $additionalSans = [],
string $resourceType, ?string $resourceType = null,
int $resourceId, ?int $resourceId = null,
?int $serverId = null,
?string $organizationName = null, ?string $organizationName = null,
int $validityDays = 365,
?string $caCert = null,
?string $caKey = null
): SslCertificate { ): SslCertificate {
$organizationName ??= self::DEFAULT_ORGANIZATION_NAME; $organizationName ??= self::DEFAULT_ORGANIZATION_NAME;
@@ -50,9 +54,9 @@ class SslHelper
$certificate = openssl_csr_sign( $certificate = openssl_csr_sign(
$csr, $csr,
null, $caCert ?? null,
$privateKey, $caKey ?? $privateKey,
90, $validityDays,
[ [
'digest_alg' => 'sha512', 'digest_alg' => 'sha512',
'config' => null, 'config' => null,
@@ -73,7 +77,8 @@ class SslHelper
'ssl_private_key' => $privateKeyStr, 'ssl_private_key' => $privateKeyStr,
'resource_type' => $resourceType, 'resource_type' => $resourceType,
'resource_id' => $resourceId, 'resource_id' => $resourceId,
'valid_until' => CarbonImmutable::now()->addDays(90), 'server_id' => $serverId,
'valid_until' => CarbonImmutable::now()->addDays($validityDays),
]); ]);
} catch (\Throwable $e) { } catch (\Throwable $e) {
throw new \RuntimeException('SSL Certificate generation failed: '.$e->getMessage(), 0, $e); throw new \RuntimeException('SSL Certificate generation failed: '.$e->getMessage(), 0, $e);