fix(ts_node/classes.tapnodetools.ts): Refactored methods and improved type annotations in TapNodeTools class.

This commit is contained in:
Philipp Kunz 2024-09-18 15:30:47 +02:00
parent 7c450876f2
commit 3d39f65ed0
3 changed files with 46 additions and 19 deletions

View File

@ -1,5 +1,12 @@
# Changelog # 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) ## 2024-09-18 - 5.1.2 - fix(TapNodeTools)
Default parameter added to createHttpsCert method Default parameter added to createHttpsCert method

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/tapbundle', 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.' description: 'A test automation library bundling utilities and tools for TAP (Test Anything Protocol) based testing, specifically tailored for tapbuffer.'
} }

View File

@ -1,13 +1,12 @@
import * as plugins from './plugins.js'; import * as plugins from './plugins.js';
import { createSign } from 'crypto';
class TapNodeTools { class TapNodeTools {
private smartshellInstance: plugins.smartshell.Smartshell; private smartshellInstance: plugins.smartshell.Smartshell;
constructor() {
} constructor() {}
public async runCommand(commandArg) { public async runCommand(commandArg: string): Promise<any> {
if (!this.smartshellInstance) { if (!this.smartshellInstance) {
this.smartshellInstance = new plugins.smartshell.Smartshell({ this.smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash', executor: 'bash',
@ -17,20 +16,20 @@ class TapNodeTools {
return result; return result;
} }
public async createHttpsCert(commonName: string = 'localhost'): Promise<{ key: string, cert: string }> { public async createHttpsCert(
const key = plugins.crypto.generateKeyPairSync('rsa', { commonName: string = 'localhost'
): Promise<{ key: string; cert: string }> {
// Generate RSA key pair
const { publicKey, privateKey } = plugins.crypto.generateKeyPairSync('rsa', {
modulusLength: 2048, modulusLength: 2048,
publicExponent: 65537, publicExponent: 65537,
}); });
const cert = '-----BEGIN CERTIFICATE-----\n' + // Create a self-signed certificate
key.publicKey.export({ const cert = this.generateSelfSignedCert(publicKey, privateKey, commonName);
type: 'spki',
format: 'pem'
}) +
'\n-----END CERTIFICATE-----\n';
const keyContent = key.privateKey.export({ // Export the private key and return the cert and key
const keyContent = privateKey.export({
type: 'pkcs8', type: 'pkcs8',
format: 'pem', format: 'pem',
}); });
@ -38,7 +37,28 @@ class TapNodeTools {
return { return {
key: keyContent as string, key: keyContent as string,
cert: cert, 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'
);
} }
} }