fix(core): update

This commit is contained in:
Philipp Kunz 2019-01-15 23:39:31 +01:00
parent 08c3eaa65f
commit 4ee1c4b08c
5 changed files with 69 additions and 48 deletions

View File

@ -30,6 +30,7 @@
"@pushrocks/smartdelay": "^2.0.2", "@pushrocks/smartdelay": "^2.0.2",
"@pushrocks/smartdns": "^3.0.8", "@pushrocks/smartdns": "^3.0.8",
"@pushrocks/smartexpress": "^3.0.4", "@pushrocks/smartexpress": "^3.0.4",
"@pushrocks/smartlog": "^2.0.9",
"@pushrocks/smartpromise": "^2.0.5", "@pushrocks/smartpromise": "^2.0.5",
"@pushrocks/smartrequest": "^1.1.14", "@pushrocks/smartrequest": "^1.1.14",
"@pushrocks/smartstring": "^3.0.8", "@pushrocks/smartstring": "^3.0.8",

View File

@ -56,10 +56,8 @@ export class CertManager {
} }
/** /**
* stores the certificate with the * stores the certificate
* @param publicKeyArg * @param optionsArg
* @param privateKeyArg
* @param csrArg
*/ */
public async storeCertificate(optionsArg: interfaces.ICert) { public async storeCertificate(optionsArg: interfaces.ICert) {
const cert = new Cert(optionsArg); const cert = new Cert(optionsArg);
@ -70,14 +68,25 @@ export class CertManager {
} }
public async getCertificateStatus(domainNameArg: string): Promise<interfaces.TCertStatus> { /**
const isPending = this.pendingMap.checkString('domainNameArg'); * announce a certificate as being in the process of being retrieved
*/
public async announceCertificate (domainNameArg: string) {
this.pendingMap.addString(domainNameArg);
}
/**
* gets the status of a certificate by certDomain name
* @param certDomainArg
*/
public async getCertificateStatus(certDomainArg: string): Promise<interfaces.TCertStatus> {
const isPending = this.pendingMap.checkString(certDomainArg);
if (isPending) { if (isPending) {
return 'pending'; return 'pending';
} }
// otherwise lets continue // otherwise lets continue
const existingCertificate = this.retrieveCertificate(domainNameArg); const existingCertificate = this.retrieveCertificate(certDomainArg);
if (existingCertificate) { if (existingCertificate) {
return 'existing'; return 'existing';
} }

View File

@ -1,34 +1,41 @@
import * as plugins from './smartacme.plugins'; import * as plugins from './smartacme.plugins';
import * as interfaces from './interfaces'; import * as interfaces from './interfaces';
import { ICertRemoteResponse } from './interfaces';
// tslint:disable-next-line: max-classes-per-file // tslint:disable-next-line: max-classes-per-file
export class CertRemoteClient { export class CertRemoteClient {
private remoteUrl: string; private remoteUrl: string;
private secret: string; private secret: string;
private logger: plugins.smartlog.Smartlog;
constructor(optionsArg: { constructor(optionsArg: {
remoteUrl: string; remoteUrl: string;
secret: string; secret: string;
logger?: plugins.smartlog.Smartlog;
}) { }) {
this.remoteUrl = optionsArg.remoteUrl; this.remoteUrl = optionsArg.remoteUrl;
this.secret = optionsArg.secret; this.secret = optionsArg.secret;
optionsArg.logger
? (this.logger = optionsArg.logger)
: (this.logger = plugins.smartlog.defaultLogger);
} }
/** /**
* *
* @param domainNameArg * @param domainNameArg
*/ */
async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> { public async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
let certificate: interfaces.ICert; let certificate: interfaces.ICert;
const doRequestCycle = async (): Promise<interfaces.ICert> => { const doRequestCycle = async (): Promise<interfaces.ICert> => {
const response: ICertRemoteResponse = (await plugins.smartrequest.postJson(this.remoteUrl, { const response: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
requestBody: <interfaces.ICertRemoteRequest>{ this.remoteUrl,
domainName: domainNameArg, {
secret: this.secret requestBody: <interfaces.ICertRemoteRequest>{
domainName: domainNameArg,
secret: this.secret
}
} }
})).body; )).body;
switch(response.status) { switch (response.status as interfaces.TCertStatus) {
case 'pending': case 'pending':
await plugins.smartdelay.delayFor(5000); await plugins.smartdelay.delayFor(5000);
const finalResponse = await doRequestCycle(); const finalResponse = await doRequestCycle();

View File

@ -49,7 +49,36 @@ export class SmartAcme {
/** /**
* the remote handler to hand the request and response to. * the remote handler to hand the request and response to.
*/ */
public certremoteHandler: (req: plugins.smartexpress.Request, res: plugins.smartexpress.Response) => Promise<any>; public certremoteHandler = async (req: plugins.smartexpress.Request, res: plugins.smartexpress.Response) => {
const requestBody: interfaces.ICertRemoteRequest = req.body;
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(requestBody.domainName);
let status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(
certDomain
);
let response: interfaces.ICertRemoteResponse;
switch (status) {
case 'existing':
response = {
status,
certificate: await (await this.certmanager.retrieveCertificate(
certDomain
)).createSavableObject()
};
break;
default:
if (status === "nonexisting") {
this.getCertificateForDomain(certDomain);
status = 'pending';
}
response = {
status
};
break;
}
res.status(200);
res.send(response);
res.end();
}
constructor(optionsArg: ISmartAcmeOptions) { constructor(optionsArg: ISmartAcmeOptions) {
this.options = optionsArg; this.options = optionsArg;
@ -76,36 +105,10 @@ export class SmartAcme {
// CertMatcher // CertMatcher
this.certmatcher = new CertMatcher(); this.certmatcher = new CertMatcher();
// CertRemoteHandler
this.certremoteHandler = async (req, res) => {
const requestBody: interfaces.ICertRemoteRequest = req.body;
const status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(requestBody.domainName);
const existingCertificate = await this.certmanager.retrieveCertificate(
requestBody.domainName
);
let response: interfaces.ICertRemoteResponse;
switch (status) {
case 'existing':
response = {
status,
certificate: await existingCertificate.createSavableObject()
};
break;
default:
response = {
status
};
break;
}
res.status(200);
res.send(response);
res.end();
};
// ACME Client // ACME Client
this.client = new plugins.acme.Client({ this.client = new plugins.acme.Client({
directoryUrl: (() => { directoryUrl: (() => {
if(this.options.environment === 'production') { if (this.options.environment === 'production') {
return plugins.acme.directory.letsencrypt.production; return plugins.acme.directory.letsencrypt.production;
} else { } else {
return plugins.acme.directory.letsencrypt.staging; return plugins.acme.directory.letsencrypt.staging;
@ -127,7 +130,7 @@ export class SmartAcme {
public async getCertificateForDomain(domainArg: string): Promise<Cert> { public async getCertificateForDomain(domainArg: string): Promise<Cert> {
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(domainArg); const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(domainArg);
await this.certmanager.announceCertificate(certDomain);
const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomain); const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomain);
if (retrievedCertificate) { if (retrievedCertificate) {

View File

@ -4,13 +4,14 @@ import * as smartdata from '@pushrocks/smartdata';
import * as smartdelay from '@pushrocks/smartdelay'; import * as smartdelay from '@pushrocks/smartdelay';
import * as smartdns from '@pushrocks/smartdns'; import * as smartdns from '@pushrocks/smartdns';
import * as smartexpress from '@pushrocks/smartexpress'; import * as smartexpress from '@pushrocks/smartexpress';
import * as smartlog from '@pushrocks/smartlog';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest'; import * as smartrequest from '@pushrocks/smartrequest';
import * as smartunique from '@pushrocks/smartunique'; import * as smartunique from '@pushrocks/smartunique';
import * as smartstring from '@pushrocks/smartstring'; import * as smartstring from '@pushrocks/smartstring';
import * as smarttime from '@pushrocks/smarttime'; import * as smarttime from '@pushrocks/smarttime';
export { lik, smartdata, smartdelay, smartdns, smartexpress, smartpromise, smartrequest, smartunique, smartstring, smarttime }; export { lik, smartdata, smartdelay, smartdns, smartexpress, smartlog, smartpromise, smartrequest, smartunique, smartstring, smarttime };
// thirs party scope // thirs party scope
import * as acme from 'acme-client'; import * as acme from 'acme-client';