2016-10-22 14:21:25 +00:00
|
|
|
import * as q from 'q'
|
2016-10-23 22:35:00 +00:00
|
|
|
import { Stringmap, Objectmap } from 'lik'
|
|
|
|
|
2016-10-22 14:21:25 +00:00
|
|
|
import * as plugins from './cert.plugins'
|
|
|
|
import * as paths from './cert.paths'
|
2016-10-23 22:35:00 +00:00
|
|
|
|
|
|
|
// classes
|
|
|
|
import { Certificate } from './cert.classes.certificate'
|
|
|
|
import { CertRepo } from './cert.classes.certrepo'
|
|
|
|
import { Letsencrypt, TLeEnv } from './cert.classes.letsencrypt'
|
|
|
|
import { ChallengeHandler } from './cert.classes.challengehandler'
|
|
|
|
|
2016-07-24 17:39:20 +00:00
|
|
|
|
|
|
|
export interface ICertConstructorOptions {
|
|
|
|
cfEmail: string,
|
|
|
|
cfKey: string,
|
2016-10-23 22:35:00 +00:00
|
|
|
sslDirPath?: string,
|
2016-07-24 17:39:20 +00:00
|
|
|
gitOriginRepo?: string,
|
2016-10-23 22:35:00 +00:00
|
|
|
leEnv?: TLeEnv
|
2016-10-22 14:21:25 +00:00
|
|
|
}
|
2016-07-24 17:39:20 +00:00
|
|
|
|
|
|
|
export class Cert {
|
2016-10-23 22:35:00 +00:00
|
|
|
domainStringRequestMap = new Stringmap()
|
|
|
|
certificateMap = new Objectmap<Certificate>()
|
|
|
|
letsencrypt: Letsencrypt
|
|
|
|
private _challengeHandler: ChallengeHandler
|
|
|
|
private _certRepo: CertRepo
|
2016-07-24 17:39:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for Cert object
|
|
|
|
*/
|
|
|
|
constructor(optionsArg: ICertConstructorOptions) {
|
|
|
|
|
2016-10-23 22:35:00 +00:00
|
|
|
// set up challengehandler
|
|
|
|
this._challengeHandler = new ChallengeHandler({
|
|
|
|
cfEmail: optionsArg.cfEmail,
|
|
|
|
cfKey: optionsArg.cfKey
|
|
|
|
})
|
2016-07-24 17:39:20 +00:00
|
|
|
|
2016-10-23 22:35:00 +00:00
|
|
|
// setup Letsencrypt
|
|
|
|
this.letsencrypt = new Letsencrypt({
|
|
|
|
leEnv: optionsArg.leEnv,
|
|
|
|
sslDir: optionsArg.sslDirPath,
|
|
|
|
challengeHandler: this._challengeHandler
|
|
|
|
})
|
|
|
|
|
|
|
|
// setup CertRpo
|
|
|
|
this._certRepo = new CertRepo({
|
|
|
|
sslDirPath: optionsArg.sslDirPath,
|
|
|
|
gitOriginRepo: optionsArg.gitOriginRepo,
|
|
|
|
certInstance: this
|
|
|
|
})
|
|
|
|
|
|
|
|
this._certRepo
|
2016-10-22 14:21:25 +00:00
|
|
|
}
|
2016-07-24 17:39:20 +00:00
|
|
|
|
|
|
|
/**
|
2016-10-23 22:35:00 +00:00
|
|
|
* adds a Certificate for a given domain
|
2016-07-24 17:39:20 +00:00
|
|
|
*/
|
2016-10-23 22:35:00 +00:00
|
|
|
addCertificate(domainNameArg: string, optionsArg: { force: boolean } = { force: false }) {
|
2016-10-22 14:21:25 +00:00
|
|
|
let done = q.defer()
|
2016-10-23 22:35:00 +00:00
|
|
|
let certificateForDomain = this.certificateMap.find((certificate) => {
|
|
|
|
return certificate.domainName === domainNameArg
|
|
|
|
})
|
|
|
|
if (certificateForDomain instanceof Certificate) {
|
|
|
|
certificateForDomain.renew()
|
|
|
|
.then(done.resolve)
|
2016-07-24 17:39:20 +00:00
|
|
|
} else {
|
2016-10-23 22:35:00 +00:00
|
|
|
certificateForDomain = new Certificate({
|
|
|
|
certInstance: this,
|
|
|
|
domainName: domainNameArg
|
|
|
|
})
|
|
|
|
certificateForDomain.renew()
|
|
|
|
.then(done.resolve)
|
2016-10-22 14:21:25 +00:00
|
|
|
}
|
|
|
|
return done.promise
|
|
|
|
}
|
2016-10-23 22:35:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* cleans up old certificates
|
|
|
|
*/
|
2016-07-24 17:39:20 +00:00
|
|
|
cleanOldCertificates() {
|
|
|
|
|
2016-10-22 14:21:25 +00:00
|
|
|
}
|
2016-07-24 17:39:20 +00:00
|
|
|
|
2016-10-23 22:35:00 +00:00
|
|
|
/**
|
|
|
|
* executes the current batch of jobs
|
|
|
|
*/
|
|
|
|
deploy() {
|
2016-07-24 17:39:20 +00:00
|
|
|
|
2016-10-22 14:21:25 +00:00
|
|
|
}
|
|
|
|
}
|