Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dangtony98 committed Jan 6, 2023
2 parents 098ae85 + 37998b8 commit 08dc453
Show file tree
Hide file tree
Showing 111 changed files with 6,106 additions and 3,872 deletions.
12 changes: 6 additions & 6 deletions backend/package-lock.json

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

6 changes: 5 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { apiLimiter } from './helpers/rateLimiter';

import {
workspace as eeWorkspaceRouter,
secret as eeSecretRouter
secret as eeSecretRouter,
secretSnapshot as eeSecretSnapshotRouter,
action as eeActionRouter
} from './ee/routes/v1';
import {
signup as v1SignupRouter,
Expand Down Expand Up @@ -70,7 +72,9 @@ if (NODE_ENV === 'production') {

// (EE) routes
app.use('/api/v1/secret', eeSecretRouter);
app.use('/api/v1/secret-snapshot', eeSecretSnapshotRouter);
app.use('/api/v1/workspace', eeWorkspaceRouter);
app.use('/api/v1/action', eeActionRouter);

// v1 routes
app.use('/api/v1/signup', v1SignupRouter);
Expand Down
8 changes: 6 additions & 2 deletions backend/src/controllers/v1/secretController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export const pullSecrets = async (req: Request, res: Response) => {
secrets = await pull({
userId: req.user._id.toString(),
workspaceId,
environment
environment,
channel: channel ? channel : 'cli',
ipAddress: req.ip
});

key = await Key.findOne({
Expand Down Expand Up @@ -188,7 +190,9 @@ export const pullSecretsServiceToken = async (req: Request, res: Response) => {
secrets = await pull({
userId: req.serviceToken.user._id.toString(),
workspaceId,
environment
environment,
channel: 'cli',
ipAddress: req.ip
});

key = {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controllers/v2/secretController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import to from "await-to-js";
import { Request, Response } from "express";
import mongoose, { Types } from "mongoose";
import Secret, { ISecret } from "../../models/secret";
import { CreateSecretRequestBody, ModifySecretRequestBody, SanitizedSecretForCreate, SanitizedSecretModify } from "../../types/secret/types";
import { CreateSecretRequestBody, ModifySecretRequestBody, SanitizedSecretForCreate, SanitizedSecretModify } from "../../types/secret";
const { ValidationError } = mongoose.Error;
import { BadRequestError, InternalServerError, UnauthorizedRequestError, ValidationError as RouteValidationError } from '../../utils/errors';
import { AnyBulkWriteOperation } from 'mongodb';
Expand Down
15 changes: 7 additions & 8 deletions backend/src/controllers/v2/workspaceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import {
ServiceToken,
ServiceTokenData
} from '../../models';
import {
createWorkspace as create,
deleteWorkspace as deleteWork
} from '../../helpers/workspace';
import {
v2PushSecrets as push,
pullSecrets as pull,
Expand Down Expand Up @@ -50,7 +46,6 @@ interface V2PushSecret {
*/
export const pushWorkspaceSecrets = async (req: Request, res: Response) => {
// upload (encrypted) secrets to workspace with id [workspaceId]

try {
let { secrets }: { secrets: V2PushSecret[] } = req.body;
const { keys, environment, channel } = req.body;
Expand All @@ -70,7 +65,9 @@ export const pushWorkspaceSecrets = async (req: Request, res: Response) => {
userId: req.user._id,
workspaceId,
environment,
secrets
secrets,
channel: channel ? channel : 'cli',
ipAddress: req.ip
});

await pushKeys({
Expand Down Expand Up @@ -136,7 +133,9 @@ export const pullSecrets = async (req: Request, res: Response) => {
secrets = await pull({
userId,
workspaceId,
environment
environment,
channel: channel ? channel : 'cli',
ipAddress: req.ip
});

if (channel !== 'cli') {
Expand Down Expand Up @@ -196,7 +195,7 @@ export const getWorkspaceServiceTokenData = async (
) => {
let serviceTokenData;
try {
const { workspaceId } = req.query;
const { workspaceId } = req.params;

serviceTokenData = await ServiceTokenData
.find({
Expand Down
31 changes: 31 additions & 0 deletions backend/src/ee/controllers/v1/actionController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Request, Response } from 'express';
import * as Sentry from '@sentry/node';
import { Action, SecretVersion } from '../../models';
import { ActionNotFoundError } from '../../../utils/errors';

export const getAction = async (req: Request, res: Response) => {
let action;
try {
const { actionId } = req.params;

action = await Action
.findById(actionId)
.populate([
'payload.secretVersions.oldSecretVersion',
'payload.secretVersions.newSecretVersion'
]);

if (!action) throw ActionNotFoundError({
message: 'Failed to find action'
});

} catch (err) {
throw ActionNotFoundError({
message: 'Failed to find action'
});
}

return res.status(200).send({
action
});
}
6 changes: 5 additions & 1 deletion backend/src/ee/controllers/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as stripeController from './stripeController';
import * as secretController from './secretController';
import * as secretSnapshotController from './secretSnapshotController';
import * as workspaceController from './workspaceController';
import * as actionController from './actionController';

export {
stripeController,
secretController,
workspaceController
secretSnapshotController,
workspaceController,
actionController
}
1 change: 1 addition & 0 deletions backend/src/ee/controllers/v1/secretController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SecretVersion } from '../../models';
secretVersions = await SecretVersion.find({
secret: secretId
})
.sort({ createdAt: -1 })
.skip(offset)
.limit(limit);

Expand Down
27 changes: 27 additions & 0 deletions backend/src/ee/controllers/v1/secretSnapshotController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Request, Response } from 'express';
import * as Sentry from '@sentry/node';
import { SecretSnapshot } from '../../models';

export const getSecretSnapshot = async (req: Request, res: Response) => {
let secretSnapshot;
try {
const { secretSnapshotId } = req.params;

secretSnapshot = await SecretSnapshot
.findById(secretSnapshotId)
.populate('secretVersions');

if (!secretSnapshot) throw new Error('Failed to find secret snapshot');

} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to get secret snapshot'
});
}

return res.status(200).send({
secretSnapshot
});
}
81 changes: 79 additions & 2 deletions backend/src/ee/controllers/v1/workspaceController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Request, Response } from 'express';
import e, { Request, Response } from 'express';
import * as Sentry from '@sentry/node';
import { SecretSnapshot } from '../../models';
import {
SecretSnapshot,
Log
} from '../../models';

/**
* Return secret snapshots for workspace with id [workspaceId]
Expand All @@ -18,6 +21,7 @@ import { SecretSnapshot } from '../../models';
secretSnapshots = await SecretSnapshot.find({
workspace: workspaceId
})
.sort({ createdAt: -1 })
.skip(offset)
.limit(limit);

Expand All @@ -32,4 +36,77 @@ import { SecretSnapshot } from '../../models';
return res.status(200).send({
secretSnapshots
});
}

/**
* Return count of secret snapshots for workspace with id [workspaceId]
* @param req
* @param res
*/
export const getWorkspaceSecretSnapshotsCount = async (req: Request, res: Response) => {
let count;
try {
const { workspaceId } = req.params;
count = await SecretSnapshot.countDocuments({
workspace: workspaceId
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to count number of secret snapshots'
});
}

return res.status(200).send({
count
});
}

/**
* Return (audit) logs for workspace with id [workspaceId]
* @param req
* @param res
* @returns
*/
export const getWorkspaceLogs = async (req: Request, res: Response) => {
let logs
try {
const { workspaceId } = req.params;

const offset: number = parseInt(req.query.offset as string);
const limit: number = parseInt(req.query.limit as string);
const sortBy: string = req.query.sortBy as string;
const userId: string = req.query.userId as string;
const actionNames: string = req.query.actionNames as string;

logs = await Log.find({
workspace: workspaceId,
...( userId ? { user: userId } : {}),
...(
actionNames
? {
actionNames: {
$in: actionNames.split(',')
}
} : {}
)
})
.sort({ createdAt: sortBy === 'recent' ? -1 : 1 })
.skip(offset)
.limit(limit)
.populate('actions')
.populate('user');

} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to get workspace logs'
});
}

return res.status(200).send({
logs
});
}
Loading

0 comments on commit 08dc453

Please sign in to comment.