Compare commits

...

12 Commits

7 changed files with 5433 additions and 4114 deletions

View File

@ -1,5 +1,43 @@
# Changelog
## 2024-11-06 - 5.4.0 - feat(node)
Add smartmongo and smarts3 integration in node tools
- Added createSmartmongo method to TapNodeTools for creating SmartMongo instances.
- Added createSmarts3 method to TapNodeTools for creating Smarts3 instances.
- Updated dependencies with smartmongo and smarts3 in package.json.
## 2024-09-19 - 5.3.0 - feat(TapNodeTools)
Add getEnvVarOnDemand method to TapNodeTools
- Introduced a new method getEnvVarOnDemand to the TapNodeTools class to fetch environment variables on demand.
- Enhanced getQenv function in TapNodeTools class to cache the Qenv instance for better performance.
## 2024-09-19 - 5.2.2 - fix(core)
Ensure reliability in test setup and execution
- Added new pre-task functionality to log starting of tasks.
- Enhanced `runCommand` method to better handle shell command execution.
- Fixed issue in `createHttpsCert` to correctly generate self-signed certificates.
## 2024-09-19 - 5.2.1 - fix(tapbundle)
Add qenv package to dependencies for environment management
- Added @push.rocks/qenv to dependencies in package.json.
- Updated TapNodeTools class in ts_node/classes.tapnodetools.ts to include getQenv method.
- Imported qenv in ts_node/plugins.ts.
## 2024-09-18 - 5.2.0 - feat(TapNodeTools)
Add ability to create HTTPS certificates with self-signed option
- Introduced a new parameter `allowSelfSigned` to the `createHttpsCert` function.
## 2024-09-18 - 5.1.4 - fix(ts_node)
Fixed issues in HTTPS certificate generation for TapNodeTools
- Updated 'createHttpsCert' method in 'TapNodeTools' to use 'smartcrypto' for generating RSA key pair and self-signed certificate.
- Corrected certificate and private key PEM encoding.
## 2024-09-18 - 5.1.3 - fix(ts_node/classes.tapnodetools.ts)
Refactored methods and improved type annotations in TapNodeTools class.

View File

@ -1,7 +1,7 @@
{
"name": "@push.rocks/tapbundle",
"private": false,
"version": "5.1.3",
"version": "5.4.0",
"description": "A test automation library bundling utilities and tools for TAP (Test Anything Protocol) based testing, specifically tailored for tapbuffer.",
"exports": {
".": "./dist_ts/index.js",
@ -26,11 +26,15 @@
"dependencies": {
"@open-wc/testing": "^4.0.0",
"@push.rocks/consolecolor": "^2.0.2",
"@push.rocks/qenv": "^6.0.5",
"@push.rocks/smartcrypto": "^2.0.4",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/smartexpect": "^1.2.1",
"@push.rocks/smartjson": "^5.0.20",
"@push.rocks/smartmongo": "^2.0.10",
"@push.rocks/smartpromise": "^4.0.4",
"@push.rocks/smarts3": "^2.2.1",
"@push.rocks/smartshell": "^3.0.6",
"@push.rocks/smarttime": "^4.0.8"
},

9389
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -11,8 +11,18 @@ tap.test('should create a https cert', async () => {
const { key, cert } = await tapNodeTools.createHttpsCert('localhost');
console.log(key);
console.log(cert);
expect(key).toInclude('-----BEGIN PRIVATE KEY-----');
expect(key).toInclude('-----BEGIN RSA PRIVATE KEY-----');
expect(cert).toInclude('-----BEGIN CERTIFICATE-----');
});
tap.test('should create a smartmongo instance', async () => {
const smartmongo = await tapNodeTools.createSmartmongo();
await smartmongo.stop();
});
tap.test('should create a smarts3 instance', async () => {
const smarts3 = await tapNodeTools.createSmarts3();
await smarts3.stop();
});
tap.start();

View File

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

View File

@ -1,11 +1,20 @@
import * as plugins from './plugins.js';
import { createSign } from 'crypto';
class TapNodeTools {
private smartshellInstance: plugins.smartshell.Smartshell;
constructor() {}
private qenv: plugins.qenv.Qenv;
public async getQenv(): Promise<plugins.qenv.Qenv> {
this.qenv = this.qenv || new plugins.qenv.Qenv('./', '.nogit/');
return this.qenv;
}
public async getEnvVarOnDemand(envVarNameArg: string): Promise<string> {
const qenv = await this.getQenv();
return qenv.getEnvVarOnDemand(envVarNameArg);
}
public async runCommand(commandArg: string): Promise<any> {
if (!this.smartshellInstance) {
this.smartshellInstance = new plugins.smartshell.Smartshell({
@ -17,48 +26,69 @@ class TapNodeTools {
}
public async createHttpsCert(
commonName: string = 'localhost'
commonName: string = 'localhost',
allowSelfSigned: boolean = true
): Promise<{ key: string; cert: string }> {
// Generate RSA key pair
const { publicKey, privateKey } = plugins.crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicExponent: 65537,
});
if (allowSelfSigned) {
// set node to allow self-signed certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
// Generate a key pair
const keys = plugins.smartcrypto.nodeForge.pki.rsa.generateKeyPair(2048);
// Create a self-signed certificate
const cert = this.generateSelfSignedCert(publicKey, privateKey, commonName);
const cert = plugins.smartcrypto.nodeForge.pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
// Export the private key and return the cert and key
const keyContent = privateKey.export({
type: 'pkcs8',
format: 'pem',
});
const attrs = [
{ name: 'commonName', value: commonName },
{ name: 'countryName', value: 'US' },
{ shortName: 'ST', value: 'California' },
{ name: 'localityName', value: 'San Francisco' },
{ name: 'organizationName', value: 'My Company' },
{ shortName: 'OU', value: 'Dev' },
];
cert.setSubject(attrs);
cert.setIssuer(attrs);
// Sign the certificate with its own private key (self-signed)
cert.sign(keys.privateKey, plugins.smartcrypto.nodeForge.md.sha256.create());
// PEM encode the private key and certificate
const pemKey = plugins.smartcrypto.nodeForge.pki.privateKeyToPem(keys.privateKey);
const pemCert = plugins.smartcrypto.nodeForge.pki.certificateToPem(cert);
return {
key: keyContent as string,
cert: cert,
key: pemKey,
cert: pemCert,
};
}
private generateSelfSignedCert(publicKey, privateKey, commonName: string): string {
const sign = createSign('SHA256');
const certData = {
subject: `/CN=${commonName}`,
publicKey: publicKey.export({ type: 'spki', format: 'pem' }),
};
/**
* create and return a smartmongo instance
*/
public async createSmartmongo() {
const smartmongoMod = await import('@push.rocks/smartmongo');
const smartmongoInstance = new smartmongoMod.SmartMongo();
await smartmongoInstance.start();
return smartmongoInstance;
}
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'
);
/**
* create and return a smarts3 instance
*/
public async createSmarts3() {
const smarts3Mod = await import('@push.rocks/smarts3');
const smarts3Instance = new smarts3Mod.Smarts3({
cleanSlate: true,
});
await smarts3Instance.start();
return smarts3Instance;
}
}

View File

@ -5,6 +5,8 @@ import * as fs from 'fs';
export { crypto,fs };
// @push.rocks scope
import * as qenv from '@push.rocks/qenv';
import * as smartcrypto from '@push.rocks/smartcrypto';
import * as smartshell from '@push.rocks/smartshell';
export { smartshell };
export { qenv, smartcrypto, smartshell };