Refactored secret settings util functions

refs https://github.com/TryGhost/Team/issues/694
refs https://linear.app/tryghost/issue/CORE-13

- The index file in services/settings was containning logic and started throwing an additional lint warning due to module length.
- The extracted secret settings utils were used in multiple places and were a good candidate to live in it's own small module
This commit is contained in:
Naz 2021-09-21 12:30:26 +02:00 committed by naz
parent ed56239523
commit 74c15c7b02
3 changed files with 34 additions and 33 deletions

View File

@ -6,23 +6,7 @@ const events = require('../../lib/common/events');
const models = require('../../models');
const SettingsCache = require('../../../shared/settings-cache');
const SettingsBREADService = require('./settings-bread-service');
// The string returned when a setting is set as write-only
const obfuscatedSetting = '••••••••';
// The function used to decide whether a setting is write-only
function isSecretSetting(setting) {
return /secret/.test(setting.key);
}
// The function that obfuscates a write-only setting
// NOTE: move this method into SettingsBREADService once once refactoring is finished
function hideValueIfSecret(setting) {
if (setting.value && isSecretSetting(setting)) {
return {...setting, value: obfuscatedSetting};
}
return setting;
}
const {obfuscatedSetting, isSecretSetting, hideValueIfSecret} = require('./settings-utils');
/**
* @returns {SettingsBREADService} instance of the PostsService

View File

@ -1,28 +1,13 @@
const _ = require('lodash');
const tpl = require('@tryghost/tpl');
const {NotFoundError, NoPermissionError, BadRequestError} = require('@tryghost/errors');
const {obfuscatedSetting, isSecretSetting, hideValueIfSecret} = require('./settings-utils');
const messages = {
problemFindingSetting: 'Problem finding setting: {key}',
accessCoreSettingFromExtReq: 'Attempted to access core setting from external request'
};
// The string returned when a setting is set as write-only
const obfuscatedSetting = '••••••••';
// The function used to decide whether a setting is write-only
function isSecretSetting(setting) {
return /secret/.test(setting.key);
}
// The function that obfuscates a write-only setting
function hideValueIfSecret(setting) {
if (setting.value && isSecretSetting(setting)) {
return {...setting, value: obfuscatedSetting};
}
return setting;
}
class SettingsBREADService {
/**
*

View File

@ -0,0 +1,32 @@
// The string returned when a setting is set as write-only
const obfuscatedSetting = '••••••••';
/**
* @description // The function used to decide whether a setting is write-only
* @param {Object} setting setting record
* @param {String} setting.key
* @returns {Boolean}
*/
function isSecretSetting(setting) {
return /secret/.test(setting.key);
}
/**
* @description The function that obfuscates a write-only setting
* @param {Object} setting setting record
* @param {String} setting.value
* @param {String} setting.key
* @returns {Object} settings record with obfuscated value if it's a secret
*/
function hideValueIfSecret(setting) {
if (setting.value && isSecretSetting(setting)) {
return {...setting, value: obfuscatedSetting};
}
return setting;
}
module.exports = {
obfuscatedSetting,
isSecretSetting,
hideValueIfSecret
};