Compare commits

...

22 Commits

Author SHA1 Message Date
bfeced5f34 3.0.9 2020-11-18 16:52:50 +00:00
24b9794a18 fix(core): update 2020-11-18 16:52:49 +00:00
a781329a47 3.0.8 2020-08-13 03:10:38 +00:00
6b5e0a1207 fix(core): update 2020-08-13 03:10:37 +00:00
2455adfbca 3.0.7 2020-08-12 16:36:06 +00:00
a2cf86b62f fix(core): update 2020-08-12 16:36:06 +00:00
7277906851 3.0.6 2020-05-17 16:21:26 +00:00
9da9ebb01e fix(core): update 2020-05-17 16:21:25 +00:00
f70684b773 3.0.5 2020-02-21 10:48:09 +00:00
8b19b206a4 fix(core): update 2020-02-21 10:48:08 +00:00
6be2866ddd 3.0.4 2020-02-19 19:17:59 +00:00
ab55d3c91a fix(core): update 2020-02-19 19:17:58 +00:00
c7ee7eb774 3.0.3 2020-02-19 18:48:50 +00:00
02daa13a2f fix(core): update 2020-02-19 18:48:49 +00:00
28944b1100 3.0.2 2020-02-10 20:36:02 +00:00
7ec04d6d3d fix(core): update 2020-02-10 20:36:01 +00:00
595d4d8894 3.0.1 2020-02-10 20:16:50 +00:00
04ed28f7d1 fix(core): update 2020-02-10 20:16:49 +00:00
6c95cec709 3.0.0 2020-02-10 20:13:07 +00:00
59173b3ca8 BREAKING CHANGE(core): streamline scope to certificate retrieval using dns challenge 2020-02-10 20:13:06 +00:00
c2036bba90 2.1.2 2020-02-10 11:15:48 +00:00
83afea95e6 fix(core): update 2020-02-10 11:15:47 +00:00
20 changed files with 9327 additions and 1626 deletions

19
.gitignore vendored
View File

@ -1,5 +1,20 @@
node_modules/ .nogit/
# artifacts
coverage/ coverage/
public/ public/
pages/ pages/
.nogit/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom

View File

