@ -45,8 +45,12 @@ function readRenewalConfig(args) {
}
function writeRenewalConfig ( args ) {
//console.log('args');
//console.log(args);
function log ( ) {
if ( args . debug ) {
console . log . apply ( console , arguments ) ;
}
}
var pyobj = args . pyobj ;
pyobj . checkpoints = parseInt ( pyobj . checkpoints , 10 ) || 0 ;
@ -61,10 +65,7 @@ function writeRenewalConfig(args) {
//|| args.domainPrivateKeyPath || args.domainKeyPath || pyobj.keyPath
|| path . join ( liveDir , 'privkey.pem' ) ;
if ( args . debug ) {
console . log ( '################ privkeyPath ################' ) ;
console . log ( privkeyPath ) ;
}
log ( '[le/core.js] privkeyPath' , privkeyPath ) ;
var updates = {
account : args . account . id
@ -159,10 +160,14 @@ function getOrCreateRenewal(args) {
}
function writeCertificateAsync ( args , defaults , handlers ) {
if ( args . debug ) {
console . log ( "got certificate!" ) ;
function log ( ) {
if ( args . debug ) {
console . log . apply ( console , arguments ) ;
}
}
log ( "[le/core.js] got certificate!" ) ;
var obj = args . pyobj ;
var result = args . pems ;
@ -178,9 +183,7 @@ function writeCertificateAsync(args, defaults, handlers) {
//|| args.domainPrivateKeyPath || args.domainKeyPath || obj.keyPath
|| path . join ( liveDir , 'privkey.pem' ) ;
if ( args . debug ) {
console . log ( '[LE] privkeyPath' , privkeyPath ) ;
}
log ( '[le/core.js] privkeyPath' , privkeyPath ) ;
var archiveDir = args . archiveDir || path . join ( args . configDir , 'archive' , args . domains [ 0 ] ) ;
@ -252,39 +255,32 @@ function writeCertificateAsync(args, defaults, handlers) {
}
function getCertificateAsync ( args , defaults , handlers ) {
function log ( ) {
if ( args . debug || defaults . debug ) {
console . log . apply ( console , arguments ) ;
}
}
var account = args . account ;
var promise ;
var keypairOpts = { public : true , pem : true } ;
if ( ! args . domainKeyPath ) {
// TODO use default path ???
if ( args . debug ) {
console . log ( '[domainKeyPath]: none' ) ;
}
promise = RSA . generateKeypairAsync ( args . rsaKeySize , 65537 , keypairOpts ) ;
}
log ( '[le/core.js] domainKeyPath:' , args . domainKeyPath ) ;
if ( args . domainKeyPath ) {
if ( args . debug ) {
console . log ( '[domainKeyPath]:' , args . domainKeyPath ) ;
}
promise = fs . readFileAsync ( args . domainKeyPath , 'ascii' ) . then ( function ( pem ) {
return RSA . import ( { privateKeyPem : pem } ) ;
} , function ( /*err*/ ) {
return RSA . generateKeypairAsync ( args . rsaKeySize , 65537 , keypairOpts ) . then ( function ( keypair ) {
return mkdirpAsync ( path . dirname ( args . domainKeyPath ) ) . then ( function ( ) {
return fs . writeFileAsync ( args . domainKeyPath , keypair . privateKeyPem , 'ascii' ) . then ( function ( ) {
return keypair ;
} ) ;
promise = fs . readFileAsync ( args . domainKeyPath , 'ascii' ) . then ( function ( pem ) {
return RSA . import ( { privateKeyPem : pem } ) ;
} , function ( /*err*/ ) {
return RSA . generateKeypairAsync ( args . rsaKeySize , 65537 , keypairOpts ) . then ( function ( keypair ) {
return mkdirpAsync ( path . dirname ( args . domainKeyPath ) ) . then ( function ( ) {
return fs . writeFileAsync ( args . domainKeyPath , keypair . privateKeyPem , 'ascii' ) . then ( function ( ) {
return keypair ;
} ) ;
} ) ;
} ) ;
}
} ) ;
return promise . then ( function ( domainKeypair ) {
if ( args . debug ) {
console . log ( "[letsencrypt/lib/core.js] get certificate" ) ;
}
log ( "[le/core.js] get certificate" ) ;
args . domainKeypair = domainKeypair ;
//args.registration = domainKey;
@ -346,35 +342,37 @@ function getCertificateAsync(args, defaults, handlers) {
}
function getOrCreateDomainCertificate ( args , defaults , handlers ) {
if ( args . duplicate ) {
// we're forcing a refresh via 'dupliate: true'
return getCertificateAsync ( args , defaults , handlers ) ;
}
return fetchFromConfigLiveDir ( args ) . then ( function ( certs ) {
// if nothing, register and save
// if something, check date (don't register unless 30+ days)
// if good, don't bother registering
// (but if we get to the point that we're actually calling
// this function, that shouldn't be the case, right?)
//console.log(certs);
if ( ! certs ) {
// no certs, seems like a good time to get some
return getCertificateAsync ( args , defaults , handlers ) ;
}
else if ( ( Date . now ( ) - certs . issuedAt ) > ( 27 * 24 * 60 * 60 * 1000 ) ) {
// cert is at least 27 days old we can renew that
return getCertificateAsync ( args , defaults , handlers ) ;
}
else if ( args . duplicate ) {
// YOLO! I be gettin' fresh certs 'erday! Yo!
var halfLife = ( certs . expiresAt - certs . issuedAt ) / 2 ;
if ( ! certs || ( Date . now ( ) - certs . issuedAt ) > halfLife ) {
// There is no cert available
// Or the cert is more than half-expired
return getCertificateAsync ( args , defaults , handlers ) ;
}
else {
console . warn ( '[WARN] Ignoring renewal attempt for certificate less than 27 days old. Use args.duplicate to force.' ) ;
// We're happy with what we have
return certs ;
}
return PromiseA . reject ( new Error (
"[ERROR] Certificate issued at '"
+ new Date ( certs . issuedAt ) . toISOString ( ) + "' and expires at '"
+ new Date ( certs . expiresAt ) . toISOString ( ) + "'. Ignoring renewal attempt until half-life at '"
+ new Date ( certs . issuedA + halfLife ) . toISOString ( ) + "'. Set { duplicate: true } to force."
) ) ;
} ) ;
}
// returns 'account' from lib/accounts { meta, regr, keypair, accountId (id) }
function getOrCreateAcmeAccount ( args , defaults , handlers ) {
function log ( ) {
if ( args . debug ) {
console . log . apply ( console , arguments ) ;
}
}
var pyconf = PromiseA . promisifyAll ( require ( 'pyconf' ) ) ;
return pyconf . readFileAsync ( args . renewalPath ) . then ( function ( renewal ) {
@ -384,9 +382,7 @@ function getOrCreateAcmeAccount(args, defaults, handlers) {
return accountId ;
} , function ( err ) {
if ( "ENOENT" === err . code ) {
if ( args . debug ) {
console . log ( "[LE] try email" ) ;
}
log ( "[le/core.js] try email" ) ;
return Accounts . getAccountIdByEmail ( args , handlers ) ;
}
@ -398,15 +394,12 @@ function getOrCreateAcmeAccount(args, defaults, handlers) {
args . _ acmeUrls = urls ;
if ( accountId ) {
if ( args . debug ) {
console . log ( '[LE] use account' ) ;
}
log ( '[le/core.js] use account' ) ;
args . accountId = accountId ;
return Accounts . getAccount ( args , handlers ) ;
} else {
if ( args . debug ) {
console . log ( '[LE] create account' ) ;
}
log ( '[le/core.js] create account' ) ;
return Accounts . createAccount ( args , handlers ) ;
}
} ) ;
@ -417,9 +410,7 @@ function getOrCreateAcmeAccount(args, defaults, handlers) {
return ;
}
* /
if ( args . debug ) {
console . log ( '[LE] created account' ) ;
}
log ( '[le/core.js] created account' ) ;
return account ;
} ) ;
/ *
@ -448,10 +439,6 @@ module.exports.create = function (defaults, handlers) {
copy = merge ( args , defaults ) ;
tplCopy ( copy ) ;
if ( copy . debug ) {
console . log ( '[LE DEBUG] reg domains' , args . domains ) ;
}
var url = require ( 'url' ) ;
var acmeLocation = url . parse ( copy . server ) ;
var acmeHostpath = path . join ( acmeLocation . hostname , acmeLocation . pathname ) ;
@ -469,8 +456,6 @@ module.exports.create = function (defaults, handlers) {
} ) . then ( function ( result ) {
return result ;
} , function ( err ) {
console . error ( '[DEBUG le/lib/core.js] registeryAsync err' ) ;
console . error ( err && err . stack || err ) ;
return PromiseA . reject ( err ) ;
} ) ;
}
@ -478,9 +463,6 @@ module.exports.create = function (defaults, handlers) {
var copy = merge ( args , defaults ) ;
tplCopy ( copy ) ;
if ( args . debug ) {
console . log ( '[LE DEBUG] fetch domains' , copy ) ;
}
return fetchFromConfigLiveDir ( copy , defaults ) ;
}
, configureAsync : function ( hargs ) {
@ -488,9 +470,6 @@ module.exports.create = function (defaults, handlers) {
var copy = merge ( hargs , defaults ) ;
tplCopy ( copy ) ;
//console.log('[LE] configureAsync copy');
//console.log(hargs);
//console.log(copy);
return getOrCreateAcmeAccount ( copy , defaults , handlers ) . then ( function ( account ) {
copy . account = account ;
return getOrCreateRenewal ( copy ) ;
@ -503,10 +482,6 @@ module.exports.create = function (defaults, handlers) {
var copy = merge ( hargs , defaults ) ;
tplCopy ( copy ) ;
if ( copy . debug ) {
console . log ( '[LE DEBUG] get configs' , copy ) ;
}
return readRenewalConfig ( copy ) . then ( function ( pyobj ) {
var exists = pyobj . checkpoints >= 0 ;
if ( ! exists ) {
@ -524,10 +499,6 @@ module.exports.create = function (defaults, handlers) {
var copy = merge ( hargs , defaults ) ;
tplCopy ( copy ) ;
if ( copy . debug ) {
console . log ( '[LE DEBUG] get configs' , copy ) ;
}
return fs . readdirAsync ( copy . renewalDir ) . then ( function ( nodes ) {
nodes = nodes . filter ( function ( node ) {
return /^[a-z0-9]+.*\.conf$/ . test ( node ) ;