2021-04-15 13:01:58 +01:00
|
|
|
|
|
|
|
import { getAPIURL, getToken, removeDoubleSlash } from "./utils.js";
|
|
|
|
|
|
|
|
export const DoesNotExistError = new Error("Does not exist.");
|
|
|
|
|
|
|
|
export async function lookupSelf() {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + "/v1/auth/token/lookup-self", {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
if ("data" in data) {
|
|
|
|
return data.data;
|
|
|
|
} else if ("errors" in data) {
|
|
|
|
throw new Error(data.errors[0]);
|
|
|
|
}
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function renewSelf() {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + "/v1/auth/token/renew-self", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({})
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
if ("errors" in data) {
|
|
|
|
throw new Error(data.errors[0]);
|
|
|
|
}
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function usernameLogin(username, password) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + `/v1/auth/userpass/login/${username}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ "username": username, "password": password })
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
if ("auth" in data) {
|
|
|
|
return data.auth.client_token;
|
|
|
|
} else if ("errors" in data) {
|
|
|
|
throw new Error(data.errors[0]);
|
|
|
|
}
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getMounts() {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + "/v1/sys/internal/ui/mounts", {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.data.secret;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSealStatus() {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + "/v1/sys/seal-status");
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function submitUnsealKey(key) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + "/v1/sys/unseal", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
"key": key
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCapabilities(baseMount, secretPath, name) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + "/v1/sys/capabilities-self", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
},
|
|
|
|
body: JSON.stringify(
|
|
|
|
{
|
|
|
|
"paths": [removeDoubleSlash(baseMount + secretPath.join("/") + "/" + name)]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.capabilities;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSecrets(baseMount, secretPath) {
|
2021-04-15 15:09:43 +01:00
|
|
|
let secretURL = "";
|
|
|
|
if (pageState.currentMountType == "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(getAPIURL() + secretURL, {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-04-15 15:09:43 +01:00
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
if (response.status == 404) {
|
|
|
|
throw DoesNotExistError;
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.data.keys;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSecretMetadata(baseMount, secretPath, name) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + `/v1/${baseMount}/metadata/${secretPath.join("")}/${name}`, {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
}
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.data;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function undeleteSecret(baseMount, secretPath, name, version) {
|
2021-04-15 15:09:43 +01:00
|
|
|
let secretURL = `/v1/${baseMount}/undelete/${secretPath.join("")}/${name}`;
|
|
|
|
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
|
|
|
|
let request = new Request(getAPIURL() + secretURL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'X-Vault-Token': getToken(),
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ "versions": [version] })
|
|
|
|
});
|
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function getSecret(baseMount, secretPath, name, version = "0") {
|
2021-04-15 15:09:43 +01:00
|
|
|
let secretURL = "";
|
|
|
|
if (pageState.currentMountType == "kv-v2") {
|
|
|
|
secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`;
|
|
|
|
if (version != 0) secretURL += `?version=${version}`;
|
|
|
|
} else {
|
|
|
|
secretURL = `/v1/${baseMount}/${secretPath.join("")}/${name}`;
|
|
|
|
}
|
|
|
|
const request = new Request(getAPIURL() + secretURL, {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-04-15 15:09:43 +01:00
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return pageState.currentMountType == "kv-v2" ? data.data.data : data.data;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteSecret(baseMount, secretPath, name, version) {
|
2021-04-15 15:09:43 +01:00
|
|
|
let secretURL = "";
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
let request;
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
if (pageState.currentMountType == "kv-v2" && version != "0") {
|
|
|
|
secretURL = `/v1/${baseMount}/delete/${secretPath.join("")}/${name}`;
|
|
|
|
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
|
|
|
|
request = new Request(getAPIURL() + secretURL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'X-Vault-Token': getToken(),
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ "versions": [version] })
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (pageState.currentMountType == "kv-v2") {
|
|
|
|
secretURL = `/v1/${baseMount}/metadata/${secretPath.join("")}/${name}`;
|
2021-04-15 13:01:58 +01:00
|
|
|
} else {
|
2021-04-15 15:09:43 +01:00
|
|
|
secretURL = `/v1/${baseMount}/${secretPath.join("")}/${name}`;
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-04-15 15:09:43 +01:00
|
|
|
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
|
|
|
|
request = new Request(getAPIURL() + secretURL, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
'X-Vault-Token': getToken()
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
|
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function createOrUpdateSecret(baseMount, secretPath, name, data) {
|
2021-04-15 15:09:43 +01:00
|
|
|
let secretURL = "";
|
|
|
|
let APIData = {};
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
if (pageState.currentMountType == "kv-v2") {
|
|
|
|
secretURL = `/v1/${baseMount}/data/${secretPath.join("")}/${name}`;
|
|
|
|
APIData = { "data": data };
|
|
|
|
} else {
|
|
|
|
secretURL = `/v1/${baseMount}/${secretPath.join("")}/${name}`;
|
|
|
|
APIData = data;
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
|
2021-04-15 15:09:43 +01:00
|
|
|
secretURL = removeDoubleSlash(secretURL).replace(/\/$/, "");
|
|
|
|
const request = new Request(getAPIURL() + secretURL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Vault-Token': getToken()
|
|
|
|
},
|
|
|
|
body: JSON.stringify(APIData, null, 0)
|
|
|
|
});
|
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getTransitKeys(baseMount) {
|
|
|
|
const request = new Request(getAPIURL() + `/v1/${baseMount}/keys?list=true`, {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
2021-04-15 15:09:43 +01:00
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
if (response.status == 404) {
|
|
|
|
throw DoesNotExistError;
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.data.keys;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:29:22 +01:00
|
|
|
export async function transitEncrypt(baseMount, name, data) {
|
|
|
|
const request = new Request(getAPIURL() + removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`), {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Vault-Token': getToken()
|
|
|
|
},
|
|
|
|
body: JSON.stringify({plaintext: data})
|
|
|
|
});
|
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
} else {
|
|
|
|
let json = await response.json();
|
|
|
|
return json.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:39:07 +01:00
|
|
|
export async function transitDecrypt(baseMount, name, data) {
|
|
|
|
const request = new Request(getAPIURL() + removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`), {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Vault-Token': getToken()
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ciphertext: data})
|
|
|
|
});
|
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
} else {
|
|
|
|
let json = await response.json();
|
|
|
|
return json.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:29:22 +01:00
|
|
|
|
2021-04-15 13:01:58 +01:00
|
|
|
export async function getTOTPKeys(baseMount) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + `/v1/${baseMount}/keys?list=true`, {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.data.keys;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getTOTPCode(baseMount, name) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + `/v1/${baseMount}/code/${name}`, {
|
|
|
|
headers: {
|
|
|
|
"X-Vault-Token": getToken(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return fetch(request).then(response => {
|
|
|
|
return response.json();
|
|
|
|
}).then(data => {
|
|
|
|
return data.data.code;
|
|
|
|
});
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function addNewTOTP(baseMount, parms) {
|
2021-04-15 15:09:43 +01:00
|
|
|
const request = new Request(getAPIURL() + removeDoubleSlash(`/v1/${baseMount}/keys/${parms.name}`), {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Vault-Token': getToken()
|
|
|
|
},
|
|
|
|
body: JSON.stringify(parms)
|
|
|
|
});
|
|
|
|
let response = await fetch(request);
|
|
|
|
if (!response.ok) {
|
|
|
|
let json = await response.json();
|
|
|
|
throw new Error(json.errors[0]);
|
|
|
|
}
|
2021-04-15 13:01:58 +01:00
|
|
|
}
|