backupVerifier.test.mjs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // @ts-check
  2. import cleanup from '../storage/support/cleanup.js'
  3. import fetch from 'node-fetch'
  4. import testServer from './support/test_backup_verifier_server.mjs'
  5. import { expect } from 'chai'
  6. import testProjects from './support/test_projects.js'
  7. import {
  8. backupPersistor,
  9. projectBlobsBucket,
  10. } from '../../../../storage/lib/backupPersistor.mjs'
  11. import {
  12. BlobStore,
  13. makeProjectKey,
  14. } from '../../../../storage/lib/blob_store/index.js'
  15. import Stream from 'stream'
  16. import * as zlib from 'node:zlib'
  17. import { promisify } from 'node:util'
  18. import { execFile } from 'node:child_process'
  19. import { NotFoundError } from '@overleaf/object-persistor/src/Errors.js'
  20. /**
  21. * @typedef {import("node-fetch").Response} Response
  22. * @typedef {import("overleaf-editor-core").Blob} Blob
  23. */
  24. /**
  25. * @param {string} historyId
  26. * @param {string} hash
  27. * @return {Promise<{stdout: string, status:number }>}
  28. */
  29. async function verifyBlobScript(historyId, hash) {
  30. try {
  31. const result = await promisify(execFile)(
  32. process.argv0,
  33. [
  34. 'storage/scripts/verify_backup_blob.mjs',
  35. `--historyId=${historyId}`,
  36. hash,
  37. ],
  38. {
  39. encoding: 'utf-8',
  40. timeout: 5_000,
  41. env: {
  42. ...process.env,
  43. LOG_LEVEL: 'warn',
  44. },
  45. }
  46. )
  47. return { status: 0, stdout: result.stdout }
  48. } catch (err) {
  49. if (err && typeof err === 'object' && 'stdout' in err && 'code' in err) {
  50. return {
  51. stdout: typeof err.stdout === 'string' ? err.stdout : '',
  52. status: typeof err.code === 'number' ? err.code : -1,
  53. }
  54. }
  55. throw err
  56. }
  57. }
  58. /**
  59. * @param {string} historyId
  60. * @param {string} hash
  61. * @return {Promise<Response>}
  62. */
  63. async function verifyBlobHTTP(historyId, hash) {
  64. return await fetch(
  65. testServer.testUrl(`/history/${historyId}/blob/${hash}/verify`),
  66. { method: 'GET' }
  67. )
  68. }
  69. /**
  70. * @param {string} historyId
  71. * @return {Promise<string>}
  72. */
  73. async function prepareProjectAndBlob(historyId) {
  74. await testProjects.createEmptyProject(historyId)
  75. const blobStore = new BlobStore(historyId)
  76. const blob = await blobStore.putString(historyId)
  77. const gzipped = zlib.gzipSync(Buffer.from(historyId))
  78. await backupPersistor.sendStream(
  79. projectBlobsBucket,
  80. makeProjectKey(historyId, blob.getHash()),
  81. Stream.Readable.from([gzipped]),
  82. { contentLength: gzipped.byteLength, contentEncoding: 'gzip' }
  83. )
  84. await checkDEKExists(historyId)
  85. return blob.getHash()
  86. }
  87. /**
  88. * @param {string} historyId
  89. * @return {Promise<void>}
  90. */
  91. async function checkDEKExists(historyId) {
  92. await backupPersistor.forProjectRO(
  93. projectBlobsBucket,
  94. makeProjectKey(historyId, '')
  95. )
  96. }
  97. describe('backupVerifier', function () {
  98. const historyIdPostgres = '42'
  99. const historyIdMongo = '000000000000000000000042'
  100. let blobHashPG, blobHashMongo, blobPathPG
  101. beforeEach(cleanup.everything)
  102. beforeEach('create health check projects', async function () {
  103. ;[blobHashPG, blobHashMongo] = await Promise.all([
  104. prepareProjectAndBlob('42'),
  105. prepareProjectAndBlob('000000000000000000000042'),
  106. ])
  107. blobPathPG = makeProjectKey(historyIdPostgres, blobHashPG)
  108. })
  109. beforeEach(testServer.listenOnRandomPort)
  110. it('renders 200 on /status', async function () {
  111. const response = await fetch(testServer.testUrl('/status'))
  112. expect(response.status).to.equal(200)
  113. })
  114. it('renders 200 on /health_check', async function () {
  115. const response = await fetch(testServer.testUrl('/health_check'))
  116. expect(response.status).to.equal(200)
  117. })
  118. describe('storage/scripts/verify_backup_blob.mjs', function () {
  119. it('throws and does not create DEK if missing', async function () {
  120. const historyId = '404'
  121. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  122. const response = await verifyBlobScript(historyId, hash)
  123. expect(response.status).to.equal(1)
  124. expect(response.stdout).to.include('dek does not exist')
  125. await expect(checkDEKExists(historyId)).to.be.rejectedWith(NotFoundError)
  126. })
  127. it('throws when deleted in db', async function () {
  128. const blobStore = new BlobStore(historyIdPostgres)
  129. await blobStore.deleteBlobs()
  130. const response = await verifyBlobScript(historyIdPostgres, blobHashPG)
  131. expect(response.status).to.equal(1)
  132. expect(response.stdout).to.include(`blob ${blobHashPG} not found`)
  133. })
  134. it('throws when not existing', async function () {
  135. await backupPersistor.deleteObject(projectBlobsBucket, blobPathPG)
  136. const result = await verifyBlobScript(historyIdPostgres, blobHashPG)
  137. expect(result.status).to.equal(1)
  138. expect(result.stdout).to.include('missing blob')
  139. })
  140. it('throws when corrupted', async function () {
  141. await backupPersistor.sendStream(
  142. projectBlobsBucket,
  143. blobPathPG,
  144. Stream.Readable.from(['something else']),
  145. { contentLength: 14 }
  146. )
  147. const result = await verifyBlobScript(historyIdPostgres, blobHashPG)
  148. expect(result.status).to.equal(1)
  149. expect(result.stdout).to.include('hash mismatch for backed up blob')
  150. })
  151. it('should successfully verify from postgres', async function () {
  152. const result = await verifyBlobScript(historyIdPostgres, blobHashPG)
  153. expect(result.status).to.equal(0)
  154. expect(result.stdout.split('\n')).to.include('OK')
  155. })
  156. it('should successfully verify from mongo', async function () {
  157. const result = await verifyBlobScript(historyIdMongo, blobHashMongo)
  158. expect(result.status).to.equal(0)
  159. expect(result.stdout.split('\n')).to.include('OK')
  160. })
  161. })
  162. describe('GET /history/:historyId/blob/:hash/verify', function () {
  163. it('returns 404 when deleted in db', async function () {
  164. const blobStore = new BlobStore(historyIdPostgres)
  165. await blobStore.deleteBlobs()
  166. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  167. expect(response.status).to.equal(404)
  168. expect(await response.text()).to.equal(`blob ${blobHashPG} not found`)
  169. })
  170. it('returns 422 and does not create DEK if missing', async function () {
  171. const historyId = '404'
  172. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  173. const response = await verifyBlobHTTP(historyId, hash)
  174. expect(response.status).to.equal(422)
  175. expect(await response.text()).to.equal('dek does not exist')
  176. await expect(checkDEKExists(historyId)).to.be.rejectedWith(NotFoundError)
  177. })
  178. it('returns 422 when not existing', async function () {
  179. await backupPersistor.deleteObject(projectBlobsBucket, blobPathPG)
  180. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  181. expect(response.status).to.equal(422)
  182. expect(await response.text()).to.equal('missing blob')
  183. })
  184. it('returns 422 when corrupted', async function () {
  185. await backupPersistor.sendStream(
  186. projectBlobsBucket,
  187. blobPathPG,
  188. Stream.Readable.from(['something else']),
  189. { contentLength: 14 }
  190. )
  191. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  192. expect(response.status).to.equal(422)
  193. expect(await response.text()).to.equal('hash mismatch for backed up blob')
  194. })
  195. it('should successfully verify from postgres', async function () {
  196. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  197. expect(response.status).to.equal(200)
  198. })
  199. it('should successfully verify from mongo', async function () {
  200. const response = await verifyBlobHTTP(historyIdMongo, blobHashMongo)
  201. expect(response.status).to.equal(200)
  202. })
  203. })
  204. })