config-manager.js 9.5 KB

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