backupVerifier.test.mjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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: process.env,
  42. }
  43. )
  44. return { status: 0, stdout: result.stdout }
  45. } catch (err) {
  46. if (err && typeof err === 'object' && 'stdout' in err && 'code' in err) {
  47. return {
  48. stdout: typeof err.stdout === 'string' ? err.stdout : '',
  49. status: typeof err.code === 'number' ? err.code : -1,
  50. }
  51. }
  52. throw err
  53. }
  54. }
  55. /**
  56. * @param {string} historyId
  57. * @param {string} hash
  58. * @return {Promise<Response>}
  59. */
  60. async function verifyBlobHTTP(historyId, hash) {
  61. return await fetch(
  62. testServer.testUrl(`/history/${historyId}/blob/${hash}/verify`),
  63. { method: 'GET' }
  64. )
  65. }
  66. /**
  67. * @param {string} historyId
  68. * @return {Promise<string>}
  69. */
  70. async function prepareProjectAndBlob(historyId) {
  71. await testProjects.createEmptyProject(historyId)
  72. const blobStore = new BlobStore(historyId)
  73. const blob = await blobStore.putString(historyId)
  74. const gzipped = zlib.gzipSync(Buffer.from(historyId))
  75. await backupPersistor.sendStream(
  76. projectBlobsBucket,
  77. makeProjectKey(historyId, blob.getHash()),
  78. Stream.Readable.from([gzipped]),
  79. { contentLength: gzipped.byteLength, contentEncoding: 'gzip' }
  80. )
  81. await checkDEKExists(historyId)
  82. return blob.getHash()
  83. }
  84. /**
  85. * @param {string} historyId
  86. * @return {Promise<void>}
  87. */
  88. async function checkDEKExists(historyId) {
  89. await backupPersistor.forProjectRO(
  90. projectBlobsBucket,
  91. makeProjectKey(historyId, '')
  92. )
  93. }
  94. describe('backupVerifier', function () {
  95. const historyIdPostgres = '42'
  96. const historyIdMongo = '000000000000000000000042'
  97. let blobHashPG, blobHashMongo, blobPathPG
  98. beforeEach(cleanup.everything)
  99. beforeEach('create health check projects', async function () {
  100. ;[blobHashPG, blobHashMongo] = await Promise.all([
  101. prepareProjectAndBlob('42'),
  102. prepareProjectAndBlob('000000000000000000000042'),
  103. ])
  104. blobPathPG = makeProjectKey(historyIdPostgres, blobHashPG)
  105. })
  106. beforeEach(testServer.listenOnRandomPort)
  107. it('renders 200 on /status', async function () {
  108. const response = await fetch(testServer.testUrl('/status'))
  109. expect(response.status).to.equal(200)
  110. })
  111. it('renders 200 on /health_check', async function () {
  112. const response = await fetch(testServer.testUrl('/health_check'))
  113. expect(response.status).to.equal(200)
  114. })
  115. describe('storage/scripts/verify_backup_blob.mjs', function () {
  116. it('throws and does not create DEK if missing', async function () {
  117. const historyId = '404'
  118. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  119. const response = await verifyBlobScript(historyId, hash)
  120. expect(response.status).to.equal(1)
  121. expect(response.stdout).to.include('dek does not exist')
  122. await expect(checkDEKExists(historyId)).to.be.rejectedWith(NotFoundError)
  123. })
  124. it('throws when deleted in db', async function () {
  125. const blobStore = new BlobStore(historyIdPostgres)
  126. await blobStore.deleteBlobs()
  127. const response = await verifyBlobScript(historyIdPostgres, blobHashPG)
  128. expect(response.status).to.equal(1)
  129. expect(response.stdout).to.include(`blob ${blobHashPG} not found`)
  130. })
  131. it('throws when not existing', async function () {
  132. await backupPersistor.deleteObject(projectBlobsBucket, blobPathPG)
  133. const result = await verifyBlobScript(historyIdPostgres, blobHashPG)
  134. expect(result.status).to.equal(1)
  135. expect(result.stdout).to.include('missing blob')
  136. })
  137. it('throws when corrupted', async function () {
  138. await backupPersistor.sendStream(
  139. projectBlobsBucket,
  140. blobPathPG,
  141. Stream.Readable.from(['something else']),
  142. { contentLength: 14 }
  143. )
  144. const result = await verifyBlobScript(historyIdPostgres, blobHashPG)
  145. expect(result.status).to.equal(1)
  146. expect(result.stdout).to.include('hash mismatch for backed up blob')
  147. })
  148. it('should successfully verify from postgres', async function () {
  149. const result = await verifyBlobScript(historyIdPostgres, blobHashPG)
  150. expect(result.status).to.equal(0)
  151. expect(result.stdout.split('\n')).to.include('OK')
  152. })
  153. it('should successfully verify from mongo', async function () {
  154. const result = await verifyBlobScript(historyIdMongo, blobHashMongo)
  155. expect(result.status).to.equal(0)
  156. expect(result.stdout.split('\n')).to.include('OK')
  157. })
  158. })
  159. describe('GET /history/:historyId/blob/:hash/verify', function () {
  160. it('returns 404 when deleted in db', async function () {
  161. const blobStore = new BlobStore(historyIdPostgres)
  162. await blobStore.deleteBlobs()
  163. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  164. expect(response.status).to.equal(404)
  165. expect(await response.text()).to.equal(`blob ${blobHashPG} not found`)
  166. })
  167. it('returns 422 and does not create DEK if missing', async function () {
  168. const historyId = '404'
  169. const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  170. const response = await verifyBlobHTTP(historyId, hash)
  171. expect(response.status).to.equal(422)
  172. expect(await response.text()).to.equal('dek does not exist')
  173. await expect(checkDEKExists(historyId)).to.be.rejectedWith(NotFoundError)
  174. })
  175. it('returns 422 when not existing', async function () {
  176. await backupPersistor.deleteObject(projectBlobsBucket, blobPathPG)
  177. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  178. expect(response.status).to.equal(422)
  179. expect(await response.text()).to.equal('missing blob')
  180. })
  181. it('returns 422 when corrupted', async function () {
  182. await backupPersistor.sendStream(
  183. projectBlobsBucket,
  184. blobPathPG,
  185. Stream.Readable.from(['something else']),
  186. { contentLength: 14 }
  187. )
  188. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  189. expect(response.status).to.equal(422)
  190. expect(await response.text()).to.equal('hash mismatch for backed up blob')
  191. })
  192. it('should successfully verify from postgres', async function () {
  193. const response = await verifyBlobHTTP(historyIdPostgres, blobHashPG)
  194. expect(response.status).to.equal(200)
  195. })
  196. it('should successfully verify from mongo', async function () {
  197. const response = await verifyBlobHTTP(historyIdMongo, blobHashMongo)
  198. expect(response.status).to.equal(200)
  199. })
  200. })
  201. })