count_encrypted_access_tokens.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const TEN_MINUTES = 1000 * 60 * 10
  2. process.env.MONGO_SOCKET_TIMEOUT =
  3. process.env.MONGO_SOCKET_TIMEOUT || TEN_MINUTES.toString()
  4. const { ReadPreference } = require('mongodb')
  5. const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
  6. const _ = require('lodash')
  7. const {
  8. formatTokenUsageStats,
  9. } = require('@overleaf/access-token-encryptor/scripts/helpers/format-usage-stats')
  10. const CASES = {
  11. users: {
  12. dropbox: 'dropbox.access_token_oauth2.encrypted',
  13. zotero: 'refProviders.zotero.encrypted',
  14. mendeley: 'refProviders.mendeley.encrypted',
  15. },
  16. githubSyncUserCredentials: {
  17. github: 'auth_token_encrypted',
  18. },
  19. }
  20. async function count(collectionName, paths) {
  21. const collection = db[collectionName]
  22. const stats = {}
  23. const projection = { _id: 0 }
  24. for (const path of Object.values(paths)) {
  25. projection[path] = 1
  26. }
  27. const cursor = collection.find(
  28. {},
  29. {
  30. readPreference: ReadPreference.SECONDARY,
  31. projection,
  32. }
  33. )
  34. for await (const doc of cursor) {
  35. for (const [name, path] of Object.entries(paths)) {
  36. const blob = _.get(doc, path)
  37. if (!blob) continue
  38. // Schema: LABEL:SALT:CIPHERTEXT:IV
  39. const [label, , , iv] = blob.split(':', 4)
  40. const version = iv ? 'v2' : 'v1'
  41. const key = [name, version, collectionName, path, label].join(':')
  42. stats[key] = (stats[key] || 0) + 1
  43. }
  44. }
  45. return stats
  46. }
  47. async function main() {
  48. await waitForDb()
  49. const STATS = {}
  50. for (const [collectionName, paths] of Object.entries(CASES)) {
  51. const stats = await count(collectionName, paths)
  52. Object.assign(STATS, stats)
  53. }
  54. formatTokenUsageStats(STATS)
  55. }
  56. main()
  57. .then(() => {
  58. process.exit(0)
  59. })
  60. .catch(err => {
  61. console.error(err)
  62. process.exit(1)
  63. })