check-certs.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 } = require('../../app/src/infrastructure/mongojs')
  15. const moment = require('moment')
  16. main()
  17. .catch(err => {
  18. console.error(err.stack)
  19. })
  20. .then(() => process.exit())
  21. async function main() {
  22. const [, , file] = process.argv
  23. console.log(`loading file ${file}`)
  24. const ukamfDB = new UKAMFDB(file)
  25. await ukamfDB.init()
  26. const activeProviderIds = await getActiveProviderIds()
  27. for (const providerId of activeProviderIds) {
  28. await checkCert(ukamfDB, providerId)
  29. }
  30. }
  31. async function checkCert(ukamfDB, providerId) {
  32. console.log(`Checking certificates for providerId: ${providerId}`)
  33. try {
  34. const { body } = await V1Api.request({
  35. json: true,
  36. qs: { university_id: providerId },
  37. uri: '/api/v1/sharelatex/university_saml'
  38. })
  39. // show notice if sso not currently enabled
  40. if (body.sso_enabled === true) {
  41. console.log(` * SSO enabled`)
  42. } else {
  43. console.log(` ! SSO NOT enabled`)
  44. }
  45. // lookup entity id in ukamf database
  46. const entity = ukamfDB.findByEntityID(body.sso_entity_id)
  47. // if entity found then compare certs
  48. if (entity) {
  49. const samlConfig = entity.getSamlConfig()
  50. // check if certificates match
  51. if (samlConfig.cert === body.sso_cert) {
  52. console.log(' * UKAMF certificate matches configuration')
  53. } else {
  54. console.log(' ! UKAMF certificate DOES NOT match configuration')
  55. }
  56. } else {
  57. console.log(` ! No UKAMF entity found for ${body.sso_entity_id}`)
  58. }
  59. // check expiration on configured certificate
  60. const certificate = Certificate.fromPEM(
  61. Buffer.from(
  62. `-----BEGIN CERTIFICATE-----\n${
  63. body.sso_cert
  64. }\n-----END CERTIFICATE-----`,
  65. 'utf8'
  66. )
  67. )
  68. const validFrom = moment(certificate.validFrom)
  69. const validTo = moment(certificate.validTo)
  70. if (validFrom.isAfter(moment())) {
  71. console.log(` ! Certificate not valid till: ${validFrom.format('LLL')}`)
  72. } else if (validTo.isBefore(moment())) {
  73. console.log(` ! Certificate expired: ${validTo.format('LLL')}`)
  74. } else if (validTo.isBefore(moment().add(60, 'days'))) {
  75. console.log(` ! Certificate expires: ${validTo.format('LLL')}`)
  76. } else {
  77. console.log(` * Certificate expires: ${validTo.format('LLL')}`)
  78. }
  79. } catch (err) {
  80. console.log(` ! ${err.statusCode} Error getting university config from v1`)
  81. }
  82. }
  83. async function getActiveProviderIds() {
  84. return new Promise((resolve, reject) => {
  85. db.users.distinct(
  86. 'samlIdentifiers.providerId',
  87. { 'samlIdentifiers.externalUserId': { $exists: true } },
  88. (err, providerIds) => {
  89. if (err) {
  90. reject(err)
  91. } else {
  92. resolve(providerIds)
  93. }
  94. }
  95. )
  96. })
  97. }