|
| 1 | +/** |
| 2 | + * Check when SSO certs expire |
| 3 | + */ |
| 4 | +const crypto = require('crypto') |
| 5 | + |
| 6 | +const TWO_WEEKS = (60 * 60 * 24 * 14 * 1000) |
| 7 | + |
| 8 | +module.exports = { |
| 9 | + name: 'checkSAMLCertificateExpiry', |
| 10 | + startup: false, |
| 11 | + delay: 5000, |
| 12 | + schedule: '@weekly', |
| 13 | + run: async function (app) { |
| 14 | + app.log.info('Checking SSO Cert life') |
| 15 | + try { |
| 16 | + const ssoConfigs = await app.db.models.SAMLProvider.getAll() |
| 17 | + for (const sso of ssoConfigs.providers) { |
| 18 | + if (sso.active && sso.type === 'saml') { |
| 19 | + if (sso.options.cert) { |
| 20 | + try { |
| 21 | + let pem = sso.options.cert |
| 22 | + if (!pem.startsWith('-----BEGIN CERTIFICATE-----\n')) { |
| 23 | + pem = `-----BEGIN CERTIFICATE-----\n${pem}\n-----END CERTIFICATE-----\n` |
| 24 | + } |
| 25 | + const cert = new crypto.X509Certificate(pem) |
| 26 | + const expiry = Date.parse(cert.validTo) |
| 27 | + const life = expiry - Date.now() |
| 28 | + if (life < TWO_WEEKS) { |
| 29 | + app.log.info(`SSO Certificate expires soon ${sso.name} ${cert.validTo}`) |
| 30 | + await emailAdmins(app, 'SSOCertsExpiring', { name: sso.name, date: cert.validTo }) |
| 31 | + } |
| 32 | + } catch (err) { |
| 33 | + app.log.debug(`Problem checking ${sso.name}'s SSO certificate ${err.toString()}`) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } catch (err) { |
| 39 | + app.log.debug(`Problem checking SSO certificate ${err.toString()}`) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function emailAdmins (app, template, context) { |
| 45 | + const admins = await app.db.models.User.admins() |
| 46 | + for (const admin of admins) { |
| 47 | + await app.postoffice.send(admin, template, context) |
| 48 | + } |
| 49 | +} |
0 commit comments