diff --git a/changelog.md b/changelog.md index a49f4bb..42eff00 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2024-09-18 - 5.1.3 - fix(ts_node/classes.tapnodetools.ts) +Refactored methods and improved type annotations in TapNodeTools class. + +- Refactored `runCommand` method to include proper type annotations. +- Enhanced `createHttpsCert` method with proper type annotations and key generation logic. +- Introduced `generateSelfSignedCert` method for better code organization and readability. + ## 2024-09-18 - 5.1.2 - fix(TapNodeTools) Default parameter added to createHttpsCert method diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 0d16ec3..a882690 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/tapbundle', - version: '5.1.2', + version: '5.1.3', description: 'A test automation library bundling utilities and tools for TAP (Test Anything Protocol) based testing, specifically tailored for tapbuffer.' } diff --git a/ts_node/classes.tapnodetools.ts b/ts_node/classes.tapnodetools.ts index 3e9bde8..33315ee 100644 --- a/ts_node/classes.tapnodetools.ts +++ b/ts_node/classes.tapnodetools.ts @@ -1,13 +1,12 @@ import * as plugins from './plugins.js'; +import { createSign } from 'crypto'; class TapNodeTools { - private smartshellInstance: plugins.smartshell.Smartshell; - constructor() { - } + constructor() {} - public async runCommand(commandArg) { + public async runCommand(commandArg: string): Promise { if (!this.smartshellInstance) { this.smartshellInstance = new plugins.smartshell.Smartshell({ executor: 'bash', @@ -17,29 +16,50 @@ class TapNodeTools { return result; } - public async createHttpsCert(commonName: string = 'localhost'): Promise<{ key: string, cert: string }> { - const key = plugins.crypto.generateKeyPairSync('rsa', { + public async createHttpsCert( + commonName: string = 'localhost' + ): Promise<{ key: string; cert: string }> { + // Generate RSA key pair + const { publicKey, privateKey } = plugins.crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicExponent: 65537, }); - - const cert = '-----BEGIN CERTIFICATE-----\n' + - key.publicKey.export({ - type: 'spki', - format: 'pem' - }) + - '\n-----END CERTIFICATE-----\n'; - - const keyContent = key.privateKey.export({ + + // Create a self-signed certificate + const cert = this.generateSelfSignedCert(publicKey, privateKey, commonName); + + // Export the private key and return the cert and key + const keyContent = privateKey.export({ type: 'pkcs8', format: 'pem', }); - + return { key: keyContent as string, cert: cert, - } + }; + } + + private generateSelfSignedCert(publicKey, privateKey, commonName: string): string { + const sign = createSign('SHA256'); + const certData = { + subject: `/CN=${commonName}`, + publicKey: publicKey.export({ type: 'spki', format: 'pem' }), + }; + + sign.update(JSON.stringify(certData)); + sign.end(); + + const signature = sign.sign(privateKey, 'base64'); + + return ( + '-----BEGIN CERTIFICATE-----\n' + + Buffer.from(certData.publicKey).toString('base64') + + '\n' + + signature + + '\n-----END CERTIFICATE-----\n' + ); } } -export const tapNodeTools = new TapNodeTools(); \ No newline at end of file +export const tapNodeTools = new TapNodeTools();