config-manager.js 9.6 KB

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