check-certs.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict'
  2. /**
  3. * Checks that all institutional sso provider certs are still current with the
  4. * data provided by the ukamf export file.
  5. *
  6. * Run with: node check-certs /path/ukamf.xml
  7. *
  8. * The ukamf metadata xml file can be downloaded from:
  9. * http://metadata.ukfederation.org.uk/
  10. */
  11. const { Certificate } = require('@fidm/x509')
  12. const UKAMFDB = require('./ukamf-db')
  13. const V1Api = require(`../../app/src/Features/V1/V1Api`).promises
  14. const { db, waitForDb } = require('../../app/src/infrastructure/mongodb')
  15. const moment = require('moment')
  16. waitForDb()
  17. .then(main)
  18. .catch(err => {
  19. console.error(err.stack)
  20. })
  21. .then(() => process.exit())
  22. async function main() {
  23. const [, , file] = process.argv
  24. console.log(`loading file ${file}`)
  25. const ukamfDB = new UKAMFDB(file)
  26. await ukamfDB.init()
  27. const activeProviderIds = await getActiveProviderIds()
  28. for (const providerId of activeProviderIds) {
  29. await checkCert(ukamfDB, providerId)
  30. }
  31. }
  32. async function checkCert(ukamfDB, providerId) {
  33. console.log(`Checking certificates for providerId: ${providerId}`)
  34. try {
  35. const { body } = await V1Api.request({
  36. json: true,
  37. qs: { university_id: providerId },
  38. uri: '/api/v1/sharelatex/university_saml'
  39. })
  40. // show notice if sso not currently enabled
  41. if (body.sso_enabled === true) {
  42. console.log(` * SSO enabled`)
  43. } else {
  44. console.log(` ! SSO NOT enabled`)
  45. }
  46. // lookup entity id in ukamf database
  47. const entity = ukamfDB.findByEntityID(body.sso_entity_id)
  48. // if entity found then compare certs
  49. if (entity) {
  50. const samlConfig = entity.getSamlConfig()
  51. // check if certificates match
  52. if (samlConfig.cert === body.sso_cert) {
  53. console.log(' * UKAMF certificate matches configuration')
  54. } else {
  55. console.log(' ! UKAMF certificate DOES NOT match configuration')
  56. }
  57. } else {
  58. console.log(` ! No UKAMF entity found for ${body.sso_entity_id}`)
  59. }
  60. // check expiration on configured certificate
  61. const certificate = Certificate.fromPEM(
  62. Buffer.from(
  63. `-----BEGIN CERTIFICATE-----\n${
  64. body.sso_cert
  65. }\n-----END CERTIFICATE-----`,
  66. 'utf8'
  67. )
  68. )
  69. const validFrom = moment(certificate.validFrom)
  70. const validTo = moment(certificate.validTo)
  71. if (validFrom.isAfter(moment())) {
  72. console.log(` ! Certificate not valid till: ${validFrom.format('LLL')}`)
  73. } else if (validTo.isBefore(moment())) {
  74. console.log(` ! Certificate expired: ${validTo.format('LLL')}`)
  75. } else if (validTo.isBefore(moment().add(60, 'days'))) {
  76. console.log(` ! Certificate expires: ${validTo.format('LLL')}`)
  77. } else {
  78. console.log(` * Certificate expires: ${validTo.format('LLL')}`)
  79. }
  80. } catch (err) {
  81. console.log(` ! ${err.statusCode} Error getting university config from v1`)
  82. }
  83. }
  84. async function getActiveProviderIds() {
  85. return db.users.distinct('samlIdentifiers.providerId', {
  86. 'samlIdentifiers.externalUserId': { $exists: true }
  87. })
  88. }