Skip to content

Instantly share code, notes, and snippets.

@hiroy
Created August 29, 2019 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroy/3e2ac8b4861035b9f3662f1e3fe98d62 to your computer and use it in GitHub Desktop.
Save hiroy/3e2ac8b4861035b9f3662f1e3fe98d62 to your computer and use it in GitHub Desktop.
SSL/TLSの証明書の有効期間をSlackに通知するツール
'use strict';
const { IncomingWebhook } = require('@slack/client');
const https = require('https');
const moment = require('moment');
async function sendSlackNotification(message) {
const url = process.env.SLACK_INCOMING_WEBHOOK_URL;
const slackIncomingWebhook = new IncomingWebhook(url);
return await slackIncomingWebhook.send({
text: message,
});
}
async function getCertificateRemainingInDays(hostname, port) {
return new Promise((resolve, reject) => {
const req = https.request({
hostname: hostname,
port: port || 443,
}, (res) => {
const certificate = res.connection.getPeerCertificate();
const expirationDate = moment(certificate.valid_to, 'MMM D HH:mm:ss YYYY GMT');
const remainingInDays = expirationDate.diff(moment(), 'days');
resolve(remainingInDays);
}).on('error', (err) => {
console.error(err.stack);
reject(err);
});
req.end();
});
}
(async () => {
const targets = [
{ hostname: 'iruca.co', port: 443 },
{ hostname: 'mimemo.io', port: 443 },
];
let message = '[CERTIFICATE INFORMATION]\n';
for (const target of targets) {
let remainingInDays = await getCertificateRemainingInDays(target.hostname, target.port);
remainingInDays += (remainingInDays > 1) ? ' days' : ' day';
message += `${target.hostname}: remaining ${remainingInDays}\n`;
}
await sendSlackNotification(message);
})();
{
"name": "check-certificate-expiration",
"version": "1.0.0",
"dependencies": {
"@slack/client": "^5.0.2",
"moment": "^2.24.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment