config-loader.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. const debug = require('debug')('growi:service:ConfigLoader');
  2. const { envUtils } = require('growi-commons');
  3. const TYPES = {
  4. NUMBER: { parse: (v) => { return parseInt(v, 10) } },
  5. STRING: { parse: (v) => { return v } },
  6. BOOLEAN: { parse: (v) => { return envUtils.toBoolean(v) } },
  7. };
  8. /**
  9. * The following env vars are excluded because these are currently used before the configuration setup.
  10. * - MONGO_URI
  11. * - NODE_ENV
  12. * - PORT
  13. * - REDIS_URI
  14. * - SESSION_NAME
  15. * - PASSWORD_SEED
  16. * - SECRET_TOKEN
  17. *
  18. * The commented out item has not yet entered the migration work.
  19. * So, parameters of these are under consideration.
  20. */
  21. const ENV_VAR_NAME_TO_CONFIG_INFO = {
  22. // FILE_UPLOAD: {
  23. // ns: ,
  24. // key: ,
  25. // type: ,
  26. // default:
  27. // },
  28. // HACKMD_URI: {
  29. // ns: ,
  30. // key: ,
  31. // type: ,
  32. // default:
  33. // },
  34. // HACKMD_URI_FOR_SERVER: {
  35. // ns: ,
  36. // key: ,
  37. // type: ,
  38. // default:
  39. // },
  40. // PLANTUML_URI: {
  41. // ns: ,
  42. // key: ,
  43. // type: ,
  44. // default:
  45. // },
  46. // BLOCKDIAG_URI: {
  47. // ns: ,
  48. // key: ,
  49. // type: ,
  50. // default:
  51. // },
  52. // OAUTH_GOOGLE_CLIENT_ID: {
  53. // ns: 'crowi',
  54. // key: 'security:passport-google:clientId',
  55. // type: ,
  56. // default:
  57. // },
  58. // OAUTH_GOOGLE_CLIENT_SECRET: {
  59. // ns: 'crowi',
  60. // key: 'security:passport-google:clientSecret',
  61. // type: ,
  62. // default:
  63. // },
  64. // OAUTH_GOOGLE_CALLBACK_URI: {
  65. // ns: 'crowi',
  66. // key: 'security:passport-google:callbackUrl',
  67. // type: ,
  68. // default:
  69. // },
  70. // OAUTH_GITHUB_CLIENT_ID: {
  71. // ns: 'crowi',
  72. // key: 'security:passport-github:clientId',
  73. // type: ,
  74. // default:
  75. // },
  76. // OAUTH_GITHUB_CLIENT_SECRET: {
  77. // ns: 'crowi',
  78. // key: 'security:passport-github:clientSecret',
  79. // type: ,
  80. // default:
  81. // },
  82. // OAUTH_GITHUB_CALLBACK_URI: {
  83. // ns: 'crowi',
  84. // key: 'security:passport-github:callbackUrl',
  85. // type: ,
  86. // default:
  87. // },
  88. // OAUTH_TWITTER_CONSUMER_KEY: {
  89. // ns: 'crowi',
  90. // key: 'security:passport-twitter:consumerKey',
  91. // type: ,
  92. // default:
  93. // },
  94. // OAUTH_TWITTER_CONSUMER_SECRET: {
  95. // ns: 'crowi',
  96. // key: 'security:passport-twitter:consumerSecret',
  97. // type: ,
  98. // default:
  99. // },
  100. // OAUTH_TWITTER_CALLBACK_URI: {
  101. // ns: 'crowi',
  102. // key: 'security:passport-twitter:callbackUrl',
  103. // type: ,
  104. // default:
  105. // },
  106. APP_SITE_URL: {
  107. ns: 'crowi',
  108. key: 'app:siteUrl',
  109. type: TYPES.STRING,
  110. default: null,
  111. },
  112. PUBLISH_OPEN_API: {
  113. ns: 'crowi',
  114. key: 'app:publishOpenAPI',
  115. type: TYPES.BOOLEAN,
  116. default: false,
  117. },
  118. MAX_FILE_SIZE: {
  119. ns: 'crowi',
  120. key: 'app:maxFileSize',
  121. type: TYPES.NUMBER,
  122. default: Infinity,
  123. },
  124. FILE_UPLOAD_TOTAL_LIMIT: {
  125. ns: 'crowi',
  126. key: 'app:fileUploadTotalLimit',
  127. type: TYPES.NUMBER,
  128. default: Infinity,
  129. },
  130. ELASTICSEARCH_URI: {
  131. ns: 'crowi',
  132. key: 'app:elasticsearchUri',
  133. type: TYPES.STRING,
  134. default: null,
  135. },
  136. SEARCHBOX_SSL_URL: {
  137. ns: 'crowi',
  138. key: 'app:searchboxSslUrl',
  139. type: TYPES.STRING,
  140. default: null,
  141. },
  142. MONGO_GRIDFS_TOTAL_LIMIT: {
  143. ns: 'crowi',
  144. key: 'gridfs:totalLimit',
  145. type: TYPES.NUMBER,
  146. default: null, // set null in default for backward compatibility
  147. // cz: Newer system respects FILE_UPLOAD_TOTAL_LIMIT.
  148. // If the default value of MONGO_GRIDFS_TOTAL_LIMIT is Infinity,
  149. // the system can't distinguish between "not specified" and "Infinity is specified".
  150. },
  151. FORCE_WIKI_MODE: {
  152. ns: 'crowi',
  153. key: 'security:wikiMode',
  154. type: TYPES.STRING,
  155. default: undefined,
  156. },
  157. USER_UPPER_LIMIT: {
  158. ns: 'crowi',
  159. key: 'security:userUpperLimit',
  160. type: TYPES.NUMBER,
  161. default: Infinity,
  162. },
  163. LOCAL_STRATEGY_ENABLED: {
  164. ns: 'crowi',
  165. key: 'security:passport-local:isEnabled',
  166. type: TYPES.BOOLEAN,
  167. default: true,
  168. },
  169. LOCAL_STRATEGY_USES_ONLY_ENV_VARS_FOR_SOME_OPTIONS: {
  170. ns: 'crowi',
  171. key: 'security:passport-local:useOnlyEnvVarsForSomeOptions',
  172. type: TYPES.BOOLEAN,
  173. default: false,
  174. },
  175. SAML_USES_ONLY_ENV_VARS_FOR_SOME_OPTIONS: {
  176. ns: 'crowi',
  177. key: 'security:passport-saml:useOnlyEnvVarsForSomeOptions',
  178. type: TYPES.BOOLEAN,
  179. default: false,
  180. },
  181. SAML_ENABLED: {
  182. ns: 'crowi',
  183. key: 'security:passport-saml:isEnabled',
  184. type: TYPES.BOOLEAN,
  185. default: null,
  186. },
  187. SAML_ENTRY_POINT: {
  188. ns: 'crowi',
  189. key: 'security:passport-saml:entryPoint',
  190. type: TYPES.STRING,
  191. default: null,
  192. },
  193. SAML_CALLBACK_URI: {
  194. ns: 'crowi',
  195. key: 'security:passport-saml:callbackUrl',
  196. type: TYPES.STRING,
  197. default: null,
  198. },
  199. SAML_ISSUER: {
  200. ns: 'crowi',
  201. key: 'security:passport-saml:issuer',
  202. type: TYPES.STRING,
  203. default: null,
  204. },
  205. SAML_ATTR_MAPPING_ID: {
  206. ns: 'crowi',
  207. key: 'security:passport-saml:attrMapId',
  208. type: TYPES.STRING,
  209. default: null,
  210. },
  211. SAML_ATTR_MAPPING_USERNAME: {
  212. ns: 'crowi',
  213. key: 'security:passport-saml:attrMapUsername',
  214. type: TYPES.STRING,
  215. default: null,
  216. },
  217. SAML_ATTR_MAPPING_MAIL: {
  218. ns: 'crowi',
  219. key: 'security:passport-saml:attrMapMail',
  220. type: TYPES.STRING,
  221. default: null,
  222. },
  223. SAML_ATTR_MAPPING_FIRST_NAME: {
  224. ns: 'crowi',
  225. key: 'security:passport-saml:attrMapFirstName',
  226. type: TYPES.STRING,
  227. default: null,
  228. },
  229. SAML_ATTR_MAPPING_LAST_NAME: {
  230. ns: 'crowi',
  231. key: 'security:passport-saml:attrMapLastName',
  232. type: TYPES.STRING,
  233. default: null,
  234. },
  235. SAML_CERT: {
  236. ns: 'crowi',
  237. key: 'security:passport-saml:cert',
  238. type: TYPES.STRING,
  239. default: null,
  240. },
  241. GCS_API_KEY_JSON_PATH: {
  242. ns: 'crowi',
  243. key: 'gcs:apiKeyJsonPath',
  244. type: TYPES.STRING,
  245. default: null,
  246. },
  247. GCS_BUCKET: {
  248. ns: 'crowi',
  249. key: 'gcs:bucket',
  250. type: TYPES.STRING,
  251. default: null,
  252. },
  253. GCS_UPLOAD_NAMESPACE: {
  254. ns: 'crowi',
  255. key: 'gcs:uploadNamespace',
  256. type: TYPES.STRING,
  257. default: null,
  258. },
  259. };
  260. class ConfigLoader {
  261. constructor(configModel) {
  262. this.configModel = configModel;
  263. }
  264. /**
  265. * return a config object
  266. */
  267. async load() {
  268. const configFromDB = await this.loadFromDB();
  269. const configFromEnvVars = this.loadFromEnvVars();
  270. // merge defaults per ns
  271. const mergedConfigFromDB = {
  272. crowi: Object.assign(this.configModel.getDefaultCrowiConfigsObject(), configFromDB.crowi),
  273. markdown: Object.assign(this.configModel.getDefaultMarkdownConfigsObject(), configFromDB.markdown),
  274. notification: Object.assign(this.configModel.getDefaultNotificationConfigsObject(), configFromDB.notification),
  275. };
  276. // In getConfig API, only null is used as a value to indicate that a config is not set.
  277. // So, if a value loaded from the database is emtpy,
  278. // it is converted to null because an empty string is used as the same meaning in the config model.
  279. // By this processing, whether a value is loaded from the database or from the environment variable,
  280. // only null indicates a config is not set.
  281. for (const namespace of Object.keys(mergedConfigFromDB)) {
  282. for (const key of Object.keys(mergedConfigFromDB[namespace])) {
  283. if (mergedConfigFromDB[namespace][key] === '') {
  284. mergedConfigFromDB[namespace][key] = null;
  285. }
  286. }
  287. }
  288. return {
  289. fromDB: mergedConfigFromDB,
  290. fromEnvVars: configFromEnvVars,
  291. };
  292. }
  293. async loadFromDB() {
  294. const config = {};
  295. const docs = await this.configModel.find().exec();
  296. for (const doc of docs) {
  297. if (!config[doc.ns]) {
  298. config[doc.ns] = {};
  299. }
  300. config[doc.ns][doc.key] = JSON.parse(doc.value);
  301. }
  302. debug('ConfigLoader#loadFromDB', config);
  303. return config;
  304. }
  305. loadFromEnvVars() {
  306. const config = {};
  307. for (const ENV_VAR_NAME of Object.keys(ENV_VAR_NAME_TO_CONFIG_INFO)) {
  308. const configInfo = ENV_VAR_NAME_TO_CONFIG_INFO[ENV_VAR_NAME];
  309. if (config[configInfo.ns] === undefined) {
  310. config[configInfo.ns] = {};
  311. }
  312. if (process.env[ENV_VAR_NAME] === undefined) {
  313. config[configInfo.ns][configInfo.key] = configInfo.default;
  314. }
  315. else {
  316. config[configInfo.ns][configInfo.key] = configInfo.type.parse(process.env[ENV_VAR_NAME]);
  317. }
  318. }
  319. debug('ConfigLoader#loadFromEnvVars', config);
  320. return config;
  321. }
  322. }
  323. module.exports = ConfigLoader;