check-certs.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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${body.sso_cert}\n-----END CERTIFICATE-----`,
  64. 'utf8'
  65. )
  66. )
  67. const validFrom = moment(certificate.validFrom)
  68. const validTo = moment(certificate.validTo)
  69. if (validFrom.isAfter(moment())) {
  70. console.log(` ! Certificate not valid till: ${validFrom.format('LLL')}`)
  71. } else if (validTo.isBefore(moment())) {
  72. console.log(` ! Certificate expired: ${validTo.format('LLL')}`)
  73. } else if (validTo.isBefore(moment().add(60, 'days'))) {
  74. console.log(` ! Certificate expires: ${validTo.format('LLL')}`)
  75. } else {
  76. console.log(` * Certificate expires: ${validTo.format('LLL')}`)
  77. }
  78. } catch (err) {
  79. console.log(` ! ${err.statusCode} Error getting university config from v1`)
  80. }
  81. }
  82. async function getActiveProviderIds() {
  83. return db.users.distinct('samlIdentifiers.providerId', {
  84. 'samlIdentifiers.externalUserId': { $exists: true },
  85. })
  86. }