Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
ad54bf41ea | |||
060ebf1b29 | |||
a87c6acb8a | |||
62d27619f4 | |||
0faebf2a79 | |||
29ea50796c | |||
26d1b7cbf0 | |||
c0c97835ea | |||
d4d50b7dcf | |||
2492fd4de2 | |||
bef54799b6 | |||
dbe09f320a | |||
18045dadaf | |||
ee300c3e12 | |||
ed4ba0cb61 | |||
a8ab27045d | |||
975c3ed190 | |||
a99dea549b | |||
f8b78c433a | |||
6c33111074 | |||
280335f6f6 | |||
b90092c043 | |||
9e1c73febf | |||
dcf1915816 | |||
748c911168 | |||
3a48cb4ea8 | |||
a035c5c0b0 |
@ -26,6 +26,7 @@ mirror:
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
@ -33,24 +34,39 @@ snyk:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
sast:
|
||||
stage: security
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
|
||||
variables:
|
||||
DOCKER_DRIVER: overlay2
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
|
||||
--volume "$PWD:/code"
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
|
||||
artifacts:
|
||||
reports:
|
||||
sast: gl-sast-report.json
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
@ -62,6 +78,7 @@ testLTS:
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
@ -118,6 +135,7 @@ pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
tags:
|
||||
@ -130,13 +148,3 @@ pages:
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
||||
|
34
README.md
34
README.md
@ -25,9 +25,39 @@ acme implementation in TypeScript
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
For further information read the linked docs at the top of this README.
|
||||
```javascript
|
||||
import { SmartAcme } from 'smartacme';
|
||||
|
||||
let smac = new SmartAcme()(async () => {
|
||||
// learn async/await, it'll make your life easier
|
||||
|
||||
// optionally accepts a filePath Arg with a stored acmeaccount.json
|
||||
// will create an account and
|
||||
let myAccount = await smac.createAcmeAccount();
|
||||
|
||||
// will return a dnsHash to set in your DNS record
|
||||
let myCert = await myAccount.createAcmeCert('example.com');
|
||||
|
||||
// gets and accepts the specified challenge
|
||||
// first argument optional, defaults to dns-01 (which is the cleanest method for production use)
|
||||
let myChallenge = await myCert.getChallenge('dns-01');
|
||||
|
||||
/* ----------
|
||||
Now you need to set the challenge in your DNS
|
||||
myChallenge.domainNamePrefixed is the address for the record
|
||||
myChallenge.dnsKeyHash is the ready to use txt record value expected by letsencrypt
|
||||
-------------*/
|
||||
})();
|
||||
```
|
||||
|
||||
## Other relevant npm modules
|
||||
|
||||
| module name | description |
|
||||
| ----------- | ------------------------------------------------------------------- |
|
||||
| cert | a higlevel production module that uses smartacme to manage certs |
|
||||
| smartnginx | a highlevel production tool for docker environments to manage nginx |
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://umbrella.zone)
|
||||
[](https://umbrella.zone
|
||||
|
@ -1,63 +0,0 @@
|
||||
# smartacme
|
||||
|
||||
acme implementation in TypeScript
|
||||
|
||||
## Availabililty
|
||||
|
||||
[](https://www.npmjs.com/package/smartacme)
|
||||
[](https://GitLab.com/umbrellazone/smartacme)
|
||||
[](https://github.com/umbrellazone/smartacme)
|
||||
[](https://umbrellazone.gitlab.io/smartacme/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/umbrellazone/smartacme/commits/master)
|
||||
[](https://GitLab.com/umbrellazone/smartacme/commits/master)
|
||||
[](https://www.npmjs.com/package/smartacme)
|
||||
[](https://david-dm.org/umbrellazone/smartacme)
|
||||
[](https://www.bithound.io/github/umbrellazone/smartacme/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/umbrellazone/smartacme)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```javascript
|
||||
import { SmartAcme } from 'smartacme';
|
||||
|
||||
let smac = new SmartAcme()(async () => {
|
||||
// learn async/await, it'll make your life easier
|
||||
|
||||
// optionally accepts a filePath Arg with a stored acmeaccount.json
|
||||
// will create an account and
|
||||
let myAccount = await smac.createAcmeAccount();
|
||||
|
||||
// will return a dnsHash to set in your DNS record
|
||||
let myCert = await myAccount.createAcmeCert('example.com');
|
||||
|
||||
// gets and accepts the specified challenge
|
||||
// first argument optional, defaults to dns-01 (which is the cleanest method for production use)
|
||||
let myChallenge = await myCert.getChallenge('dns-01');
|
||||
|
||||
/* ----------
|
||||
Now you need to set the challenge in your DNS
|
||||
myChallenge.domainNamePrefixed is the address for the record
|
||||
myChallenge.dnsKeyHash is the ready to use txt record value expected by letsencrypt
|
||||
-------------*/
|
||||
})();
|
||||
```
|
||||
|
||||
## Other relevant npm modules
|
||||
|
||||
| module name | description |
|
||||
| ----------- | ------------------------------------------------------------------- |
|
||||
| cert | a higlevel production module that uses smartacme to manage certs |
|
||||
| smartnginx | a highlevel production tool for docker environments to manage nginx |
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://umbrella.zone
|
2434
package-lock.json
generated
2434
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "smartacme",
|
||||
"version": "1.1.4",
|
||||
"name": "@pushrocks/smartacme",
|
||||
"version": "2.0.13",
|
||||
"private": false,
|
||||
"description": "acme implementation in TypeScript",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "echo \"Not needed for now\""
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -25,18 +25,26 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/umbrellazone/smartacme#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^3.0.4",
|
||||
"@pushrocks/smartdata": "^3.1.13",
|
||||
"@pushrocks/smartdelay": "^2.0.2",
|
||||
"@pushrocks/smartdns": "^3.0.8",
|
||||
"@pushrocks/smartexpress": "^3.0.0",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"acme-v2": "^1.2.0",
|
||||
"rsa-compat": "^1.5.1"
|
||||
"@pushrocks/smartrequest": "^1.1.14",
|
||||
"@pushrocks/smarttime": "^3.0.5",
|
||||
"@pushrocks/smartunique": "^3.0.1",
|
||||
"acme-client": "^2.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.12",
|
||||
"@gitzone/tstest": "^1.0.13",
|
||||
"@types/node": "^10.5.8",
|
||||
"cflare": "^1.0.5",
|
||||
"qenv": "^1.1.7",
|
||||
"tapbundle": "^2.0.2"
|
||||
"@gitzone/tsbuild": "^2.1.4",
|
||||
"@gitzone/tsrun": "^1.1.17",
|
||||
"@gitzone/tstest": "^1.0.18",
|
||||
"@mojoio/cloudflare": "^2.0.0",
|
||||
"@pushrocks/qenv": "^3.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.12.18",
|
||||
"tslint": "^5.12.0",
|
||||
"tslint-config-prettier": "^1.17.0"
|
||||
}
|
||||
}
|
||||
|
9
qenv.yml
9
qenv.yml
@ -1,3 +1,6 @@
|
||||
vars:
|
||||
- CF_EMAIL
|
||||
- CF_KEY
|
||||
required:
|
||||
- CF_EMAIL
|
||||
- CF_KEY
|
||||
- MONGODB_URL
|
||||
- MONGODB_PASSWORD
|
||||
- MONGODB_DATABASE
|
||||
|
27
test/test.ts
27
test/test.ts
@ -1,14 +1,33 @@
|
||||
import { tap, expect } from 'tapbundle';
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
|
||||
import * as smartacme from '../ts/index';
|
||||
|
||||
let smartAcmeInstance: smartacme.SmartAcme;
|
||||
|
||||
tap.test('should create a valid instance of SmartAcme', async () => {
|
||||
smartAcmeInstance = new smartacme.SmartAcme();
|
||||
smartAcmeInstance = new smartacme.SmartAcme({
|
||||
accountEmail: 'domains@lossless.org',
|
||||
accountPrivateKey: null,
|
||||
mongoDescriptor: {
|
||||
mongoDbName: testQenv.getEnvVarOnDemand('MONGODB_DATABASE'),
|
||||
mongoDbPass: testQenv.getEnvVarOnDemand('MONGODB_PASSWORD'),
|
||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGODB_URL')
|
||||
},
|
||||
removeChallenge: async (...args) => {
|
||||
console.log(args);
|
||||
},
|
||||
setChallenge: async (...args) => {
|
||||
console.log(args);
|
||||
},
|
||||
validateRemoteRequest: async () => {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
await smartAcmeInstance.init();
|
||||
console.log(smartAcmeInstance.directoryUrls);
|
||||
await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
||||
// await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -1 +1,3 @@
|
||||
export * from './smartacme.classes.smartacme';
|
||||
|
||||
export * from './smartacme.classes.certremoteclient';
|
||||
|
8
ts/interfaces/accountdata.ts
Normal file
8
ts/interfaces/accountdata.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export interface IAccountData {
|
||||
id: number;
|
||||
key: { kty: 'RSA'; n: string; e: string; kid: string };
|
||||
contact: string[];
|
||||
initialIp: string;
|
||||
createdAt: string;
|
||||
status: string;
|
||||
}
|
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;
|
||||
}
|
3
ts/interfaces/index.ts
Normal file
3
ts/interfaces/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './accountdata';
|
||||
export * from './cert';
|
||||
export * from './certremote';
|
37
ts/smartacme.classes.cert.ts
Normal file
37
ts/smartacme.classes.cert.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { CertManager } from './smartacme.classes.certmanager';
|
||||
|
||||
import { Collection, svDb, unI } from '@pushrocks/smartdata';
|
||||
|
||||
@plugins.smartdata.Collection(() => {
|
||||
return CertManager.activeDB;
|
||||
})
|
||||
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert {
|
||||
@unI()
|
||||
public index: string;
|
||||
|
||||
@svDb()
|
||||
public domainName: string;
|
||||
|
||||
@svDb()
|
||||
public created: number;
|
||||
|
||||
@svDb()
|
||||
public privateKey: string;
|
||||
|
||||
@svDb()
|
||||
public publicKey: string;
|
||||
|
||||
@svDb()
|
||||
public csr: string;
|
||||
|
||||
constructor(privateKeyArg: string, publicKeyArg: string, csrArg: string) {
|
||||
super();
|
||||
this.privateKey = privateKeyArg;
|
||||
this.publicKey = publicKeyArg;
|
||||
this.csr = csrArg;
|
||||
}
|
||||
}
|
92
ts/smartacme.classes.certmanager.ts
Normal file
92
ts/smartacme.classes.certmanager.ts
Normal file
@ -0,0 +1,92 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import { Cert } from './smartacme.classes.cert';
|
||||
import { SmartAcme } from './smartacme.classes.smartacme';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
|
||||
export class CertManager {
|
||||
// =========
|
||||
// STATIC
|
||||
// =========
|
||||
public static activeDB: plugins.smartdata.SmartdataDb;
|
||||
|
||||
|
||||
// =========
|
||||
// INSTANCE
|
||||
// =========
|
||||
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
public smartdataDb: plugins.smartdata.SmartdataDb;
|
||||
|
||||
public pendingMap: plugins.lik.Stringmap;
|
||||
|
||||
constructor(smartAcmeArg: SmartAcme,optionsArg: {
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
}) {
|
||||
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
||||
}
|
||||
|
||||
public async init () {
|
||||
// Smartdata DB
|
||||
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
|
||||
await this.smartdataDb.init();
|
||||
CertManager.activeDB = this.smartdataDb;
|
||||
|
||||
// Pending Map
|
||||
this.pendingMap = new plugins.lik.Stringmap();
|
||||
};
|
||||
|
||||
/**
|
||||
* retrieves a certificate
|
||||
* @returns the Cert class or null
|
||||
* @param domainName the domain Name to retrieve the vcertificate for
|
||||
*/
|
||||
public async retrieveCertificate(domainName: string): Promise<Cert> {
|
||||
await this.checkCerts();
|
||||
const existingCertificate: Cert = await Cert.getInstance({
|
||||
name: domainName
|
||||
});
|
||||
|
||||
if(existingCertificate) {
|
||||
return existingCertificate;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* stores the certificate with the
|
||||
* @param publicKeyArg
|
||||
* @param privateKeyArg
|
||||
* @param csrArg
|
||||
*/
|
||||
public async storeCertificate(privateKeyArg: string, publicKeyArg: string, csrArg: string) {
|
||||
const cert = new Cert(privateKeyArg, publicKeyArg, csrArg);
|
||||
cert.save();
|
||||
};
|
||||
|
||||
public async deleteCertificate(domainNameArg: string) {
|
||||
|
||||
}
|
||||
|
||||
public async getCertificateStatus(domainNameArg: string): Promise<interfaces.TCertStatus> {
|
||||
const isPending = this.pendingMap.checkString('domainNameArg');
|
||||
if (isPending) {
|
||||
return 'pending';
|
||||
}
|
||||
|
||||
// otherwise lets continue
|
||||
const existingCertificate = this.retrieveCertificate(domainNameArg);
|
||||
if (existingCertificate) {
|
||||
return 'existing';
|
||||
}
|
||||
|
||||
return 'nonexisting';
|
||||
}
|
||||
|
||||
/**
|
||||
* checks all certs for expiration
|
||||
*/
|
||||
private async checkCerts() {};
|
||||
}
|
47
ts/smartacme.classes.certremoteclient.ts
Normal file
47
ts/smartacme.classes.certremoteclient.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
import { ICertRemoteResponse } from './interfaces';
|
||||
|
||||
// tslint:disable-next-line: max-classes-per-file
|
||||
export class CertRemoteClient {
|
||||
private remoteUrl: string;
|
||||
private secret: string;
|
||||
|
||||
constructor(optionsArg: {
|
||||
remoteUrl: string;
|
||||
secret: string;
|
||||
}) {
|
||||
this.remoteUrl = optionsArg.remoteUrl;
|
||||
this.secret = optionsArg.secret;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param domainNameArg
|
||||
*/
|
||||
async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
||||
let certificate: interfaces.ICert;
|
||||
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
||||
const response: ICertRemoteResponse = (await plugins.smartrequest.postJson(this.remoteUrl, {
|
||||
requestBody: <interfaces.ICertRemoteRequest>{
|
||||
domainName: domainNameArg,
|
||||
secret: this.secret
|
||||
}
|
||||
})).body;
|
||||
switch(response.status) {
|
||||
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,27 +0,0 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
const rsa = require('rsa-compat').RSA;
|
||||
|
||||
export class KeyPair {
|
||||
rsaKeyPair: any;
|
||||
|
||||
/**
|
||||
* generates a fresh rsa keyPair
|
||||
*/
|
||||
static async generateFresh(): Promise<KeyPair> {
|
||||
const done = plugins.smartpromise.defer();
|
||||
var options = { bitlen: 2048, exp: 65537, public: true, pem: true, internal: true };
|
||||
rsa.generateKeypair(options, function(err, keypair) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
done.resolve(keypair);
|
||||
});
|
||||
const result: any = await done.promise;
|
||||
const keyPair = new KeyPair(result);
|
||||
return keyPair;
|
||||
}
|
||||
|
||||
constructor(rsaKeyPairArg) {
|
||||
this.rsaKeyPair = rsaKeyPairArg;
|
||||
}
|
||||
}
|
@ -1,86 +1,167 @@
|
||||
const acme = require('acme-v2').ACME.create({
|
||||
RSA: require('rsa-compat').RSA,
|
||||
|
||||
// used for constructing user-agent
|
||||
os: require('os'),
|
||||
process: require('process'),
|
||||
|
||||
// used for overriding the default user-agent
|
||||
userAgent: 'My custom UA String',
|
||||
getUserAgentString: function(deps) {
|
||||
return 'My custom UA String';
|
||||
},
|
||||
|
||||
// don't try to validate challenges locally
|
||||
skipChallengeTest: true
|
||||
});
|
||||
|
||||
import { KeyPair } from './smartacme.classes.keypair';
|
||||
import * as plugins from './smartacme.plugins';
|
||||
const rsa = require('rsa-compat').RSA;
|
||||
import { CertManager } from './smartacme.classes.certmanager';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
import { request } from 'http';
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export interface ISmartAcmeOptions {
|
||||
accountPrivateKey?: string;
|
||||
accountEmail: string;
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||
removeChallenge: (domainName: string) => Promise<any>;
|
||||
validateRemoteRequest: () => Promise<boolean>;
|
||||
}
|
||||
|
||||
export class SmartAcme {
|
||||
domainKeyPair: KeyPair;
|
||||
accountKeyPair: KeyPair;
|
||||
accountData: any;
|
||||
directoryUrls: any;
|
||||
private options: ISmartAcmeOptions;
|
||||
|
||||
async init() {
|
||||
// get directory url
|
||||
this.directoryUrls = await acme.init('https://acme-staging-v02.api.letsencrypt.org/directory');
|
||||
// the acme client
|
||||
private client: any;
|
||||
private smartdns = new plugins.smartdns.Smartdns();
|
||||
|
||||
// create keyPairs
|
||||
this.domainKeyPair = await KeyPair.generateFresh();
|
||||
this.accountKeyPair = await KeyPair.generateFresh();
|
||||
// the account private key
|
||||
private privateKey: string;
|
||||
|
||||
// get account
|
||||
const registrationData = await acme.accounts
|
||||
.create({
|
||||
email: 'domains@lossless.org', // valid email (server checks MX records)
|
||||
accountKeypair: this.accountKeyPair.rsaKeyPair,
|
||||
agreeToTerms: async tosUrl => {
|
||||
return tosUrl;
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
console.log(e);
|
||||
});
|
||||
this.accountData = registrationData;
|
||||
// challenge fullfillment
|
||||
private setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||
private removeChallenge: (domainName: string) => Promise<any>;
|
||||
private validateRemoteRequest: () => Promise<boolean>;
|
||||
|
||||
// certmanager
|
||||
private certmanager: CertManager;
|
||||
private certremoteHandler: plugins.smartexpress.Handler;
|
||||
|
||||
constructor(optionsArg: ISmartAcmeOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
async getCertificateForDomain(domain) {
|
||||
const result = await acme.certificates
|
||||
.create({
|
||||
domainKeypair: this.domainKeyPair.rsaKeyPair,
|
||||
accountKeypair: this.accountKeyPair.rsaKeyPair,
|
||||
domains: ['bleu.de'],
|
||||
challengeType: 'dns-01',
|
||||
/**
|
||||
* inits the instance
|
||||
*/
|
||||
public async init() {
|
||||
this.privateKey =
|
||||
this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey());
|
||||
this.setChallenge = this.options.setChallenge;
|
||||
this.removeChallenge = this.options.removeChallenge;
|
||||
|
||||
setChallenge: async (hostname, key, val, cb) => {
|
||||
console.log('set challenge');
|
||||
console.log(hostname);
|
||||
//console.log(key);
|
||||
//console.log(val);
|
||||
const dnsKey = rsa.utils.toWebsafeBase64(
|
||||
require('crypto')
|
||||
.createHash('sha256')
|
||||
.update(val)
|
||||
.digest('base64')
|
||||
);
|
||||
// CertMangaer
|
||||
this.certmanager = new CertManager(this, {
|
||||
mongoDescriptor: this.options.mongoDescriptor
|
||||
});
|
||||
await this.certmanager.init();
|
||||
|
||||
console.log(dnsKey);
|
||||
await plugins.smartdelay.delayFor(20000);
|
||||
console.log('ready!');
|
||||
cb();
|
||||
}, // return Promise
|
||||
removeChallenge: async (hostname, key) => {
|
||||
console.log('removing challenge');
|
||||
return;
|
||||
} // return Promise
|
||||
})
|
||||
.catch(e => {
|
||||
console.log(e);
|
||||
}); // returns Promise<pems={ privkey (key), cert, chain (ca) }>
|
||||
console.log(result);
|
||||
// CertRemoteHandler
|
||||
this.certremoteHandler = new plugins.smartexpress.Handler('POST', 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: {
|
||||
created: existingCertificate.created,
|
||||
csr: existingCertificate.csr,
|
||||
domainName: existingCertificate.domainName,
|
||||
privateKey: existingCertificate.privateKey,
|
||||
publicKey: existingCertificate.publicKey
|
||||
}
|
||||
};
|
||||
break;
|
||||
default:
|
||||
response = {
|
||||
status
|
||||
};
|
||||
break;
|
||||
}
|
||||
res.status(200);
|
||||
res.send(response);
|
||||
res.end();
|
||||
});
|
||||
|
||||
// ACME Client
|
||||
this.client = new plugins.acme.Client({
|
||||
directoryUrl: plugins.acme.directory.letsencrypt.staging,
|
||||
accountKey: this.privateKey
|
||||
});
|
||||
|
||||
/* Register account */
|
||||
await this.client.createAccount({
|
||||
termsOfServiceAgreed: true,
|
||||
contact: [`mailto:${this.options.accountEmail}`]
|
||||
});
|
||||
}
|
||||
|
||||
public async getCertificateForDomain(domainArg: string) {
|
||||
const domain = domainArg;
|
||||
|
||||
const retrievedCertificate = await this.certmanager.retrieveCertificate(domain);
|
||||
|
||||
if (retrievedCertificate) {
|
||||
return retrievedCertificate;
|
||||
}
|
||||
|
||||
/* Place new order */
|
||||
const order = await this.client.createOrder({
|
||||
identifiers: [{ type: 'dns', value: domain }, { type: 'dns', value: `*.${domain}` }]
|
||||
});
|
||||
|
||||
/* Get authorizations and select challenges */
|
||||
const authorizations = await this.client.getAuthorizations(order);
|
||||
|
||||
for (const authz of authorizations) {
|
||||
console.log(authz);
|
||||
const domainDnsName: string = `_acme-challenge.${authz.identifier.value}`;
|
||||
const dnsChallenge: string = authz.challenges.find(challengeArg => {
|
||||
return challengeArg.type === 'dns-01';
|
||||
});
|
||||
// process.exit(1);
|
||||
const keyAuthorization: string = await this.client.getChallengeKeyAuthorization(dnsChallenge);
|
||||
|
||||
try {
|
||||
/* Satisfy challenge */
|
||||
await this.setChallenge(domainDnsName, keyAuthorization);
|
||||
await this.smartdns.checkUntilAvailable(domainDnsName, 'TXT', keyAuthorization, 100, 5000);
|
||||
|
||||
/* Verify that challenge is satisfied */
|
||||
await this.client.verifyChallenge(authz, dnsChallenge);
|
||||
|
||||
/* Notify ACME provider that challenge is satisfied */
|
||||
await this.client.completeChallenge(dnsChallenge);
|
||||
|
||||
/* Wait for ACME provider to respond with valid status */
|
||||
await this.client.waitForValidStatus(dnsChallenge);
|
||||
} finally {
|
||||
/* Clean up challenge response */
|
||||
try {
|
||||
await this.removeChallenge(domainDnsName);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Finalize order */
|
||||
const [key, csr] = await plugins.acme.forge.createCsr({
|
||||
commonName: `*.${domain}`,
|
||||
altNames: [domain]
|
||||
});
|
||||
|
||||
await this.client.finalizeOrder(order, csr);
|
||||
const cert = await this.client.getCertificate(order);
|
||||
|
||||
/* Done */
|
||||
console.log(`CSR:\n${csr.toString()}`);
|
||||
console.log(`Private key:\n${key.toString()}`);
|
||||
console.log(`Certificate:\n${cert.toString()}`);
|
||||
|
||||
this.certmanager.storeCertificate(key.toString(), cert.toString(), csr.toString());
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,17 @@
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
// @pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdata from '@pushrocks/smartdata';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartdns from '@pushrocks/smartdns';
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartunique from '@pushrocks/smartunique';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { smartpromise, smartdelay };
|
||||
export { lik, smartdata, smartdelay, smartdns, smartexpress, smartpromise, smartrequest, smartunique, smarttime };
|
||||
|
||||
// thirs party scope
|
||||
import * as acme from 'acme-client';
|
||||
|
||||
export { acme };
|
||||
|
7
tsconfig.json
Normal file
7
tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"target": "es2017",
|
||||
"module": "commonjs"
|
||||
}
|
||||
}
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user