Skip to content

Commit

Permalink
Merge pull request #682 from atimapreandrew/remove-unnecessary-backen…
Browse files Browse the repository at this point in the history
…d-dependencies

removed await-to-js and builder-pattern dependencies from backend
  • Loading branch information
dangtony98 authored Jun 25, 2023
2 parents 6b222ba + ca3f7ba commit 68c8dad
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 138 deletions.
25 changes: 0 additions & 25 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
"@types/crypto-js": "^4.1.1",
"@types/libsodium-wrappers": "^0.7.10",
"argon2": "^0.30.3",
"await-to-js": "^3.0.0",
"aws-sdk": "^2.1364.0",
"axios": "^1.3.5",
"axios-retry": "^3.4.0",
"bcrypt": "^5.1.0",
"bigint-conversion": "^2.4.0",
"builder-pattern": "^2.2.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
Expand Down
116 changes: 30 additions & 86 deletions backend/src/controllers/v2/secretController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import to from "await-to-js";
import { Request, Response } from "express";
import mongoose, { Types } from "mongoose";
import Secret, { ISecret } from "../../models/secret";
Expand Down Expand Up @@ -56,10 +55,7 @@ export const createSecret = async (req: Request, res: Response) => {
keyEncoding: ENCODING_SCHEME_UTF8
};

const [error, secret] = await to(Secret.create(sanitizedSecret).then());
if (error instanceof ValidationError) {
throw RouteValidationError({ message: error.message, stack: error.stack });
}
const secret = await new Secret(sanitizedSecret).save();

if (postHogClient) {
postHogClient.capture({
Expand All @@ -81,7 +77,7 @@ export const createSecret = async (req: Request, res: Response) => {
};

/**
* Create many secrets for workspace wiht id [workspaceId] and environment [environment]
* Create many secrets for workspace with id [workspaceId] and environment [environment]
* @param req
* @param res
*/
Expand Down Expand Up @@ -116,20 +112,7 @@ export const createSecrets = async (req: Request, res: Response) => {
sanitizedSecretesToCreate.push(safeUpdateFields);
});

const [bulkCreateError, secrets] = await to(Secret.insertMany(sanitizedSecretesToCreate).then());
if (bulkCreateError) {
if (bulkCreateError instanceof ValidationError) {
throw RouteValidationError({
message: bulkCreateError.message,
stack: bulkCreateError.stack
});
}

throw InternalServerError({
message: "Unable to process your batch create request. Please try again",
stack: bulkCreateError.stack
});
}
const secrets = await Secret.insertMany(sanitizedSecretesToCreate);

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -160,14 +143,7 @@ export const deleteSecrets = async (req: Request, res: Response) => {
const { workspaceId, environmentName } = req.params;
const secretIdsToDelete: string[] = req.body.secretIds;

const [secretIdsUserCanDeleteError, secretIdsUserCanDelete] = await to(
Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 }).then()
);
if (secretIdsUserCanDeleteError) {
throw InternalServerError({
message: `Unable to fetch secrets you own: [error=${secretIdsUserCanDeleteError.message}]`
});
}
const secretIdsUserCanDelete = await Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 });

const secretsUserCanDeleteSet: Set<string> = new Set(
secretIdsUserCanDelete.map((objectId) => objectId._id.toString())
Expand All @@ -189,16 +165,7 @@ export const deleteSecrets = async (req: Request, res: Response) => {
}
});

const [bulkDeleteError] = await to(Secret.bulkWrite(deleteOperationsToPerform).then());
if (bulkDeleteError) {
if (bulkDeleteError instanceof ValidationError) {
throw RouteValidationError({
message: "Unable to apply modifications, please try again",
stack: bulkDeleteError.stack
});
}
throw InternalServerError();
}
await Secret.bulkWrite(deleteOperationsToPerform);

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -255,12 +222,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
const postHogClient = await TelemetryService.getPostHogClient();
const { workspaceId, environmentName } = req.params;
const secretsModificationsRequested: ModifySecretRequestBody[] = req.body.secrets;
const [secretIdsUserCanModifyError, secretIdsUserCanModify] = await to(
Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 }).then()
);
if (secretIdsUserCanModifyError) {
throw InternalServerError({ message: "Unable to fetch secrets you own" });
}
const secretIdsUserCanModify = await Secret.find({ workspace: workspaceId, environment: environmentName }, { _id: 1 });

const secretsUserCanModifySet: Set<string> = new Set(
secretIdsUserCanModify.map((objectId) => objectId._id.toString())
Expand Down Expand Up @@ -298,19 +260,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
}
});

