Skip to content

Commit

Permalink
chore: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Jan 29, 2025
1 parent 399a051 commit 34d173e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
2 changes: 0 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ export * from "./pki/certificate_manager";
export * from "./misc/subject";

export { CertificateAuthority } from "./ca/certificate_authority";
// export * from "./misc/install_prerequisite";
// export * from "./toolbox/with_openssl/toolbox";
39 changes: 17 additions & 22 deletions lib/pki/certificate_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ import { SubjectOptions } from "../misc/subject";
import { CertificateStatus, Filename, KeySize, Thumbprint } from "../toolbox/common";

import { debugLog, warningLog } from "../toolbox/debug";
import { make_path, mkdir } from "../toolbox/common2";
import { make_path, mkdirSync } from "../toolbox/common2";

import { CreateSelfSignCertificateParam, CreateSelfSignCertificateWithConfigParam } from "../toolbox/common";
import { createCertificateSigningRequestWithOpenSSL, dumpCertificate } from "../toolbox/with_openssl";
import { createSelfSignedCertificate } from "../toolbox/without_openssl";
import { createCertificateSigningRequestAsync, createSelfSignedCertificate } from "../toolbox/without_openssl";

import _simple_config_template from "./templates/simple_config_template.cnf";
/**
Expand Down Expand Up @@ -266,7 +265,7 @@ export class CertificateManager {
this.location = make_path(options.location, "");
this.keySize = options.keySize;

mkdir(options.location);
mkdirSync(options.location);

// istanbul ignore next
if (!fs.existsSync(this.location)) {
Expand Down Expand Up @@ -583,18 +582,18 @@ export class CertificateManager {
private async _initialize(): Promise<void> {
assert((this.state = CertificateManagerState.Initializing));
const pkiDir = this.location;
mkdir(pkiDir);
mkdir(path.join(pkiDir, "own"));
mkdir(path.join(pkiDir, "own/certs"));
mkdir(path.join(pkiDir, "own/private"));
mkdir(path.join(pkiDir, "rejected"));
mkdir(path.join(pkiDir, "trusted"));
mkdir(path.join(pkiDir, "trusted/certs"));
mkdir(path.join(pkiDir, "trusted/crl"));

mkdir(path.join(pkiDir, "issuers"));
mkdir(path.join(pkiDir, "issuers/certs")); // contains Trusted CA certificates
mkdir(path.join(pkiDir, "issuers/crl")); // contains CRL of revoked CA certificates
mkdirSync(pkiDir);
mkdirSync(path.join(pkiDir, "own"));
mkdirSync(path.join(pkiDir, "own/certs"));
mkdirSync(path.join(pkiDir, "own/private"));
mkdirSync(path.join(pkiDir, "rejected"));
mkdirSync(path.join(pkiDir, "trusted"));
mkdirSync(path.join(pkiDir, "trusted/certs"));
mkdirSync(path.join(pkiDir, "trusted/crl"));

mkdirSync(path.join(pkiDir, "issuers"));
mkdirSync(path.join(pkiDir, "issuers/certs")); // contains Trusted CA certificates
mkdirSync(path.join(pkiDir, "issuers/crl")); // contains CRL of revoked CA certificates

if (!fs.existsSync(this.configFile) || !fs.existsSync(this.privateKey)) {
return await this.withLock2(async () => {
Expand Down Expand Up @@ -705,7 +704,7 @@ export class CertificateManager {
const now = new Date();
const today = now.toISOString().slice(0, 10) + "_" + now.getTime();
const certificateSigningRequestFilename = path.join(this.rootDir, "own/certs", "certificate_" + today + ".csr");
await createCertificateSigningRequestWithOpenSSL(certificateSigningRequestFilename, _params);
await createCertificateSigningRequestAsync(certificateSigningRequestFilename, _params);
return certificateSigningRequestFilename;
});
}
Expand Down Expand Up @@ -817,11 +816,7 @@ export class CertificateManager {
wantedIssuerKey,
selectedTrustedCertificates.length,
);
for (const entry of selectedTrustedCertificates) {
const data = await dumpCertificate(entry.filename);
debugLog(" ", entry.filename);
debugLog(data);
}

}
return selectedTrustedCertificates.length > 0 ? selectedTrustedCertificates[0].certificate : null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/toolbox/common2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function certificateFileExist(certificateFile: string): boolean {
return true;
}

export async function mkdir(folder: string): Promise<void> {
export function mkdirSync(folder: string): void {
if (!fs.existsSync(folder)) {
// istanbul ignore next
debugLog(chalk.white(" .. constructing "), folder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import assert from "assert";
import fs from "fs";
import { CreateCertificateSigningRequestWithConfigOptions } from "../common";
import { Subject, pemToPrivateKey } from "node-opcua-crypto";
import { createCertificateSigningRequest } from "node-opcua-crypto";
import { display, displaySubtitle } from "../display";
import { createCertificateSigningRequest as createCertificateSigningRequest1 } from "node-opcua-crypto";

/**
* create a certificate signing request
Expand All @@ -50,7 +50,7 @@ export async function createCertificateSigningRequestAsync(
const privateKeyPem = await fs.promises.readFile(params.privateKey, "utf-8");
const privateKey = await pemToPrivateKey(privateKeyPem);

const { csr } = await createCertificateSigningRequest1({
const { csr } = await createCertificateSigningRequest({
privateKey,
dns: params.dns,
ip: params.ip,
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import rimraf from "rimraf";

import { g_config, mkdir, warningLog } from "../lib/index";
import { g_config, mkdirSync, warningLog } from "../lib/index";

const tmpFolder = path.join(__dirname, "../tmp");

Expand Down Expand Up @@ -46,7 +46,7 @@ export function beforeTest(self: Mocha.Suite, nextFunction?: () => Promise<void>
warningLog(" .... cleaning temporary folders ...", tmpFolder);
await rimraf(tmpFolder);
warningLog(" ..... folder cleaned");
await mkdir(tmpFolder);
mkdirSync(tmpFolder);
warningLog(" ..... creating empty folder", tmpFolder);
}
await next();
Expand Down
4 changes: 2 additions & 2 deletions test/test_toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import "should";

import { mkdir, warningLog } from "../lib";
import { mkdirSync, warningLog } from "../lib";
import { getPublicKeyFromCertificate, getPublicKeyFromPrivateKey } from "../lib/toolbox/with_openssl";
import { beforeTest } from "./helpers";
import { generatePrivateKeyFile } from "node-opcua-crypto";
Expand All @@ -16,7 +16,7 @@ describe("testing NodeOPCUA PKI Toolbox", function (this: Mocha.Suite) {
before(async () => {

privateKey = path.join(testData.tmpFolder, "some_private_key");
await mkdir(testData.tmpFolder);
mkdirSync(testData.tmpFolder);

warningLog("generating private key");
await generatePrivateKeyFile(privateKey, 2048);
Expand Down

0 comments on commit 34d173e

Please sign in to comment.