@redocly/cli
Version:
[@Redocly](https://redocly.com) CLI is your all-in-one API documentation utility. It builds, manages, improves, and quality-checks your API descriptions, all of which comes in handy for various phases of the API Lifecycle. Create your own rulesets to make
48 lines • 2.02 kB
JavaScript
import * as fs from 'node:fs';
import * as path from 'node:path';
export function resolveMtlsCertificates(perDomainCertificates = {}, workingDir) {
const resolved = {};
for (const [domain, certs] of Object.entries(perDomainCertificates)) {
resolved[domain] = {
clientCert: resolveCertificate(certs['clientCert'], workingDir),
clientKey: resolveCertificate(certs['clientKey'], workingDir),
caCert: resolveCertificate(certs['caCert'], workingDir),
};
}
return resolved;
}
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