config-manager.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. const debug = require('debug')('growi:service:ConfigManager');
  2. const pathUtils = require('growi-commons').pathUtils;
  3. const ConfigLoader = require('../service/config-loader');
  4. const KEYS_FOR_SAML_USE_ONLY_ENV_OPTION = [
  5. 'security:passport-saml:isEnabled',
  6. 'security:passport-saml:entryPoint',
  7. 'security:passport-saml:issuer',
  8. 'security:passport-saml:attrMapId',
  9. 'security:passport-saml:attrMapUsername',
  10. 'security:passport-saml:attrMapMail',
  11. 'security:passport-saml:attrMapFirstName',
  12. 'security:passport-saml:attrMapLastName',
  13. 'security:passport-saml:cert',
  14. ];
  15. class ConfigManager {
  16. constructor(configModel) {
  17. this.configModel = configModel;
  18. this.configLoader = new ConfigLoader(this.configModel);
  19. this.configObject = null;
  20. this.getConfig = this.getConfig.bind(this);
  21. }
  22. /**
  23. * load configs from the database and the environment variables
  24. */
  25. async loadConfigs() {
  26. this.configObject = await this.configLoader.load();
  27. debug('ConfigManager#loadConfigs', this.configObject);
  28. }
  29. /**
  30. * get a config specified by namespace & key
  31. *
  32. * Basically, this searches a specified config from configs loaded from the database at first
  33. * and then from configs loaded from the environment variables.
  34. *
  35. * In some case, this search method changes.
  36. *
  37. * the followings are the meanings of each special return value.
  38. * - null: a specified config is not set.
  39. * - undefined: a specified config does not exist.
  40. */
  41. getConfig(namespace, key) {
  42. if (this.searchOnlyFromEnvVarConfigs('crowi', 'security:passport-saml:useOnlyEnvVarsForSomeOptions')) {
  43. return this.searchInSAMLUseOnlyEnvMode(namespace, key);
  44. }
  45. return this.defaultSearch(namespace, key);
  46. }
  47. /**
  48. * get a config specified by namespace & key from configs loaded from the database
  49. *
  50. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  51. */
  52. getConfigFromDB(namespace, key) {
  53. return this.searchOnlyFromDBConfigs(namespace, key);
  54. }
  55. /**
  56. * get a config specified by namespace & key from configs loaded from the environment variables
  57. *
  58. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  59. */
  60. getConfigFromEnvVars(namespace, key) {
  61. return this.searchOnlyFromEnvVarConfigs(namespace, key);
  62. }
  63. /**
  64. * get the site url
  65. *
  66. * If the config for the site url is not set, this returns a message "[The site URL is not set. Please set it!]".
  67. *
  68. * With version 3.2.3 and below, there is no config for the site URL, so the system always uses auto-generated site URL.
  69. * With version 3.2.4 to 3.3.4, the system uses the auto-generated site URL only if the config is not set.
  70. * With version 3.3.5 and above, the system use only a value from the config.
  71. */
  72. /* eslint-disable no-else-return */
  73. getSiteUrl() {
  74. const siteUrl = this.getConfig('crowi', 'app:siteUrl');
  75. if (siteUrl != null) {
  76. return pathUtils.removeTrailingSlash(siteUrl);
  77. }
  78. else {
  79. return '[The site URL is not set. Please set it!]';
  80. }
  81. }
  82. /* eslint-enable no-else-return */
  83. getIsUploadable() {
  84. const method = process.env.FILE_UPLOAD || 'aws';
  85. if (method === 'aws' && (
  86. !this.getConfig('crowi', 'aws:accessKeyId')
  87. || !this.getConfig('crowi', 'aws:secretAccessKey')
  88. || !this.getConfig('crowi', 'aws:region')
  89. || !this.getConfig('crowi', 'aws:bucket'))) {
  90. return false;
  91. }
  92. return method !== 'none';
  93. }
  94. getTagWhiteList() {
  95. const { tags } = require('@commons/service/xss/recommended-whitelist');
  96. const isEnabledXssPrevention = this.getConfig('markdown', 'markdown:xss:isEnabledPrevention');
  97. const xssOpiton = this.getConfig('markdown', 'markdown:xss:option');
  98. if (isEnabledXssPrevention) {
  99. switch (xssOpiton) {
  100. case 1: // ignore all: use default option
  101. return [];
  102. case 2: // recommended
  103. return tags;
  104. case 3: // custom white list
  105. return this.getConfig('markdown', 'markdown:xss:tagWhiteList');
  106. default:
  107. return [];
  108. }
  109. }
  110. else {
  111. return [];
  112. }
  113. }
  114. getAttrWhiteList() {
  115. const { attrs } = require('@commons/service/xss/recommended-whitelist');
  116. const isEnabledXssPrevention = this.getConfig('markdown', 'markdown:xss:isEnabledPrevention');
  117. const xssOpiton = this.getConfig('markdown', 'markdown:xss:option');
  118. if (isEnabledXssPrevention) {
  119. switch (xssOpiton) {
  120. case 1: // ignore all: use default option
  121. return [];
  122. case 2: // recommended
  123. return attrs;
  124. case 3: // custom white list
  125. return this.getConfig('markdown', 'markdown:xss:attrWhiteList');
  126. default:
  127. return [];
  128. }
  129. }
  130. else {
  131. return [];
  132. }
  133. }
  134. getIsPublicWikiOnly() {
  135. // CONF.RF save PUBLIC_WIKI_ONLY in mongodb?
  136. const publicWikiOnly = process.env.PUBLIC_WIKI_ONLY;
  137. if (publicWikiOnly === 'true' || publicWikiOnly === 1) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. getIsGuestAllowedToRead() {
  143. const SECURITY_RESTRICT_GUEST_MODE_DENY = 'Deny';
  144. const SECURITY_RESTRICT_GUEST_MODE_READONLY = 'Readonly';
  145. const SECURITY_REGISTRATION_MODE_OPEN = 'Open';
  146. const SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted';
  147. const SECURITY_REGISTRATION_MODE_CLOSED = 'Closed';
  148. // return true if puclic wiki mode
  149. if (this.getIsPublicWikiOnly()) {
  150. return true;
  151. }
  152. // return false if undefined
  153. const isRestrictGuestMode = this.getConfig('crowi', 'security:restrictGuestMode');
  154. if (isRestrictGuestMode) {
  155. return false;
  156. }
  157. return SECURITY_RESTRICT_GUEST_MODE_READONLY === isRestrictGuestMode;
  158. }
  159. /**
  160. * update configs in the same namespace
  161. *
  162. * Specified values are encoded by convertInsertValue.
  163. * In it, an empty string is converted to null that indicates a config is not set.
  164. *
  165. * For example:
  166. * ```
  167. * updateConfigsInTheSameNamespace(
  168. * 'some namespace',
  169. * {
  170. * 'some key 1': 'value 1',
  171. * 'some key 2': 'value 2',
  172. * ...
  173. * }
  174. * );
  175. * ```
  176. */
  177. async updateConfigsInTheSameNamespace(namespace, configs) {
  178. const queries = [];
  179. for (const key of Object.keys(configs)) {
  180. queries.push({
  181. updateOne: {
  182. filter: { ns: namespace, key },
  183. update: { ns: namespace, key, value: this.convertInsertValue(configs[key]) },
  184. upsert: true,
  185. },
  186. });
  187. }
  188. await this.configModel.bulkWrite(queries);
  189. await this.loadConfigs();
  190. }
  191. /**
  192. * Execute only once for installing application
  193. */
  194. async initDB(globalLang) {
  195. const initialConfig = this.configModel.getConfigsObjectForInstalling();
  196. initialConfig['app:globalLang'] = globalLang;
  197. await this.updateConfigsInTheSameNamespace('crowi', initialConfig);
  198. }
  199. async isDBInitialized() {
  200. const appInstalled = await this.getConfigFromDB('crowi', 'app:installed');
  201. return appInstalled;
  202. }
  203. /*
  204. * All of the methods below are private APIs.
  205. */
  206. /**
  207. * search a specified config from configs loaded from the database at first
  208. * and then from configs loaded from the environment variables
  209. */
  210. defaultSearch(namespace, key) {
  211. if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  212. return undefined;
  213. }
  214. if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  215. return this.configObject.fromDB[namespace][key];
  216. }
  217. if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
  218. return this.configObject.fromEnvVars[namespace][key];
  219. }
  220. if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
  221. /* eslint-disable no-else-return */
  222. if (this.configObject.fromDB[namespace][key] !== null) {
  223. return this.configObject.fromDB[namespace][key];
  224. }
  225. else {
  226. return this.configObject.fromEnvVars[namespace][key];
  227. }
  228. /* eslint-enable no-else-return */
  229. }
  230. }
  231. /**
  232. * For the configs specified by KEYS_FOR_SAML_USE_ONLY_ENV_OPTION,
  233. * this searches only from configs loaded from the environment variables.
  234. * For the other configs, this searches as the same way to defaultSearch.
  235. */
  236. /* eslint-disable no-else-return */
  237. searchInSAMLUseOnlyEnvMode(namespace, key) {
  238. if (namespace === 'crowi' && KEYS_FOR_SAML_USE_ONLY_ENV_OPTION.includes(key)) {
  239. return this.searchOnlyFromEnvVarConfigs(namespace, key);
  240. }
  241. else {
  242. return this.defaultSearch(namespace, key);
  243. }
  244. }
  245. /* eslint-enable no-else-return */
  246. /**
  247. * search a specified config from configs loaded from the database
  248. */
  249. searchOnlyFromDBConfigs(namespace, key) {
  250. if (!this.configExistsInDB(namespace, key)) {
  251. return undefined;
  252. }
  253. return this.configObject.fromDB[namespace][key];
  254. }
  255. /**
  256. * search a specified config from configs loaded from the environment variables
  257. */
  258. searchOnlyFromEnvVarConfigs(namespace, key) {
  259. if (!this.configExistsInEnvVars(namespace, key)) {
  260. return undefined;
  261. }
  262. return this.configObject.fromEnvVars[namespace][key];
  263. }
  264. /**
  265. * check whether a specified config exists in configs loaded from the database
  266. */
  267. configExistsInDB(namespace, key) {
  268. if (this.configObject.fromDB[namespace] === undefined) {
  269. return false;
  270. }
  271. return this.configObject.fromDB[namespace][key] !== undefined;
  272. }
  273. /**
  274. * check whether a specified config exists in configs loaded from the environment variables
  275. */
  276. configExistsInEnvVars(namespace, key) {
  277. if (this.configObject.fromEnvVars[namespace] === undefined) {
  278. return false;
  279. }
  280. return this.configObject.fromEnvVars[namespace][key] !== undefined;
  281. }
  282. convertInsertValue(value) {
  283. return JSON.stringify(value === '' ? null : value);
  284. }
  285. }
  286. module.exports = ConfigManager;