From 7476de83a43bcd14a2d3a1d0be0f2e5ad6452fa3 Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Fri, 7 Jan 2022 15:27:10 +0000 Subject: [PATCH] move all api functions into their own file, use that instead of globally --- src/api/API.ts | 633 ++++++++++++++++++ src/api/auth/listAuth.ts | 13 - src/api/auth/usernameLogin.ts | 19 - .../userpass/createOrUpdateUserPassUser.ts | 23 - src/api/auth/userpass/deleteUserPassUser.ts | 14 - src/api/auth/userpass/getUserPassUser.ts | 13 - src/api/auth/userpass/listUserPassUsers.ts | 12 - src/api/kv/createOrUpdateSecret.ts | 34 - src/api/kv/deleteSecret.ts | 26 - src/api/kv/getSecret.ts | 52 -- src/api/kv/getSecretMetadata.ts | 24 - src/api/kv/getSecrets.ts | 24 - src/api/kv/undeleteSecret.ts | 29 - src/api/sys/deleteMount.ts | 10 - src/api/sys/getCapabilities.ts | 43 -- src/api/sys/getMounts.ts | 34 - src/api/sys/getSealStatus.ts | 16 - src/api/sys/lookupSelf.ts | 13 - src/api/sys/newMount.ts | 23 - src/api/sys/policies/createOrUpdatePolicy.ts | 15 - src/api/sys/policies/deletePolicy.ts | 10 - src/api/sys/policies/getPolicies.ts | 12 - src/api/sys/policies/getPolicy.ts | 12 - src/api/sys/renewSelf.ts | 14 - src/api/sys/sealVault.ts | 10 - src/api/sys/submitUnsealKey.ts | 15 - src/api/totp/addNewTOTP.ts | 18 - src/api/totp/deleteTOTP.ts | 10 - src/api/totp/getTOTPCode.ts | 13 - src/api/totp/getTOTPKeys.ts | 13 - src/api/transit/getTransitKey.ts | 14 - src/api/transit/getTransitKeys.ts | 13 - src/api/transit/newTransitKey.ts | 21 - src/api/transit/transitDecrypt.ts | 34 - src/api/transit/transitEncrypt.ts | 34 - src/api/transit/transitRewrap.ts | 35 - src/api/types/capabilities.ts | 4 + src/api/types/mount.ts | 18 + src/api/types/seal.ts | 5 + src/api/types/secret.ts | 3 + src/api/types/transit.ts | 25 + src/api/types/{userpass => }/user.ts | 0 src/globalAPI.ts | 4 + src/main.tsx | 29 +- src/pageUtils.ts | 13 +- src/pages.tsx | 121 ++-- src/types/DefaultPageProps.ts | 2 + src/ui/pages/Access/AccessHome.tsx | 2 +- src/ui/pages/Access/Auth/AuthHome.tsx | 3 +- src/ui/pages/Access/Auth/AuthViewConfig.tsx | 3 +- .../Auth/userpass/UserPassUserDelete.tsx | 3 +- .../Access/Auth/userpass/UserPassUserEdit.tsx | 10 +- .../Access/Auth/userpass/UserPassUserNew.tsx | 11 +- .../Access/Auth/userpass/UserPassUserView.tsx | 5 +- .../Auth/userpass/UserPassUsersList.tsx | 3 +- src/ui/pages/Home.tsx | 8 +- src/ui/pages/Login.tsx | 10 +- src/ui/pages/Me.tsx | 9 +- src/ui/pages/Policies/PoliciesHome.tsx | 5 +- src/ui/pages/Policies/PolicyDelete.tsx | 5 +- src/ui/pages/Policies/PolicyEdit.tsx | 14 +- src/ui/pages/Policies/PolicyNew.tsx | 6 +- src/ui/pages/Policies/PolicyView.tsx | 5 +- src/ui/pages/PwGen.tsx | 3 +- src/ui/pages/Secrets/DeleteSecretsEngine.tsx | 3 +- .../pages/Secrets/KeyValue/KeyValueDelete.tsx | 3 +- .../pages/Secrets/KeyValue/KeyValueEdit.tsx | 38 +- .../pages/Secrets/KeyValue/KeyValueList.tsx | 20 +- src/ui/pages/Secrets/KeyValue/KeyValueNew.tsx | 3 +- .../pages/Secrets/KeyValue/KeyValueView.tsx | 7 +- .../pages/Secrets/NewEngines/NewKVEngine.tsx | 6 +- .../Secrets/NewEngines/NewTOTPEngine.tsx | 6 +- .../Secrets/NewEngines/NewTransitEngine.tsx | 6 +- src/ui/pages/Secrets/NewSecretsEngine.tsx | 3 +- src/ui/pages/Secrets/SecretsHome.tsx | 13 +- src/ui/pages/Secrets/TOTP/TOTPDelete.tsx | 3 +- src/ui/pages/Secrets/TOTP/TOTPList.tsx | 25 +- src/ui/pages/Secrets/TOTP/TOTPNew.tsx | 10 +- .../pages/Secrets/Transit/TransitDecrypt.tsx | 3 +- .../pages/Secrets/Transit/TransitEncrypt.tsx | 3 +- src/ui/pages/Secrets/Transit/TransitList.tsx | 21 +- src/ui/pages/Secrets/Transit/TransitNew.tsx | 3 +- .../pages/Secrets/Transit/TransitRewrap.tsx | 6 +- src/ui/pages/Secrets/Transit/TransitView.tsx | 3 +- src/ui/pages/Unseal.tsx | 6 +- 85 files changed, 938 insertions(+), 930 deletions(-) create mode 100644 src/api/API.ts delete mode 100644 src/api/auth/listAuth.ts delete mode 100644 src/api/auth/usernameLogin.ts delete mode 100644 src/api/auth/userpass/createOrUpdateUserPassUser.ts delete mode 100644 src/api/auth/userpass/deleteUserPassUser.ts delete mode 100644 src/api/auth/userpass/getUserPassUser.ts delete mode 100644 src/api/auth/userpass/listUserPassUsers.ts delete mode 100644 src/api/kv/createOrUpdateSecret.ts delete mode 100644 src/api/kv/deleteSecret.ts delete mode 100644 src/api/kv/getSecret.ts delete mode 100644 src/api/kv/getSecretMetadata.ts delete mode 100644 src/api/kv/getSecrets.ts delete mode 100644 src/api/kv/undeleteSecret.ts delete mode 100644 src/api/sys/deleteMount.ts delete mode 100644 src/api/sys/getCapabilities.ts delete mode 100644 src/api/sys/getMounts.ts delete mode 100644 src/api/sys/getSealStatus.ts delete mode 100644 src/api/sys/lookupSelf.ts delete mode 100644 src/api/sys/newMount.ts delete mode 100644 src/api/sys/policies/createOrUpdatePolicy.ts delete mode 100644 src/api/sys/policies/deletePolicy.ts delete mode 100644 src/api/sys/policies/getPolicies.ts delete mode 100644 src/api/sys/policies/getPolicy.ts delete mode 100644 src/api/sys/renewSelf.ts delete mode 100644 src/api/sys/sealVault.ts delete mode 100644 src/api/sys/submitUnsealKey.ts delete mode 100644 src/api/totp/addNewTOTP.ts delete mode 100644 src/api/totp/deleteTOTP.ts delete mode 100644 src/api/totp/getTOTPCode.ts delete mode 100644 src/api/totp/getTOTPKeys.ts delete mode 100644 src/api/transit/getTransitKey.ts delete mode 100644 src/api/transit/getTransitKeys.ts delete mode 100644 src/api/transit/newTransitKey.ts delete mode 100644 src/api/transit/transitDecrypt.ts delete mode 100644 src/api/transit/transitEncrypt.ts delete mode 100644 src/api/transit/transitRewrap.ts create mode 100644 src/api/types/capabilities.ts create mode 100644 src/api/types/mount.ts create mode 100644 src/api/types/seal.ts create mode 100644 src/api/types/secret.ts rename src/api/types/{userpass => }/user.ts (100%) create mode 100644 src/globalAPI.ts diff --git a/src/api/API.ts b/src/api/API.ts new file mode 100644 index 0000000..89a7223 --- /dev/null +++ b/src/api/API.ts @@ -0,0 +1,633 @@ +import { Settings } from "../settings/Settings"; +import { DoesNotExistError } from "../types/internalErrors"; +import { removeDoubleSlash } from "../utils"; +import { BaseAPIResponse } from "./types/api"; +import { AuthListAPIType, AuthListType } from "./types/auth"; +import { CapabilitiesType } from "./types/capabilities"; +import { MountsType, MountType, NewMountParams } from "./types/mount"; +import { SealStatusType } from "./types/seal"; +import { TokenInfo } from "./types/token"; +import { + DecryptionPayload, + DecryptionResult, + EncryptionPayload, + EncryptionResult, + RewrapPayload, + RewrapResult, + TransitKeyType, +} from "./types/transit"; +import { UserType, UserTypeAPIResp } from "./types/user"; + +async function checkResponse(resp: Response): Promise { + if (resp.ok) return; + if (resp.status == 404) throw DoesNotExistError; + + let json: BaseAPIResponse; + try { + json = (await resp.json()) as BaseAPIResponse; + } catch { + // Do Nothing + } + + if (json?.errors?.length >= 1) { + throw new Error(json.errors[0]); + } +} + +export class API { + private settings: Settings; + + constructor(settings: Settings) { + this.settings = settings; + } + + getHeaders(): Record { + return { + "X-Vault-Token": this.settings.token, + }; + } + appendAPIURL(url: string): string { + return this.settings.apiURL + url; + } + + // List all supported auth methods + async listAuth(): Promise { + const request = new Request(this.appendAPIURL(`/v1/sys/auth`), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as AuthListAPIType; + return data.data; + } + + // Tries to login with username and password, returns token + async usernameLogin(username: string, password: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/auth/userpass/login/${username}`), { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ username: username, password: password }), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { + auth: { client_token: string }; + }; + return data.auth.client_token; + } + + async createOrUpdateUserPassUser( + path: string, + username: string, + data: Partial, + ): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/auth/${path}/users/${username}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(data, null, 0), + }, + ); + const resp = await fetch(request); + await checkResponse(resp); + } + + async deleteUserPassUser(path: string, username: string): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/auth/${path}/users/${username}`)), + { + method: "DELETE", + headers: this.getHeaders(), + }, + ); + const resp = await fetch(request); + await checkResponse(resp); + } + + async getUserPassUser(path: string, username: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/auth/${path}/users/${username}`), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as UserTypeAPIResp; + return data.data; + } + + async listUserPassUsers(path: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/auth/${path}/users?list=true`), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { keys: string[] } }; + return data.data.keys; + } + + async getMount(mountName: string): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/internal/ui/mounts/" + mountName), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: MountType }; + return data.data; + } + + async getMounts(): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/internal/ui/mounts"), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { secret: MountsType } }; + return data.data.secret; + } + + async newMount(parms: NewMountParams): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/sys/mounts/${parms.name}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(parms), + }, + ); + const resp = await fetch(request); + await checkResponse(resp); + } + + async deleteMount(mountPath: string): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/mounts/" + mountPath), { + method: "DELETE", + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async getCapabilitiesPath(path: string | string[]): Promise { + if (!Array.isArray(path)) { + path = [path]; + } + + const request = new Request(this.appendAPIURL("/v1/sys/capabilities-self"), { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify({ + paths: path, + }), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { capabilities: string[] }; + return data; + } + + async getCapsPath(path: string | string[]): Promise { + return (await this.getCapabilitiesPath(path)).capabilities; + } + + async getCapabilities( + baseMount: string, + secretPath: string[], + name: string, + ): Promise { + return await this.getCapabilitiesPath( + removeDoubleSlash(baseMount + secretPath.join("/") + "/" + name), + ); + } + + async sealVault(): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/seal"), { + method: "PUT", + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async submitUnsealKey(key: string): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/unseal"), { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + key: key, + }), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async getSealStatus(): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/seal-status")); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as SealStatusType; + return data; + } + + async lookupSelf(): Promise { + const request = new Request(this.appendAPIURL("/v1/auth/token/lookup-self"), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: TokenInfo }; + return data.data; + } + + async renewSelf(): Promise { + const request = new Request(this.appendAPIURL("/v1/auth/token/renew-self"), { + method: "POST", + headers: { + ...this.getHeaders(), + "Content-Type": "application/json", + }, + body: JSON.stringify({}), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async createOrUpdatePolicy(name: string, policy_data: string): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/policies/acl/" + name), { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify({ policy: policy_data }, null, 0), + }); + + const resp = await fetch(request); + await checkResponse(resp); + } + + async deletePolicy(name: string): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/policies/acl/" + name), { + method: "DELETE", + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async getPolicies(): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/policies/acl?list=true"), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { keys: string[] } }; + return data.data.keys; + } + + async getPolicy(name: string): Promise { + const request = new Request(this.appendAPIURL("/v1/sys/policies/acl/" + name), { + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { policy: string } }; + return data.data.policy; + } + + async createOrUpdateSecret( + baseMount: string, + secretPath: string[], + name: string, + data: Record, + ): Promise { + let secretURL = ""; + let APIData = {}; + + const mountInfo = await this.getMount(baseMount); + if (mountInfo.options.version == "2") { + secretURL = `/v1/${baseMount}/data/${secretPath.join("/")}/${name}`; + APIData = { data: data }; + } else { + secretURL = `/v1/${baseMount}/${secretPath.join("/")}/${name}`; + APIData = data; + } + + secretURL = removeDoubleSlash(secretURL).replace(/\/$/, ""); + const request = new Request(this.appendAPIURL(secretURL), { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(APIData, null, 0), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async deleteSecret(baseMount: string, secretPath: string[], name: string): Promise { + let secretURL = ""; + let request; + + const mountInfo = await this.getMount(baseMount); + if (mountInfo.options.version == "2") { + secretURL = `/v1/${baseMount}/metadata/${secretPath.join("/")}/${name}`; + } else { + secretURL = `/v1/${baseMount}/${secretPath.join("/")}/${name}`; + } + secretURL = removeDoubleSlash(secretURL).replace(/\/$/, ""); + request = new Request(this.appendAPIURL(secretURL), { + method: "DELETE", + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async getSecretKV1( + baseMount: string, + secretPath: string[], + name: string, + ): Promise> { + const request = new Request( + this.appendAPIURL(`/v1/${baseMount}/${secretPath.join("")}/${name}`), + { + headers: this.getHeaders(), + }, + ); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as unknown; + + return (data as { data: Record }).data; + } + + async getSecretKV2( + baseMount: string, + secretPath: string[], + name: string, + ): Promise> { + let secretURL = ""; + + secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`; + + const request = new Request(this.appendAPIURL(secretURL), { + headers: this.getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as unknown; + return (data as { data: { data: Record } }).data.data; + } + + async getSecret( + baseMount: string, + secretPath: string[], + name: string, + ): Promise> { + const mountInfo = await this.getMount(baseMount); + if (mountInfo.options.version == "2") { + return await this.getSecretKV2(baseMount, secretPath, name); + } else { + return await this.getSecretKV1(baseMount, secretPath, name); + } + } + + async getSecretMetadata( + baseMount: string, + secretPath: string[], + name: string, + ): Promise { + const request = new Request( + this.appendAPIURL(`/v1/${baseMount}/metadata/${secretPath.join("")}/${name}`), + { + headers: this.getHeaders(), + }, + ); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: SecretMetadataType }; + return data.data; + } + + async getSecrets(baseMount: string, secretPath: string[]): Promise { + let secretURL = ""; + + const mountInfo = await this.getMount(baseMount); + if (mountInfo.options.version == "2") { + secretURL = `/v1/${baseMount}/metadata/${secretPath.join("/")}?list=true`; + } else { + secretURL = `/v1/${baseMount}/${secretPath.join("/")}?list=true`; + } + const request = new Request(this.appendAPIURL(secretURL), { + headers: this.getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { keys: string[] } }; + return data.data.keys; + } + + async addNewTOTP(baseMount: string, parms: { name: string }): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/keys/${parms.name}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(parms), + }, + ); + const resp = await fetch(request); + await checkResponse(resp); + } + + async deleteTOTP(baseMount: string, name: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/${baseMount}/keys/${name}`), { + method: "DELETE", + headers: this.getHeaders(), + }); + const resp = await fetch(request); + await checkResponse(resp); + } + + async getTOTPCode(baseMount: string, name: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/${baseMount}/code/${name}`), { + headers: this.getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { code: string } }; + return data.data.code; + } + + async getTOTPKeys(baseMount: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/${baseMount}/keys?list=true`), { + headers: this.getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { keys: string[] } }; + return data.data.keys; + } + + async getTransitKey(baseMount: string, name: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/${baseMount}/keys/${name}`), { + headers: this.getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: TransitKeyType }; + return data.data; + } + + async getTransitKeys(baseMount: string): Promise { + const request = new Request(this.appendAPIURL(`/v1/${baseMount}/keys?list=true`), { + headers: this.getHeaders(), + }); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { data: { keys: string[] } }; + return data.data.keys; + } + + async newTransitKey(baseMount: string, parms: { name: string; type: string }): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/keys/${parms.name}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(parms), + }, + ); + const resp = await fetch(request); + await checkResponse(resp); + } + + async transitDecrypt( + baseMount: string, + name: string, + payload: DecryptionPayload, + ): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(payload), + }, + ); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { + data?: DecryptionResult; + }; + + return data.data; + } + + async transitEncrypt( + baseMount: string, + name: string, + payload: EncryptionPayload, + ): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(payload), + }, + ); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { + data?: EncryptionResult; + }; + + return data.data; + } + + async transitRewrap( + baseMount: string, + name: string, + payload: RewrapPayload, + ): Promise { + const request = new Request( + this.appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/rewrap/${name}`)), + { + method: "POST", + headers: { + "Content-Type": "application/json", + ...this.getHeaders(), + }, + body: JSON.stringify(payload), + }, + ); + + const resp = await fetch(request); + await checkResponse(resp); + + const data = (await resp.json()) as { + data?: RewrapResult; + }; + + return data.data; + } +} diff --git a/src/api/auth/listAuth.ts b/src/api/auth/listAuth.ts deleted file mode 100644 index 8267bac..0000000 --- a/src/api/auth/listAuth.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { AuthListAPIType, AuthListType } from "../types/auth"; -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function listAuth(): Promise { - const request = new Request(appendAPIURL(`/v1/sys/auth`), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as AuthListAPIType; - return data.data; -} diff --git a/src/api/auth/usernameLogin.ts b/src/api/auth/usernameLogin.ts deleted file mode 100644 index 5384c8a..0000000 --- a/src/api/auth/usernameLogin.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { appendAPIURL, checkResponse } from "../apiUtils"; - -export async function usernameLogin(username: string, password: string): Promise { - const request = new Request(appendAPIURL(`/v1/auth/userpass/login/${username}`), { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ username: username, password: password }), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { - auth: { client_token: string }; - }; - return data.auth.client_token; -} diff --git a/src/api/auth/userpass/createOrUpdateUserPassUser.ts b/src/api/auth/userpass/createOrUpdateUserPassUser.ts deleted file mode 100644 index 0e15e9b..0000000 --- a/src/api/auth/userpass/createOrUpdateUserPassUser.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { UserType } from "../../types/userpass/user"; -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; -import { removeDoubleSlash } from "../../../utils"; - -export async function createOrUpdateUserPassUser( - path: string, - username: string, - data: Partial, -): Promise { - const request = new Request( - appendAPIURL(removeDoubleSlash(`/v1/auth/${path}/users/${username}`)), - { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(data, null, 0), - }, - ); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/auth/userpass/deleteUserPassUser.ts b/src/api/auth/userpass/deleteUserPassUser.ts deleted file mode 100644 index 9278dd5..0000000 --- a/src/api/auth/userpass/deleteUserPassUser.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; -import { removeDoubleSlash } from "../../../utils"; - -export async function deleteUserPassUser(path: string, username: string): Promise { - const request = new Request( - appendAPIURL(removeDoubleSlash(`/v1/auth/${path}/users/${username}`)), - { - method: "DELETE", - headers: getHeaders(), - }, - ); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/auth/userpass/getUserPassUser.ts b/src/api/auth/userpass/getUserPassUser.ts deleted file mode 100644 index 5ce5b8e..0000000 --- a/src/api/auth/userpass/getUserPassUser.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { UserType, UserTypeAPIResp } from "../../types/userpass/user"; -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; - -export async function getUserPassUser(path: string, username: string): Promise { - const request = new Request(appendAPIURL(`/v1/auth/${path}/users/${username}`), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as UserTypeAPIResp; - return data.data; -} diff --git a/src/api/auth/userpass/listUserPassUsers.ts b/src/api/auth/userpass/listUserPassUsers.ts deleted file mode 100644 index 417b2e6..0000000 --- a/src/api/auth/userpass/listUserPassUsers.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; - -export async function listUserPassUsers(path: string): Promise { - const request = new Request(appendAPIURL(`/v1/auth/${path}/users?list=true`), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { keys: string[] } }; - return data.data.keys; -} diff --git a/src/api/kv/createOrUpdateSecret.ts b/src/api/kv/createOrUpdateSecret.ts deleted file mode 100644 index f2cf946..0000000 --- a/src/api/kv/createOrUpdateSecret.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { getMount } from "../sys/getMounts"; -import { removeDoubleSlash } from "../../utils"; - -export async function createOrUpdateSecret( - baseMount: string, - secretPath: string[], - name: string, - data: Record, -): Promise { - let secretURL = ""; - let APIData = {}; - - const mountInfo = await getMount(baseMount); - if (mountInfo.options.version == "2") { - secretURL = `/v1/${baseMount}/data/${secretPath.join("/")}/${name}`; - APIData = { data: data }; - } else { - secretURL = `/v1/${baseMount}/${secretPath.join("/")}/${name}`; - APIData = data; - } - - secretURL = removeDoubleSlash(secretURL).replace(/\/$/, ""); - const request = new Request(appendAPIURL(secretURL), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(APIData, null, 0), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/kv/deleteSecret.ts b/src/api/kv/deleteSecret.ts deleted file mode 100644 index 9c450a1..0000000 --- a/src/api/kv/deleteSecret.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { getMount } from "../sys/getMounts"; -import { removeDoubleSlash } from "../../utils"; - -export async function deleteSecret( - baseMount: string, - secretPath: string[], - name: string, -): Promise { - let secretURL = ""; - let request; - - const mountInfo = await getMount(baseMount); - if (mountInfo.options.version == "2") { - secretURL = `/v1/${baseMount}/metadata/${secretPath.join("/")}/${name}`; - } else { - secretURL = `/v1/${baseMount}/${secretPath.join("/")}/${name}`; - } - secretURL = removeDoubleSlash(secretURL).replace(/\/$/, ""); - request = new Request(appendAPIURL(secretURL), { - method: "DELETE", - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/kv/getSecret.ts b/src/api/kv/getSecret.ts deleted file mode 100644 index 156bd66..0000000 --- a/src/api/kv/getSecret.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { getMount } from "../sys/getMounts"; - -export async function getSecretKV1( - baseMount: string, - secretPath: string[], - name: string, -): Promise> { - const request = new Request(appendAPIURL(`/v1/${baseMount}/${secretPath.join("")}/${name}`), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as unknown; - - return (data as { data: Record }).data; -} - -export async function getSecretKV2( - baseMount: string, - secretPath: string[], - name: string, -): Promise> { - let secretURL = ""; - - secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`; - - const request = new Request(appendAPIURL(secretURL), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as unknown; - return (data as { data: { data: Record } }).data.data; -} - -export async function getSecret( - baseMount: string, - secretPath: string[], - name: string, -): Promise> { - const mountInfo = await getMount(baseMount); - if (mountInfo.options.version == "2") { - return await getSecretKV2(baseMount, secretPath, name); - } else { - return await getSecretKV1(baseMount, secretPath, name); - } -} diff --git a/src/api/kv/getSecretMetadata.ts b/src/api/kv/getSecretMetadata.ts deleted file mode 100644 index 657db3b..0000000 --- a/src/api/kv/getSecretMetadata.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -type SecretMetadataType = { - versions: Record; -}; - -export async function getSecretMetadata( - baseMount: string, - secretPath: string[], - name: string, -): Promise { - const request = new Request( - appendAPIURL(`/v1/${baseMount}/metadata/${secretPath.join("")}/${name}`), - { - headers: getHeaders(), - }, - ); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: SecretMetadataType }; - return data.data; -} diff --git a/src/api/kv/getSecrets.ts b/src/api/kv/getSecrets.ts deleted file mode 100644 index d51f98f..0000000 --- a/src/api/kv/getSecrets.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function getSecrets(baseMount: string, secretPath: string[]): Promise { - let secretURL = ""; - - // TODO: FIX THIS - const secretMountType = "kv-v2"; - - if (secretMountType == "kv-v2") { - secretURL = `/v1/${baseMount}/metadata/${secretPath.join("/")}?list=true`; - } else { - // cubbyhole and v1 are identical - secretURL = `/v1/${baseMount}/${secretPath.join("/")}?list=true`; - } - const request = new Request(appendAPIURL(secretURL), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { keys: string[] } }; - return data.data.keys; -} diff --git a/src/api/kv/undeleteSecret.ts b/src/api/kv/undeleteSecret.ts deleted file mode 100644 index 1aab24e..0000000 --- a/src/api/kv/undeleteSecret.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { getObjectKeys, removeDoubleSlash } from "../../utils"; -import { getSecretMetadata } from "./getSecretMetadata"; - -export async function undeleteSecret( - baseMount: string, - secretPath: string[], - name: string, - version: string | null = null, -): Promise { - let secretURL = `/v1/${baseMount}/undelete/${secretPath.join("/")}/${name}`; - secretURL = removeDoubleSlash(secretURL).replace(/\/$/, ""); - if (version == null) { - const meta = await getSecretMetadata(baseMount, secretPath, name); - const versions = getObjectKeys(meta.versions); - version = String(versions[versions.length - 1]); - } - - const request = new Request(appendAPIURL(secretURL), { - method: "POST", - headers: { - ...getHeaders(), - "Content-Type": "application/json", - }, - body: JSON.stringify({ versions: [version] }), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/deleteMount.ts b/src/api/sys/deleteMount.ts deleted file mode 100644 index 4b88d34..0000000 --- a/src/api/sys/deleteMount.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function deleteMount(mountPath: string): Promise { - const request = new Request(appendAPIURL("/v1/sys/mounts/" + mountPath), { - method: "DELETE", - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/getCapabilities.ts b/src/api/sys/getCapabilities.ts deleted file mode 100644 index 0c30ee4..0000000 --- a/src/api/sys/getCapabilities.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -export type CapabilitiesType = { - [path: string]: string[]; - capabilities?: string[]; -}; - -export async function getCapabilitiesPath(path: string | string[]): Promise { - if (!Array.isArray(path)) { - path = [path]; - } - - const request = new Request(appendAPIURL("/v1/sys/capabilities-self"), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify({ - paths: path, - }), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { capabilities: string[] }; - return data; -} - -export async function getCapsPath(path: string | string[]): Promise { - return (await getCapabilitiesPath(path)).capabilities; -} - -export async function getCapabilities( - baseMount: string, - secretPath: string[], - name: string, -): Promise { - return await getCapabilitiesPath( - removeDoubleSlash(baseMount + secretPath.join("/") + "/" + name), - ); -} diff --git a/src/api/sys/getMounts.ts b/src/api/sys/getMounts.ts deleted file mode 100644 index 1589e55..0000000 --- a/src/api/sys/getMounts.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export type MountType = { - type: string; - options: { - version: string; - }; -}; - -export type MountsType = { - [key: string]: MountType; -}; - -export async function getMounts(): Promise { - const request = new Request(appendAPIURL("/v1/sys/internal/ui/mounts"), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { secret: MountsType } }; - return data.data.secret; -} - -export async function getMount(mountName: string): Promise { - const request = new Request(appendAPIURL("/v1/sys/internal/ui/mounts/" + mountName), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: MountType }; - return data.data; -} diff --git a/src/api/sys/getSealStatus.ts b/src/api/sys/getSealStatus.ts deleted file mode 100644 index bd43066..0000000 --- a/src/api/sys/getSealStatus.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { appendAPIURL, checkResponse } from "../apiUtils"; - -export type SealStatusType = { - progress: number; - t: number; - sealed: boolean; -}; - -export async function getSealStatus(): Promise { - const request = new Request(appendAPIURL("/v1/sys/seal-status")); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as SealStatusType; - return data; -} diff --git a/src/api/sys/lookupSelf.ts b/src/api/sys/lookupSelf.ts deleted file mode 100644 index 7cc26fb..0000000 --- a/src/api/sys/lookupSelf.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { TokenInfo } from "../types/token"; -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function lookupSelf(): Promise { - const request = new Request(appendAPIURL("/v1/auth/token/lookup-self"), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: TokenInfo }; - return data.data; -} diff --git a/src/api/sys/newMount.ts b/src/api/sys/newMount.ts deleted file mode 100644 index febf604..0000000 --- a/src/api/sys/newMount.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -type NewMountParams = { - name: string; - type: string; - options?: { - version: string; - }; -}; - -export async function newMount(parms: NewMountParams): Promise { - const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/sys/mounts/${parms.name}`)), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(parms), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/policies/createOrUpdatePolicy.ts b/src/api/sys/policies/createOrUpdatePolicy.ts deleted file mode 100644 index ba978bf..0000000 --- a/src/api/sys/policies/createOrUpdatePolicy.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; - -export async function createOrUpdatePolicy(name: string, policy_data: string): Promise { - const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify({ policy: policy_data }, null, 0), - }); - - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/policies/deletePolicy.ts b/src/api/sys/policies/deletePolicy.ts deleted file mode 100644 index fe187d8..0000000 --- a/src/api/sys/policies/deletePolicy.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; - -export async function deletePolicy(name: string): Promise { - const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { - method: "DELETE", - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/policies/getPolicies.ts b/src/api/sys/policies/getPolicies.ts deleted file mode 100644 index 2c927da..0000000 --- a/src/api/sys/policies/getPolicies.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; - -export async function getPolicies(): Promise { - const request = new Request(appendAPIURL("/v1/sys/policies/acl?list=true"), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { keys: string[] } }; - return data.data.keys; -} diff --git a/src/api/sys/policies/getPolicy.ts b/src/api/sys/policies/getPolicy.ts deleted file mode 100644 index e9d7746..0000000 --- a/src/api/sys/policies/getPolicy.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../../apiUtils"; - -export async function getPolicy(name: string): Promise { - const request = new Request(appendAPIURL("/v1/sys/policies/acl/" + name), { - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { policy: string } }; - return data.data.policy; -} diff --git a/src/api/sys/renewSelf.ts b/src/api/sys/renewSelf.ts deleted file mode 100644 index a13a14e..0000000 --- a/src/api/sys/renewSelf.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function renewSelf(): Promise { - const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), { - method: "POST", - headers: { - ...getHeaders(), - "Content-Type": "application/json", - }, - body: JSON.stringify({}), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/sealVault.ts b/src/api/sys/sealVault.ts deleted file mode 100644 index a9140ba..0000000 --- a/src/api/sys/sealVault.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function sealVault(): Promise { - const request = new Request(appendAPIURL("/v1/sys/seal"), { - method: "PUT", - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/sys/submitUnsealKey.ts b/src/api/sys/submitUnsealKey.ts deleted file mode 100644 index c2a1913..0000000 --- a/src/api/sys/submitUnsealKey.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { appendAPIURL, checkResponse } from "../apiUtils"; - -export async function submitUnsealKey(key: string): Promise { - const request = new Request(appendAPIURL("/v1/sys/unseal"), { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - key: key, - }), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/totp/addNewTOTP.ts b/src/api/totp/addNewTOTP.ts deleted file mode 100644 index 018fe26..0000000 --- a/src/api/totp/addNewTOTP.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -export async function addNewTOTP(baseMount: string, parms: { name: string }): Promise { - const request = new Request( - appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/keys/${parms.name}`)), - { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(parms), - }, - ); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/totp/deleteTOTP.ts b/src/api/totp/deleteTOTP.ts deleted file mode 100644 index 3ac643f..0000000 --- a/src/api/totp/deleteTOTP.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function deleteTOTP(baseMount: string, name: string): Promise { - const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), { - method: "DELETE", - headers: getHeaders(), - }); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/totp/getTOTPCode.ts b/src/api/totp/getTOTPCode.ts deleted file mode 100644 index cbacd80..0000000 --- a/src/api/totp/getTOTPCode.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function getTOTPCode(baseMount: string, name: string): Promise { - const request = new Request(appendAPIURL(`/v1/${baseMount}/code/${name}`), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { code: string } }; - return data.data.code; -} diff --git a/src/api/totp/getTOTPKeys.ts b/src/api/totp/getTOTPKeys.ts deleted file mode 100644 index 221e6e7..0000000 --- a/src/api/totp/getTOTPKeys.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function getTOTPKeys(baseMount: string): Promise { - const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { keys: string[] } }; - return data.data.keys; -} diff --git a/src/api/transit/getTransitKey.ts b/src/api/transit/getTransitKey.ts deleted file mode 100644 index ad03371..0000000 --- a/src/api/transit/getTransitKey.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { TransitKeyType } from "../types/transit"; -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function getTransitKey(baseMount: string, name: string): Promise { - const request = new Request(appendAPIURL(`/v1/${baseMount}/keys/${name}`), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: TransitKeyType }; - return data.data; -} diff --git a/src/api/transit/getTransitKeys.ts b/src/api/transit/getTransitKeys.ts deleted file mode 100644 index 23e687f..0000000 --- a/src/api/transit/getTransitKeys.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; - -export async function getTransitKeys(baseMount: string): Promise { - const request = new Request(appendAPIURL(`/v1/${baseMount}/keys?list=true`), { - headers: getHeaders(), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { data: { keys: string[] } }; - return data.data.keys; -} diff --git a/src/api/transit/newTransitKey.ts b/src/api/transit/newTransitKey.ts deleted file mode 100644 index 92f99b3..0000000 --- a/src/api/transit/newTransitKey.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -export async function newTransitKey( - baseMount: string, - parms: { name: string; type: string }, -): Promise { - const request = new Request( - appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/keys/${parms.name}`)), - { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(parms), - }, - ); - const resp = await fetch(request); - await checkResponse(resp); -} diff --git a/src/api/transit/transitDecrypt.ts b/src/api/transit/transitDecrypt.ts deleted file mode 100644 index 6cf52aa..0000000 --- a/src/api/transit/transitDecrypt.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -type DecryptionResult = { - plaintext: string; -}; - -type DecryptionPayload = { - ciphertext: string; -}; - -export async function transitDecrypt( - baseMount: string, - name: string, - payload: DecryptionPayload, -): Promise { - const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(payload), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { - data?: DecryptionResult; - }; - - return data.data; -} diff --git a/src/api/transit/transitEncrypt.ts b/src/api/transit/transitEncrypt.ts deleted file mode 100644 index faebab2..0000000 --- a/src/api/transit/transitEncrypt.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -type EncryptionResult = { - ciphertext: string; -}; - -type EncryptionPayload = { - plaintext: string; -}; - -export async function transitEncrypt( - baseMount: string, - name: string, - payload: EncryptionPayload, -): Promise { - const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(payload), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { - data?: EncryptionResult; - }; - - return data.data; -} diff --git a/src/api/transit/transitRewrap.ts b/src/api/transit/transitRewrap.ts deleted file mode 100644 index 81ed4e8..0000000 --- a/src/api/transit/transitRewrap.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; -import { removeDoubleSlash } from "../../utils"; - -type RewrapResult = { - ciphertext: string; -}; - -type RewrapPayload = { - ciphertext: string; - key_version?: number; -}; - -export async function transitRewrap( - baseMount: string, - name: string, - payload: RewrapPayload, -): Promise { - const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/rewrap/${name}`)), { - method: "POST", - headers: { - "Content-Type": "application/json", - ...getHeaders(), - }, - body: JSON.stringify(payload), - }); - - const resp = await fetch(request); - await checkResponse(resp); - - const data = (await resp.json()) as { - data?: RewrapResult; - }; - - return data.data; -} diff --git a/src/api/types/capabilities.ts b/src/api/types/capabilities.ts new file mode 100644 index 0000000..687a720 --- /dev/null +++ b/src/api/types/capabilities.ts @@ -0,0 +1,4 @@ +export type CapabilitiesType = { + [path: string]: string[]; + capabilities?: string[]; +}; diff --git a/src/api/types/mount.ts b/src/api/types/mount.ts new file mode 100644 index 0000000..bf443db --- /dev/null +++ b/src/api/types/mount.ts @@ -0,0 +1,18 @@ +export type MountType = { + type: string; + options: { + version: string; + }; +}; + +export type MountsType = { + [key: string]: MountType; +}; + +export type NewMountParams = { + name: string; + type: string; + options?: { + version: string; + }; +}; diff --git a/src/api/types/seal.ts b/src/api/types/seal.ts new file mode 100644 index 0000000..e292eaf --- /dev/null +++ b/src/api/types/seal.ts @@ -0,0 +1,5 @@ +export type SealStatusType = { + progress: number; + t: number; + sealed: boolean; +}; diff --git a/src/api/types/secret.ts b/src/api/types/secret.ts new file mode 100644 index 0000000..3e2bec8 --- /dev/null +++ b/src/api/types/secret.ts @@ -0,0 +1,3 @@ +type SecretMetadataType = { + versions: Record; +}; diff --git a/src/api/types/transit.ts b/src/api/types/transit.ts index 8419928..9b9e458 100644 --- a/src/api/types/transit.ts +++ b/src/api/types/transit.ts @@ -33,3 +33,28 @@ export type TransitKeyType = TransitKeyBaseType & { supports_derivation: boolean; supports_signing: boolean; }; + +export type DecryptionResult = { + plaintext: string; +}; + +export type DecryptionPayload = { + ciphertext: string; +}; + +export type EncryptionResult = { + ciphertext: string; +}; + +export type EncryptionPayload = { + plaintext: string; +}; + +export type RewrapResult = { + ciphertext: string; +}; + +export type RewrapPayload = { + ciphertext: string; + key_version?: number; +}; diff --git a/src/api/types/userpass/user.ts b/src/api/types/user.ts similarity index 100% rename from src/api/types/userpass/user.ts rename to src/api/types/user.ts diff --git a/src/globalAPI.ts b/src/globalAPI.ts new file mode 100644 index 0000000..58de8b4 --- /dev/null +++ b/src/globalAPI.ts @@ -0,0 +1,4 @@ +import { API } from "./api/API"; +import { settings } from "./globalSettings"; + +export const api = new API(settings); diff --git a/src/main.tsx b/src/main.tsx index 181b565..6b87944 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -23,14 +23,15 @@ import translations from "./translations/index.mjs"; // Actual Imports import { formatDistance } from "./formatDistance"; -import { getSealStatus } from "./api/sys/getSealStatus"; //import { pageList } from "./allPages"; import { render } from "preact"; import { NavBar } from "./ui/elements/NavBar"; +import { api } from "./globalAPI"; import { settings } from "./globalSettings"; import { playground } from "./playground"; import i18next from "i18next"; import { Main } from "./pages"; +import { getCurrentUrl, route } from "preact-router"; async function onLoad(): Promise { document.documentElement.dir = settings.pageDirection; @@ -53,18 +54,20 @@ async function onLoad(): Promise { //await pageRouter.changePage(pageState.currentPage); - //setInterval(async () => { - // if ((await pageRouter.getCurrentPageID()) != "UNSEAL") { - // if (pageState.apiURL.length != 0) { - // return; - // } - // const sealStatus = await getSealStatus(); - // if (sealStatus.sealed) { - // await pageRouter.changePage("UNSEAL"); - // return; - // } - // } - //}, 5000); + setInterval(async () => { + console.log(getCurrentUrl()); + if (getCurrentUrl() != "/unseal") { + // TODO: check if api is accessable, not if its not set + if (settings.apiURL.length == 0) { + return; + } + const sealStatus = await api.getSealStatus(); + if (sealStatus.sealed) { + route("/unseal", false); + return; + } + } + }, 5000); } document.addEventListener( diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 7c61397..9147a2f 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -1,12 +1,11 @@ import { Settings } from "./settings/Settings"; -import { getSealStatus } from "./api/sys/getSealStatus"; -import { lookupSelf } from "./api/sys/lookupSelf"; import { route } from "preact-router"; import ClipboardJS from "clipboard"; import UIkit from "uikit"; import i18next from "i18next"; +import { API } from "./api/API"; -async function prePageChecksReal(settings: Settings) { +async function prePageChecksReal(api: API, settings: Settings) { if (settings.language.length == 0) { route("/set_language", true); throw new Error("Language Not Set"); @@ -17,23 +16,23 @@ async function prePageChecksReal(settings: Settings) { throw new Error("Vault URL Not Set"); } - const sealStatus = await getSealStatus(); + const sealStatus = await api.getSealStatus(); if (sealStatus.sealed) { route("/unseal", true); throw new Error("Vault Sealed"); } try { - await lookupSelf(); + await api.lookupSelf(); } catch (e) { route("/login", true); throw e; } } -export async function prePageChecks(settings: Settings): Promise { +export async function prePageChecks(api: API, settings: Settings): Promise { try { - await prePageChecksReal(settings); + await prePageChecksReal(api, settings); } catch (e) { console.log("OHNO", e); return false; diff --git a/src/pages.tsx b/src/pages.tsx index 2626333..4e98445 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -1,5 +1,6 @@ import Router from "preact-router"; import { settings } from "./globalSettings"; +import { api } from "./globalAPI"; import { DeleteSecretsEngine } from "./ui/pages/Secrets/DeleteSecretsEngine"; import { Home } from "./ui/pages/Home"; @@ -44,54 +45,94 @@ import { UserPassUserEdit } from "./ui/pages/Access/Auth/userpass/UserPassUserEd export const Main = () => ( - - - - - - - + + + + + + + - - + + - - - - + + + + - - - - - + + + + + - - - + + + - - - - - - + + + + + + - - - - - + + + + + - - - + + + - - - - - + + + + +

PAGE NOT YET IMPLEMENTED

diff --git a/src/types/DefaultPageProps.ts b/src/types/DefaultPageProps.ts index fd0ce6a..28de958 100644 --- a/src/types/DefaultPageProps.ts +++ b/src/types/DefaultPageProps.ts @@ -1,6 +1,8 @@ +import { API } from "../api/API"; import { Settings } from "../settings/Settings"; export type DefaultPageProps = { settings: Settings; + api: API; matches?: { [key: string]: string }; }; diff --git a/src/ui/pages/Access/AccessHome.tsx b/src/ui/pages/Access/AccessHome.tsx index 7d679ba..1a99891 100644 --- a/src/ui/pages/Access/AccessHome.tsx +++ b/src/ui/pages/Access/AccessHome.tsx @@ -9,7 +9,7 @@ import { PageTitle } from "../../elements/PageTitle"; export class AccessHomePage extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.settings))) return; + if (!(await prePageChecks(this.props.api, this.props.settings))) return; } render() { diff --git a/src/ui/pages/Access/Auth/AuthHome.tsx b/src/ui/pages/Access/Auth/AuthHome.tsx index 5a24cc3..572d0e8 100644 --- a/src/ui/pages/Access/Auth/AuthHome.tsx +++ b/src/ui/pages/Access/Auth/AuthHome.tsx @@ -1,6 +1,5 @@ import { AuthMethod } from "../../../../api/types/auth"; import { Component, JSX } from "preact"; -import { listAuth } from "../../../../api/auth/listAuth"; import { notImplemented } from "../../../../pageUtils"; import { objectToMap } from "../../../../utils"; import i18next from "i18next"; @@ -52,7 +51,7 @@ export function AuthListElement(props: AuthListElementProps): JSX.Element { export class AuthHome extends Component }> { async componentDidMount() { - const authList = objectToMap(await listAuth()) as Map; + const authList = objectToMap(await this.props.api.listAuth()) as Map; this.setState({ authList }); } render() { diff --git a/src/ui/pages/Access/Auth/AuthViewConfig.tsx b/src/ui/pages/Access/Auth/AuthViewConfig.tsx index 50ad75f..d4c5c4d 100644 --- a/src/ui/pages/Access/Auth/AuthViewConfig.tsx +++ b/src/ui/pages/Access/Auth/AuthViewConfig.tsx @@ -1,6 +1,5 @@ import { AuthMethod } from "../../../../api/types/auth"; import { HeaderAndContent } from "../../../elements/HeaderAndContent"; -import { listAuth } from "../../../../api/auth/listAuth"; import { objectToMap, toStr } from "../../../../utils"; import { Component } from "preact"; import i18next from "i18next"; @@ -10,7 +9,7 @@ import { PageTitle } from "../../../elements/PageTitle"; export class AuthViewConfig extends Component { async componentDidMount() { const baseMount = this.props.matches["baseMount"]; - const authList = objectToMap(await listAuth()) as Map; + const authList = objectToMap(await this.props.api.listAuth()) as Map; const authMethod = authList.get(baseMount + "/"); this.setState({ authMethod: authMethod }); } diff --git a/src/ui/pages/Access/Auth/userpass/UserPassUserDelete.tsx b/src/ui/pages/Access/Auth/userpass/UserPassUserDelete.tsx index 565295a..6855a60 100644 --- a/src/ui/pages/Access/Auth/userpass/UserPassUserDelete.tsx +++ b/src/ui/pages/Access/Auth/userpass/UserPassUserDelete.tsx @@ -1,4 +1,3 @@ -import { deleteUserPassUser } from "../../../../../api/auth/userpass/deleteUserPassUser"; import { Component } from "preact"; import i18next from "i18next"; import { DefaultPageProps } from "../../../../../types/DefaultPageProps"; @@ -19,7 +18,7 @@ export class UserPassUserDelete extends Component {
diff --git a/src/ui/pages/Me.tsx b/src/ui/pages/Me.tsx index 766a435..42fef50 100644 --- a/src/ui/pages/Me.tsx +++ b/src/ui/pages/Me.tsx @@ -2,10 +2,7 @@ import { Component, JSX, createRef } from "preact"; import { DefaultPageProps } from "../../types/DefaultPageProps"; import { PageTitle } from "../elements/PageTitle"; import { addClipboardNotifications, setErrorText } from "../../pageUtils"; -import { getCapsPath } from "../../api/sys/getCapabilities"; -import { renewSelf } from "../../api/sys/renewSelf"; import { route } from "preact-router"; -import { sealVault } from "../../api/sys/sealVault"; import ClipboardJS from "clipboard"; import i18next from "i18next"; @@ -45,7 +42,7 @@ export class Me extends Component { async componentDidMount() { let canSealVault = false; try { - const caps = await getCapsPath("sys/seal"); + const caps = await this.props.api.getCapsPath("sys/seal"); canSealVault = caps.includes("sudo") && caps.includes("update"); } catch (e) { canSealVault = false; @@ -80,7 +77,7 @@ export class Me extends Component { { try { - await renewSelf(); + await this.props.api.renewSelf(); route("/"); } catch (e: unknown) { const error = e as Error; @@ -95,7 +92,7 @@ export class Me extends Component {
  • { - await sealVault(); + await this.props.api.sealVault(); route("/unseal", true); }} > diff --git a/src/ui/pages/Policies/PoliciesHome.tsx b/src/ui/pages/Policies/PoliciesHome.tsx index aa9a1d7..4b38c3a 100644 --- a/src/ui/pages/Policies/PoliciesHome.tsx +++ b/src/ui/pages/Policies/PoliciesHome.tsx @@ -1,5 +1,4 @@ import { Margin } from "../../elements/Margin"; -import { getPolicies } from "../../../api/sys/policies/getPolicies"; import { prePageChecks } from "../../../pageUtils"; import { Component } from "preact"; import i18next from "i18next"; @@ -10,9 +9,9 @@ import { policyNewURL, policyViewURL } from "../pageLinks"; export class PoliciesHome extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.settings))) return; + if (!(await prePageChecks(this.props.api, this.props.settings))) return; - let policies = await getPolicies(); + let policies = await this.props.api.getPolicies(); policies = policies.sort(); policies = policies.filter(function (policy_name) { return policy_name !== "root"; diff --git a/src/ui/pages/Policies/PolicyDelete.tsx b/src/ui/pages/Policies/PolicyDelete.tsx index 10d6253..aafdd1c 100644 --- a/src/ui/pages/Policies/PolicyDelete.tsx +++ b/src/ui/pages/Policies/PolicyDelete.tsx @@ -1,5 +1,4 @@ -import { deletePolicy } from "../../../api/sys/policies/deletePolicy"; -import { Component, render } from "preact"; +import { Component } from "preact"; import { setErrorText } from "../../../pageUtils"; import i18next from "i18next"; import { DefaultPageProps } from "../../../types/DefaultPageProps"; @@ -18,7 +17,7 @@ export class PolicyDelete extends Component { class="uk-button uk-button-danger" onClick={async () => { try { - await deletePolicy(policyName); + await this.props.api.deletePolicy(policyName); route("/policies"); } catch (e: unknown) { const error = e as Error; diff --git a/src/ui/pages/Policies/PolicyEdit.tsx b/src/ui/pages/Policies/PolicyEdit.tsx index ed4beae..f844eef 100644 --- a/src/ui/pages/Policies/PolicyEdit.tsx +++ b/src/ui/pages/Policies/PolicyEdit.tsx @@ -2,8 +2,6 @@ import { CodeEditor } from "../../elements/CodeEditor"; import { Component, JSX, render } from "preact"; import { Margin } from "../../elements/Margin"; import { MarginInline } from "../../elements/MarginInline"; -import { createOrUpdatePolicy } from "../../../api/sys/policies/createOrUpdatePolicy"; -import { getPolicy } from "../../../api/sys/policies/getPolicy"; import { setErrorText } from "../../../pageUtils"; import i18next from "i18next"; import { DefaultPageProps } from "../../../types/DefaultPageProps"; @@ -11,7 +9,7 @@ import { PageTitle } from "../../elements/PageTitle"; import { route } from "preact-router"; import { policyViewURL } from "../pageLinks"; -type PolicyEditorProps = { +type PolicyEditorProps = DefaultPageProps & { policyName: string; }; @@ -37,7 +35,7 @@ export class PolicyEditor extends Component { - const policyData = await getPolicy(this.props.policyName); + const policyData = await this.props.api.getPolicy(this.props.policyName); this.setState({ dataLoaded: true, policyData: policyData, @@ -100,7 +98,11 @@ export class PolicyEdit extends Component { <>
    - +
    ); diff --git a/src/ui/pages/Policies/PolicyNew.tsx b/src/ui/pages/Policies/PolicyNew.tsx index 49acd54..274261d 100644 --- a/src/ui/pages/Policies/PolicyNew.tsx +++ b/src/ui/pages/Policies/PolicyNew.tsx @@ -1,7 +1,5 @@ import { Form } from "../../elements/Form"; import { Margin } from "../../elements/Margin"; -import { createOrUpdatePolicy } from "../../../api/sys/policies/createOrUpdatePolicy"; -import { getPolicies } from "../../../api/sys/policies/getPolicies"; import { Component } from "preact"; import { setErrorText } from "../../../pageUtils"; import i18next from "i18next"; @@ -19,13 +17,13 @@ export class PolicyNew extends Component {
    { const name = formData.get("name") as string; - if ((await getPolicies()).includes(name)) { + if ((await this.props.api.getPolicies()).includes(name)) { setErrorText(i18next.t("policy_new_already_exists")); return; } try { - await createOrUpdatePolicy(name, " "); + await this.props.api.createOrUpdatePolicy(name, " "); route(policyViewURL(name)); } catch (e: unknown) { const error = e as Error; diff --git a/src/ui/pages/Policies/PolicyView.tsx b/src/ui/pages/Policies/PolicyView.tsx index c33f7fc..3f034f1 100644 --- a/src/ui/pages/Policies/PolicyView.tsx +++ b/src/ui/pages/Policies/PolicyView.tsx @@ -1,6 +1,5 @@ import { CodeBlock } from "../../elements/CodeBlock"; import { Margin } from "../../elements/Margin"; -import { getPolicy } from "../../../api/sys/policies/getPolicy"; import { prePageChecks } from "../../../pageUtils"; import { Component } from "preact"; import i18next from "i18next"; @@ -14,10 +13,10 @@ export class PolicyView extends Component< { policy: string; policyName: string } > { async componentDidMount() { - if (!(await prePageChecks(this.props.settings))) return; + if (!(await prePageChecks(this.props.api, this.props.settings))) return; console.log(this.props); const policyName = this.props.matches["policyName"]; - const policy = await getPolicy(policyName); + const policy = await this.props.api.getPolicy(policyName); this.setState({ policy, policyName, diff --git a/src/ui/pages/PwGen.tsx b/src/ui/pages/PwGen.tsx index 3c98f86..041719e 100644 --- a/src/ui/pages/PwGen.tsx +++ b/src/ui/pages/PwGen.tsx @@ -4,6 +4,7 @@ import { Form } from "../elements/Form"; import { Margin } from "../elements/Margin"; import { PageTitle } from "../elements/PageTitle"; import i18next from "i18next"; +import { DefaultPageProps } from "../../types/DefaultPageProps"; const passwordLengthMin = 1; const passwordLengthMax = 64; @@ -51,7 +52,7 @@ type PasswordGeneratorState = { alphabet: string; }; -export class PasswordGenerator extends Component<{}, PasswordGeneratorState> { +export class PasswordGenerator extends Component { constructor() { super(); this.state = { diff --git a/src/ui/pages/Secrets/DeleteSecretsEngine.tsx b/src/ui/pages/Secrets/DeleteSecretsEngine.tsx index 93954c8..00db74f 100644 --- a/src/ui/pages/Secrets/DeleteSecretsEngine.tsx +++ b/src/ui/pages/Secrets/DeleteSecretsEngine.tsx @@ -3,7 +3,6 @@ import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { Form } from "../../elements/Form"; import { MarginInline } from "../../elements/MarginInline"; import { PageTitle } from "../../elements/PageTitle"; -import { deleteMount } from "../../../api/sys/deleteMount"; import { route } from "preact-router"; import { setErrorText } from "../../../pageUtils"; import i18next from "i18next"; @@ -36,7 +35,7 @@ export class DeleteSecretsEngine extends Component { async onSubmit(): Promise { try { - await deleteMount(this.props.matches["mount"]); + await this.props.api.deleteMount(this.props.matches["mount"]); route("/secrets"); } catch (e: unknown) { const error = e as Error; diff --git a/src/ui/pages/Secrets/KeyValue/KeyValueDelete.tsx b/src/ui/pages/Secrets/KeyValue/KeyValueDelete.tsx index d48be02..f97b4f5 100644 --- a/src/ui/pages/Secrets/KeyValue/KeyValueDelete.tsx +++ b/src/ui/pages/Secrets/KeyValue/KeyValueDelete.tsx @@ -1,7 +1,6 @@ import { Component, render } from "preact"; import { DefaultPageProps } from "../../../../types/DefaultPageProps"; import { SecretTitleElement } from "../SecretTitleElement"; -import { deleteSecret } from "../../../../api/kv/deleteSecret"; import i18next from "i18next"; export class KeyValueDelete extends Component { @@ -24,7 +23,7 @@ export class KeyValueDelete extends Component {