config-loader.js 11 KB

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