UNPKG

@redocly/cli

Version:

[@Redocly](https://redocly.com) CLI is your all-in-one OpenAPI utility. It builds, manages, improves, and quality-checks your OpenAPI descriptions, all of which comes in handy for various phases of the API Lifecycle. Create your own rulesets to make API g

45 lines 1.89 kB
import * as fs from 'node:fs'; import * as path from 'node:path'; export function resolveMtlsCertificates(mtlsCertificates = {}, workingDir) { const { clientCert, clientKey, caCert } = mtlsCertificates; return { clientCert: resolveCertificate(clientCert, workingDir), clientKey: resolveCertificate(clientKey, workingDir), caCert: resolveCertificate(caCert, workingDir), }; } function resolveCertificate(cert, workingDir) { if (!cert) return undefined; try { // Check if the string looks like a certificate content const isCertContent = cert.includes('-----BEGIN') && cert.includes('-----END'); if (!isCertContent) { const certPath = path.resolve(workingDir, cert); // If not a certificate content, treat as file path fs.accessSync(certPath, fs.constants.R_OK); return fs.readFileSync(certPath, 'utf-8'); } // Return the certificate content as-is return formatCertificate(cert); } catch (error) { throw new Error(`Failed to read certificate: ${error.message}`); } } function formatCertificate(cert) { // Split the content into header, body, and footer const matches = cert.match(/^(-----BEGIN[^-]+-----)\r?\n([A-Za-z0-9+/=\r\n\t ]+)\r?\n(-----END[^-]+-----)/); if (!matches) { throw new Error('Invalid certificate format'); } const [, header, body, footer] = matches; // Format the body with proper line breaks (64 characters per line) const formattedBody = body .replace(/\s+/g, '') // Remove all whitespace .match(/.{1,64}/g) // Split into 64-character chunks ?.join('\n') || ''; // Join with newlines // Reconstruct the properly formatted certificate return `${header}\n${formattedBody}\n${footer}`; } //# sourceMappingURL=resolve-mtls-certificates.js.map