config-loader.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. MONGO_GRIDFS_TOTAL_LIMIT: {
  131. ns: 'crowi',
  132. key: 'gridfs:totalLimit',
  133. type: TYPES.NUMBER,
  134. default: Infinity,
  135. },
  136. FORCE_WIKI_MODE: {
  137. ns: 'crowi',
  138. key: 'security:wikiMode',
  139. type: TYPES.STRING,
  140. default: undefined,
  141. },
  142. LOCAL_STRATEGY_ENABLED: {
  143. ns: 'crowi',
  144. key: 'security:passport-local:isEnabled',
  145. type: TYPES.BOOLEAN,
  146. default: true,
  147. },
  148. LOCAL_STRATEGY_USES_ONLY_ENV_VARS_FOR_SOME_OPTIONS: {
  149. ns: 'crowi',
  150. key: 'security:passport-local:useOnlyEnvVarsForSomeOptions',
  151. type: TYPES.BOOLEAN,
  152. default: false,
  153. },
  154. SAML_USES_ONLY_ENV_VARS_FOR_SOME_OPTIONS: {
  155. ns: 'crowi',
  156. key: 'security:passport-saml:useOnlyEnvVarsForSomeOptions',
  157. type: TYPES.BOOLEAN,
  158. default: false,
  159. },
  160. SAML_ENABLED: {
  161. ns: 'crowi',
  162. key: 'security:passport-saml:isEnabled',
  163. type: TYPES.BOOLEAN,
  164. default: null,
  165. },
  166. SAML_ENTRY_POINT: {
  167. ns: 'crowi',
  168. key: 'security:passport-saml:entryPoint',
  169. type: TYPES.STRING,
  170. default: null,
  171. },
  172. SAML_CALLBACK_URI: {
  173. ns: 'crowi',
  174. key: 'security:passport-saml:callbackUrl',
  175. type: TYPES.STRING,
  176. default: null,
  177. },
  178. SAML_ISSUER: {
  179. ns: 'crowi',
  180. key: 'security:passport-saml:issuer',
  181. type: TYPES.STRING,
  182. default: null,
  183. },
  184. SAML_ATTR_MAPPING_ID: {
  185. ns: 'crowi',
  186. key: 'security:passport-saml:attrMapId',
  187. type: TYPES.STRING,
  188. default: null,
  189. },
  190. SAML_ATTR_MAPPING_USERNAME: {
  191. ns: 'crowi',
  192. key: 'security:passport-saml:attrMapUsername',
  193. type: TYPES.STRING,
  194. default: null,
  195. },
  196. SAML_ATTR_MAPPING_MAIL: {
  197. ns: 'crowi',
  198. key: 'security:passport-saml:attrMapMail',
  199. type: TYPES.STRING,
  200. default: null,
  201. },
  202. SAML_ATTR_MAPPING_FIRST_NAME: {
  203. ns: 'crowi',
  204. key: 'security:passport-saml:attrMapFirstName',
  205. type: TYPES.STRING,
  206. default: null,
  207. },
  208. SAML_ATTR_MAPPING_LAST_NAME: {
  209. ns: 'crowi',
  210. key: 'security:passport-saml:attrMapLastName',
  211. type: TYPES.STRING,
  212. default: null,
  213. },
  214. SAML_CERT: {
  215. ns: 'crowi',
  216. key: 'security:passport-saml:cert',
  217. type: TYPES.STRING,
  218. default: null,
  219. },
  220. };
  221. class ConfigLoader {
  222. constructor(configModel) {
  223. this.configModel = configModel;
  224. }
  225. /**
  226. * return a config object
  227. */
  228. async load() {
  229. const configFromDB = await this.loadFromDB();
  230. const configFromEnvVars = this.loadFromEnvVars();
  231. // merge defaults
  232. let mergedConfigFromDB = Object.assign({ crowi: this.configModel.getDefaultCrowiConfigsObject() }, configFromDB);
  233. mergedConfigFromDB = Object.assign({ markdown: this.configModel.getDefaultMarkdownConfigsObject() }, mergedConfigFromDB);
  234. mergedConfigFromDB = Object.assign({ notification: this.configModel.getDefaultNotificationConfigsObject() }, mergedConfigFromDB);
  235. // In getConfig API, only null is used as a value to indicate that a config is not set.
  236. // So, if a value loaded from the database is emtpy,
  237. // it is converted to null because an empty string is used as the same meaning in the config model.
  238. // By this processing, whether a value is loaded from the database or from the environment variable,
  239. // only null indicates a config is not set.
  240. for (const namespace of Object.keys(mergedConfigFromDB)) {
  241. for (const key of Object.keys(mergedConfigFromDB[namespace])) {
  242. if (mergedConfigFromDB[namespace][key] === '') {
  243. mergedConfigFromDB[namespace][key] = null;
  244. }
  245. }
  246. }
  247. return {
  248. fromDB: mergedConfigFromDB,
  249. fromEnvVars: configFromEnvVars,
  250. };
  251. }
  252. async loadFromDB() {
  253. const config = {};
  254. const docs = await this.configModel.find().exec();
  255. for (const doc of docs) {
  256. if (!config[doc.ns]) {
  257. config[doc.ns] = {};
  258. }
  259. config[doc.ns][doc.key] = JSON.parse(doc.value);
  260. }
  261. debug('ConfigLoader#loadFromDB', config);
  262. return config;
  263. }
  264. loadFromEnvVars() {
  265. const config = {};
  266. for (const ENV_VAR_NAME of Object.keys(ENV_VAR_NAME_TO_CONFIG_INFO)) {
  267. const configInfo = ENV_VAR_NAME_TO_CONFIG_INFO[ENV_VAR_NAME];
  268. if (config[configInfo.ns] === undefined) {
  269. config[configInfo.ns] = {};
  270. }
  271. if (process.env[ENV_VAR_NAME] === undefined) {
  272. config[configInfo.ns][configInfo.key] = configInfo.default;
  273. }
  274. else {
  275. config[configInfo.ns][configInfo.key] = configInfo.type.parse(process.env[ENV_VAR_NAME]);
  276. }
  277. }
  278. debug('ConfigLoader#loadFromEnvVars', config);
  279. return config;
  280. }
  281. }
  282. module.exports = ConfigLoader;