Skip to content

Commit

Permalink
fix: subject argument
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Mar 26, 2022
1 parent 59beb17 commit 95b6cce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
37 changes: 26 additions & 11 deletions lib/pki/certificate_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ export interface CertificateManagerOptions {
location: string;
}

export interface Callback11<C> {
(err: null, t: C): void;
(err: Error): void;
}
export interface Callback22 {
(err?: Error | null): void;
(err?: Error): void;
}

export interface CreateSelfSignCertificateParam1 extends CreateSelfSignCertificateParam {
outputFile?: Filename; // default : own/cert/self_signed_certificate.pem
subject: SubjectOptions | string;
Expand Down Expand Up @@ -538,12 +547,12 @@ export class CertificateManager {
return callback();
}
this.state = CertificateManagerState.Initializing;
return this._initialize((err?: Error) => {
return this._initialize((err?: Error | null) => {
this.state = CertificateManagerState.Initialized;
return callback(err);
});
}
private _initialize(callback: (err?: Error) => void): void {
private _initialize(callback: ErrorCallback): void {
assert((this.state = CertificateManagerState.Initializing));
const pkiDir = this.location;
mkdir(pkiDir);
Expand Down Expand Up @@ -597,7 +606,7 @@ export class CertificateManager {
this._readCertificates(() => callback());
}
});
}, callback);
}, callback as ErrorCallback);
}

public async dispose(): Promise<void> {
Expand Down Expand Up @@ -626,9 +635,12 @@ export class CertificateManager {
}
}

private withLock<C extends (err?: Error | null, t?: unknown) => void>(action: (callback: C) => void, callback: C) {
private withLock<T extends void = void>(action: (callback: Callback22) => void, callback: Callback22): void;
private withLock<T>(action: (callback: Callback11<T>) => void, callback: Callback11<T>): void;
// eslint-disable-next-line @typescript-eslint/ban-types
private withLock(action: Function, callback: Function): void {
this.withLock2(promisify<any>(action as any))
.then((t: any) => callback(null, t))
.then((t: unknown) => callback(null, t))
.catch((err) => callback(err));
}
private async withLock2<T>(action: () => Promise<T>): Promise<T> {
Expand All @@ -645,7 +657,7 @@ export class CertificateManager {
public async createSelfSignedCertificate(params: CreateSelfSignCertificateParam1): Promise<void>;
public createSelfSignedCertificate(params: CreateSelfSignCertificateParam1, callback: ErrorCallback): void;
public createSelfSignedCertificate(params: CreateSelfSignCertificateParam1, ...args: any[]): any {
const callback = args[0];
const callback = args[0] as ErrorCallback;
assert(typeof params.applicationUri === "string", "expecting applicationUri");
if (!fs.existsSync(this.privateKey)) {
return callback(new Error("Cannot find private key " + this.privateKey));
Expand All @@ -658,7 +670,7 @@ export class CertificateManager {
_params.rootDir = this.rootDir;
_params.configFile = this.configFile;
_params.privateKey = this.privateKey;
this.withLock<ErrorCallback>((callback) => {
this.withLock((callback) => {
createSelfSignCertificate(certificateFilename, _params, callback);
}, callback);
}
Expand All @@ -685,13 +697,16 @@ export class CertificateManager {
_params.configFile = this.configFile;
_params.privateKey = this.privateKey;

this.withLock((callback) => {
this.withLock<string>((callback) => {
// compose a file name for the request
const now = new Date();
const today = now.toISOString().slice(0, 10) + "_" + now.getTime();
const certificateSigningRequestFilename = path.join(this.rootDir, "own/certs", "certificate_" + today + ".csr");
createCertificateSigningRequest(certificateSigningRequestFilename, _params, (err?: Error) => {
return callback!(err!, certificateSigningRequestFilename);
if (err) {
return callback(err);
}
return callback(null, certificateSigningRequestFilename);
});
}, callback);
}
Expand Down Expand Up @@ -857,8 +872,8 @@ export class CertificateManager {
newStatus === "rejected"
? this.rejectedFolder
: newStatus === "trusted"
? this.trustedFolder
: this.rejectedFolder;
? this.trustedFolder
: this.rejectedFolder;
const certificateDest = path.join(destFolder, path.basename(certificateSrc));

debugLog("_moveCertificate1", fingerprint.substr(0, 10), "old name", certificateSrc);
Expand Down
4 changes: 2 additions & 2 deletions test/test_crypto_create_CA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe("testing test_crypto_create_CA", function (this: Mocha.Suite) {
it("@2 should create a CA with a customer subject", (done: ErrorCallback) => {
const cwd = path.join(__dirname, "../tmp/tmpCAcustomSubject");
fs.mkdirSync(cwd);
call_crypto_create_CA("createCA --keySize 4096 --subject '/CN=Toto/C=FR/O=MyOrganization'", cwd, () => {
call_crypto_create_CA("createCA --keySize 4096 --subject /CN=Toto/C=FR/O=MyOrganization", cwd, () => {
const caPrivateKey = path.join(__dirname, "../tmp/tmpCAcustomSubject/certificates/CA/private/cakey.pem");
fs.existsSync(caPrivateKey).should.eql(true, "caPrivateKey shall exist : " + caPrivateKey);
done();
Expand Down Expand Up @@ -347,7 +347,7 @@ describe("testing test_crypto_create_CA", function (this: Mocha.Suite) {
const validity = 10; // days

call_crypto_create_CA(
"certificate -v " + validity + " --subject='/C=FR/ST=Centre/L=Orleans/O=SomeOrganization/CN=Hello' -o mycert.pem",
"certificate -v " + validity + " --subject=/C=FR/ST=Centre/L=Orleans/O=SomeOrganization/CN=Hello -o mycert.pem",
cwd,
() => {
fs.existsSync(expectedCertificate).should.eql(true);
Expand Down

0 comments on commit 95b6cce

Please sign in to comment.