config-manager.js 10 KB

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