const [bulkModificationInfoError, bulkModificationInfo] = await to(
Secret.bulkWrite(updateOperationsToPerform).then()
);
if (bulkModificationInfoError) {
if (bulkModificationInfoError instanceof ValidationError) {
throw RouteValidationError({
message: "Unable to apply modifications, please try again",
stack: bulkModificationInfoError.stack
});
}

throw InternalServerError();
}
await Secret.bulkWrite(updateOperationsToPerform);

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -340,12 +290,7 @@ export const updateSecret = async (req: Request, res: Response) => {
const { workspaceId, environmentName } = req.params;
const secretModificationsRequested: ModifySecretRequestBody = req.body.secret;

const [secretIdUserCanModifyError, secretIdUserCanModify] = await to(
Secret.findOne({ workspace: workspaceId, environment: environmentName }, { _id: 1 }).then()
);
if (secretIdUserCanModifyError && !secretIdUserCanModify) {
throw BadRequestError();
}
const secretIdUserCanModify = await Secret.findOne({ workspace: workspaceId, environment: environmentName }, { _id: 1 });

const sanitizedSecret: SanitizedSecretModify = {
secretKeyCiphertext: secretModificationsRequested.secretKeyCiphertext,
Expand All @@ -362,18 +307,20 @@ export const updateSecret = async (req: Request, res: Response) => {
secretCommentHash: secretModificationsRequested.secretCommentHash
};

const [error, singleModificationUpdate] = await to(
Secret.updateOne(
{ _id: secretModificationsRequested._id, workspace: workspaceId },
{ $inc: { version: 1 }, $set: sanitizedSecret }
).then()
);
if (error instanceof ValidationError) {
throw RouteValidationError({
message: "Unable to apply modifications, please try again",
stack: error.stack
});
}
const singleModificationUpdate = await Secret.updateOne(
{ _id: secretModificationsRequested._id, workspace: workspaceId },
{ $inc: { version: 1 }, $set: sanitizedSecret }
)
.catch((error) => {
if (error instanceof ValidationError) {
throw RouteValidationError({
message: "Unable to apply modifications, please try again",
stack: error.stack
});
}

throw error;
});

if (postHogClient) {
postHogClient.capture({
Expand Down Expand Up @@ -419,21 +366,18 @@ export const getSecrets = async (req: Request, res: Response) => {
userEmail = user.email;
}

const [err, secrets] = await to(
Secret.find({
workspace: workspaceId,
environment,
$or: [{ user: userId }, { user: { $exists: false } }],
type: { $in: [SECRET_SHARED, SECRET_PERSONAL] }
}).then()
);

if (err) {
const secrets = await Secret.find({
workspace: workspaceId,
environment,
$or: [{ user: userId }, { user: { $exists: false } }],
type: { $in: [SECRET_SHARED, SECRET_PERSONAL] }
})
.catch((err) => {
throw RouteValidationError({
message: "Failed to get secrets, please try again",
stack: err.stack
});
}
})

if (postHogClient) {
postHogClient.capture({
Expand Down
31 changes: 11 additions & 20 deletions backend/src/controllers/v2/tagController.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import { Request, Response } from "express";
import { Types } from "mongoose";
import { Membership, Secret } from "../../models";
import Tag, { ITag } from "../../models/tag";
import { Builder } from "builder-pattern";
import to from "await-to-js";
import Tag from "../../models/tag";
import { BadRequestError, UnauthorizedRequestError } from "../../utils/errors";
import { MongoError } from "mongodb";

export const createWorkspaceTag = async (req: Request, res: Response) => {
const { workspaceId } = req.params;
const { name, slug } = req.body;
const sanitizedTagToCreate = Builder<ITag>()
.name(name)
.workspace(new Types.ObjectId(workspaceId))
.slug(slug)
.user(new Types.ObjectId(req.user._id))
.build();

const [err, createdTag] = await to(Tag.create(sanitizedTagToCreate));

if (err) {
if ((err as MongoError).code === 11000) {
throw BadRequestError({ message: "Tags must be unique in a workspace" });
}

throw err;
}


const tagToCreate = {
name,
workspace: new Types.ObjectId(workspaceId),
slug,
user: new Types.ObjectId(req.user._id),
};

const createdTag = await new Tag(tagToCreate);

res.json(createdTag);
};

Expand Down
9 changes: 4 additions & 5 deletions backend/src/ee/controllers/v1/membershipController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { IMembershipPermission } from "../../../models/membership";
import { BadRequestError, UnauthorizedRequestError } from "../../../utils/errors";
import { ADMIN, MEMBER } from "../../../variables/organization";
import { PERMISSION_READ_SECRETS, PERMISSION_WRITE_SECRETS } from "../../../variables";
import { Builder } from "builder-pattern"
import _ from "lodash";

export const denyMembershipPermissions = async (req: Request, res: Response) => {
Expand All @@ -15,10 +14,10 @@ export const denyMembershipPermissions = async (req: Request, res: Response) =>
throw BadRequestError({ message: "One or more required fields are missing from the request or have incorrect type" })
}

return Builder<IMembershipPermission>()
.environmentSlug(permission.environmentSlug)
.ability(permission.ability)
.build();
return {
environmentSlug: permission.environmentSlug,
ability: permission.ability
}
})

const sanitizedMembershipPermissionsUnique = _.uniqWith(sanitizedMembershipPermissions, _.isEqual)
Expand Down

0 comments on commit 68c8dad

Please sign in to comment.