config-loader.js 8.6 KB

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