Refactor info widget sanitizing / privateOptions

pull/377/head
Michael Shamoon 2 years ago
parent dced918804
commit 48a09e5a99

@ -1,6 +1,6 @@
import { httpProxy } from "utils/proxy/http";
import createLogger from "utils/logger";
import { getPrivateWidgetOptions } from "utils/config/service-helpers";
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
const logger = createLogger("glances");

@ -4,8 +4,9 @@ import path from "path";
import yaml from "js-yaml";
import checkAndCopyConfig, { sanitizePrivateOptions } from "utils/config/config";
import checkAndCopyConfig from "utils/config/config";
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/config/service-helpers";
import { cleanWidgetGroups, widgetsFromConfig } from "utils/config/widget-helpers";
export async function bookmarksResponse() {
checkAndCopyConfig("bookmarks.yaml");
@ -29,24 +30,17 @@ export async function bookmarksResponse() {
}
export async function widgetsResponse() {
checkAndCopyConfig("widgets.yaml");
let configuredWidgets;
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const widgets = yaml.load(fileContents);
if (!widgets) return [];
// map easy to write YAML objects into easy to consume JS arrays
const widgetsArray = widgets.map((group, index) => ({
type: Object.keys(group)[0],
options: {
index,
...sanitizePrivateOptions(group[Object.keys(group)[0]])
},
}));
try {
configuredWidgets = cleanWidgetGroups(await widgetsFromConfig());
} catch (e) {
console.error("Failed to load widgets, please check widgets.yaml for errors or remove example entries.");
if (e) console.error(e);
configuredWidgets = [];
}
return widgetsArray;
return configuredWidgets;
}
export async function servicesResponse() {

@ -33,17 +33,4 @@ export function getSettings() {
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
const fileContents = readFileSync(settingsYaml, "utf8");
return yaml.load(fileContents);
}
export function sanitizePrivateOptions(options, privateOnly = false) {
const privateOptions = ["url", "username", "password", "key"];
const sanitizedOptions = {};
Object.keys(options).forEach((key) => {
if (!privateOnly && !privateOptions.includes(key)) {
sanitizedOptions[key] = options[key];
} else if (privateOnly && privateOptions.includes(key)) {
sanitizedOptions[key] = options[key];
}
});
return sanitizedOptions;
}

@ -5,7 +5,7 @@ import yaml from "js-yaml";
import Docker from "dockerode";
import * as shvl from "shvl";
import checkAndCopyConfig, { sanitizePrivateOptions } from "utils/config/config";
import checkAndCopyConfig from "utils/config/config";
import getDockerArguments from "utils/config/docker";
export async function servicesFromConfig() {
@ -165,23 +165,4 @@ export default async function getServiceWidget(group, service) {
}
return false;
}
export async function getPrivateWidgetOptions(type, index) {
checkAndCopyConfig("widgets.yaml");
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const widgets = yaml.load(fileContents);
if (!widgets) return [];
const privateOptions = widgets.map((group, widgetIndex) => ({
type: Object.keys(group)[0],
index: widgetIndex,
options: sanitizePrivateOptions(group[Object.keys(group)[0]], true),
}));
return (type !== undefined && index !== undefined) ? privateOptions.find(o => o.type === type && o.index === parseInt(index, 10))?.options : privateOptions;
}
}

@ -0,0 +1,73 @@
import { promises as fs } from "fs";
import path from "path";
import yaml from "js-yaml";
import checkAndCopyConfig from "utils/config/config";
export async function widgetsFromConfig() {
checkAndCopyConfig("widgets.yaml");
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const widgets = yaml.load(fileContents);
if (!widgets) return [];
// map easy to write YAML objects into easy to consume JS arrays
const widgetsArray = widgets.map((group, index) => ({
type: Object.keys(group)[0],
options: {
index,
...group[Object.keys(group)[0]]
},
}));
return widgetsArray;
}
export async function cleanWidgetGroups(widgets) {
return widgets.map((widget, index) => {
const sanitizedOptions = widget.options;
const optionKeys = Object.keys(sanitizedOptions);
["url", "username", "password", "key"].forEach((pO) => {
if (optionKeys.includes(pO)) {
delete sanitizedOptions[pO];
}
});
return {
type: widget.type,
options: {
index,
...sanitizedOptions
},
}
});
}
export async function getPrivateWidgetOptions(type, widgetIndex) {
const widgets = await widgetsFromConfig();
const privateOptions = widgets.map((widget) => {
const {
index,
url,
username,
password,
key
} = widget.options;
return {
type: widget.type,
options: {
index,
url,
username,
password,
key
},
}
});
return (type !== undefined && widgetIndex !== undefined) ? privateOptions.find(o => o.type === type && o.options.index === parseInt(widgetIndex, 10))?.options : privateOptions;
}

@ -3,7 +3,8 @@ import cache from "memory-cache";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
import getServiceWidget, { getPrivateWidgetOptions } from "utils/config/service-helpers";
import getServiceWidget from "utils/config/service-helpers";
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";

Loading…
Cancel
Save