config-loader.js 9.9 KB

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