@ -1,16 +1,16 @@
# gitzone standard # gitzone ci_default
image: hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache: cache:
paths: paths:
- .npmci_cache/ - .npmci_cache/
key: "$CI_BUILD_STAGE" key: '$CI_BUILD_STAGE'
stages: stages:
- security - security
- test - test
- release - release
- metadata - metadata
# ==================== # ====================
# security stage # security stage
@ -18,127 +18,114 @@ stages:
mirror: mirror:
stage: security stage: security
script: script:
- npmci git mirror - npmci git mirror
only:
- tags
tags: tags:
- docker - lossless
- notpriv - docker
- notpriv
snyk: auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security stage: security
script: script:
- npmci npm prepare - npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts - npmci command npm install --ignore-scripts
- npmci command snyk test - npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags: tags:
- docker - docker
- notpriv
sast:
stage: security
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
variables:
DOCKER_DRIVER: overlay2
allow_failure: true 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 # test stage
# ==================== # ====================
testLTS: testStable:
stage: test stage: test
script: script:
- npmci npm prepare - npmci npm prepare
- npmci node install lts - npmci node install stable
- npmci npm install - npmci npm install
- npmci npm test - npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv
testBuild:
testSTABLE:
stage: test stage: test
script: script:
- npmci npm prepare - npmci npm prepare
- npmci node install stable - npmci node install stable
- npmci npm install - npmci npm install
- npmci npm test - npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv
release: release:
stage: release stage: release
script: script:
- npmci node install stable - npmci node install stable
- npmci npm publish - npmci npm publish
only: only:
- tags - tags
tags: tags:
- docker - lossless
- notpriv - docker
- notpriv
# ==================== # ====================
# metadata stage # metadata stage
# ==================== # ====================
codequality: codequality:
stage: metadata stage: metadata
image: docker:stable
allow_failure: true allow_failure: true
services: only:
- docker:stable-dind - tags
script: script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - npmci command npm install -g tslint typescript
- docker run - npmci npm prepare
--env SOURCE_CODE="$PWD" - npmci npm install
--volume "$PWD":/code - npmci command "tslint -c tslint.json ./ts/**/*.ts"
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags: tags:
- docker - lossless
- priv - docker
- priv
trigger: trigger:
stage: metadata stage: metadata
script: script:
- npmci trigger - npmci trigger
only: only:
- tags - tags
tags: tags:
- docker - lossless
- notpriv - docker
- notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata stage: metadata
script: script:
- npmci command npm install -g typedoc typescript - npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare - npmci npm prepare
- npmci npm install - npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/ - npmci command tsdoc
tags: tags:
- lossless
- docker - docker
- notpriv - notpriv
only: only:
@ -146,5 +133,5 @@ pages:
artifacts: artifacts:
expire_in: 1 week expire_in: 1 week
paths: paths:
- public - public
allow_failure: true allow_failure: true

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "current file",
"type": "node",
"request": "launch",
"args": [
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -1,36 +0,0 @@
# @pushrocks/smartuniverse
acme with an easy yet powerful interface in TypeScript
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartuniverse)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartuniverse)
* [github.com (source mirror)](https://github.com/pushrocks/smartuniverse)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartuniverse/)
## Status for master
[![build status](https://gitlab.com/pushrocks/smartuniverse/badges/master/build.svg)](https://gitlab.com/pushrocks/smartuniverse/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartuniverse/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartuniverse/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartuniverse.svg)](https://www.npmjs.com/package/@pushrocks/smartuniverse)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartuniverse/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartuniverse)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
Use TypeScript for best in class instellisense.
```javascript
import { SmartAcme } from 'smartacme';
let smac = new SmartAcme()
// TODO
```
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

View File

@ -1,11 +1,12 @@
{ {
"gitzone": { "gitzone": {
"projectType": "npm",
"module": { "module": {
"githost": "gitlab.com", "githost": "gitlab.com",
"gitscope": "pushrocks", "gitscope": "pushrocks",
"gitrepo": "smartuniverse", "gitrepo": "smartacme",
"shortDescription": "acme with an easy yet powerful interface in TypeScript", "shortDescription": "acme with an easy yet powerful interface in TypeScript",
"npmPackagename": "@pushrocks/smartuniverse", "npmPackagename": "@pushrocks/smartacme",
"license": "MIT", "license": "MIT",
"projectDomain": "push.rocks" "projectDomain": "push.rocks"
} }
@ -14,4 +15,4 @@
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
} }
} }

10217
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
{ {
"name": "@pushrocks/smartacme", "name": "@pushrocks/smartacme",
"version": "2.1.1", "version": "3.0.9",
"private": false, "private": false,
"description": "acme with an easy yet powerful interface in TypeScript", "description": "acme with an easy yet powerful interface in TypeScript",
"main": "dist/index.js", "main": "dist_ts/index.js",
"typings": "dist/index.d.ts", "typings": "dist_ts/index.d.ts",
"scripts": { "scripts": {
"test": "(tstest test/)", "test": "(tstest test/)",
"build": "(tsbuild)" "build": "(tsbuild --web)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -25,28 +25,44 @@
}, },
"homepage": "https://gitlab.com/umbrellazone/smartacme#README", "homepage": "https://gitlab.com/umbrellazone/smartacme#README",
"dependencies": { "dependencies": {
"@pushrocks/lik": "^3.0.13", "@pushrocks/lik": "^4.0.17",
"@pushrocks/smartdata": "^3.1.23", "@pushrocks/smartdata": "^3.1.54",
"@pushrocks/smartdelay": "^2.0.6", "@pushrocks/smartdelay": "^2.0.10",
"@pushrocks/smartdns": "^3.0.8", "@pushrocks/smartdns": "^4.0.4",
"@pushrocks/smartexpress": "^3.0.54", "@pushrocks/smartexpress": "^3.0.97",
"@pushrocks/smartlog": "^2.0.21", "@pushrocks/smartlog": "^2.0.39",
"@pushrocks/smartpromise": "^3.0.6", "@pushrocks/smartpromise": "^3.1.3",
"@pushrocks/smartrequest": "^1.1.47", "@pushrocks/smartrequest": "^1.1.51",
"@pushrocks/smartstring": "^3.0.17", "@pushrocks/smartstring": "^3.0.18",
"@pushrocks/smarttime": "^3.0.12", "@pushrocks/smarttime": "^3.0.37",
"@pushrocks/smartunique": "^3.0.1", "@pushrocks/smartunique": "^3.0.3",
"acme-client": "2.2.2" "@tsclass/tsclass": "^3.0.29",
"acme-client": "^3.3.1"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.17", "@gitzone/tsbuild": "^2.1.25",
"@gitzone/tsrun": "^1.2.8", "@gitzone/tsrun": "^1.2.12",
"@gitzone/tstest": "^1.0.28", "@gitzone/tstest": "^1.0.52",
"@mojoio/cloudflare": "^3.0.5", "@mojoio/cloudflare": "^5.0.6",
"@pushrocks/qenv": "^4.0.6", "@pushrocks/qenv": "^4.0.10",
"@pushrocks/tapbundle": "^3.2.0", "@pushrocks/tapbundle": "^3.2.9",
"@types/node": "^13.1.8", "@types/node": "^14.14.7",
"tslint": "^5.20.1", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0" "tslint-config-prettier": "^1.18.0"
} },
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
} }

View File

@ -1,6 +1,5 @@
required: required:
- CF_EMAIL - CF_TOKEN
- CF_KEY
- MONGODB_URL - MONGODB_URL
- MONGODB_PASSWORD - MONGODB_PASSWORD
- MONGODB_DATABASE - MONGODB_DATABASE

66
readme.md Normal file
View File

@ -0,0 +1,66 @@
# @pushrocks/smartacme
acme with an easy yet powerful interface in TypeScript
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartacme)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartacme)
* [github.com (source mirror)](https://github.com/pushrocks/smartacme)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartacme/)
## Status for master
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartacme/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartacme/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartacme)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartacme)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartacme)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartacme)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartacme)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
## Usage
Use TypeScript for best in class instellisense.
```javascript
import { SmartAcme } from 'smartacme';
const run = async () => {
smartAcmeInstance = new smartacme.SmartAcme({
accountEmail: 'domains@lossless.org',
accountPrivateKey: null,
mongoDescriptor: {
mongoDbName: testQenv.getEnvVarRequired('MONGODB_DATABASE'),
mongoDbPass: testQenv.getEnvVarRequired('MONGODB_PASSWORD'),
mongoDbUrl: testQenv.getEnvVarRequired('MONGODB_URL'),
},
removeChallenge: async (dnsChallenge) => {
// somehow provide a function that is able to remove the dns challenge
},
setChallenge: async (dnsChallenge) => {
// somehow provide a function that is able to the dns challenge
},
environment: 'integration',
});
await smartAcmeInstance.init();
// myCert has properties for public/private keys and csr ;)
const myCert = await smartAcmeInstance.getCertificateForDomain('bleu.de');
};
```
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -1,7 +1,9 @@
import { tap, expect } from '@pushrocks/tapbundle'; import { tap, expect } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv'; import { Qenv } from '@pushrocks/qenv';
import * as cloudflare from '@mojoio/cloudflare';
const testQenv = new Qenv('./', './.nogit/'); const testQenv = new Qenv('./', './.nogit/');
const testCloudflare = new cloudflare.CloudflareAccount(testQenv.getEnvVarOnDemand('CF_TOKEN'));
import * as smartacme from '../ts/index'; import * as smartacme from '../ts/index';
@ -14,18 +16,21 @@ tap.test('should create a valid instance of SmartAcme', async () => {
mongoDescriptor: { mongoDescriptor: {
mongoDbName: testQenv.getEnvVarRequired('MONGODB_DATABASE'), mongoDbName: testQenv.getEnvVarRequired('MONGODB_DATABASE'),
mongoDbPass: testQenv.getEnvVarRequired('MONGODB_PASSWORD'), mongoDbPass: testQenv.getEnvVarRequired('MONGODB_PASSWORD'),
mongoDbUrl: testQenv.getEnvVarRequired('MONGODB_URL') mongoDbUrl: testQenv.getEnvVarRequired('MONGODB_URL'),
}, },
removeChallenge: async (...args) => { removeChallenge: async (dnsChallenge) => {
console.log(args); testCloudflare.convenience.acmeRemoveDnsChallenge(dnsChallenge);
}, },
setChallenge: async (...args) => { setChallenge: async (dnsChallenge) => {
console.log(args); testCloudflare.convenience.acmeSetDnsChallenge(dnsChallenge);
}, },
environment: 'integration' environment: 'integration',
}); });
await smartAcmeInstance.init(); await smartAcmeInstance.init();
// await smartAcmeInstance.getCertificateForDomain('bleu.de'); });
tap.test('should get a domain certificate', async () => {
await smartAcmeInstance.getCertificateForDomain('bleu.de');
}); });
tap.test('certmatcher should correctly match domains', async () => { tap.test('certmatcher should correctly match domains', async () => {

View File

@ -1,3 +1 @@
export * from './smartacme.classes.smartacme'; export * from './smartacme.classes.smartacme';
export * from './smartacme.classes.certremoteclient';

View File

@ -1,10 +0,0 @@
export type TCertStatus = 'existing' | 'nonexisting' | 'pending' | 'failed';
export interface ICert {
id: string;
domainName: string;
created: number;
privateKey: string;
publicKey: string;
csr: string;
}

View File

@ -1,11 +0,0 @@
import { ICert, TCertStatus } from './cert';
export interface ICertRemoteRequest {
secret: string;
domainName: string;
}
export interface ICertRemoteResponse {
status: TCertStatus;
certificate?: ICert;
}

View File

@ -1,3 +1 @@
export * from './accountdata'; export * from './accountdata';
export * from './cert';
export * from './certremote';

View File

@ -9,7 +9,8 @@ import { Collection, svDb, unI } from '@pushrocks/smartdata';
@plugins.smartdata.Collection(() => { @plugins.smartdata.Collection(() => {
return CertManager.activeDB; return CertManager.activeDB;
}) })
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert { export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert, plugins.tsclass.network.ICert>
implements plugins.tsclass.network.ICert {
@unI() @unI()
public id: string; public id: string;
@ -28,29 +29,32 @@ export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements inte
@svDb() @svDb()
public csr: string; public csr: string;
@svDb()
public validUntil: number;
public isStillValid(): boolean {
return this.validUntil >= Date.now();
}
/** public shouldBeRenewed(): boolean {
* computed value for when the certificate is still valid const shouldBeValidAtLeastUntil =
*/ Date.now() +
get validUntil (): number { plugins.smarttime.getMilliSecondsFromUnits({
return this.created + plugins.smarttime.getMilliSecondsFromUnits({ days: 10,
days: 90 });
return !(this.validUntil >= shouldBeValidAtLeastUntil);
}
public update(certDataArg: plugins.tsclass.network.ICert) {
Object.keys(certDataArg).forEach((key) => {
this[key] = certDataArg[key];
}); });
} }
constructor(optionsArg: plugins.tsclass.network.ICert) {
get isStillValid (): boolean {
const shouldBeValitAtLeastUntil = Date.now() + plugins.smarttime.getMilliSecondsFromUnits({
days: 10
});
return this.validUntil >= shouldBeValitAtLeastUntil;
}
constructor(optionsArg: interfaces.ICert) {
super(); super();
if (optionsArg) { if (optionsArg) {
Object.keys(optionsArg).forEach(key => { Object.keys(optionsArg).forEach((key) => {
this[key] = optionsArg[key]; this[key] = optionsArg[key];
}); });
} }

View File

@ -16,7 +16,7 @@ export class CertManager {
private mongoDescriptor: plugins.smartdata.IMongoDescriptor; private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
public smartdataDb: plugins.smartdata.SmartdataDb; public smartdataDb: plugins.smartdata.SmartdataDb;
public pendingMap: plugins.lik.Stringmap; public interestMap: plugins.lik.InterestMap<string, Cert>;
constructor( constructor(
smartAcmeArg: SmartAcme, smartAcmeArg: SmartAcme,
@ -34,18 +34,17 @@ export class CertManager {
CertManager.activeDB = this.smartdataDb; CertManager.activeDB = this.smartdataDb;
// Pending Map // Pending Map
this.pendingMap = new plugins.lik.Stringmap(); this.interestMap = new plugins.lik.InterestMap((certName) => certName);
} }
/** /**
* retrieves a certificate * retrieves a certificate
* @returns the Cert class or null * @returns the Cert class or null
* @param domainName the domain Name to retrieve the vcertificate for * @param certDomainNameArg the domain Name to retrieve the vcertificate for
*/ */
public async retrieveCertificate(domainName: string): Promise<Cert> { public async retrieveCertificate(certDomainNameArg: string): Promise<Cert> {
await this.checkCerts(); const existingCertificate: Cert = await Cert.getInstance<Cert>({
const existingCertificate: Cert = await Cert.getInstance({ domainName: certDomainNameArg,
domainName
}); });
if (existingCertificate) { if (existingCertificate) {
@ -59,44 +58,20 @@ export class CertManager {
* stores the certificate * stores the certificate
* @param optionsArg * @param optionsArg
*/ */
public async storeCertificate(optionsArg: interfaces.ICert) { public async storeCertificate(optionsArg: plugins.tsclass.network.ICert) {
const cert = new Cert(optionsArg); const cert = new Cert(optionsArg);
await cert.save(); await cert.save();
this.pendingMap.removeString(optionsArg.domainName); const interest = this.interestMap.findInterest(cert.domainName);
} if (interest) {
interest.fullfillInterest(cert);
public async deleteCertificate(domainNameArg: string) {} interest.markLost();
/**
* 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 = await this.retrieveCertificate(certDomainArg);
if (existingCertificate) {
return 'existing';
}
return 'nonexisting';
} }
/** public async deleteCertificate(certDomainNameArg: string) {
* checks all certs for expiration const cert: Cert = await Cert.getInstance<Cert>({
*/ domainName: certDomainNameArg,
private async checkCerts() { });
// TODO await cert.delete();
} }
} }

View File

@ -1,56 +0,0 @@
import * as plugins from './smartacme.plugins';
import * as interfaces from './interfaces';
// tslint:disable-next-line: max-classes-per-file
export class CertRemoteClient {
private remoteUrl: string;
private secret: string;
private logger: plugins.smartlog.Smartlog;
constructor(optionsArg: {
remoteUrl: 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 responseBody: interfaces.ICertRemoteResponse = (await plugins.smartrequest.postJson(
this.remoteUrl,
{
requestBody: <interfaces.ICertRemoteRequest>{
domainName: domainNameArg,
secret: this.secret
}
}
)).body;
switch (responseBody.status as interfaces.TCertStatus) {
case 'pending':
this.logger.log('info', `request for ${domainNameArg} still pending!`);
await plugins.smartdelay.delayFor(5000);
const finalResponse = await doRequestCycle();
return finalResponse;
case 'existing':
this.logger.log('ok', `got certificate for ${domainNameArg}`);
return responseBody.certificate;
case 'failed':
default:
console.log(`could not retrieve certificate for ${domainNameArg}`);
return null;
}
};
certificate = await doRequestCycle();
return certificate;
}
}

View File

@ -3,9 +3,6 @@ import { Cert } from './smartacme.classes.cert';
import { CertManager } from './smartacme.classes.certmanager'; import { CertManager } from './smartacme.classes.certmanager';
import { CertMatcher } from './smartacme.classes.certmatcher'; import { CertMatcher } from './smartacme.classes.certmatcher';
import * as interfaces from './interfaces';
import { request } from 'http';
/** /**
* the options for the class @see SmartAcme * the options for the class @see SmartAcme
*/ */
@ -13,10 +10,9 @@ export interface ISmartAcmeOptions {
accountPrivateKey?: string; accountPrivateKey?: string;
accountEmail: string; accountEmail: string;
mongoDescriptor: plugins.smartdata.IMongoDescriptor; mongoDescriptor: plugins.smartdata.IMongoDescriptor;
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>; setChallenge: (dnsChallengeArg: plugins.tsclass.network.IDnsChallenge) => Promise<any>;
removeChallenge: (domainName: string) => Promise<any>; removeChallenge: (dnsChallengeArg: plugins.tsclass.network.IDnsChallenge) => Promise<any>;
environment: 'production' | 'integration'; environment: 'production' | 'integration';
logger?: plugins.smartlog.Smartlog;
} }
/** /**
@ -34,65 +30,23 @@ 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; public logger: plugins.smartlog.ConsoleLog;
// the account private key // the account private key
private privateKey: string; private privateKey: string;
// challenge fullfillment // challenge fullfillment
private setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>; private setChallenge: (dnsChallengeArg: plugins.tsclass.network.IDnsChallenge) => Promise<any>;
private removeChallenge: (domainName: string) => Promise<any>; private removeChallenge: (dnsChallengeArg: plugins.tsclass.network.IDnsChallenge) => Promise<any>;
// certmanager // certmanager
private certmanager: CertManager; private certmanager: CertManager;
private certmatcher: CertMatcher; 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;
this.logger.log('ok', `got certificate request for ${requestBody.domainName}`);
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(
requestBody.domainName
);
this.logger.log('ok', `mapping ${requestBody.domainName} to ${certDomain}`);
let status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(certDomain);
let response: interfaces.ICertRemoteResponse;
switch (status) {
case 'existing':
this.logger.log('ok', `certificate exists for ${certDomain}. Sending certificate!`);
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;
this.options.logger this.logger = new plugins.smartlog.ConsoleLog();
? (this.logger = optionsArg.logger)
: (this.logger = plugins.smartlog.defaultLogger);
} }
/** /**
@ -103,13 +57,13 @@ export class SmartAcme {
*/ */
public async init() { public async init() {
this.privateKey = this.privateKey =
this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey()); this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey()).toString();
this.setChallenge = this.options.setChallenge; this.setChallenge = this.options.setChallenge;
this.removeChallenge = this.options.removeChallenge; this.removeChallenge = this.options.removeChallenge;
// CertMangaer // CertMangaer
this.certmanager = new CertManager(this, { this.certmanager = new CertManager(this, {
mongoDescriptor: this.options.mongoDescriptor mongoDescriptor: this.options.mongoDescriptor,
}); });
await this.certmanager.init(); await this.certmanager.init();
@ -125,13 +79,13 @@ export class SmartAcme {
return plugins.acme.directory.letsencrypt.staging; return plugins.acme.directory.letsencrypt.staging;
} }
})(), })(),
accountKey: this.privateKey accountKey: this.privateKey,
}); });
/* Register account */ /* Register account */
await this.client.createAccount({ await this.client.createAccount({
termsOfServiceAgreed: true, termsOfServiceAgreed: true,
contact: [`mailto:${this.options.accountEmail}`] contact: [`mailto:${this.options.accountEmail}`],
}); });
} }
@ -144,8 +98,8 @@ export class SmartAcme {
* it runs through the following steps * it runs through the following steps
* *
* * look in the database * * look in the database
* * if in the database return it * * if in the database and still valid return it
* * of not in the database announce it * * if not in the database announce it
* * then get it from letsencrypt * * then get it from letsencrypt
* * store it * * store it
* * remove it from the pending map (which it go onto by announcing it) * * remove it from the pending map (which it go onto by announcing it)
@ -154,18 +108,31 @@ export class SmartAcme {
* @param domainArg * @param domainArg
*/ */
public async getCertificateForDomain(domainArg: string): Promise<Cert> { public async getCertificateForDomain(domainArg: string): Promise<Cert> {
const certDomain = this.certmatcher.getCertificateDomainNameByDomainName(domainArg); const certDomainName = this.certmatcher.getCertificateDomainNameByDomainName(domainArg);
const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomain); const retrievedCertificate = await this.certmanager.retrieveCertificate(certDomainName);
if (retrievedCertificate) { if (
!retrievedCertificate &&
(await this.certmanager.interestMap.checkInterest(certDomainName))
) {
const existingCertificateInterest = this.certmanager.interestMap.findInterest(certDomainName);
const certificate = existingCertificateInterest.interestFullfilled;
return certificate;
} else if (retrievedCertificate && !retrievedCertificate.shouldBeRenewed()) {
return retrievedCertificate; return retrievedCertificate;
} else { } else if (retrievedCertificate && retrievedCertificate.shouldBeRenewed()) {
await this.certmanager.announceCertificate(certDomain); await retrievedCertificate.delete();
} }
// lets make sure others get the same interest
const currentDomainInterst = await this.certmanager.interestMap.addInterest(certDomainName);
/* Place new order */ /* Place new order */
const order = await this.client.createOrder({ const order = await this.client.createOrder({
identifiers: [{ type: 'dns', value: certDomain }, { type: 'dns', value: `*.${certDomain}` }] identifiers: [
{ type: 'dns', value: certDomainName },
{ type: 'dns', value: `*.${certDomainName}` },
],
}); });
/* Get authorizations and select challenges */ /* Get authorizations and select challenges */
@ -173,8 +140,8 @@ export class SmartAcme {
for (const authz of authorizations) { for (const authz of authorizations) {
console.log(authz); console.log(authz);
const domainDnsName: string = `_acme-challenge.${authz.identifier.value}`; const fullHostName: string = `_acme-challenge.${authz.identifier.value}`;
const dnsChallenge: string = authz.challenges.find(challengeArg => { const dnsChallenge: string = authz.challenges.find((challengeArg) => {
return challengeArg.type === 'dns-01'; return challengeArg.type === 'dns-01';
}); });
// process.exit(1); // process.exit(1);
@ -182,8 +149,12 @@ export class SmartAcme {
try { try {
/* Satisfy challenge */ /* Satisfy challenge */
await this.setChallenge(domainDnsName, keyAuthorization); await this.setChallenge({
await this.smartdns.checkUntilAvailable(domainDnsName, 'TXT', keyAuthorization, 100, 5000); hostName: fullHostName,
challenge: keyAuthorization,
});
await plugins.smartdelay.delayFor(30000);
await this.smartdns.checkUntilAvailable(fullHostName, 'TXT', keyAuthorization, 100, 5000);
console.log('Cool down an extra 60 second for region availability'); console.log('Cool down an extra 60 second for region availability');
await plugins.smartdelay.delayFor(60000); await plugins.smartdelay.delayFor(60000);
@ -198,7 +169,10 @@ export class SmartAcme {
} finally { } finally {
/* Clean up challenge response */ /* Clean up challenge response */
try { try {
await this.removeChallenge(domainDnsName); await this.removeChallenge({
hostName: fullHostName,
challenge: keyAuthorization,
});
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
@ -207,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: `*.${certDomain}`, commonName: `*.${certDomainName}`,
altNames: [certDomain] altNames: [certDomainName],
}); });
await this.client.finalizeOrder(order, csr); await this.client.finalizeOrder(order, csr);
@ -218,14 +192,21 @@ export class SmartAcme {
await this.certmanager.storeCertificate({ await this.certmanager.storeCertificate({
id: plugins.smartunique.shortId(), id: plugins.smartunique.shortId(),
domainName: certDomain, domainName: certDomainName,
privateKey: key.toString(), privateKey: key.toString(),
publicKey: cert.toString(), publicKey: cert.toString(),
csr: csr.toString(), csr: csr.toString(),
created: Date.now() created: Date.now(),
validUntil:
Date.now() +
plugins.smarttime.getMilliSecondsFromUnits({
days: 90,
}),
}); });
const newCertificate = await this.certmanager.retrieveCertificate(certDomain); const newCertificate = await this.certmanager.retrieveCertificate(certDomainName);
currentDomainInterst.fullfillInterest(newCertificate);
currentDomainInterst.destroy();
return newCertificate; return newCertificate;
} }
} }

View File

@ -22,10 +22,15 @@ export {
smartrequest, smartrequest,
smartunique, smartunique,
smartstring, smartstring,
smarttime smarttime,
}; };
// thirs party scope // @tsclass scope
import * as tsclass from '@tsclass/tsclass';
export { tsclass };
// third party scope
import * as acme from 'acme-client'; import * as acme from 'acme-client';
export { acme }; export { acme };