Update modify secrets api v2 so that fields are optional

This commit is contained in:
Maidul Islam
2023-01-12 15:31:58 -08:00
parent 47fd48b7b0
commit 71f60f1589
4 changed files with 141 additions and 158 deletions
+24 -29
View File
@@ -242,7 +242,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
secretCommentTag: string; secretCommentTag: string;
} }
const ops = req.body.secrets.map((secret: PatchSecret) => { const updateOperationsToPerform = req.body.secrets.map((secret: PatchSecret) => {
const { const {
secretKeyCiphertext, secretKeyCiphertext,
secretKeyIV, secretKeyIV,
@@ -254,6 +254,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
secretCommentIV, secretCommentIV,
secretCommentTag secretCommentTag
} = secret; } = secret;
return ({ return ({
updateOne: { updateOne: {
filter: { _id: new Types.ObjectId(secret.id) }, filter: { _id: new Types.ObjectId(secret.id) },
@@ -280,15 +281,17 @@ export const updateSecrets = async (req: Request, res: Response) => {
} }
}); });
}); });
await Secret.bulkWrite(ops);
const newSecretsObj: { [key: string]: PatchSecret } = {}; await Secret.bulkWrite(updateOperationsToPerform);
const secretModificationsBySecretId: { [key: string]: PatchSecret } = {};
req.body.secrets.forEach((secret: PatchSecret) => { req.body.secrets.forEach((secret: PatchSecret) => {
newSecretsObj[secret.id] = secret; secretModificationsBySecretId[secret.id] = secret;
}); });
await EESecretService.addSecretVersions({ const ListOfSecretsBeforeModifications = req.secrets
secretVersions: req.secrets.map((secret: ISecret) => { const secretVersions = {
secretVersions: ListOfSecretsBeforeModifications.map((secret: ISecret) => {
const { const {
secretKeyCiphertext, secretKeyCiphertext,
secretKeyIV, secretKeyIV,
@@ -298,37 +301,29 @@ export const updateSecrets = async (req: Request, res: Response) => {
secretValueTag, secretValueTag,
secretCommentCiphertext, secretCommentCiphertext,
secretCommentIV, secretCommentIV,
secretCommentTag secretCommentTag,
} = newSecretsObj[secret._id.toString()] } = secretModificationsBySecretId[secret._id.toString()]
return ({ return ({
secret: secret._id, secret: secret._id,
version: secret.version + 1, version: secret.version + 1,
workspace: secret.workspace, workspace: secret.workspace,
type: secret.type, type: secret.type,
environment: secret.environment, environment: secret.environment,
isDeleted: false, secretKeyCiphertext: secretKeyCiphertext ? secretKeyCiphertext : secret.secretKeyCiphertext,
secretKeyCiphertext, secretKeyIV: secretKeyIV ? secretKeyIV : secret.secretKeyIV,
secretKeyIV, secretKeyTag: secretKeyTag ? secretKeyTag : secret.secretKeyTag,
secretKeyTag, secretValueCiphertext: secretValueCiphertext ? secretValueCiphertext : secret.secretValueCiphertext,
secretValueCiphertext, secretValueIV: secretValueIV ? secretValueIV : secret.secretValueIV,
secretValueIV, secretValueTag: secretValueTag ? secretValueTag : secret.secretValueTag,
secretValueTag, secretCommentCiphertext: secretCommentCiphertext ? secretCommentCiphertext : secret.secretCommentCiphertext,
...(( secretCommentIV: secretCommentIV ? secretCommentIV : secret.secretCommentIV,
secretCommentCiphertext && secretCommentTag: secretCommentTag ? secretCommentTag : secret.secretCommentTag,
secretCommentIV &&
secretCommentTag
) ? {
secretCommentCiphertext,
secretCommentIV,
secretCommentTag
} : {
secretCommentCiphertext: '',
secretCommentIV: '',
secretCommentTag: ''
})
}); });
}) })
}); }
await EESecretService.addSecretVersions(secretVersions);
// group secrets into workspaces so updated secrets can // group secrets into workspaces so updated secrets can
+2 -2
View File
@@ -18,7 +18,7 @@ import {
* @param {String} obj.workspaceId * @param {String} obj.workspaceId
* @returns {SecretSnapshot} secretSnapshot - new secret snapshot * @returns {SecretSnapshot} secretSnapshot - new secret snapshot
*/ */
const takeSecretSnapshotHelper = async ({ const takeSecretSnapshotHelper = async ({
workspaceId workspaceId
}: { }: {
workspaceId: string; workspaceId: string;
@@ -87,7 +87,7 @@ const addSecretVersionsHelper = async ({
} catch (err) { } catch (err) {
Sentry.setUser(null); Sentry.setUser(null);
Sentry.captureException(err); Sentry.captureException(err);
throw new Error('Failed to add secret versions'); throw new Error(`Failed to add secret versions [err=${err}]`);
} }
return newSecretVersions; return newSecretVersions;
+4 -16
View File
@@ -95,29 +95,17 @@ router.patch(
if (value.length === 0) throw new Error('secrets cannot be an empty array') if (value.length === 0) throw new Error('secrets cannot be an empty array')
for (const secret of value) { for (const secret of value) {
if ( if (
!secret.id || !secret.id
!secret.secretKeyCiphertext ||
!secret.secretKeyIV ||
!secret.secretKeyTag ||
!secret.secretValueCiphertext ||
!secret.secretValueIV ||
!secret.secretValueTag
) { ) {
throw new Error('secrets array must contain objects that have required secret properties'); throw new Error('Each secret must contain a ID property');
} }
} }
} else if (typeof value === 'object') { } else if (typeof value === 'object') {
// case: update 1 secret // case: update 1 secret
if ( if (
!value.id || !value.id
!value.secretKeyCiphertext ||
!value.secretKeyIV ||
!value.secretKeyTag ||
!value.secretValueCiphertext ||
!value.secretValueIV ||
!value.secretValueTag
) { ) {
throw new Error('secrets object is missing required secret properties'); throw new Error('secret must contain a ID property');
} }
} else { } else {
throw new Error('secrets must be an object or an array of objects') throw new Error('secrets must be an object or an array of objects')