Files
vault/ui/lib/core/addon/utils/advanced-secret.js
Chelsea Shaw f0d8dab056 UI: Obscure values for nested KV v2 secret (#24530)
* Add obfuscateData method and tests

* add obscure option to JsonEditor + tests

* Enable obscured values for KV v2 details when secret is advanced

* coverage on kv acceptance test

* Add changelog
2023-12-14 19:55:45 +00:00

41 lines
1.0 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
/**
* Method to check whether the secret value is a nested object (returns true)
* All other values return false
* @param value string or stringified JSON
* @returns boolean
*/
export function isAdvancedSecret(value) {
try {
const json = JSON.parse(value);
if (Array.isArray(json)) return false;
return Object.values(json).some((value) => typeof value !== 'string');
} catch (e) {
return false;
}
}
/**
* Method to obfuscate all values in a map, including nested values and arrays
* @param obj object
* @returns object
*/
export function obfuscateData(obj) {
if (typeof obj !== 'object' || Array.isArray(obj)) return obj;
const newObj = {};
for (const key of Object.keys(obj)) {
if (Array.isArray(obj[key])) {
newObj[key] = obj[key].map(() => '********');
} else if (typeof obj[key] === 'object') {
newObj[key] = obfuscateData(obj[key]);
} else {
newObj[key] = '********';
}
}
return newObj;
}