From 185dd5c17d2d034fc2871500f360418e73eb24da Mon Sep 17 00:00:00 2001 From: beckerinj Date: Thu, 12 May 2022 14:04:56 -0400 Subject: [PATCH] optional custom passkey --- core/src/plugins/db/servers.ts | 1 + core/src/util/periphery/container.ts | 47 ++++++++++--------- core/src/util/periphery/deploy.ts | 4 +- core/src/util/periphery/git.ts | 12 ++--- core/src/util/periphery/networks.ts | 27 ++++++----- core/src/util/periphery/server.ts | 12 ++--- core/src/util/periphery/status.ts | 5 +- .../components/server/tabs/config/Config.tsx | 2 + .../components/server/tabs/config/Passkey.tsx | 27 +++++++++++ types/types.d.ts | 2 + 10 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 frontend/src/components/server/tabs/config/Passkey.tsx diff --git a/core/src/plugins/db/servers.ts b/core/src/plugins/db/servers.ts index c25b140b5..4ab37d33a 100644 --- a/core/src/plugins/db/servers.ts +++ b/core/src/plugins/db/servers.ts @@ -15,6 +15,7 @@ const servers = fp((app: FastifyInstance, _: {}, done: () => void) => { cpuAlert: { type: Number, default: CPU_USAGE_NOTIFY_LIMIT }, memAlert: { type: Number, default: MEM_USAGE_NOTIFY_LIMIT }, diskAlert: { type: Number, default: DISK_USAGE_NOTIFY_LIMIT }, + passkey: String, }); app.decorate("servers", model(app, "Server", schema)); diff --git a/core/src/util/periphery/container.ts b/core/src/util/periphery/container.ts index 975107007..9313216b1 100644 --- a/core/src/util/periphery/container.ts +++ b/core/src/util/periphery/container.ts @@ -1,81 +1,86 @@ -import { Collection, CommandLogError, ContainerStatus, Log, Server } from "@monitor/types"; +import { + Collection, + CommandLogError, + ContainerStatus, + Log, + Server, +} from "@monitor/types"; import { generateQuery } from "@monitor/util"; import axios from "axios"; import { SECRETS } from "../../config"; -export async function getPeripheryContainers({ address }: Server) { - return (await axios.get(`${address}/containers`, { - headers: { - Authorization: SECRETS.PASSKEY, - }, - }).then(({ data }) => data)) as Collection; +export async function getPeripheryContainers({ address, passkey }: Server) { + return (await axios + .get(`${address}/containers`, { + headers: { + Authorization: passkey || SECRETS.PASSKEY, + }, + }) + .then(({ data }) => data)) as Collection; } export async function getPeripheryContainer( - { address }: Server, + { address, passkey }: Server, name: string ) { - return (await axios + return (await axios .get(`${address}/container/${name}`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data)) as ContainerStatus | "not deployed"; } export async function getPeripheryContainerLog( - { address }: Server, + { address, passkey }: Server, name: string, tail?: number ) { return (await axios .get(`${address}/container/log/${name}${generateQuery({ tail })}`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, - }) .then(({ data }) => data)) as Log; } export async function startPeripheryContainer( - { address }: Server, + { address, passkey }: Server, name: string ) { return (await axios .get(`${address}/container/start/${name}`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data)) as CommandLogError; } export async function stopPeripheryContainer( - { address }: Server, + { address, passkey }: Server, name: string ) { return (await axios .get(`${address}/container/stop/${name}`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data)) as CommandLogError; } export async function deletePeripheryContainer( - { address }: Server, + { address, passkey }: Server, name: string ) { return (await axios .get(`${address}/container/delete/${name}`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data)) as CommandLogError; } - - diff --git a/core/src/util/periphery/deploy.ts b/core/src/util/periphery/deploy.ts index 8faa94bf2..1885bb001 100644 --- a/core/src/util/periphery/deploy.ts +++ b/core/src/util/periphery/deploy.ts @@ -4,7 +4,7 @@ import axios from "axios"; import { SECRETS } from "../../config"; export async function deployPeriphery( - { address }: Server, + { address, passkey }: Server, deployment: Deployment, image?: string, dockerAccount?: string @@ -20,7 +20,7 @@ export async function deployPeriphery( }, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, } ) diff --git a/core/src/util/periphery/git.ts b/core/src/util/periphery/git.ts index dd326c625..95c71957c 100644 --- a/core/src/util/periphery/git.ts +++ b/core/src/util/periphery/git.ts @@ -3,7 +3,7 @@ import axios from "axios"; import { SECRETS } from "../../config"; export async function clonePeriphery( - { address }: Server, + { address, passkey }: Server, deployment: Deployment ) { return (await axios @@ -12,7 +12,7 @@ export async function clonePeriphery( { deployment }, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, } ) @@ -20,7 +20,7 @@ export async function clonePeriphery( } export async function pullPeriphery( - { address }: Server, + { address, passkey }: Server, deployment: Deployment ) { return (await axios @@ -29,7 +29,7 @@ export async function pullPeriphery( { deployment }, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, } ) @@ -37,7 +37,7 @@ export async function pullPeriphery( } export async function deleteRepoPeriphery( - { address }: Server, + { address, passkey }: Server, deployment: Deployment ) { return (await axios @@ -46,7 +46,7 @@ export async function deleteRepoPeriphery( { deployment }, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, } ) diff --git a/core/src/util/periphery/networks.ts b/core/src/util/periphery/networks.ts index ed4c210e8..390aefc7f 100644 --- a/core/src/util/periphery/networks.ts +++ b/core/src/util/periphery/networks.ts @@ -3,44 +3,47 @@ import { generateQuery } from "@monitor/util"; import axios from "axios"; import { SECRETS } from "../../config"; -export async function getPeripheryNetworks({ address }: Server) { +export async function getPeripheryNetworks({ address, passkey }: Server) { return await axios .get(`${address}/networks`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data); } -export async function prunePeripheryNetworks({ address }: Server) { +export async function prunePeripheryNetworks({ address, passkey }: Server) { return await axios .get(`${address}/networks/prune`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data); } -export async function createPeripheryNetwork({ address }: Server, name: string, driver?: string) { +export async function createPeripheryNetwork({ address, passkey }: Server, name: string, driver?: string) { return await axios - .get(`${address}/network/create/${name}${generateQuery({ driver })}`, { - headers: { - Authorization: SECRETS.PASSKEY, - }, - }) + .get( + `${address}/network/create/${name}${generateQuery({ driver })}`, + { + headers: { + Authorization: passkey || SECRETS.PASSKEY, + }, + } + ) .then(({ data }) => data); } export async function deletePeripheryNetwork( - { address }: Server, + { address, passkey }: Server, name: string ) { return await axios .get(`${address}/network/delete/${name}`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data); diff --git a/core/src/util/periphery/server.ts b/core/src/util/periphery/server.ts index ca64aa084..919e63a07 100644 --- a/core/src/util/periphery/server.ts +++ b/core/src/util/periphery/server.ts @@ -2,31 +2,31 @@ import { CommandLogError, DockerStat, Server, SystemStats } from "@monitor/types import axios from "axios"; import { SECRETS } from "../../config"; -export async function prunePeripheryImages({ address }: Server) { +export async function prunePeripheryImages({ address, passkey }: Server) { return await axios .get(`${address}/images/prune`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data); } -export async function getPeripheryDockerStats({ address }: Server) { +export async function getPeripheryDockerStats({ address, passkey }: Server) { return await axios .get(`${address}/stats`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data); } -export async function getPeripherySystemStats({ address }: Server) { +export async function getPeripherySystemStats({ address, passkey }: Server) { return await axios .get(`${address}/sys-stats`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, }) .then(({ data }) => data); diff --git a/core/src/util/periphery/status.ts b/core/src/util/periphery/status.ts index 8c49fb17a..60662777f 100644 --- a/core/src/util/periphery/status.ts +++ b/core/src/util/periphery/status.ts @@ -6,6 +6,7 @@ export async function serverStatusPeriphery({ address, enabled, isCore, + passkey, }: Server) { // returns true if can be reached, false else if (isCore) return true; @@ -19,7 +20,7 @@ export async function serverStatusPeriphery({ try { await axios.get(`${address}/status`, { headers: { - Authorization: SECRETS.PASSKEY, + Authorization: passkey || SECRETS.PASSKEY, }, signal: controller.signal, }); @@ -29,4 +30,4 @@ export async function serverStatusPeriphery({ } finally { clearTimeout(timeout); } -} \ No newline at end of file +} diff --git a/frontend/src/components/server/tabs/config/Config.tsx b/frontend/src/components/server/tabs/config/Config.tsx index f4a5dd5e3..14ee16561 100644 --- a/frontend/src/components/server/tabs/config/Config.tsx +++ b/frontend/src/components/server/tabs/config/Config.tsx @@ -7,6 +7,7 @@ import Address from "./Address"; import Alerts from "./Alerts"; import Enabled from "./Enabled"; import Networks from "./Networks"; +import Passkey from "./Passkey"; import { useConfig } from "./Provider"; const Config: Component<{}> = (p) => { @@ -18,6 +19,7 @@ const Config: Component<{}> = (p) => {
+ diff --git a/frontend/src/components/server/tabs/config/Passkey.tsx b/frontend/src/components/server/tabs/config/Passkey.tsx new file mode 100644 index 000000000..aa40b7401 --- /dev/null +++ b/frontend/src/components/server/tabs/config/Passkey.tsx @@ -0,0 +1,27 @@ +import { Component } from "solid-js"; +import { useTheme } from "../../../../state/ThemeProvider"; +import { combineClasses } from "../../../../util/helpers"; +import Input from "../../../util/Input"; +import Flex from "../../../util/layout/Flex"; +import { useConfig } from "./Provider"; + +const Passkey: Component<{}> = (p) => { + const { server, setServer } = useConfig(); + const { themeClass } = useTheme(); + return ( + +

passkey

+ setServer("passkey", value)} + style={{ width: "13rem" }} + /> +
+ ); +}; + +export default Passkey; diff --git a/types/types.d.ts b/types/types.d.ts index b5156ae55..f8c4a676f 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -55,6 +55,8 @@ export type Server = { cpuAlert?: number; // 0 - 100 memAlert?: number; // 0 - 100 diskAlert?: number; // 0 - 100 + // optional custom passkey + passkey?: string; }; export type DockerBuildArgs = {