Skip to content

Commit

Permalink
Fix vault init (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt authored May 5, 2020
1 parent 3ccea8e commit 470eeec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import conf from '../configuration';
const VAULT_BASE_URI = conf.VAULT.VAULT_BASE_URI;
const VAULT_KV_ENGINE = conf.VAULT.VAULT_KV_ENGINE;

interface VaultProfile {
password?: string;
git_user_name?: string;
git_user_mail?: string;
git_credentials_cache_duration?: string;
}

class VaultAPI {
async getSecretsList(path = '') {
// This ts ignore is due to the fact we use an interceptor
// to return the data instead of an object containing the data
// @ts-ignore
const { data } = await axiosVault({
method: 'list',
url: `/v1/${VAULT_KV_ENGINE}/metadata${path}`,
Expand All @@ -25,14 +35,14 @@ class VaultAPI {
return data.data.data ? data.data.data : [];
}

async createPath(path, payload) {
async createPath(path: string, payload) {
return axiosVault.put(
`/v1/${VAULT_KV_ENGINE}/data${path}`,
payload || { data: { foo: 'bar' } }
);
}

async uploadSecret(path, data) {
async uploadSecret(path: string, data) {
const old = await this.getSecret(path);
await axiosVault.put(`/v1/${VAULT_KV_ENGINE}/data${path}`, {
data: { ...old, ...data },
Expand Down Expand Up @@ -68,7 +78,7 @@ const buildDefaultPwd = () =>
numbers: true,
});

export const initVaultData = (idep, name, mail) => {
export const initVaultData = (idep: string, name: string, mail: string) => {
axiosVault(
`${VAULT_BASE_URI}/v1/${VAULT_KV_ENGINE}/data/${idep}/.onyxia/profile`
)
Expand Down Expand Up @@ -101,18 +111,23 @@ export const initVaultData = (idep, name, mail) => {
}
)
.catch(() => {
resetVaultData(idep, undefined, name, mail);
resetVaultData(idep, {
password: buildDefaultPwd(),
git_user_name: name,
git_user_mail: mail,
git_credentials_cache_duration: '0',
});
});
};

export const resetVaultData = (idep, data) => {
export const resetVaultData = (idep: string, data: VaultProfile) => {
const payload = { data };
axiosVault
.post(`/v1/${VAULT_KV_ENGINE}/data/${idep}/.onyxia/profile`, payload)
.then(() => store.dispatch(newVaultData(payload.data)));
};

export const resetVaultPwd = (idep) =>
export const resetVaultPwd = (idep: string) =>
resetVaultData(idep, { password: buildDefaultPwd() });

/**
Expand All @@ -129,7 +144,7 @@ axiosVault.interceptors.request.use(
(error) => Promise.reject(error)
);

const authorizeConfig = (token) => (config) => ({
const authorizeConfig = (token: string) => (config) => ({
...config,
headers: { 'X-Vault-Token': token },
'Content-Type': 'application/json;charset=utf-8',
Expand Down

0 comments on commit 470eeec

Please sign in to comment.