config-manager.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const debug = require('debug')('growi:service:ConfigManager');
  2. const pathUtils = require('@commons/util/path-utils');
  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. getSiteUrl() {
  72. const siteUrl = this.getConfig('crowi', 'app:siteUrl');
  73. if (siteUrl != null) {
  74. return pathUtils.removeTrailingSlash(siteUrl);
  75. }
  76. else {
  77. return '[The site URL is not set. Please set it!]';
  78. }
  79. }
  80. /**
  81. * update configs in the same namespace
  82. *
  83. * Specified values are encoded by convertInsertValue.
  84. * In it, an empty string is converted to null that indicates a config is not set.
  85. *
  86. * For example:
  87. * ```
  88. * updateConfigsInTheSameNamespace(
  89. * 'some namespace',
  90. * {
  91. * 'some key 1': 'value 1',
  92. * 'some key 2': 'value 2',
  93. * ...
  94. * }
  95. * );
  96. * ```
  97. */
  98. async updateConfigsInTheSameNamespace(namespace, configs) {
  99. let queries = [];
  100. for (const key of Object.keys(configs)) {
  101. queries.push({
  102. updateOne: {
  103. filter: { ns: namespace, key: key },
  104. update: { ns: namespace, key: key, value: this.convertInsertValue(configs[key]) },
  105. upsert: true
  106. }
  107. });
  108. }
  109. await this.configModel.bulkWrite(queries);
  110. await this.loadConfigs();
  111. }
  112. /*
  113. * All of the methods below are private APIs.
  114. */
  115. /**
  116. * search a specified config from configs loaded from the database at first
  117. * and then from configs loaded from the environment variables
  118. */
  119. defaultSearch(namespace, key) {
  120. if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  121. return undefined;
  122. }
  123. if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key) ) {
  124. return this.configObject.fromDB[namespace][key];
  125. }
  126. if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key) ) {
  127. return this.configObject.fromEnvVars[namespace][key];
  128. }
  129. if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key) ) {
  130. if (this.configObject.fromDB[namespace][key] !== null) {
  131. return this.configObject.fromDB[namespace][key];
  132. }
  133. else {
  134. return this.configObject.fromEnvVars[namespace][key];
  135. }
  136. }
  137. }
  138. /**
  139. * For the configs specified by KEYS_FOR_SAML_USE_ONLY_ENV_OPTION,
  140. * this searches only from configs loaded from the environment variables.
  141. * For the other configs, this searches as the same way to defaultSearch.
  142. */
  143. searchInSAMLUseOnlyEnvMode(namespace, key) {
  144. if (namespace === 'crowi' && KEYS_FOR_SAML_USE_ONLY_ENV_OPTION.includes(key)) {
  145. return this.searchOnlyFromEnvVarConfigs(namespace, key);
  146. }
  147. else {
  148. return this.defaultSearch(namespace, key);
  149. }
  150. }
  151. /**
  152. * search a specified config from configs loaded from the database
  153. */
  154. searchOnlyFromDBConfigs(namespace, key) {
  155. if (!this.configExistsInDB(namespace, key)) {
  156. return undefined;
  157. }
  158. return this.configObject.fromDB[namespace][key];
  159. }
  160. /**
  161. * search a specified config from configs loaded from the environment variables
  162. */
  163. searchOnlyFromEnvVarConfigs(namespace, key) {
  164. if (!this.configExistsInEnvVars(namespace, key)) {
  165. return undefined;
  166. }
  167. return this.configObject.fromEnvVars[namespace][key];
  168. }
  169. /**
  170. * check whether a specified config exists in configs loaded from the database
  171. */
  172. configExistsInDB(namespace, key) {
  173. if (this.configObject.fromDB[namespace] === undefined) {
  174. return false;
  175. }
  176. return this.configObject.fromDB[namespace][key] !== undefined;
  177. }
  178. /**
  179. * check whether a specified config exists in configs loaded from the environment variables
  180. */
  181. configExistsInEnvVars(namespace, key) {
  182. if (this.configObject.fromEnvVars[namespace] === undefined) {
  183. return false;
  184. }
  185. return this.configObject.fromEnvVars[namespace][key] !== undefined;
  186. }
  187. convertInsertValue(value) {
  188. return JSON.stringify(value === '' ? null : value);
  189. }
  190. }
  191. module.exports = ConfigManager;