TestConfig.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const crypto = require('crypto')
  4. const https = require('https')
  5. // use functions to get a fresh copy, not a reference, each time
  6. function s3BaseConfig() {
  7. return {
  8. endpoint: process.env.AWS_S3_ENDPOINT,
  9. pathStyle: true,
  10. partSize: 100 * 1024 * 1024,
  11. httpOptions: {
  12. agent: new https.Agent({
  13. rejectUnauthorized: true,
  14. ca: [fs.readFileSync('/certs/public.crt')],
  15. }),
  16. },
  17. }
  18. }
  19. function s3Config() {
  20. return {
  21. key: process.env.AWS_ACCESS_KEY_ID,
  22. secret: process.env.AWS_SECRET_ACCESS_KEY,
  23. ...s3BaseConfig(),
  24. }
  25. }
  26. function s3SSECConfig() {
  27. return {
  28. ...s3Config(),
  29. pathIsProjectFolder(_bucketName, path) {
  30. return !!path.match(/^[a-f0-9]+\/$/)
  31. },
  32. pathToDataEncryptionKeyPath(_bucketName, path) {
  33. const [projectFolder] = path.match(/^[a-f0-9]+\//)
  34. return {
  35. bucketName: process.env.AWS_S3_USER_FILES_DEK_BUCKET_NAME,
  36. path: Path.join(projectFolder, 'dek'),
  37. }
  38. },
  39. async getKeyEncryptionKey() {
  40. return crypto.generateKeySync('aes', { length: 256 }).export()
  41. },
  42. }
  43. }
  44. function s3ConfigDefaultProviderCredentials() {
  45. return {
  46. ...s3BaseConfig(),
  47. }
  48. }
  49. function s3Stores() {
  50. return {
  51. user_files: process.env.AWS_S3_USER_FILES_BUCKET_NAME,
  52. template_files: process.env.AWS_S3_TEMPLATE_FILES_BUCKET_NAME,
  53. }
  54. }
  55. function gcsConfig() {
  56. return {
  57. endpoint: {
  58. apiEndpoint: process.env.GCS_API_ENDPOINT,
  59. projectId: 'fake',
  60. },
  61. directoryKeyRegex: /^[0-9a-fA-F]{24}\/[0-9a-fA-F]{24}/,
  62. unlockBeforeDelete: false, // fake-gcs does not support this
  63. deletedBucketSuffix: '-deleted',
  64. }
  65. }
  66. function gcsStores() {
  67. return {
  68. user_files: process.env.GCS_USER_FILES_BUCKET_NAME,
  69. template_files: process.env.GCS_TEMPLATE_FILES_BUCKET_NAME,
  70. }
  71. }
  72. function fsStores() {
  73. return {
  74. user_files: Path.resolve(__dirname, '../../../user_files'),
  75. template_files: Path.resolve(__dirname, '../../../template_files'),
  76. }
  77. }
  78. function fallbackStores(primaryConfig, fallbackConfig) {
  79. return {
  80. [primaryConfig.user_files]: fallbackConfig.user_files,
  81. [primaryConfig.template_files]: fallbackConfig.template_files,
  82. }
  83. }
  84. const BackendSettings = {
  85. SHARD_01_FSPersistor: {
  86. backend: 'fs',
  87. stores: fsStores(),
  88. },
  89. SHARD_01_S3Persistor: {
  90. backend: 's3',
  91. s3: s3Config(),
  92. stores: s3Stores(),
  93. },
  94. SHARD_01_S3PersistorDefaultProviderCredentials: {
  95. backend: 's3',
  96. s3: s3ConfigDefaultProviderCredentials(),
  97. stores: s3Stores(),
  98. },
  99. SHARD_01_GcsPersistor: {
  100. backend: 'gcs',
  101. gcs: gcsConfig(),
  102. stores: gcsStores(),
  103. },
  104. SHARD_01_PerProjectEncryptedS3Persistor: {
  105. backend: 's3SSEC',
  106. s3SSEC: s3SSECConfig(),
  107. stores: s3Stores(),
  108. },
  109. SHARD_02_FallbackS3ToFSPersistor: {
  110. backend: 's3',
  111. s3: s3Config(),
  112. stores: s3Stores(),
  113. fallback: {
  114. backend: 'fs',
  115. buckets: fallbackStores(s3Stores(), fsStores()),
  116. },
  117. },
  118. SHARD_02_FallbackFSToS3Persistor: {
  119. backend: 'fs',
  120. s3: s3Config(),
  121. stores: fsStores(),
  122. fallback: {
  123. backend: 's3',
  124. buckets: fallbackStores(fsStores(), s3Stores()),
  125. },
  126. },
  127. SHARD_03_FallbackGcsToS3Persistor: {
  128. backend: 'gcs',
  129. gcs: gcsConfig(),
  130. stores: gcsStores(),
  131. s3: s3Config(),
  132. fallback: {
  133. backend: 's3',
  134. buckets: fallbackStores(gcsStores(), s3Stores()),
  135. },
  136. },
  137. SHARD_03_FallbackS3ToGcsPersistor: {
  138. backend: 's3',
  139. // can use the same bucket names for gcs and s3 (in tests)
  140. stores: s3Stores(),
  141. s3: s3Config(),
  142. gcs: gcsConfig(),
  143. fallback: {
  144. backend: 'gcs',
  145. buckets: fallbackStores(s3Stores(), gcsStores()),
  146. },
  147. },
  148. }
  149. function checkForUnexpectedTestFile() {
  150. const awareOfSharding = [
  151. 'FilestoreApp.js',
  152. 'FilestoreTests.js',
  153. 'TestConfig.js',
  154. 'TestHelper.js',
  155. ]
  156. for (const file of fs.readdirSync(__dirname).sort()) {
  157. if (!awareOfSharding.includes(file)) {
  158. throw new Error(
  159. `Found new test file ${file}: All tests must be aware of the SHARD_ prefix.`
  160. )
  161. }
  162. }
  163. }
  164. checkForUnexpectedTestFile()
  165. module.exports = {
  166. BackendSettings,
  167. s3Config,
  168. }