config-loader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. APP_SITE_URL: {
  108. ns: 'crowi',
  109. key: 'app:siteUrl',
  110. type: TYPES.STRING,
  111. default: null,
  112. },
  113. PUBLISH_OPEN_API: {
  114. ns: 'crowi',
  115. key: 'app:publishOpenAPI',
  116. type: TYPES.BOOLEAN,
  117. default: false,
  118. },
  119. MAX_FILE_SIZE: {
  120. ns: 'crowi',
  121. key: 'app:maxFileSize',
  122. type: TYPES.NUMBER,
  123. default: Infinity,
  124. },
  125. FILE_UPLOAD_TOTAL_LIMIT: {
  126. ns: 'crowi',
  127. key: 'app:fileUploadTotalLimit',
  128. type: TYPES.NUMBER,
  129. default: Infinity,
  130. },
  131. FILE_UPLOAD_DISABLED: {
  132. ns: 'crowi',
  133. key: 'app:fileUploadDisabled',
  134. type: TYPES.BOOLEAN,
  135. default: false,
  136. },
  137. USE_INTERNAL_REDIRECT: {
  138. ns: 'crowi',
  139. key: 'app:useInternalRedirect',
  140. type: TYPES.BOOLEAN,
  141. default: false,
  142. },
  143. INTERNAL_REDIRECT_PATH: {
  144. ns: 'crowi',
  145. key: 'app:internalRedirectPath',
  146. type: TYPES.STRING,
  147. default: "/growi-internal/",
  148. },
  149. ELASTICSEARCH_URI: {
  150. ns: 'crowi',
  151. key: 'app:elasticsearchUri',
  152. type: TYPES.STRING,
  153. default: null,
  154. },
  155. ELASTICSEARCH_REQUEST_TIMEOUT: {
  156. ns: 'crowi',
  157. key: 'app:elasticsearchRequestTimeout',
  158. type: TYPES.NUMBER,
  159. default: 8000, // msec
  160. },
  161. SEARCHBOX_SSL_URL: {
  162. ns: 'crowi',
  163. key: 'app:searchboxSslUrl',
  164. type: TYPES.STRING,
  165. default: null,
  166. },
  167. MONGO_GRIDFS_TOTAL_LIMIT: {
  168. ns: 'crowi',
  169. key: 'gridfs:totalLimit',
  170. type: TYPES.NUMBER,
  171. default: null, // set null in default for backward compatibility
  172. // cz: Newer system respects FILE_UPLOAD_TOTAL_LIMIT.
  173. // If the default value of MONGO_GRIDFS_TOTAL_LIMIT is Infinity,
  174. // the system can't distinguish between "not specified" and "Infinity is specified".
  175. },
  176. FORCE_WIKI_MODE: {
  177. ns: 'crowi',
  178. key: 'security:wikiMode',
  179. type: TYPES.STRING,
  180. default: undefined,
  181. },
  182. USER_UPPER_LIMIT: {
  183. ns: 'crowi',
  184. key: 'security:userUpperLimit',
  185. type: TYPES.NUMBER,
  186. default: Infinity,
  187. },
  188. LOCAL_STRATEGY_ENABLED: {
  189. ns: 'crowi',
  190. key: 'security:passport-local:isEnabled',
  191. type: TYPES.BOOLEAN,
  192. default: true,
  193. },
  194. LOCAL_STRATEGY_USES_ONLY_ENV_VARS_FOR_SOME_OPTIONS: {
  195. ns: 'crowi',
  196. key: 'security:passport-local:useOnlyEnvVarsForSomeOptions',
  197. type: TYPES.BOOLEAN,
  198. default: false,
  199. },
  200. SAML_USES_ONLY_ENV_VARS_FOR_SOME_OPTIONS: {
  201. ns: 'crowi',
  202. key: 'security:passport-saml:useOnlyEnvVarsForSomeOptions',
  203. type: TYPES.BOOLEAN,
  204. default: false,
  205. },
  206. SAML_ENABLED: {
  207. ns: 'crowi',
  208. key: 'security:passport-saml:isEnabled',
  209. type: TYPES.BOOLEAN,
  210. default: null,
  211. },
  212. SAML_ENTRY_POINT: {
  213. ns: 'crowi',
  214. key: 'security:passport-saml:entryPoint',
  215. type: TYPES.STRING,
  216. default: null,
  217. },
  218. SAML_CALLBACK_URI: {
  219. ns: 'crowi',
  220. key: 'security:passport-saml:callbackUrl',
  221. type: TYPES.STRING,
  222. default: null,
  223. },
  224. SAML_ISSUER: {
  225. ns: 'crowi',
  226. key: 'security:passport-saml:issuer',
  227. type: TYPES.STRING,
  228. default: null,
  229. },
  230. SAML_ATTR_MAPPING_ID: {
  231. ns: 'crowi',
  232. key: 'security:passport-saml:attrMapId',
  233. type: TYPES.STRING,
  234. default: null,
  235. },
  236. SAML_ATTR_MAPPING_USERNAME: {
  237. ns: 'crowi',
  238. key: 'security:passport-saml:attrMapUsername',
  239. type: TYPES.STRING,
  240. default: null,
  241. },
  242. SAML_ATTR_MAPPING_MAIL: {
  243. ns: 'crowi',
  244. key: 'security:passport-saml:attrMapMail',
  245. type: TYPES.STRING,
  246. default: null,
  247. },
  248. SAML_ATTR_MAPPING_FIRST_NAME: {
  249. ns: 'crowi',
  250. key: 'security:passport-saml:attrMapFirstName',
  251. type: TYPES.STRING,
  252. default: null,
  253. },
  254. SAML_ATTR_MAPPING_LAST_NAME: {
  255. ns: 'crowi',
  256. key: 'security:passport-saml:attrMapLastName',
  257. type: TYPES.STRING,
  258. default: null,
  259. },
  260. SAML_CERT: {
  261. ns: 'crowi',
  262. key: 'security:passport-saml:cert',
  263. type: TYPES.STRING,
  264. default: null,
  265. },
  266. SAML_ABLC_RULE: {
  267. ns: 'crowi',
  268. key: 'security:passport-saml:ABLCRule',
  269. type: TYPES.STRING,
  270. default: null,
  271. },
  272. GCS_API_KEY_JSON_PATH: {
  273. ns: 'crowi',
  274. key: 'gcs:apiKeyJsonPath',
  275. type: TYPES.STRING,
  276. default: null,
  277. },
  278. GCS_BUCKET: {
  279. ns: 'crowi',
  280. key: 'gcs:bucket',
  281. type: TYPES.STRING,
  282. default: null,
  283. },
  284. GCS_UPLOAD_NAMESPACE: {
  285. ns: 'crowi',
  286. key: 'gcs:uploadNamespace',
  287. type: TYPES.STRING,
  288. default: null,
  289. },
  290. };
  291. class ConfigLoader {
  292. constructor(configModel) {
  293. this.configModel = configModel;
  294. }
  295. /**
  296. * return a config object
  297. */
  298. async load() {
  299. const configFromDB = await this.loadFromDB();
  300. const configFromEnvVars = this.loadFromEnvVars();
  301. // merge defaults per ns
  302. const mergedConfigFromDB = {
  303. crowi: Object.assign(this.configModel.getDefaultCrowiConfigsObject(), configFromDB.crowi),
  304. markdown: Object.assign(this.configModel.getDefaultMarkdownConfigsObject(), configFromDB.markdown),
  305. notification: Object.assign(this.configModel.getDefaultNotificationConfigsObject(), configFromDB.notification),
  306. };
  307. // In getConfig API, only null is used as a value to indicate that a config is not set.
  308. // So, if a value loaded from the database is empty,
  309. // it is converted to null because an empty string is used as the same meaning in the config model.
  310. // By this processing, whether a value is loaded from the database or from the environment variable,
  311. // only null indicates a config is not set.
  312. for (const namespace of Object.keys(mergedConfigFromDB)) {
  313. for (const key of Object.keys(mergedConfigFromDB[namespace])) {
  314. if (mergedConfigFromDB[namespace][key] === '') {
  315. mergedConfigFromDB[namespace][key] = null;
  316. }
  317. }
  318. }
  319. return {
  320. fromDB: mergedConfigFromDB,
  321. fromEnvVars: configFromEnvVars,
  322. };
  323. }
  324. async loadFromDB() {
  325. const config = {};
  326. const docs = await this.configModel.find().exec();
  327. for (const doc of docs) {
  328. if (!config[doc.ns]) {
  329. config[doc.ns] = {};
  330. }
  331. config[doc.ns][doc.key] = JSON.parse(doc.value);
  332. }
  333. debug('ConfigLoader#loadFromDB', config);
  334. return config;
  335. }
  336. loadFromEnvVars() {
  337. const config = {};
  338. for (const ENV_VAR_NAME of Object.keys(ENV_VAR_NAME_TO_CONFIG_INFO)) {
  339. const configInfo = ENV_VAR_NAME_TO_CONFIG_INFO[ENV_VAR_NAME];
  340. if (config[configInfo.ns] === undefined) {
  341. config[configInfo.ns] = {};
  342. }
  343. if (process.env[ENV_VAR_NAME] === undefined) {
  344. config[configInfo.ns][configInfo.key] = configInfo.default;
  345. }
  346. else {
  347. config[configInfo.ns][configInfo.key] = configInfo.type.parse(process.env[ENV_VAR_NAME]);
  348. }
  349. }
  350. debug('ConfigLoader#loadFromEnvVars', config);
  351. return config;
  352. }
  353. /**
  354. * get config from the environment variables for display admin page
  355. *
  356. * **use this only admin home page.**
  357. */
  358. static getEnvVarsForDisplay(avoidSecurity = false) {
  359. const config = {};
  360. for (const ENV_VAR_NAME of Object.keys(ENV_VAR_NAME_TO_CONFIG_INFO)) {
  361. const configInfo = ENV_VAR_NAME_TO_CONFIG_INFO[ENV_VAR_NAME];
  362. if (process.env[ENV_VAR_NAME] === undefined) {
  363. continue;
  364. }
  365. if (isSecurityEnv(configInfo.key) && avoidSecurity) {
  366. continue;
  367. }
  368. config[ENV_VAR_NAME] = configInfo.type.parse(process.env[ENV_VAR_NAME]);
  369. }
  370. debug('ConfigLoader#getEnvVarsForDisplay', config);
  371. return config;
  372. }
  373. }
  374. module.exports = ConfigLoader;