1
0
Fork 0

Change signature o getCapabilities.

This commit is contained in:
Kitteh 2021-05-29 10:19:18 +01:00
parent 4c4a69464f
commit b079d485db
4 changed files with 19 additions and 8 deletions

View file

@ -1,7 +1,18 @@
import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils"; import { appendAPIURL, checkResponse, getHeaders } from "../apiUtils";
import { removeDoubleSlash } from "../../utils"; import { removeDoubleSlash } from "../../utils";
export async function getCapabilitiesPath(path: string): Promise<string[]> { export type CapabilitiesType = {
[path: string]: string[];
capabilities?: string[];
}
export async function getCapabilitiesPath(path: string | string[]): Promise<CapabilitiesType> {
if (!Array.isArray(path)) {
path = [removeDoubleSlash(path)]
} else {
path = path.map((s) => removeDoubleSlash(s));
}
const request = new Request(appendAPIURL("/v1/sys/capabilities-self"), { const request = new Request(appendAPIURL("/v1/sys/capabilities-self"), {
method: "POST", method: "POST",
headers: { headers: {
@ -9,21 +20,21 @@ export async function getCapabilitiesPath(path: string): Promise<string[]> {
...getHeaders(), ...getHeaders(),
}, },
body: JSON.stringify({ body: JSON.stringify({
paths: [removeDoubleSlash(path)], paths: path,
}), }),
}); });
const resp = await fetch(request); const resp = await fetch(request);
await checkResponse(resp); await checkResponse(resp);
const data = (await resp.json()) as { capabilities: string[] }; const data = (await resp.json()) as { capabilities: string[] };
return data.capabilities; return data;
} }
export async function getCapabilities( export async function getCapabilities(
baseMount: string, baseMount: string,
secretPath: string[], secretPath: string[],
name: string, name: string,
): Promise<string[]> { ): Promise<CapabilitiesType> {
return await getCapabilitiesPath( return await getCapabilitiesPath(
removeDoubleSlash(baseMount + secretPath.join("/") + "/" + name), removeDoubleSlash(baseMount + secretPath.join("/") + "/" + name),
); );

View file

@ -34,7 +34,7 @@ export class MePage extends Page {
let canSealVault = false; let canSealVault = false;
try { try {
const caps = await getCapabilitiesPath("sys/seal"); const caps = (await getCapabilitiesPath("sys/seal")).capabilities;
canSealVault = caps.includes("sudo") && caps.includes("update"); canSealVault = caps.includes("sudo") && caps.includes("update");
} catch (e) { } catch (e) {
canSealVault = false; canSealVault = false;

View file

@ -55,11 +55,11 @@ export class KeyValueSecretPage extends Page {
} }
} }
async render(): Promise<void> { async render(): Promise<void> {
const caps = await getCapabilities( const caps = (await getCapabilities(
this.state.baseMount, this.state.baseMount,
this.state.secretPath, this.state.secretPath,
this.state.secretItem, this.state.secretItem,
); )).capabilities;
const secretInfo = await getSecret( const secretInfo = await getSecret(
this.state.baseMount, this.state.baseMount,

View file

@ -74,7 +74,7 @@ export class SecretsHomePage extends Page {
this.state.secretItem = ""; this.state.secretItem = "";
this.state.secretVersion = null; this.state.secretVersion = null;
const mountsCapabilities = await getCapabilitiesPath("/sys/mounts"); const mountsCapabilities = (await getCapabilitiesPath("/sys/mounts")).capabilities;
const mounts = await getMounts(); const mounts = await getMounts();
// sort it by secretPath so it's in alphabetical order consistantly. // sort it by secretPath so it's in alphabetical order consistantly.
const mountsMap = sortedObjectMap(mounts); const mountsMap = sortedObjectMap(mounts);