Skip to content

Commit

Permalink
Upgdate log format to match Site.js and remove debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
aral committed Jun 15, 2020
1 parent 5c834f8 commit 31611b2
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 53 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased (work is currently in progress on 1.0.0)
## Unreleased

Nothing yet.

## [1.0.1] - 2020-06-15

### Changed

- Update log format to match Site.js output.

### Fixed

- Remove debug output.

## [1.0.0] - 2020-04-15

Initial release.
2 changes: 0 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ const server = AutoEncrypt.https.createServer(options, (request, response) => {
response.end('Hello, world!')
})

console.log(AutoEncrypt)

server.listen(443, () => {
console.log(`\n ✨ “Hello, world!” server is running…\n`)
console.log(server.address())
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class AutoEncrypt {

function sniError (symbolName, callback, emoji, ...args) {
const error = Symbol.for(symbolName)
log(` ${emoji} [@small-tech/auto-connect] ${throws.errors[error](...args)}`)
log(` ${emoji} auto-encrypt❩ ${throws.errors[error](...args)}`)
callback(throws.createError(error, ...args))
}

Expand Down
2 changes: 0 additions & 2 deletions lib/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
////////////////////////////////////////////////////////////////////////////////

const fs = require('fs-extra')
const log = require('./util/log')
const Throws = require('./util/Throws')
const NewAccountRequest = require('./acme-requests/NewAccountRequest')

Expand Down Expand Up @@ -60,7 +59,6 @@ class Account {
this.data = await (new NewAccountRequest()).execute()
fs.writeFileSync(accountPath, JSON.stringify(this.data), 'utf-8')
}
log('Account', this.data)
}

// TODO: throw error if Account has not been initialised instead of crashing in getter below.
Expand Down
2 changes: 1 addition & 1 deletion lib/AcmeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AcmeRequest {

// According to RFC 8555 § 6.5, a bad nonce error should result in retry attempt.
if (error.status === 400 && error.type === 'urn:ietf:params:acme:error:badNonce') {
log(' 🔄 [Auto Encrypt] Server returned a bad nonce error. Retrying with provided nonce. (RFC 8555 § 6.5)')
log(' 🔄 ❨auto-encrypt❩ Server returned a bad nonce error. Retrying with provided nonce. (RFC 8555 § 6.5)')
const serverProvidedNonce = errorHeaders['replay-nonce']

// Take the original request details (arguments array passed to the prepare() method) and
Expand Down
13 changes: 6 additions & 7 deletions lib/Authorisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class Authorisation extends EventEmitter {
async init () {
try {
this.data = await (new AuthorisationRequest()).execute(this.authorisationUrl)
log('Authorisation', this.data)
this.authorisation = this.data.body
} catch (error) {
// TODO: Handle the error.
Expand All @@ -73,7 +72,7 @@ class Authorisation extends EventEmitter {
// then decided to provision a certificate for ar.al and www.ar.al. The authorisation for ar.al will still be
// valid until the expiry period.
if (this.authorisation.status === 'valid') {
log(` 💗 Authorisation was previously validated and is still valid.`)
log(` 💗 ❨auto-encrypt❩ Authorisation was previously validated and is still valid.`)
return true
}

Expand All @@ -93,7 +92,7 @@ class Authorisation extends EventEmitter {
if (request.url === `/.well-known/acme-challenge/${this.challenge.token}`) {
// OK, this is the authorisation we’re being pinged for by the Let’s Encrypt servers.
// Respond with the response it expects according to RFC 8555 § 8.1 (Key Authorizations)
log(` 👍 Responding to ACME authorisation request for ${this.domain}`)
log(` 👍 ❨auto-encrypt❩ Responding to ACME authorisation request for ${this.domain}`)

// TODO: We should validate (as much as possible) that this is actually coming from Let’s
// ===== Encrypt’s servers.
Expand Down Expand Up @@ -143,7 +142,7 @@ class Authorisation extends EventEmitter {

this.alreadyPollingForValidationState = true

log(` 🧐 Starting to poll for authorisation state for domain ${this.domain}…`)
log(` 🧐 ❨auto-encrypt❩ Starting to poll for authorisation state for domain ${this.domain}…`)

// Note: while this is an async function, we are not awaiting the result
// ===== here. Our goal is to simply trigger the start of polling. We do
Expand All @@ -153,12 +152,12 @@ class Authorisation extends EventEmitter {

async pollForValidationState () {

log(` 👋 Polling for authorisation state for domain ${this.domain}…`)
log(` 👋 ❨auto-encrypt❩ Polling for authorisation state for domain ${this.domain}…`)

const result = await (new AuthorisationRequest()).execute(this.authorisationUrl)

if (result.body.status === 'valid') {
log(` 🎉 Authorisation validated for domain ${this.domain}`)
log(` 🎉 ❨auto-encrypt❩ Authorisation validated for domain ${this.domain}`)
this.emit(Authorisation.VALIDATED)
return
} else {
Expand All @@ -171,7 +170,7 @@ class Authorisation extends EventEmitter {
retryAfterHeader = parseInt(retryAfterHeader)
}

log(` Authorisation not valid yet for domain ${this.domain}. Waiting to check again in ${pollingDuration/1000} second${pollingDuration === 1000 ? '' : 's'}…`)
log(` ⌚ ❨auto-encrypt❩ Authorisation not valid yet for domain ${this.domain}. Waiting to check again in ${pollingDuration/1000} second${pollingDuration === 1000 ? '' : 's'}…`)

await waitFor(pollingDuration)
await this.pollForValidationState()
Expand Down
33 changes: 16 additions & 17 deletions lib/Certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class Certificate {
this.pem = fs.readFileSync(this.#configuration.certificatePath, 'utf-8')
this.identity = new CertificateIdentity(this.#configuration)

log(' 📃 Certificate exists, loaded it (and the corresponding private key) from disk.')
log(' 📃 ❨auto-encrypt❩ Certificate exists, loaded it (and the corresponding private key) from disk.')
this.startCheckingForRenewal(/* alsoCheckNow = */ true)
} else {
log(' 📃 Certificate does not exist; will be provisioned on first hit of the server.')
log(' 📃 ❨auto-encrypt❩ Certificate does not exist; will be provisioned on first hit of the server.')
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ class Certificate {
this.#_issueDate = moment(details.issuedAt)
this.#_expiryDate = moment(details.expiresAt)

log(` 📆 Certificate set. Serial #: ${details.serialNumber} Issuer: ${details.issuer} Subject: ${details.subject}. Alternative names: ${details.alternativeNames}. Issued ${this.issueDate.calendar().toLowerCase()} (${this.issueDate.fromNow()}) and expires ${this.expiryDate.calendar().toLowerCase()} (${this.expiryDate.fromNow()}).`)
log(` 📆 ❨auto-encrypt❩ Certificate set. Serial #: ${details.serialNumber} Issuer: ${details.issuer} Subject: ${details.subject}. Alternative names: ${details.alternativeNames}. Issued ${this.issueDate.calendar().toLowerCase()} (${this.issueDate.fromNow()}) and expires ${this.expiryDate.calendar().toLowerCase()} (${this.expiryDate.fromNow()}).`)
}

set identity (certificateIdentity) {
Expand Down Expand Up @@ -149,22 +149,22 @@ class Certificate {
const certificatePath = this.#configuration.certificatePath

if (fs.existsSync(oldCertificateIdentityPath) && fs.existsSync(oldCertificatePath)) {
log(' 🚑 [Auto Correct] Warning: Failed renewal attempt detected. Old certificate files found. Attempting to recover…')
log(' 🚑 ❨auto-encrypt❩ Warning: Failed renewal attempt detected. Old certificate files found. Attempting to recover…')
// Edge case: check if the process succeeded (perhaps the power went out right after the certificate was
// written but before we had a chance to clean up the old files.)
if (fs.existsSync(certificateIdentityPath) && fs.existsSync(certificatePath)) {
log(' 🚑 [Auto Correct] A new certificate was also found. Going to delete the old one and use that.')
log(' 🚑 ❨auto-encrypt❩ A new certificate was also found. Going to delete the old one and use that.')
fs.removeSync(oldCertificateIdentityPath)
fs.removeSync(oldCertificatePath)
} else {
// The renewal process must have failed. Delete any previous state and restore the old certificate.
log(' 🚑 [Auto Correct] Cleaning up previous state and restoring old certificate…')
log(' 🚑 ❨auto-encrypt❩ Cleaning up previous state and restoring old certificate…')
fs.removeSync(certificateIdentityPath)
fs.removeSync(certificatePath)
fs.renameSync(oldCertificateIdentityPath, certificateIdentityPath)
fs.renameSync(oldCertificatePath, certificatePath)
}
log(' 🚑 [Auto Correct] Recovery attempt complete.')
log(' 🚑 ❨auto-encrypt❩ Recovery attempt complete.')
}
}

Expand Down Expand Up @@ -222,7 +222,7 @@ class Certificate {
* @returns {Promise} Fulfils once a certificate has been provisioned.
*/
async provisionCertificate () {
log(` 🤖 [Auto Encrypt] Provisioning Let’s Encrypt certificates for ${this.#domains}.`)
log(` 🤖 ❨auto-encrypt❩ Provisioning Let’s Encrypt certificates for ${this.#domains}.`)

// Create a new order.
const order = await Order.getInstanceAsync(this.#configuration, this.#accountIdentity)
Expand All @@ -234,7 +234,7 @@ class Certificate {
// Start checking for renewal updates, every day, starting tomorrow.
this.startCheckingForRenewal(/* alsoCheckNow = */ false)

log(` 🤖🎉 Auto Encrypt: successfully provisioned Let’s Encrypt certificate for ${this.#domains}.`)
log(` 🎉 ❨auto-encrypt❩ successfully provisioned Let’s Encrypt certificate for ${this.#domains}.`)
}

/**
Expand All @@ -252,7 +252,7 @@ class Certificate {
// cache the secureContext so that the server will start using the new certificate right away.
// If it’s not successful, restore the old files.
//
log(` 🤖 [Auto Encrypt] Renewing Let’s Encrypt certificate for ${this.#domains}.`)
log(` 🤖 ❨auto-encrypt❩ Renewing Let’s Encrypt certificate for ${this.#domains}.`)

this.stopCheckingForRenewal()

Expand Down Expand Up @@ -292,19 +292,19 @@ class Certificate {
* has been renewed.
*/
async checkForRenewal () {
log( ' 🧐 [Auto Encrypt] Checking if we need to renew the certificate… ')
log( ' 🧐 ❨auto-encrypt❩ Checking if we need to renew the certificate… ')
const currentDate = moment()
if (currentDate.isSameOrAfter(this.#renewalDate)) {
//
// Certificate needs renewal.
//
log(` 🌱 [Auto Encrypt] Certificate expires in 30 days or less. Renewing certificate…`)
log(` 🌱 ❨auto-encrypt❩ Certificate expires in 30 days or less. Renewing certificate…`)
// Note: this is not a blocking process. We transparently start using the new certificate
// when it is ready.
await this.renewCertificate()
log(` 🌱 [Auto Encrypt] Successfully renewed Let’s Encrypt certificate.`)
log(` 🌱 ❨auto-encrypt❩ Successfully renewed Let’s Encrypt certificate.`)
} else {
log(' 👍 [Auto Encrypt] Certificate has more than 30 days before it expires. Will check again tomorrow.')
log(' 👍 ❨auto-encrypt❩ Certificate has more than 30 days before it expires. Will check again tomorrow.')
}
}

Expand Down Expand Up @@ -333,7 +333,7 @@ class Certificate {
const onceADay = 24 /* hours */ * 60 /* minutes */ * 60 /* seconds */ * 1000 /* ms */
this.#checkForRenewalIntervalId = setInterval(this.checkForRenewal, onceADay)

log(' ⏰ [Auto Encrypt] Set up timer to check for certificate renewal once a day.')
log(' ⏰ ❨auto-encrypt❩ Set up timer to check for certificate renewal once a day.')
}

/**
Expand Down Expand Up @@ -370,7 +370,7 @@ class Certificate {
}

__changeRenewalDate (momentDate) {
log(' Warning: changing renewal date on the certificate instance. I hope you know what you’re doing.')
log(' ⚠ ❨auto-encrypt❩ Warning: changing renewal date on the certificate instance. I hope you know what you’re doing.')
this.#renewalDate = momentDate
}

Expand Down Expand Up @@ -398,5 +398,4 @@ class Certificate {
}
}


module.exports = Certificate
10 changes: 5 additions & 5 deletions lib/ChallengeServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class ChallengeServer {

static async destroySharedInstance () {
if (ChallengeServer.instance === null) {
log(' 🚮 [Auto Correct] Challenge Server was never setup. Nothing to destroy.')
log(' 🚮 ❨auto-encrypt❩ Challenge Server was never setup. Nothing to destroy.')
return
}
log(' 🚮 [Auto Correct] Destroying Challenge Server…')
log(' 🚮 ❨auto-encrypt❩ Destroying Challenge Server…')
await ChallengeServer.instance.destroy()
ChallengeServer.instance = null
log(' 🚮 [Auto Correct] Challenge Server is destroyed.')
log(' 🚮 ❨auto-encrypt❩ Challenge Server is destroyed.')
}

addResponder (responder) {
Expand Down Expand Up @@ -79,7 +79,7 @@ class ChallengeServer {
// If this is not an ACME authorisation request, as nothing else should be using insecure HTTP,
// forward the request to HTTPS.
if (!responded) {
log(` Received non-ACME HTTP request for ${request.url}, not responding.`)
log(` ⚠ ❨auto-encrypt❩ Received non-ACME HTTP request for ${request.url}, not responding.`)
response.statusCode = 403
response.end('403: forbidden')
}
Expand All @@ -100,7 +100,7 @@ class ChallengeServer {
await new Promise((resolve, reject) => {
try {
this.server.listen(80, () => {
log(` 🔒 [Auto Encrypt] HTTP server is listening for challenges`)
log(` 🔒 ❨auto-encrypt❩ HTTP server is listening for challenges`)
resolve()
})
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Configuration {
this.#certificatePath = path.join(this.#certificateDirectoryPath, 'certificate.pem')
this.#certificateIdentityPath = path.join(this.#certificateDirectoryPath, 'certificate-identity.pem')

log(' ⚙️ [Auto Encrypt] Configuration initialised.')
log(' ⚙️ ❨auto-encrypt❩ Configuration initialised.')
}

//
Expand Down
2 changes: 1 addition & 1 deletion lib/Directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Directory {
this.#letsEncryptServer = configuration.server
this.#directoryRequest = prepareRequest('GET', 'json', this.#letsEncryptServer.endpoint)

log(` 📕 [Auto Encrypt] Directory is using endpoint ${this.#letsEncryptServer.endpoint}`)
log(` 📕 ❨auto-encrypt❩ Directory is using endpoint ${this.#letsEncryptServer.endpoint}`)
}

// (Async) Fetches the latest Urls from the Let’s Encrypt ACME endpoint being used.
Expand Down
2 changes: 1 addition & 1 deletion lib/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Identity {
throws.error(Symbol.for('UnsupportedIdentityType'))
}

log(` 👤 Creating identity (${identityFilePath})`)
log(` 👤 ❨auto-encrypt❩ Creating identity (${identityFilePath})`)

this.#identityFilePath = identityFilePath

Expand Down
4 changes: 2 additions & 2 deletions lib/Nonce.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class Nonce {

set (freshNonce) {
if (freshNonce === undefined || freshNonce === null) {
log(' ⚠ [Auto Encrypt] nonce.set called with undefined/null. Not saving nonce. No effect on functionality. ')
log(' ⚠ ❨auto-encrypt❩ nonce.set called with undefined/null. Not saving nonce. No effect on functionality. ')
return
}

if (freshNonce.match(/^[A-Za-z0-9_-]+$/) === null) {
log (' ⚠ [Auto Encrypt] nonce.set with non-Base64-encoded nonce. Not saving nonce. No effect on functionality.')
log (' ⚠ ❨auto-encrypt❩ nonce.set with non-Base64-encoded nonce. Not saving nonce. No effect on functionality.')
return
}

Expand Down
Loading

0 comments on commit 31611b2

Please sign in to comment.