Skip to content

Commit

Permalink
Refactor info widget sanitizing / privateOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
shamoon committed Oct 12, 2022
1 parent dced918 commit 48a09e5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/pages/api/widgets/glances.js
Original file line number Diff line number Diff line change
@@ -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");

Expand Down
28 changes: 11 additions & 17 deletions src/utils/config/api-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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() {
Expand Down
13 changes: 0 additions & 13 deletions src/utils/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
23 changes: 2 additions & 21 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}

}
73 changes: 73 additions & 0 deletions src/utils/config/widget-helpers.js
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 1 deletion src/widgets/unifi/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit 48a09e5

Please sign in to comment.