Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
a1134cf227 | |||
4ee1c4b08c | |||
08c3eaa65f | |||
2717f08476 | |||
f16dbeea32 | |||
a0c0230419 | |||
0d1ebf2d1a | |||
6edbf3cb46 | |||
b26f7ac3e9 | |||
5129c5d601 | |||
d09b3fd1bc | |||
14fccd40d8 | |||
c0f45a10e0 | |||
f9db3d28fe | |||
c3fd8750b2 | |||
2b3c28c7a1 | |||
d6b1f942b3 | |||
7eff6ea36a | |||
1ef3615a49 | |||
3653cdc797 | |||
c0271648fc | |||
5546fa5f49 | |||
54fe89860e | |||
d1edf75f6f | |||
6f9c644221 | |||
0b26054687 | |||
e3323ed4ef | |||
24f692636c | |||
a9f709ee7b | |||
1b11b637a5 | |||
ad54bf41ea | |||
060ebf1b29 |
23
package-lock.json
generated
23
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartacme",
|
"name": "@pushrocks/smartacme",
|
||||||
"version": "2.0.12",
|
"version": "2.0.28",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -170,13 +170,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@pushrocks/smartexpress": {
|
"@pushrocks/smartexpress": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.4",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartexpress/-/smartexpress-3.0.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartexpress/-/smartexpress-3.0.4.tgz",
|
||||||
"integrity": "sha512-F3x+AFUxkKxb5WgQuIZsfkrNzMz0PaYYIw4gdQbAPn5N/UCEP4eE2OQWXnAcCSYwsXI8hgjEuUOC9gTbCSPDRg==",
|
"integrity": "sha512-sn6yQpTGylvT/t4arCN7te8EUzimhv9JB+73XOwv503u6HokE//nT8eCAhkjprH3hgHpIgvR3iPGI8UBSCJFsw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/lik": "^3.0.4",
|
"@pushrocks/lik": "^3.0.4",
|
||||||
"@pushrocks/smartfile": "^6.0.11",
|
"@pushrocks/smartfile": "^6.0.11",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^2.0.5",
|
||||||
|
"@pushrocks/smartrequest": "^1.1.14",
|
||||||
"@types/express": "^4.16.0",
|
"@types/express": "^4.16.0",
|
||||||
"@types/finalhandler": "0.0.33",
|
"@types/finalhandler": "0.0.33",
|
||||||
"@types/helmet": "0.0.42",
|
"@types/helmet": "0.0.42",
|
||||||
@ -271,12 +272,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@pushrocks/smartstring": {
|
"@pushrocks/smartstring": {
|
||||||
"version": "3.0.5",
|
"version": "3.0.8",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartstring/-/smartstring-3.0.5.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartstring/-/smartstring-3.0.8.tgz",
|
||||||
"integrity": "sha512-Bws2s6+Pc0HIXA35QH6cl6LJywhZ1U5icjyYMx+1kH8nURG+njzkacfarr9NWqyVYYOVu7SgXLzp9sR0eH7k9g==",
|
"integrity": "sha512-twmCGF47+TJoiCpj6t5MxwcKDDJEATuKqJXxftcHOlXWW743LJ/1bCpMueDdtumo5EheUfSxb43aRse3DU3wig==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"crypto-random-string": "^1.0.0",
|
"crypto-random-string": "^1.0.0",
|
||||||
"js-base64": "^2.4.9",
|
"js-base64": "^2.5.0",
|
||||||
"normalize-newline": "^3.0.0",
|
"normalize-newline": "^3.0.0",
|
||||||
"randomatic": "^3.1.1",
|
"randomatic": "^3.1.1",
|
||||||
"strip-indent": "^2.0.0"
|
"strip-indent": "^2.0.0"
|
||||||
@ -1614,9 +1615,9 @@
|
|||||||
"integrity": "sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU="
|
"integrity": "sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU="
|
||||||
},
|
},
|
||||||
"luxon": {
|
"luxon": {
|
||||||
"version": "1.9.0",
|
"version": "1.10.0",
|
||||||
"resolved": "https://verdaccio.lossless.one/luxon/-/luxon-1.9.0.tgz",
|
"resolved": "https://verdaccio.lossless.one/luxon/-/luxon-1.10.0.tgz",
|
||||||
"integrity": "sha512-N1kSwtIEhM/gIRGASXPgi1CwfQZX5VTjndYFjOsZdEEtWij2uSoRrgDGWwViZCUNY9Rwh4UVG/TLcUinHM20cA=="
|
"integrity": "sha512-ry3GKh//v3isD6oJN5pFWmdh+3GiScwv9q8VgG6fZ2j1guGOol2vVVdo4GBAWCrcq5RHOqSeipqHBnOu/u024Q=="
|
||||||
},
|
},
|
||||||
"make-error": {
|
"make-error": {
|
||||||
"version": "1.3.5",
|
"version": "1.3.5",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartacme",
|
"name": "@pushrocks/smartacme",
|
||||||
"version": "2.0.12",
|
"version": "2.0.28",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "acme implementation in TypeScript",
|
"description": "acme implementation in TypeScript",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -29,12 +29,14 @@
|
|||||||
"@pushrocks/smartdata": "^3.1.13",
|
"@pushrocks/smartdata": "^3.1.13",
|
||||||
"@pushrocks/smartdelay": "^2.0.2",
|
"@pushrocks/smartdelay": "^2.0.2",
|
||||||
"@pushrocks/smartdns": "^3.0.8",
|
"@pushrocks/smartdns": "^3.0.8",
|
||||||
"@pushrocks/smartexpress": "^3.0.0",
|
"@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/smarttime": "^3.0.5",
|
"@pushrocks/smarttime": "^3.0.5",
|
||||||
"@pushrocks/smartunique": "^3.0.1",
|
"@pushrocks/smartunique": "^3.0.1",
|
||||||
"acme-client": "^2.2.2"
|
"acme-client": "2.2.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.4",
|
"@gitzone/tsbuild": "^2.1.4",
|
||||||
|
24
test/test.ts
24
test/test.ts
@ -11,20 +11,34 @@ tap.test('should create a valid instance of SmartAcme', async () => {
|
|||||||
smartAcmeInstance = new smartacme.SmartAcme({
|
smartAcmeInstance = new smartacme.SmartAcme({
|
||||||
accountEmail: 'domains@lossless.org',
|
accountEmail: 'domains@lossless.org',
|
||||||
accountPrivateKey: null,
|
accountPrivateKey: null,
|
||||||
|
mongoDescriptor: {
|
||||||
|
mongoDbName: testQenv.getEnvVarOnDemand('MONGODB_DATABASE'),
|
||||||
|
mongoDbPass: testQenv.getEnvVarOnDemand('MONGODB_PASSWORD'),
|
||||||
|
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGODB_URL')
|
||||||
|
},
|
||||||
removeChallenge: async (...args) => {
|
removeChallenge: async (...args) => {
|
||||||
console.log(args);
|
console.log(args);
|
||||||
},
|
},
|
||||||
setChallenge: async (...args) => {
|
setChallenge: async (...args) => {
|
||||||
console.log(args);
|
console.log(args);
|
||||||
},
|
},
|
||||||
mongoDescriptor: {
|
validateRemoteRequest: async () => {
|
||||||
mongoDbName: testQenv.getEnvVarOnDemand('MONGODB_DATABASE'),
|
return true;
|
||||||
mongoDbPass: testQenv.getEnvVarOnDemand('MONGODB_PASSWORD'),
|
|
||||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGODB_URL')
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await smartAcmeInstance.init();
|
await smartAcmeInstance.init();
|
||||||
await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
// await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('certmatcher should correctly match domains', async () => {
|
||||||
|
const certMatcherMod = await import('../ts/smartacme.classes.certmatcher');
|
||||||
|
const certMatcher = new certMatcherMod.CertMatcher();
|
||||||
|
const matchedCert = certMatcher.getCertificateDomainNameByDomainName('level3.level2.level1');
|
||||||
|
expect(matchedCert).to.equal('level2.level1');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should stop correctly', async () => {
|
||||||
|
await smartAcmeInstance.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
9
ts/interfaces/cert.ts
Normal file
9
ts/interfaces/cert.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export type TCertStatus = 'existing' | 'nonexisting' | 'pending' | 'failed';
|
||||||
|
|
||||||
|
export interface ICert {
|
||||||
|
domainName: string;
|
||||||
|
created: number;
|
||||||
|
privateKey: string;
|
||||||
|
publicKey: string;
|
||||||
|
csr: string;
|
||||||
|
}
|
11
ts/interfaces/certremote.ts
Normal file
11
ts/interfaces/certremote.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { ICert, TCertStatus } from './cert';
|
||||||
|
|
||||||
|
export interface ICertRemoteRequest {
|
||||||
|
secret: string;
|
||||||
|
domainName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICertRemoteResponse {
|
||||||
|
status: TCertStatus;
|
||||||
|
certificate?: ICert;
|
||||||
|
}
|
@ -1 +1,3 @@
|
|||||||
export * from './accountdata';
|
export * from './accountdata';
|
||||||
|
export * from './cert';
|
||||||
|
export * from './certremote';
|
||||||
|
@ -1,34 +1,40 @@
|
|||||||
import * as plugins from './smartacme.plugins';
|
import * as plugins from './smartacme.plugins';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
import { CertManager } from './smartacme.classes.certmanager';
|
import { CertManager } from './smartacme.classes.certmanager';
|
||||||
|
|
||||||
import { Collection, svDb, unI } from '@pushrocks/smartdata';
|
import { Collection, svDb, unI } from '@pushrocks/smartdata';
|
||||||
|
import { ICert } from './interfaces';
|
||||||
|
|
||||||
@plugins.smartdata.Collection(() => {
|
@plugins.smartdata.Collection(() => {
|
||||||
return CertManager.activeDB;
|
return CertManager.activeDB;
|
||||||
})
|
})
|
||||||
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> {
|
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert {
|
||||||
@unI()
|
@unI()
|
||||||
public index: string;
|
public index: string;
|
||||||
|
|
||||||
@svDb()
|
@svDb()
|
||||||
domainName: string;
|
public domainName: string;
|
||||||
|
|
||||||
@svDb()
|
@svDb()
|
||||||
created: number;
|
public created: number;
|
||||||
|
|
||||||
@svDb()
|
@svDb()
|
||||||
privateKey: string;
|
public privateKey: string;
|
||||||
|
|
||||||
@svDb()
|
@svDb()
|
||||||
publicKey: string;
|
public publicKey: string;
|
||||||
|
|
||||||
@svDb()
|
@svDb()
|
||||||
csr: string;
|
public csr: string;
|
||||||
|
|
||||||
constructor(privateKeyArg: string, publicKeyArg: string, csrArg: string) {
|
constructor(optionsArg: ICert) {
|
||||||
super();
|
super();
|
||||||
this.privateKey = privateKeyArg;
|
if (optionsArg) {
|
||||||
this.publicKey = publicKeyArg;
|
Object.keys(optionsArg).forEach(key => {
|
||||||
this.csr = csrArg;
|
this[key] = optionsArg[key];
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import * as plugins from './smartacme.plugins';
|
import * as plugins from './smartacme.plugins';
|
||||||
import { Cert } from './smartacme.classes.cert';
|
import { Cert } from './smartacme.classes.cert';
|
||||||
|
import { SmartAcme } from './smartacme.classes.smartacme';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
|
||||||
export class CertManager {
|
export class CertManager {
|
||||||
@ -15,17 +18,23 @@ export class CertManager {
|
|||||||
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||||
public smartdataDb: plugins.smartdata.SmartdataDb;
|
public smartdataDb: plugins.smartdata.SmartdataDb;
|
||||||
|
|
||||||
constructor(optionsArg: {
|
public pendingMap: plugins.lik.Stringmap;
|
||||||
|
|
||||||
|
constructor(smartAcmeArg: SmartAcme,optionsArg: {
|
||||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||||
}) {
|
}) {
|
||||||
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init () {
|
public async init () {
|
||||||
|
// Smartdata DB
|
||||||
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
|
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
|
||||||
await this.smartdataDb.init();
|
await this.smartdataDb.init();
|
||||||
CertManager.activeDB = this.smartdataDb;
|
CertManager.activeDB = this.smartdataDb;
|
||||||
};
|
|
||||||
|
// Pending Map
|
||||||
|
this.pendingMap = new plugins.lik.Stringmap();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* retrieves a certificate
|
* retrieves a certificate
|
||||||
@ -33,8 +42,9 @@ export class CertManager {
|
|||||||
* @param domainName the domain Name to retrieve the vcertificate for
|
* @param domainName the domain Name to retrieve the vcertificate for
|
||||||
*/
|
*/
|
||||||
public async retrieveCertificate(domainName: string): Promise<Cert> {
|
public async retrieveCertificate(domainName: string): Promise<Cert> {
|
||||||
|
await this.checkCerts();
|
||||||
const existingCertificate: Cert = await Cert.getInstance({
|
const existingCertificate: Cert = await Cert.getInstance({
|
||||||
name: domainName
|
domainName
|
||||||
});
|
});
|
||||||
|
|
||||||
if(existingCertificate) {
|
if(existingCertificate) {
|
||||||
@ -46,22 +56,46 @@ export class CertManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stores the certificate with the
|
* stores the certificate
|
||||||
* @param publicKeyArg
|
* @param optionsArg
|
||||||
* @param privateKeyArg
|
|
||||||
* @param csrArg
|
|
||||||
*/
|
*/
|
||||||
public async storeCertificate(privateKeyArg: string, publicKeyArg: string, csrArg: string) {
|
public async storeCertificate(optionsArg: interfaces.ICert) {
|
||||||
const cert = new Cert(privateKeyArg, publicKeyArg, csrArg);
|
const cert = new Cert(optionsArg);
|
||||||
cert.save();
|
await cert.save();
|
||||||
};
|
}
|
||||||
|
|
||||||
public async deleteCertificate(domainName: string) {
|
public async deleteCertificate(domainNameArg: string) {
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
||||||
|
return 'pending';
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise lets continue
|
||||||
|
const existingCertificate = this.retrieveCertificate(certDomainArg);
|
||||||
|
if (existingCertificate) {
|
||||||
|
return 'existing';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'nonexisting';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks all certs for expiration
|
* checks all certs for expiration
|
||||||
*/
|
*/
|
||||||
checkCerts() {}
|
private async checkCerts() {};
|
||||||
}
|
}
|
||||||
|
10
ts/smartacme.classes.certmatcher.ts
Normal file
10
ts/smartacme.classes.certmatcher.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import * as plugins from './smartacme.plugins';
|
||||||
|
|
||||||
|
export class CertMatcher {
|
||||||
|
public getCertificateDomainNameByDomainName(domainNameArg: string): string {
|
||||||
|
const originalDomain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
if (!originalDomain.level4) {
|
||||||
|
return `${originalDomain.level2}.${originalDomain.level1}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,54 @@
|
|||||||
import * as plugins from './smartacme.plugins';
|
import * as plugins from './smartacme.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
// tslint:disable-next-line: max-classes-per-file
|
||||||
export class CertRemoteClient {
|
export class CertRemoteClient {
|
||||||
|
private remoteUrl: 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.secret = optionsArg.secret;
|
||||||
|
optionsArg.logger
|
||||||
|
? (this.logger = optionsArg.logger)
|
||||||
|
: (this.logger = plugins.smartlog.defaultLogger);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param domainNameArg
|
||||||
|
*/
|
||||||
|
public async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
||||||
|
let certificate: interfaces.ICert;
|
||||||
|
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
||||||
|
const response: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
|
||||||
|
this.remoteUrl,
|
||||||
|
{
|
||||||
|
requestBody: <interfaces.ICertRemoteRequest>{
|
||||||
|
domainName: domainNameArg,
|
||||||
|
secret: this.secret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)).body;
|
||||||
|
switch (response.status as interfaces.TCertStatus) {
|
||||||
|
case 'pending':
|
||||||
|
await plugins.smartdelay.delayFor(5000);
|
||||||
|
const finalResponse = await doRequestCycle();
|
||||||
|
return finalResponse;
|
||||||
|
case 'existing':
|
||||||
|
return response.certificate;
|
||||||
|
case 'failed':
|
||||||
|
default:
|
||||||
|
console.log(`could not retrieve certificate for ${domainNameArg}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
certificate = await doRequestCycle();
|
||||||
|
return certificate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
import * as plugins from './smartacme.plugins';
|
|
||||||
|
|
||||||
export class CertRemoteHandler {}
|
|
@ -1,18 +1,33 @@
|
|||||||
import * as plugins from './smartacme.plugins';
|
import * as plugins from './smartacme.plugins';
|
||||||
|
import { Cert } from './smartacme.classes.cert';
|
||||||
import { CertManager } from './smartacme.classes.certmanager';
|
import { CertManager } from './smartacme.classes.certmanager';
|
||||||
import { CertRemoteHandler } from './smartacme.classes.certremotehandler';
|
import { CertMatcher } from './smartacme.classes.certmatcher';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
import { request } from 'http';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* the options for the class @see SmartAcme
|
||||||
*/
|
*/
|
||||||
export interface ISmartAcmeOptions {
|
export interface ISmartAcmeOptions {
|
||||||
accountPrivateKey?: string;
|
accountPrivateKey?: string;
|
||||||
accountEmail: string;
|
accountEmail: string;
|
||||||
|
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||||
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||||
removeChallenge: (domainName: string) => Promise<any>;
|
removeChallenge: (domainName: string) => Promise<any>;
|
||||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
environment: 'production' | 'integration';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class SmartAcme
|
||||||
|
* can be used for setting up communication with an ACME authority
|
||||||
|
*
|
||||||
|
* ```ts
|
||||||
|
* const mySmartAcmeInstance = new SmartAcme({
|
||||||
|
* // see ISmartAcmeOptions for options
|
||||||
|
* })
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
export class SmartAcme {
|
export class SmartAcme {
|
||||||
private options: ISmartAcmeOptions;
|
private options: ISmartAcmeOptions;
|
||||||
|
|
||||||
@ -29,25 +44,76 @@ export class SmartAcme {
|
|||||||
|
|
||||||
// certmanager
|
// certmanager
|
||||||
private certmanager: CertManager;
|
private certmanager: CertManager;
|
||||||
private certremoteHandler: CertRemoteHandler;
|
private certmatcher: CertMatcher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the remote handler to hand the request and response to.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inits the instance
|
||||||
|
* ```ts
|
||||||
|
* await myCloudlyInstance.init() // does not support options
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
public async init() {
|
public async init() {
|
||||||
this.privateKey = this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey());
|
this.privateKey =
|
||||||
|
this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey());
|
||||||
this.setChallenge = this.options.setChallenge;
|
this.setChallenge = this.options.setChallenge;
|
||||||
this.removeChallenge = this.options.removeChallenge;
|
this.removeChallenge = this.options.removeChallenge;
|
||||||
|
|
||||||
this.certmanager = new CertManager({
|
// CertMangaer
|
||||||
|
this.certmanager = new CertManager(this, {
|
||||||
mongoDescriptor: this.options.mongoDescriptor
|
mongoDescriptor: this.options.mongoDescriptor
|
||||||
});
|
});
|
||||||
await this.certmanager.init();
|
await this.certmanager.init();
|
||||||
this.certremoteHandler = new CertRemoteHandler();
|
|
||||||
|
|
||||||
|
// CertMatcher
|
||||||
|
this.certmatcher = new CertMatcher();
|
||||||
|
|
||||||
|
// ACME Client
|
||||||
this.client = new plugins.acme.Client({
|
this.client = new plugins.acme.Client({
|
||||||
directoryUrl: plugins.acme.directory.letsencrypt.staging,
|
directoryUrl: (() => {
|
||||||
|
if (this.options.environment === 'production') {
|
||||||
|
return plugins.acme.directory.letsencrypt.production;
|
||||||
|
} else {
|
||||||
|
return plugins.acme.directory.letsencrypt.staging;
|
||||||
|
}
|
||||||
|
})(),
|
||||||
accountKey: this.privateKey
|
accountKey: this.privateKey
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -58,18 +124,22 @@ export class SmartAcme {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCertificateForDomain(domainArg: string) {
|
public async stop() {
|
||||||
const domain = domainArg;
|
await this.certmanager.smartdataDb.close();
|
||||||
|
}
|
||||||
|
|
||||||
const retrievedCertificate = await this.certmanager.retrieveCertificate(domain);
|
public async getCertificateForDomain(domainArg: string): Promise<Cert> {
|
||||||
|
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(domainArg);
|
||||||
|
await this.certmanager.announceCertificate(certDomain);
|
||||||
|
const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomain);
|
||||||
|
|
||||||
if(retrievedCertificate) {
|
if (retrievedCertificate) {
|
||||||
return retrievedCertificate;
|
return retrievedCertificate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Place new order */
|
/* Place new order */
|
||||||
const order = await this.client.createOrder({
|
const order = await this.client.createOrder({
|
||||||
identifiers: [{ type: 'dns', value: domain }, { type: 'dns', value: `*.${domain}` }]
|
identifiers: [{ type: 'dns', value: certDomain }, { type: 'dns', value: `*.${certDomain}` }]
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Get authorizations and select challenges */
|
/* Get authorizations and select challenges */
|
||||||
@ -88,6 +158,8 @@ export class SmartAcme {
|
|||||||
/* Satisfy challenge */
|
/* Satisfy challenge */
|
||||||
await this.setChallenge(domainDnsName, keyAuthorization);
|
await this.setChallenge(domainDnsName, keyAuthorization);
|
||||||
await this.smartdns.checkUntilAvailable(domainDnsName, 'TXT', keyAuthorization, 100, 5000);
|
await this.smartdns.checkUntilAvailable(domainDnsName, 'TXT', keyAuthorization, 100, 5000);
|
||||||
|
console.log('Cool down an extra 60 second for region availability');
|
||||||
|
await plugins.smartdelay.delayFor(60000);
|
||||||
|
|
||||||
/* Verify that challenge is satisfied */
|
/* Verify that challenge is satisfied */
|
||||||
await this.client.verifyChallenge(authz, dnsChallenge);
|
await this.client.verifyChallenge(authz, dnsChallenge);
|
||||||
@ -109,8 +181,8 @@ export class SmartAcme {
|
|||||||
|
|
||||||
/* Finalize order */
|
/* Finalize order */
|
||||||
const [key, csr] = await plugins.acme.forge.createCsr({
|
const [key, csr] = await plugins.acme.forge.createCsr({
|
||||||
commonName: `*.${domain}`,
|
commonName: `*.${certDomain}`,
|
||||||
altNames: [domain]
|
altNames: [certDomain]
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.client.finalizeOrder(order, csr);
|
await this.client.finalizeOrder(order, csr);
|
||||||
@ -121,8 +193,15 @@ export class SmartAcme {
|
|||||||
console.log(`Private key:\n${key.toString()}`);
|
console.log(`Private key:\n${key.toString()}`);
|
||||||
console.log(`Certificate:\n${cert.toString()}`);
|
console.log(`Certificate:\n${cert.toString()}`);
|
||||||
|
|
||||||
this.certmanager.storeCertificate(key.toString(), cert.toString(), csr.toString());
|
await this.certmanager.storeCertificate({
|
||||||
}
|
domainName: certDomain,
|
||||||
|
privateKey: key.toString(),
|
||||||
|
publicKey: cert.toString(),
|
||||||
|
csr: csr.toString(),
|
||||||
|
created: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
toStorageObject() {}
|
const newCertificate = await this.certmanager.retrieveCertificate(certDomain);
|
||||||
|
return newCertificate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,12 +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 smarttime from '@pushrocks/smarttime';
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
|
|
||||||
export { lik, smartdata, smartdelay, smartdns, smartexpress, smartpromise, smartrequest, smartunique, 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';
|
||||||
|
Reference in New Issue
Block a user