Compare commits

...

10 Commits

Author SHA1 Message Date
82b1d68576 2.0.35 2019-01-17 22:50:22 +01:00
e04b23aceb fix(core): update 2019-01-17 22:50:21 +01:00
8e255938b5 2.0.34 2019-01-17 22:47:58 +01:00
f2eb9666a7 fix(core): update 2019-01-17 22:47:58 +01:00
cbdb0c8b08 2.0.33 2019-01-17 22:13:10 +01:00
f821f4d9cc fix(core): update 2019-01-17 22:13:10 +01:00
6cfcf21d95 2.0.32 2019-01-17 01:15:22 +01:00
a33090bb5e fix(core): update 2019-01-17 01:15:22 +01:00
3151829f85 2.0.31 2019-01-16 22:34:38 +01:00
eca63e588c fix(core): update 2019-01-16 22:34:38 +01:00
7 changed files with 51 additions and 21 deletions

10
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartacme", "name": "@pushrocks/smartacme",
"version": "2.0.30", "version": "2.0.35",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -206,11 +206,11 @@
} }
}, },
"@pushrocks/smartlog": { "@pushrocks/smartlog": {
"version": "2.0.10", "version": "2.0.11",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartlog/-/smartlog-2.0.10.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartlog/-/smartlog-2.0.11.tgz",
"integrity": "sha512-4Ir4/JvfL+d5VsN+O8BpFSgUc1mWgr9s6S/cwYBCAIrWdqGomUM5RsZs57RMDn9MWW0JGJjGhUA0M+A8LSSgHw==", "integrity": "sha512-V8SMzAAqDpl1+CJ9b3w5e/1ARjk4JOPZ35qZIllVBhHr8meCqnPE2immlfBYWmEp1xy3ntdAA+Lgewtu+iVk6A==",
"requires": { "requires": {
"@pushrocks/smartlog-interfaces": "^2.0.2" "@pushrocks/smartlog-interfaces": "^2.0.5"
} }
}, },
"@pushrocks/smartlog-interfaces": { "@pushrocks/smartlog-interfaces": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartacme", "name": "@pushrocks/smartacme",
"version": "2.0.30", "version": "2.0.35",
"private": false, "private": false,
"description": "acme implementation in TypeScript", "description": "acme implementation in TypeScript",
"main": "dist/index.js", "main": "dist/index.js",
@ -30,7 +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.6", "@pushrocks/smartexpress": "^3.0.6",
"@pushrocks/smartlog": "^2.0.10", "@pushrocks/smartlog": "^2.0.11",
"@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

@ -1,6 +1,7 @@
export type TCertStatus = 'existing' | 'nonexisting' | 'pending' | 'failed'; export type TCertStatus = 'existing' | 'nonexisting' | 'pending' | 'failed';
export interface ICert { export interface ICert {
id: string;
domainName: string; domainName: string;
created: number; created: number;
privateKey: string; privateKey: string;

View File

@ -12,7 +12,7 @@ import { ICert } from './interfaces';
}) })
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert { export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert {
@unI() @unI()
public index: string; public id: string;
@svDb() @svDb()
public domainName: string; public domainName: string;

View File

@ -62,6 +62,7 @@ export class CertManager {
public async storeCertificate(optionsArg: interfaces.ICert) { public async storeCertificate(optionsArg: interfaces.ICert) {
const cert = new Cert(optionsArg); const cert = new Cert(optionsArg);
await cert.save(); await cert.save();
this.pendingMap.removeString(optionsArg.domainName);
} }
public async deleteCertificate(domainNameArg: string) { public async deleteCertificate(domainNameArg: string) {
@ -97,5 +98,7 @@ export class CertManager {
/** /**
* checks all certs for expiration * checks all certs for expiration
*/ */
private async checkCerts() {}; private async checkCerts() {
// TODO
};
} }

View File

@ -26,7 +26,7 @@ export class CertRemoteClient {
public 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: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson( const responseBody: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
this.remoteUrl, this.remoteUrl,
{ {
requestBody: <interfaces.ICertRemoteRequest>{ requestBody: <interfaces.ICertRemoteRequest>{
@ -35,13 +35,15 @@ export class CertRemoteClient {
} }
} }
)).body; )).body;
switch (response.status as interfaces.TCertStatus) { console.log(responseBody);
switch (responseBody.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();
return finalResponse; return finalResponse;
case 'existing': case 'existing':
return response.certificate; this.logger.log('ok', `got certificate for ${domainNameArg}`);
return responseBody.certificate;
case 'failed': case 'failed':
default: default:
console.log(`could not retrieve certificate for ${domainNameArg}`); console.log(`could not retrieve certificate for ${domainNameArg}`);

View File

@ -16,6 +16,7 @@ export interface ISmartAcmeOptions {
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>; setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
removeChallenge: (domainName: string) => Promise<any>; removeChallenge: (domainName: string) => Promise<any>;
environment: 'production' | 'integration'; environment: 'production' | 'integration';
logger?: plugins.smartlog.Smartlog;
} }
/** /**
@ -34,6 +35,7 @@ export class SmartAcme {
// the acme client // the acme client
private client: any; private client: any;
private smartdns = new plugins.smartdns.Smartdns(); private smartdns = new plugins.smartdns.Smartdns();
public logger: plugins.smartlog.Smartlog;
// the account private key // the account private key
private privateKey: string; private privateKey: string;
@ -49,15 +51,21 @@ 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 = async (req: plugins.smartexpress.Request, res: plugins.smartexpress.Response) => { public certremoteHandler = async (
req: plugins.smartexpress.Request,
res: plugins.smartexpress.Response
) => {
const requestBody: interfaces.ICertRemoteRequest = req.body; const requestBody: interfaces.ICertRemoteRequest = req.body;
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(requestBody.domainName); this.logger.log('ok', `got certificate request for ${requestBody.domainName}`);
let status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus( const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(
certDomain requestBody.domainName
); );
this.logger.log('ok', `mapping ${requestBody.domainName} to ${certDomain}`);
let status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(certDomain);
let response: interfaces.ICertRemoteResponse; let response: interfaces.ICertRemoteResponse;
switch (status) { switch (status) {
case 'existing': case 'existing':
this.logger.log('ok', `certificate exists for ${certDomain}. Sending certificate!`);
response = { response = {
status, status,
certificate: await (await this.certmanager.retrieveCertificate( certificate: await (await this.certmanager.retrieveCertificate(
@ -66,7 +74,7 @@ export class SmartAcme {
}; };
break; break;
default: default:
if (status === "nonexisting") { if (status === 'nonexisting') {
this.getCertificateForDomain(certDomain); this.getCertificateForDomain(certDomain);
status = 'pending'; status = 'pending';
} }
@ -82,6 +90,9 @@ export class SmartAcme {
constructor(optionsArg: ISmartAcmeOptions) { constructor(optionsArg: ISmartAcmeOptions) {
this.options = optionsArg; this.options = optionsArg;
this.options.logger
? (this.logger = optionsArg.logger)
: (this.logger = plugins.smartlog.defaultLogger);
} }
/** /**
@ -128,13 +139,28 @@ export class SmartAcme {
await this.certmanager.smartdataDb.close(); await this.certmanager.smartdataDb.close();
} }
/**
* gets a certificate
* it runs through the following steps
*
* * look in the database
* * if in the database return it
* * of not in the database announce it
* * then get it from letsencrypt
* * store it
* * remove it from the pending map (which it go onto by announcing it)
* * retrieve it from the databse and return it
*
* @param domainArg
*/
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) {
return retrievedCertificate; return retrievedCertificate;
} else {
await this.certmanager.announceCertificate(certDomain);
} }
/* Place new order */ /* Place new order */
@ -189,11 +215,9 @@ export class SmartAcme {
const cert = await this.client.getCertificate(order); const cert = await this.client.getCertificate(order);
/* Done */ /* Done */
console.log(`CSR:\n${csr.toString()}`);
console.log(`Private key:\n${key.toString()}`);
console.log(`Certificate:\n${cert.toString()}`);
await this.certmanager.storeCertificate({ await this.certmanager.storeCertificate({
id: plugins.smartunique.shortId(),
domainName: certDomain, domainName: certDomain,
privateKey: key.toString(), privateKey: key.toString(),
publicKey: cert.toString(), publicKey: cert.toString(),