mirror of
https://github.com/dyzulk/trustlab-api.git
synced 2026-01-26 05:15:35 +07:00
feat: unify ca_certificates migrations (clean slate)
This commit is contained in:
@@ -27,6 +27,7 @@ class PublicCaController extends Controller
|
||||
'serial' => $cert->serial_number,
|
||||
'expires_at' => $cert->valid_to->toIso8601String(),
|
||||
'cdn_url' => $cert->cert_path ? Storage::disk('r2-public')->url($cert->cert_path) : null,
|
||||
'der_cdn_url' => $cert->der_path ? Storage::disk('r2-public')->url($cert->der_path) : null,
|
||||
];
|
||||
});
|
||||
|
||||
@@ -52,6 +53,11 @@ class PublicCaController extends Controller
|
||||
}
|
||||
|
||||
if ($format === 'der') {
|
||||
// Redirect to CDN if path exists and format is DER
|
||||
if ($cert->der_path) {
|
||||
return redirect()->away(Storage::disk('r2-public')->url($cert->der_path));
|
||||
}
|
||||
|
||||
// Convert PEM to DER (Base64 decode the body)
|
||||
$pem = $cert->cert_content;
|
||||
$lines = explode("\n", trim($pem));
|
||||
|
||||
@@ -24,6 +24,7 @@ class CaCertificate extends Model
|
||||
'valid_from',
|
||||
'valid_to',
|
||||
'cert_path',
|
||||
'der_path',
|
||||
'last_synced_at',
|
||||
'download_count',
|
||||
'last_downloaded_at'
|
||||
|
||||
@@ -412,20 +412,39 @@ class OpenSslService
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Upload CA certificate (public) to R2 CDN.
|
||||
* Upload CA certificate (public) to R2 CDN in both PEM and DER formats.
|
||||
*/
|
||||
public function uploadToCdn(CaCertificate $cert)
|
||||
{
|
||||
try {
|
||||
$filename = 'ca/' . Str::slug($cert->common_name) . '-' . $cert->uuid . '.crt';
|
||||
$baseFilename = 'ca/' . Str::slug($cert->common_name) . '-' . $cert->uuid;
|
||||
$pemFilename = $baseFilename . '.crt';
|
||||
$derFilename = $baseFilename . '.der';
|
||||
|
||||
Storage::disk('r2-public')->put($filename, $cert->cert_content, [
|
||||
// 1. Upload PEM (.crt)
|
||||
Storage::disk('r2-public')->put($pemFilename, $cert->cert_content, [
|
||||
'visibility' => 'public',
|
||||
'ContentType' => 'application/x-x509-ca-cert'
|
||||
]);
|
||||
|
||||
// 2. Convert to DER and Upload (.der)
|
||||
$lines = explode("\n", trim($cert->cert_content));
|
||||
$payload = '';
|
||||
foreach ($lines as $line) {
|
||||
if (!str_starts_with($line, '-----')) {
|
||||
$payload .= trim($line);
|
||||
}
|
||||
}
|
||||
$derContent = base64_decode($payload);
|
||||
|
||||
Storage::disk('r2-public')->put($derFilename, $derContent, [
|
||||
'visibility' => 'public',
|
||||
'ContentType' => 'application/x-x509-ca-cert'
|
||||
]);
|
||||
|
||||
$cert->update([
|
||||
'cert_path' => $filename,
|
||||
'cert_path' => $pemFilename,
|
||||
'der_path' => $derFilename,
|
||||
'last_synced_at' => now()
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user