Created
August 29, 2019 13:55
-
-
Save hiroy/3e2ac8b4861035b9f3662f1e3fe98d62 to your computer and use it in GitHub Desktop.
SSL/TLSの証明書の有効期間をSlackに通知するツール
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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