config-manager.js 9.8 KB

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