config-manager.js 6.4 KB

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