DocArchiveManager.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-useless-escape,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let DocArchive
  15. const MongoManager = require('./MongoManager')
  16. const Errors = require('./Errors')
  17. const logger = require('logger-sharelatex')
  18. const _ = require('underscore')
  19. const async = require('async')
  20. const settings = require('settings-sharelatex')
  21. const request = require('request')
  22. const crypto = require('crypto')
  23. const RangeManager = require('./RangeManager')
  24. const thirtySeconds = 30 * 1000
  25. module.exports = DocArchive = {
  26. archiveAllDocs(project_id, callback) {
  27. if (callback == null) {
  28. callback = function(err, docs) {}
  29. }
  30. return MongoManager.getProjectsDocs(
  31. project_id,
  32. { include_deleted: true },
  33. { lines: true, ranges: true, rev: true, inS3: true },
  34. function(err, docs) {
  35. if (err != null) {
  36. return callback(err)
  37. } else if (docs == null) {
  38. return callback(
  39. new Errors.NotFoundError(`No docs for project ${project_id}`)
  40. )
  41. }
  42. docs = _.filter(docs, doc => doc.inS3 !== true)
  43. const jobs = _.map(docs, doc => cb =>
  44. DocArchive.archiveDoc(project_id, doc, cb)
  45. )
  46. return async.parallelLimit(jobs, 5, callback)
  47. }
  48. )
  49. },
  50. archiveDoc(project_id, doc, callback) {
  51. let options
  52. logger.log({ project_id, doc_id: doc._id }, 'sending doc to s3')
  53. try {
  54. options = DocArchive.buildS3Options(project_id + '/' + doc._id)
  55. } catch (e) {
  56. return callback(e)
  57. }
  58. return DocArchive._mongoDocToS3Doc(doc, function(error, json_doc) {
  59. if (error != null) {
  60. return callback(error)
  61. }
  62. options.body = json_doc
  63. options.headers = { 'Content-Type': 'application/json' }
  64. return request.put(options, function(err, res) {
  65. if (err != null || res.statusCode !== 200) {
  66. logger.err(
  67. {
  68. err,
  69. res,
  70. project_id,
  71. doc_id: doc._id,
  72. statusCode: res != null ? res.statusCode : undefined
  73. },
  74. 'something went wrong archiving doc in aws'
  75. )
  76. return callback(new Error('Error in S3 request'))
  77. }
  78. const md5lines = crypto
  79. .createHash('md5')
  80. .update(json_doc, 'utf8')
  81. .digest('hex')
  82. const md5response = res.headers.etag.toString().replace(/\"/g, '')
  83. if (md5lines !== md5response) {
  84. logger.err(
  85. {
  86. responseMD5: md5response,
  87. linesMD5: md5lines,
  88. project_id,
  89. doc_id: doc != null ? doc._id : undefined
  90. },
  91. 'err in response md5 from s3'
  92. )
  93. return callback(new Error('Error in S3 md5 response'))
  94. }
  95. return MongoManager.markDocAsArchived(doc._id, doc.rev, function(err) {
  96. if (err != null) {
  97. return callback(err)
  98. }
  99. return callback()
  100. })
  101. })
  102. })
  103. },
  104. unArchiveAllDocs(project_id, callback) {
  105. if (callback == null) {
  106. callback = function(err) {}
  107. }
  108. return MongoManager.getArchivedProjectDocs(project_id, function(err, docs) {
  109. if (err != null) {
  110. logger.err({ err, project_id }, 'error unarchiving all docs')
  111. return callback(err)
  112. } else if (docs == null) {
  113. return callback(
  114. new Errors.NotFoundError(`No docs for project ${project_id}`)
  115. )
  116. }
  117. const jobs = _.map(
  118. docs,
  119. doc =>
  120. function(cb) {
  121. if (doc.inS3 == null) {
  122. return cb()
  123. } else {
  124. return DocArchive.unarchiveDoc(project_id, doc._id, cb)
  125. }
  126. }
  127. )
  128. return async.parallelLimit(jobs, 5, callback)
  129. })
  130. },
  131. unarchiveDoc(project_id, doc_id, callback) {
  132. let options
  133. logger.log({ project_id, doc_id }, 'getting doc from s3')
  134. try {
  135. options = DocArchive.buildS3Options(project_id + '/' + doc_id)
  136. } catch (e) {
  137. return callback(e)
  138. }
  139. options.json = true
  140. return request.get(options, function(err, res, doc) {
  141. if (err != null || res.statusCode !== 200) {
  142. logger.err(
  143. { err, res, project_id, doc_id },
  144. 'something went wrong unarchiving doc from aws'
  145. )
  146. return callback(new Errors.NotFoundError('Error in S3 request'))
  147. }
  148. return DocArchive._s3DocToMongoDoc(doc, function(error, mongo_doc) {
  149. if (error != null) {
  150. return callback(error)
  151. }
  152. return MongoManager.upsertIntoDocCollection(
  153. project_id,
  154. doc_id.toString(),
  155. mongo_doc,
  156. function(err) {
  157. if (err != null) {
  158. return callback(err)
  159. }
  160. logger.log({ project_id, doc_id }, 'deleting doc from s3')
  161. return DocArchive._deleteDocFromS3(project_id, doc_id, callback)
  162. }
  163. )
  164. })
  165. })
  166. },
  167. destroyAllDocs(project_id, callback) {
  168. if (callback == null) {
  169. callback = function(err) {}
  170. }
  171. return MongoManager.getProjectsDocs(
  172. project_id,
  173. { include_deleted: true },
  174. { _id: 1 },
  175. function(err, docs) {
  176. if (err != null) {
  177. logger.err({ err, project_id }, "error getting project's docs")
  178. return callback(err)
  179. } else if (docs == null) {
  180. return callback()
  181. }
  182. const jobs = _.map(docs, doc => cb =>
  183. DocArchive.destroyDoc(project_id, doc._id, cb)
  184. )
  185. return async.parallelLimit(jobs, 5, callback)
  186. }
  187. )
  188. },
  189. destroyDoc(project_id, doc_id, callback) {
  190. logger.log({ project_id, doc_id }, 'removing doc from mongo and s3')
  191. return MongoManager.findDoc(project_id, doc_id, { inS3: 1 }, function(
  192. error,
  193. doc
  194. ) {
  195. if (error != null) {
  196. return callback(error)
  197. }
  198. if (doc == null) {
  199. return callback(new Errors.NotFoundError('Doc not found in Mongo'))
  200. }
  201. if (doc.inS3 === true) {
  202. return DocArchive._deleteDocFromS3(project_id, doc_id, function(err) {
  203. if (err != null) {
  204. return err
  205. }
  206. return MongoManager.destroyDoc(doc_id, callback)
  207. })
  208. } else {
  209. return MongoManager.destroyDoc(doc_id, callback)
  210. }
  211. })
  212. },
  213. _deleteDocFromS3(project_id, doc_id, callback) {
  214. let options
  215. try {
  216. options = DocArchive.buildS3Options(project_id + '/' + doc_id)
  217. } catch (e) {
  218. return callback(e)
  219. }
  220. options.json = true
  221. return request.del(options, function(err, res, body) {
  222. if (err != null || res.statusCode !== 204) {
  223. logger.err(
  224. { err, res, project_id, doc_id },
  225. 'something went wrong deleting doc from aws'
  226. )
  227. return callback(new Error('Error in S3 request'))
  228. }
  229. return callback()
  230. })
  231. },
  232. _s3DocToMongoDoc(doc, callback) {
  233. if (callback == null) {
  234. callback = function(error, mongo_doc) {}
  235. }
  236. const mongo_doc = {}
  237. if (doc.schema_v === 1 && doc.lines != null) {
  238. mongo_doc.lines = doc.lines
  239. if (doc.ranges != null) {
  240. mongo_doc.ranges = RangeManager.jsonRangesToMongo(doc.ranges)
  241. }
  242. } else if (doc instanceof Array) {
  243. mongo_doc.lines = doc
  244. } else {
  245. return callback(new Error("I don't understand the doc format in s3"))
  246. }
  247. return callback(null, mongo_doc)
  248. },
  249. _mongoDocToS3Doc(doc, callback) {
  250. if (callback == null) {
  251. callback = function(error, s3_doc) {}
  252. }
  253. if (doc.lines == null) {
  254. return callback(new Error('doc has no lines'))
  255. }
  256. const json = JSON.stringify({
  257. lines: doc.lines,
  258. ranges: doc.ranges,
  259. schema_v: 1
  260. })
  261. if (json.indexOf('\u0000') !== -1) {
  262. const error = new Error('null bytes detected')
  263. logger.err({ err: error, doc, json }, error.message)
  264. return callback(error)
  265. }
  266. return callback(null, json)
  267. },
  268. buildS3Options(key) {
  269. if (settings.docstore.s3 == null) {
  270. throw new Error('S3 settings are not configured')
  271. }
  272. return {
  273. aws: {
  274. key: settings.docstore.s3.key,
  275. secret: settings.docstore.s3.secret,
  276. bucket: settings.docstore.s3.bucket
  277. },
  278. timeout: thirtySeconds,
  279. uri: `https://${settings.docstore.s3.bucket}.s3.amazonaws.com/${key}`
  280. }
  281. }
  282. }