// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import * as path from 'node:path'; import { homedir } from 'node:os'; import { FileSystem } from '@rushstack/node-core-library'; /** * Options for configuring paths and filenames used by the `CertificateStore`. * @public */ export interface ICertificateStoreOptions { /** * Path to the directory where the certificate store will be created. * If not provided, it defaults to `/.rushstack`. */ storePath?: string; /** * Filename of the CA certificate file within the store directory. * If not provided, it defaults to `rushstack-ca.pem`. */ caCertificateFilename?: string; /** * Filename of the TLS certificate file within the store directory. * If not provided, it defaults to `rushstack-serve.pem`. */ certificateFilename?: string; /** * Filename of the TLS key file within the store directory. * If not provided, it defaults to `rushstack-serve.key`. */ keyFilename?: string; } /** * Store to retrieve and save debug certificate data. * @public */ export class CertificateStore { readonly #caCertificatePath: string; readonly #certificatePath: string; readonly #keyPath: string; readonly #storePath: string; #caCertificateData: string | undefined; #certificateData: string | undefined; #keyData: string | undefined; public constructor(options: ICertificateStoreOptions = {}) { const requestedStorePath: string | undefined = options.storePath; let storePath: string | undefined; let debugCertificateManagerConfig: ICertificateStoreOptions | undefined = undefined; if (requestedStorePath) { storePath = path.resolve(requestedStorePath); } else { // TLS Sync extension configuration lives in `.vscode/debug-certificate-manager.json` let currentDir: string | undefined = process.cwd(); while (currentDir) { const debugCertificateManagerConfigFile: string = path.join( currentDir, '.vscode', 'debug-certificate-manager.json' ); if (FileSystem.exists(debugCertificateManagerConfigFile)) { const configContent: string = FileSystem.readFile(debugCertificateManagerConfigFile); debugCertificateManagerConfig = JSON.parse(configContent) as ICertificateStoreOptions; if (debugCertificateManagerConfig.storePath) { storePath = debugCertificateManagerConfig.storePath; if (storePath.startsWith('~')) { storePath = path.join(homedir(), storePath.slice(2)); } else { storePath = path.resolve(currentDir, debugCertificateManagerConfig.storePath); } } break; // found the config file, stop searching } const parentDir: string | undefined = path.dirname(currentDir); if (parentDir === currentDir) { break; // reached the root directory } currentDir = parentDir; } if (!storePath) { // Fallback to the user's home directory under `.rushstack` const unresolvedUserFolder: string = homedir(); const userProfilePath: string = path.resolve(unresolvedUserFolder); if (!FileSystem.exists(userProfilePath)) { throw new Error("Unable to determine the current user's home directory"); } storePath = path.join(userProfilePath, '.rushstack'); } } FileSystem.ensureFolder(storePath); const caCertificatePath: string = path.join( storePath, options.caCertificateFilename ?? debugCertificateManagerConfig?.caCertificateFilename ?? 'rushstack-ca.pem' ); const certificatePath: string = path.join( storePath, options.certificateFilename ?? debugCertificateManagerConfig?.certificateFilename ?? 'rushstack-serve.pem' ); const keyPath: string = path.join( storePath, options.keyFilename ?? debugCertificateManagerConfig?.keyFilename ?? 'rushstack-serve.key' ); this.#storePath = storePath; this.#caCertificatePath = caCertificatePath; this.#certificatePath = certificatePath; this.#keyPath = keyPath; } /** * Path to the directory where the debug certificates are stored. */ public get storePath(): string { return this.#storePath; } /** * Path to the saved debug CA certificate */ public get caCertificatePath(): string { return this.#caCertificatePath; } /** * Path to the saved debug TLS certificate */ public get certificatePath(): string { return this.#certificatePath; } /** * Path to the saved debug TLS key */ public get keyPath(): string { return this.#keyPath; } /** * Debug Certificate Authority certificate pem file contents. */ public get caCertificateData(): string | undefined { if (!this.#caCertificateData) { try { this.#caCertificateData = FileSystem.readFile(this.#caCertificatePath); } catch (err) { if (!FileSystem.isNotExistError(err)) { throw err; } } } return this.#caCertificateData; } public set caCertificateData(certificate: string | undefined) { if (certificate) { FileSystem.writeFile(this.#caCertificatePath, certificate); } else if (FileSystem.exists(this.#caCertificatePath)) { FileSystem.deleteFile(this.#caCertificatePath); } this.#caCertificateData = certificate; } /** * Debug TLS Server certificate pem file contents. */ public get certificateData(): string | undefined { if (!this.#certificateData) { try { this.#certificateData = FileSystem.readFile(this.#certificatePath); } catch (err) { if (!FileSystem.isNotExistError(err)) { throw err; } } } return this.#certificateData; } public set certificateData(certificate: string | undefined) { if (certificate) { FileSystem.writeFile(this.#certificatePath, certificate); } else if (FileSystem.exists(this.#certificatePath)) { FileSystem.deleteFile(this.#certificatePath); } this.#certificateData = certificate; } /** * Key used to sign the debug pem certificate. */ public get keyData(): string | undefined { if (!this.#keyData) { try { this.#keyData = FileSystem.readFile(this.#keyPath); } catch (err) { if (!FileSystem.isNotExistError(err)) { throw err; } } } return this.#keyData; } public set keyData(key: string | undefined) { if (key) { FileSystem.writeFile(this.#keyPath, key); } else if (FileSystem.exists(this.#keyPath)) { FileSystem.deleteFile(this.#keyPath); } this.#keyData = key; } }