mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-03 14:03:40 +01:00
Refactor info widget sanitizing / privateOptions
This commit is contained in:
parent
dced918804
commit
48a09e5a99
@ -1,6 +1,6 @@
|
|||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
import createLogger from "utils/logger";
|
import createLogger from "utils/logger";
|
||||||
import { getPrivateWidgetOptions } from "utils/config/service-helpers";
|
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
|
||||||
|
|
||||||
const logger = createLogger("glances");
|
const logger = createLogger("glances");
|
||||||
|
|
||||||
|
@ -4,8 +4,9 @@ import path from "path";
|
|||||||
|
|
||||||
import yaml from "js-yaml";
|
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 { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/config/service-helpers";
|
||||||
|
import { cleanWidgetGroups, widgetsFromConfig } from "utils/config/widget-helpers";
|
||||||
|
|
||||||
export async function bookmarksResponse() {
|
export async function bookmarksResponse() {
|
||||||
checkAndCopyConfig("bookmarks.yaml");
|
checkAndCopyConfig("bookmarks.yaml");
|
||||||
@ -29,24 +30,17 @@ export async function bookmarksResponse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function widgetsResponse() {
|
export async function widgetsResponse() {
|
||||||
checkAndCopyConfig("widgets.yaml");
|
let configuredWidgets;
|
||||||
|
|
||||||
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
|
try {
|
||||||
const fileContents = await fs.readFile(widgetsYaml, "utf8");
|
configuredWidgets = cleanWidgetGroups(await widgetsFromConfig());
|
||||||
const widgets = yaml.load(fileContents);
|
} catch (e) {
|
||||||
|
console.error("Failed to load widgets, please check widgets.yaml for errors or remove example entries.");
|
||||||
|
if (e) console.error(e);
|
||||||
|
configuredWidgets = [];
|
||||||
|
}
|
||||||
|
|
||||||
if (!widgets) return [];
|
return configuredWidgets;
|
||||||
|
|
||||||
// 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]])
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
return widgetsArray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function servicesResponse() {
|
export async function servicesResponse() {
|
||||||
|
@ -34,16 +34,3 @@ export function getSettings() {
|
|||||||
const fileContents = readFileSync(settingsYaml, "utf8");
|
const fileContents = readFileSync(settingsYaml, "utf8");
|
||||||
return yaml.load(fileContents);
|
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 Docker from "dockerode";
|
||||||
import * as shvl from "shvl";
|
import * as shvl from "shvl";
|
||||||
|
|
||||||
import checkAndCopyConfig, { sanitizePrivateOptions } from "utils/config/config";
|
import checkAndCopyConfig from "utils/config/config";
|
||||||
import getDockerArguments from "utils/config/docker";
|
import getDockerArguments from "utils/config/docker";
|
||||||
|
|
||||||
export async function servicesFromConfig() {
|
export async function servicesFromConfig() {
|
||||||
@ -166,22 +166,3 @@ export default async function getServiceWidget(group, service) {
|
|||||||
|
|
||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
|
|
73
src/utils/config/widget-helpers.js
Normal file
73
src/utils/config/widget-helpers.js
Normal file
@ -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 { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
|
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 createLogger from "utils/logger";
|
||||||
import widgets from "widgets/widgets";
|
import widgets from "widgets/widgets";
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user