config-manager.ts 10 KB

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