config-manager.js 5.7 KB

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