config-manager.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. const logger = require('@alias/logger')('growi:service:ConfigManager');
  2. const ConfigLoader = require('./config-loader');
  3. const KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION = [
  4. 'security:passport-local:isEnabled',
  5. ];
  6. const KEYS_FOR_SAML_USE_ONLY_ENV_OPTION = [
  7. 'security:passport-saml:isEnabled',
  8. 'security:passport-saml:entryPoint',
  9. 'security:passport-saml:issuer',
  10. 'security:passport-saml:attrMapId',
  11. 'security:passport-saml:attrMapUsername',
  12. 'security:passport-saml:attrMapMail',
  13. 'security:passport-saml:attrMapFirstName',
  14. 'security:passport-saml:attrMapLastName',
  15. 'security:passport-saml:cert',
  16. 'security:passport-saml:ABLCRule',
  17. ];
  18. class ConfigManager {
  19. constructor(configModel) {
  20. this.configModel = configModel;
  21. this.configLoader = new ConfigLoader(this.configModel);
  22. this.configObject = null;
  23. this.configKeys = [];
  24. this.getConfig = this.getConfig.bind(this);
  25. }
  26. /**
  27. * load configs from the database and the environment variables
  28. */
  29. async loadConfigs() {
  30. this.configObject = await this.configLoader.load();
  31. logger.debug('ConfigManager#loadConfigs', this.configObject);
  32. // cache all config keys
  33. this.reloadConfigKeys();
  34. }
  35. /**
  36. * Set ConfigPubsubDelegator instance
  37. * @param {ConfigPubsubDelegator} configPubsub
  38. */
  39. async setPubsub(configPubsub) {
  40. this.configPubsub = configPubsub;
  41. this.configPubsub.addMessageHandler((message) => {
  42. logger.info('message recieved', message);
  43. });
  44. }
  45. /**
  46. * get a config specified by namespace & key
  47. *
  48. * Basically, this searches a specified config from configs loaded from the database at first
  49. * and then from configs loaded from the environment variables.
  50. *
  51. * In some case, this search method changes.
  52. *
  53. * the followings are the meanings of each special return value.
  54. * - null: a specified config is not set.
  55. * - undefined: a specified config does not exist.
  56. */
  57. getConfig(namespace, key) {
  58. let value;
  59. if (this.shouldSearchedFromEnvVarsOnly(namespace, key)) {
  60. value = this.searchOnlyFromEnvVarConfigs(namespace, key);
  61. }
  62. else {
  63. value = this.defaultSearch(namespace, key);
  64. }
  65. logger.debug(key, value);
  66. return value;
  67. }
  68. /**
  69. * get a config specified by namespace and regular expression
  70. */
  71. getConfigByRegExp(namespace, regexp) {
  72. const result = {};
  73. for (const key of this.configKeys) {
  74. if (regexp.test(key)) {
  75. result[key] = this.getConfig(namespace, key);
  76. }
  77. }
  78. return result;
  79. }
  80. /**
  81. * get a config specified by namespace and prefix
  82. */
  83. getConfigByPrefix(namespace, prefix) {
  84. const regexp = new RegExp(`^${prefix}`);
  85. return this.getConfigByRegExp(namespace, regexp);
  86. }
  87. /**
  88. * generate an array of config keys from this.configObject
  89. */
  90. getConfigKeys() {
  91. // type: fromDB, fromEnvVars
  92. const types = Object.keys(this.configObject);
  93. let namespaces = [];
  94. let keys = [];
  95. for (const type of types) {
  96. if (this.configObject[type] != null) {
  97. // ns: crowi, markdown, notification
  98. namespaces = [...namespaces, ...Object.keys(this.configObject[type])];
  99. }
  100. }
  101. // remove duplicates
  102. namespaces = [...new Set(namespaces)];
  103. for (const type of types) {
  104. for (const ns of namespaces) {
  105. if (this.configObject[type][ns] != null) {
  106. keys = [...keys, ...Object.keys(this.configObject[type][ns])];
  107. }
  108. }
  109. }
  110. // remove duplicates
  111. keys = [...new Set(keys)];
  112. return keys;
  113. }
  114. reloadConfigKeys() {
  115. this.configKeys = this.getConfigKeys();
  116. }
  117. /**
  118. * get a config specified by namespace & key from configs loaded from the database
  119. *
  120. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  121. */
  122. getConfigFromDB(namespace, key) {
  123. return this.searchOnlyFromDBConfigs(namespace, key);
  124. }
  125. /**
  126. * get a config specified by namespace & key from configs loaded from the environment variables
  127. *
  128. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  129. */
  130. getConfigFromEnvVars(namespace, key) {
  131. return this.searchOnlyFromEnvVarConfigs(namespace, key);
  132. }
  133. /**
  134. * update configs in the same namespace
  135. *
  136. * Specified values are encoded by convertInsertValue.
  137. * In it, an empty string is converted to null that indicates a config is not set.
  138. *
  139. * For example:
  140. * ```
  141. * updateConfigsInTheSameNamespace(
  142. * 'some namespace',
  143. * {
  144. * 'some key 1': 'value 1',
  145. * 'some key 2': 'value 2',
  146. * ...
  147. * }
  148. * );
  149. * ```
  150. */
  151. async updateConfigsInTheSameNamespace(namespace, configs) {
  152. const queries = [];
  153. for (const key of Object.keys(configs)) {
  154. queries.push({
  155. updateOne: {
  156. filter: { ns: namespace, key },
  157. update: { ns: namespace, key, value: this.convertInsertValue(configs[key]) },
  158. upsert: true,
  159. },
  160. });
  161. }
  162. await this.configModel.bulkWrite(queries);
  163. await this.loadConfigs();
  164. this.reloadConfigKeys();
  165. }
  166. /**
  167. * return whether the specified namespace/key should be retrieved only from env vars
  168. */
  169. shouldSearchedFromEnvVarsOnly(namespace, key) {
  170. return (namespace === 'crowi' && (
  171. // local strategy
  172. (
  173. KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION.includes(key)
  174. && this.defaultSearch('crowi', 'security:passport-local:useOnlyEnvVarsForSomeOptions')
  175. )
  176. // saml strategy
  177. || (
  178. KEYS_FOR_SAML_USE_ONLY_ENV_OPTION.includes(key)
  179. && this.defaultSearch('crowi', 'security:passport-saml:useOnlyEnvVarsForSomeOptions')
  180. )
  181. ));
  182. }
  183. /*
  184. * All of the methods below are private APIs.
  185. */
  186. /**
  187. * search a specified config from configs loaded from the database at first
  188. * and then from configs loaded from the environment variables
  189. */
  190. defaultSearch(namespace, key) {
  191. // does not exist neither in db nor in env vars
  192. if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  193. logger.debug(`${namespace}.${key} does not exist neither in db nor in env vars`);
  194. return undefined;
  195. }
  196. // only exists in db
  197. if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
  198. logger.debug(`${namespace}.${key} only exists in db`);
  199. return this.configObject.fromDB[namespace][key];
  200. }
  201. // only exists env vars
  202. if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
  203. logger.debug(`${namespace}.${key} only exists in env vars`);
  204. return this.configObject.fromEnvVars[namespace][key];
  205. }
  206. // exists both in db and in env vars [db > env var]
  207. if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
  208. if (this.configObject.fromDB[namespace][key] !== null) {
  209. logger.debug(`${namespace}.${key} exists both in db and in env vars. loaded from db`);
  210. return this.configObject.fromDB[namespace][key];
  211. }
  212. /* eslint-disable-next-line no-else-return */
  213. else {
  214. logger.debug(`${namespace}.${key} exists both in db and in env vars. loaded from env vars`);
  215. return this.configObject.fromEnvVars[namespace][key];
  216. }
  217. }
  218. }
  219. /**
  220. * search a specified config from configs loaded from the database
  221. */
  222. searchOnlyFromDBConfigs(namespace, key) {
  223. if (!this.configExistsInDB(namespace, key)) {
  224. return undefined;
  225. }
  226. return this.configObject.fromDB[namespace][key];
  227. }
  228. /**
  229. * search a specified config from configs loaded from the environment variables
  230. */
  231. searchOnlyFromEnvVarConfigs(namespace, key) {
  232. if (!this.configExistsInEnvVars(namespace, key)) {
  233. return undefined;
  234. }
  235. return this.configObject.fromEnvVars[namespace][key];
  236. }
  237. /**
  238. * check whether a specified config exists in configs loaded from the database
  239. */
  240. configExistsInDB(namespace, key) {
  241. if (this.configObject.fromDB[namespace] === undefined) {
  242. return false;
  243. }
  244. return this.configObject.fromDB[namespace][key] !== undefined;
  245. }
  246. /**
  247. * check whether a specified config exists in configs loaded from the environment variables
  248. */
  249. configExistsInEnvVars(namespace, key) {
  250. if (this.configObject.fromEnvVars[namespace] === undefined) {
  251. return false;
  252. }
  253. return this.configObject.fromEnvVars[namespace][key] !== undefined;
  254. }
  255. convertInsertValue(value) {
  256. return JSON.stringify(value === '' ? null : value);
  257. }
  258. }
  259. module.exports = ConfigManager;