Skip to content
This repository was archived by the owner on Aug 21, 2019. It is now read-only.

check if any certificate needs to be renewed #38

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

leipert
Copy link

@leipert leipert commented Jan 15, 2018

@travismiller was faster on automating the pages workflow, thank you! 🎉

On my local branch, I added a quick check, if certificate renewal is even needed at all.
High level workflow is like this

Only get a new certificate, if at least one of the pages given in the arguments:

  1. is not registered yet
  2. does not have a certificate
  3. has an expired certificate
  4. has an certificate expiring within X days (default 15)
$ ./index.js --domain xyz.leipert.io --domain leipert.io \
    --email [email protected] \
    --repository https://gitlab.com/leipert/leipert.gitlab.io \
    --path /public/.well-known/acme-challenge
All domains xyz.leipert.io, leipert.io have a valid certificate (expiration in more than 15 days)

@tmaier
Copy link

tmaier commented Jan 16, 2018

Great idea. Consider to increase the default to 31 days, as this fits better to the Pipeline Scheduling feature supported if #36 gets merged.

The updated README suggests to run the pipeline once per month. With a default of 15, the user would miss the renewal date.

@rolodato
Copy link
Owner

Thanks for the PR! I would make the following changes for consistency with certbot renew:

  • Default expiration should be 30 days
  • No option to configure a default expiration
  • Add a --force-renewal option that bypasses the expiration check and renews anyway

@tmaier
Copy link

tmaier commented Jan 16, 2018

The reasons why I proposed 31 instead of 30 are

  • some months have 31 days (with 30, they would not request a new certificate)
  • you could come into a race condition where the previous build and validation step took longer than the current one (can be just a few minutes). In this case the the delta would be higher than 30 days and the renewal would be skipped

@leipert
Copy link
Author

leipert commented Jan 17, 2018

I have to say, that I agree with @rolodato on that one:

  1. Consistency with certbot is a good idea.
  2. if you run gitlab-le with a cronjob each day, it doesn't matter whether it's 30 or 31 days.

@leipert
Copy link
Author

leipert commented Jan 17, 2018

I updated the behavior as suggested. Additionally I made sure that the exit code is 0 if all certificates are valid

@leipert leipert mentioned this pull request Jan 17, 2018
@leipert leipert force-pushed the feature/checkIfCertificatesExpiring branch from 6cde205 to 1be80c8 Compare January 17, 2018 10:16
@leipert
Copy link
Author

leipert commented Jan 22, 2018

/ping @rolodato

@@ -26,6 +26,10 @@ module.exports = yargs
describe: 'Upload challenge files with a Jekyll-compatible YAML front matter (see https://jekyllrb.com/docs/frontmatter)',
type: 'boolean',
default: false
}).option('force-renewal', {
describe: 'Force renewal of certificate, even if it expires in more than 30 days',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be phrased with plural instead:

Force renewal of certificates, even if they expire in more than 30 days.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues with that:

  1. --production also is in singular:

    Obtain a real certificate instead of a dummy one

  2. Technically you only obtain one certificate per execution, right?

Should I still change the text?

package.json Outdated
@@ -9,6 +9,7 @@
"dependencies": {
"bluebird": "3.5.0",
"le-acme-core": "https://git.daplie.com/rolodato/le-acme-core/repository/archive.tar.gz",
"moment": "^2.20.1",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to introduce this dependency, we can do a little date arithmetic instead :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the dependency

lib.js Outdated
const path = require('path');
const { URL } = require('url');

const EXPIRATION_IN_DAYS = 30;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be repliaced by const EXPIRATION = ms('30 days') if we get rid of the moment dependency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the dependency

lib.js Outdated

if (needsNoRenewal) {
console.log(`All domains (${options.domain.join(', ')}) have a valid certificate (expiration in more than ${EXPIRATION_IN_DAYS} days)`);
process.exit(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try to avoid this process.exit here - this file should only be used as a library for the main program (main.js). I think the better approach would be to refactor this file so the exported function (getCertificates) resolves successfully but with an empty array as a result, which means that everything went OK but no new certificates needed to be generated.

lib.js Outdated
@@ -99,8 +102,27 @@ module.exports = (options) => {
});
};

const hasValidCertificate = (pagesDomain) => {
if (options.domain.includes(pagesDomain.domain)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should not care about option.domain, it should only care about the domain passed as a parameter. If we need to filter other domains it should be done at the call site instead of here.

lib.js Outdated
@@ -99,8 +102,27 @@ module.exports = (options) => {
});
};

const hasValidCertificate = (pagesDomain) => {
if (options.domain.includes(pagesDomain.domain)) {
if (pagesDomain.certificate) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid nesting these ifs so deeply? :)

@leipert leipert force-pushed the feature/checkIfCertificatesExpiring branch from c538f91 to b7cf54c Compare January 30, 2018 21:45
@leipert
Copy link
Author

leipert commented Jan 30, 2018

@rolodato, I fixed all the issues you described in your review (except the singular/plural one).
For the process.exit change, I had to refactor a bit:

I moved registering of ACME account, certificate retrieval and certificate upload into a new function runACMEWorkflow.

@leipert
Copy link
Author

leipert commented Feb 14, 2018

Hey @rolodato,

I thought, I'd ping again ;)

@leipert
Copy link
Author

leipert commented Apr 14, 2018

@rolodato Ping ping :P

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants