Skip to content

Commit

Permalink
add delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Szotkowski committed Sep 19, 2023
1 parent a0fac10 commit a9ea985
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class UserController {
@repository(StepRepository) public stepRepository: StepRepository,
@repository(UserLinkRepository)
public userLinkRepository: UserLinkRepository,
) {}
) { }

@post('/login', {
responses: {
Expand Down Expand Up @@ -605,7 +605,9 @@ export class UserController {
if (!passwordMatched) {
throw new HttpErrors.Unauthorized('Password is not valid');
}
await this.vaultService.deleteUserKey(String(userOriginal.id));
await this.vaultService.deleteUser(String(userOriginal.id));
await this.vaultService.deleteUserPolicy(String(userOriginal.id));
if (userOriginal.deleteHash) {
await this.imgurService.deleteImage(userOriginal.deleteHash);
}
Expand Down
72 changes: 55 additions & 17 deletions src/services/vault-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,49 +63,49 @@ export class VaultService {
}
}

async createUserPolicy(id: string): Promise<void> {
async createUser(password: string, id: string): Promise<void> {
try {
let policyData = fs.readFileSync(
`./src/services/example-user-policy.hcl`,
'utf-8',
);
policyData = policyData.replace('{{id}}', id);
const data = {
password: password,
policies: [String(id)],
};
const response = await fetch(
`${this.vaultEndpoint}/v1/sys/policy/${id}`,
`${this.vaultEndpoint}/v1/auth/userpass/users/${id}`,
{
method: 'POST',
headers: {
'X-Vault-Token': this.rootToken,
},
body: JSON.stringify({data: policyData}),
body: JSON.stringify(data),
},
);
if (!response.ok) {
throw new Error(`Unable to create policy`);
throw new Error(`Unable to create user`);
}
} catch (error) {
throw new Error(`Authentication error: ${error.message}`);
}
}

async createUser(password: string, id: string): Promise<void> {
async createUserPolicy(id: string): Promise<void> {
try {
const data = {
password: password,
policies: [String(id)],
};
let policyData = fs.readFileSync(
`./src/services/example-user-policy.hcl`,
'utf-8',
);
policyData = policyData.replace('{{id}}', id);
const response = await fetch(
`${this.vaultEndpoint}/v1/auth/userpass/users/${id}`,
`${this.vaultEndpoint}/v1/sys/policy/${id}`,
{
method: 'POST',
headers: {
'X-Vault-Token': this.rootToken,
},
body: JSON.stringify(data),
body: JSON.stringify({data: policyData}),
},
);
if (!response.ok) {
throw new Error(`Unable to create user`);
throw new Error(`Unable to create policy`);
}
} catch (error) {
throw new Error(`Authentication error: ${error.message}`);
Expand Down Expand Up @@ -172,4 +172,42 @@ export class VaultService {
throw new Error(`Authentication error: ${error.message}`);
}
}

async deleteUserPolicy(id: string): Promise<void> {
try {
const response = await fetch(
`${this.vaultEndpoint}/v1/sys/policy/acl/${id}`,
{
method: 'DELETE',
headers: {
'X-Vault-Token': this.rootToken,
},
},
);
if (!response.ok) {
throw new Error(`Unable to delete policy`);
}
} catch (error) {
throw new Error(`Authentication error: ${error.message}`);
}
}

async deleteUserKey(id: string): Promise<void> {
try {
const response = await fetch(
`${this.vaultEndpoint}/v1/transit/keys/${id}`,
{
method: 'DELETE',
headers: {
'X-Vault-Token': this.rootToken,
},
},
);
if (!response.ok) {
throw new Error(`Unable to delete key`);
}
} catch (error) {
throw new Error(`Authentication error: ${error.message}`);
}
}
}

0 comments on commit a9ea985

Please sign in to comment.