config-manager.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. }
  21. /**
  22. * load configs from the database and the environment variables
  23. */
  24. async loadConfigs() {
  25. this.configObject = await this.configLoader.load();
  26. debug('ConfigManager#loadConfigs', this.configObject);
  27. }
  28. /**
  29. * get a config specified by namespace & key
  30. *
  31. * Basically, this searches a specified config from configs loaded from the database at first
  32. * and then from configs loaded from the environment variables.
  33. *
  34. * In some case, this search method changes.
  35. *
  36. * the followings are the meanings of each special return value.
  37. * - null: a specified config is not set.
  38. * - undefined: a specified config does not exist.
  39. */
  40. getConfig(namespace, key) {
  41. if (this.searchOnlyFromEnvVarConfigs('crowi', 'security:passport-saml:useOnlyEnvVarsForSomeOptions')) {
  42. return this.searchInSAMLUseOnlyEnvMode(namespace, key);
  43. }
  44. return this.defaultSearch(namespace, key);
  45. }
  46. /**
  47. * get a config specified by namespace & key from configs loaded from the database
  48. *
  49. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  50. */
  51. getConfigFromDB(namespace, key) {
  52. return this.searchOnlyFromDBConfigs(namespace, key);
  53. }
  54. /**
  55. * get a config specified by namespace & key from configs loaded from the environment variables
  56. *
  57. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  58. */
  59. getConfigFromEnvVars(namespace, key) {
  60. return this.searchOnlyFromEnvVarConfigs(namespace, key);
  61. }
  62. /**
  63. * get the site url
  64. *
  65. * If the config for the site url is not set, this returns a message "[The site URL is not set. Please set it!]".
  66. *
  67. * With version 3.2.3 and below, there is no config for the site URL, so the system always uses auto-generated site URL.
  68. * With version 3.2.4 to 3.3.4, the system uses the auto-generated site URL only if the config is not set.
  69. * With version 3.3.5 and above, the system use only a value from the config.
  70. */
  71. /* eslint-disable no-else-return */
  72. getSiteUrl() {
  73. const siteUrl = this.getConfig('crowi', 'app:siteUrl');
  74. if (siteUrl != null) {
  75. return pathUtils.removeTrailingSlash(siteUrl);
  76. }
  77. else {
  78. return '[The site URL is not set. Please set it!]';
  79. }
  80. }
  81. /* eslint-enable no-else-return */
  82. /**
  83. * update configs in the same namespace
  84. *
  85. * Specified values are encoded by convertInsertValue.
  86. * In it, an empty string is converted to null that indicates a config is not set.
  87. *
  88. * For example:
  89. * ```
  90. * updateConfigsInTheSameNamespace(
  91. * 'some namespace',
  92. * {
  93. * 'some key 1': 'value 1',
  94. * 'some key 2': 'value 2',
  95. * ...
  96. * }
  97. * );
  98. * ```
  99. */
  100. async updateConfigsInTheSameNamespace(namespace, configs) {
  101. const queries = [];
  102. for (const key of Object.keys(configs)) {
  103. queries.push({
  104. updateOne: {
  105. filter: { ns: namespace, key },
  106. update: { ns: namespace, key, value: this.convertInsertValue(configs[key]) },
  107. upsert: true,
  108. },
  109. });
  110. }
  111. await this.configModel.bulkWrite(queries);
  112. await this.loadConfigs();
  113. }
  114. /**
  115. * Execute only once for installing application
  116. */
  117. async initDB(globalLang) {
  118. const initialConfig = this.configModel.getConfigsObjectForInstalling();
  119. initialConfig['app:globalLang'] = globalLang;
  120. await this.updateConfigsInTheSameNamespace('crowi', initialConfig);
  121. }
  122. async isDBInitialized() {
  123. const appInstalled = await this.getConfigFromDB('crowi', 'app:installed');
  124. return appInstalled;
  125. }
  126. /*
  127. * All of the methods below are private APIs.
  128. */
  129. /**
  130. * search a specified config from configs loaded from the database at first
  131. * and then from configs loaded from the environment variables
  132. */
  133. defaultSearch(namespace, key) {
  134. if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  135. return undefined;
  136. }
  137. if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  138. return this.configObject.fromDB[namespace][key];
  139. }
  140. if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
  141. return this.configObject.fromEnvVars[namespace][key];
  142. }
  143. if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
  144. /* eslint-disable no-else-return */
  145. if (this.configObject.fromDB[namespace][key] !== null) {
  146. return this.configObject.fromDB[namespace][key];
  147. }
  148. else {
  149. return this.configObject.fromEnvVars[namespace][key];
  150. }
  151. /* eslint-enable no-else-return */
  152. }
  153. }
  154. /**
  155. * For the configs specified by KEYS_FOR_SAML_USE_ONLY_ENV_OPTION,
  156. * this searches only from configs loaded from the environment variables.
  157. * For the other configs, this searches as the same way to defaultSearch.
  158. */
  159. /* eslint-disable no-else-return */
  160. searchInSAMLUseOnlyEnvMode(namespace, key) {
  161. if (namespace === 'crowi' && KEYS_FOR_SAML_USE_ONLY_ENV_OPTION.includes(key)) {
  162. return this.searchOnlyFromEnvVarConfigs(namespace, key);
  163. }
  164. else {
  165. return this.defaultSearch(namespace, key);
  166. }
  167. }
  168. /* eslint-enable no-else-return */
  169. /**
  170. * search a specified config from configs loaded from the database
  171. */
  172. searchOnlyFromDBConfigs(namespace, key) {
  173. if (!this.configExistsInDB(namespace, key)) {
  174. return undefined;
  175. }
  176. return this.configObject.fromDB[namespace][key];
  177. }
  178. /**
  179. * search a specified config from configs loaded from the environment variables
  180. */
  181. searchOnlyFromEnvVarConfigs(namespace, key) {
  182. if (!this.configExistsInEnvVars(namespace, key)) {
  183. return undefined;
  184. }
  185. return this.configObject.fromEnvVars[namespace][key];
  186. }
  187. /**
  188. * check whether a specified config exists in configs loaded from the database
  189. */
  190. configExistsInDB(namespace, key) {
  191. if (this.configObject.fromDB[namespace] === undefined) {
  192. return false;
  193. }
  194. return this.configObject.fromDB[namespace][key] !== undefined;
  195. }
  196. /**
  197. * check whether a specified config exists in configs loaded from the environment variables
  198. */
  199. configExistsInEnvVars(namespace, key) {
  200. if (this.configObject.fromEnvVars[namespace] === undefined) {
  201. return false;
  202. }
  203. return this.configObject.fromEnvVars[namespace][key] !== undefined;
  204. }
  205. convertInsertValue(value) {
  206. return JSON.stringify(value === '' ? null : value);
  207. }
  208. }
  209. module.exports = ConfigManager;