feat: changed secret update to use id

This commit is contained in:
Akhil Mohan
2023-10-17 15:38:30 +05:30
parent 275416a08f
commit ee54fdabe1
10 changed files with 35 additions and 11 deletions

View File

@@ -476,7 +476,7 @@ export const getSecrets = async (req: Request, res: Response) => {
if (folderId && folderId !== "root") { if (folderId && folderId !== "root") {
const folder = await Folder.findOne({ workspace: workspaceId, environment }); const folder = await Folder.findOne({ workspace: workspaceId, environment });
if (!folder) throw BadRequestError({ message: "Folder not found" }); if (!folder) return res.send({ secrets: [] });
secretPath = getFolderWithPathFromId(folder.nodes, folderId).folderPath; secretPath = getFolderWithPathFromId(folder.nodes, folderId).folderPath;
} }
@@ -673,6 +673,7 @@ export const updateSecretByName = async (req: Request, res: Response) => {
secretValueCiphertext, secretValueCiphertext,
secretValueTag, secretValueTag,
secretValueIV, secretValueIV,
secretId,
type, type,
environment, environment,
secretPath, secretPath,
@@ -741,6 +742,7 @@ export const updateSecretByName = async (req: Request, res: Response) => {
workspaceId: new Types.ObjectId(workspaceId), workspaceId: new Types.ObjectId(workspaceId),
environment, environment,
type, type,
secretId,
authData: req.authData, authData: req.authData,
newSecretName, newSecretName,
secretValueCiphertext, secretValueCiphertext,

View File

@@ -790,6 +790,7 @@ export const getSecretHelper = async ({
export const updateSecretHelper = async ({ export const updateSecretHelper = async ({
secretName, secretName,
workspaceId, workspaceId,
secretId,
environment, environment,
type, type,
authData, authData,
@@ -812,11 +813,20 @@ export const updateSecretHelper = async ({
workspaceId: new Types.ObjectId(workspaceId) workspaceId: new Types.ObjectId(workspaceId)
}); });
const oldSecretBlindIndex = await generateSecretBlindIndexWithSaltHelper({ let oldSecretBlindIndex = await generateSecretBlindIndexWithSaltHelper({
secretName, secretName,
salt salt
}); });
if (secretId) {
const secret = await Secret.findOne({
workspace: workspaceId,
environment,
_id: secretId
}).select("secretBlindIndex");
if (secret && secret.secretBlindIndex) oldSecretBlindIndex = secret.secretBlindIndex;
}
let secret: ISecret | null = null; let secret: ISecret | null = null;
const folderId = await getFolderIdFromServiceToken(workspaceId, environment, secretPath); const folderId = await getFolderIdFromServiceToken(workspaceId, environment, secretPath);
@@ -891,6 +901,9 @@ export const updateSecretHelper = async ({
skipMultilineEncoding, skipMultilineEncoding,
secretBlindIndex: newSecretNameBlindIndex, secretBlindIndex: newSecretNameBlindIndex,
$inc: { version: 1 } $inc: { version: 1 }
},
{
new: true
} }
); );
} }

View File

@@ -44,6 +44,7 @@ export interface GetSecretParams {
export interface UpdateSecretParams { export interface UpdateSecretParams {
secretName: string; secretName: string;
newSecretName?: string; newSecretName?: string;
secretId?: string;
secretKeyCiphertext?: string; secretKeyCiphertext?: string;
secretKeyIV?: string; secretKeyIV?: string;
secretKeyTag?: string; secretKeyTag?: string;

View File

@@ -353,6 +353,7 @@ export const UpdateSecretByNameV3 = z.object({
body: z.object({ body: z.object({
workspaceId: z.string().trim(), workspaceId: z.string().trim(),
environment: z.string().trim(), environment: z.string().trim(),
secretId: z.string().trim().optional(),
type: z.enum([SECRET_SHARED, SECRET_PERSONAL]), type: z.enum([SECRET_SHARED, SECRET_PERSONAL]),
secretPath: z.string().trim().default("/"), secretPath: z.string().trim().default("/"),
secretValueCiphertext: z.string().trim(), secretValueCiphertext: z.string().trim(),

View File

@@ -131,6 +131,7 @@ export const useUpdateSecretV3 = ({
mutationFn: async ({ mutationFn: async ({
secretPath = "/", secretPath = "/",
type, type,
secretId,
environment, environment,
workspaceId, workspaceId,
secretName, secretName,
@@ -157,6 +158,7 @@ export const useUpdateSecretV3 = ({
environment, environment,
type, type,
secretPath, secretPath,
secretId,
...encryptSecret(randomBytes, newSecretName ?? secretName, secretValue, secretComment), ...encryptSecret(randomBytes, newSecretName ?? secretName, secretValue, secretComment),
tags, tags,
skipMultilineEncoding, skipMultilineEncoding,

View File

@@ -109,6 +109,7 @@ export type TUpdateSecretsV3DTO = {
skipMultilineEncoding?: boolean; skipMultilineEncoding?: boolean;
newSecretName?: string; newSecretName?: string;
secretName: string; secretName: string;
secretId?: string;
secretValue: string; secretValue: string;
secretComment?: string; secretComment?: string;
tags?: string[]; tags?: string[];

View File

@@ -153,6 +153,7 @@ export const SecretListView = ({
workspaceId, workspaceId,
secretPath, secretPath,
secretName: key, secretName: key,
secretId,
secretValue: value || "", secretValue: value || "",
type, type,
latestFileKey: decryptFileKey, latestFileKey: decryptFileKey,
@@ -201,11 +202,14 @@ export const SecretListView = ({
try { try {
// personal secret change // personal secret change
if (overrideAction === "deleted") { if (overrideAction === "deleted") {
await handleSecretOperation("delete", "personal", oldKey); await handleSecretOperation("delete", "personal", oldKey, {
secretId: orgSecret.idOverride
});
} else if (overrideAction && idOverride) { } else if (overrideAction && idOverride) {
await handleSecretOperation("update", "personal", oldKey, { await handleSecretOperation("update", "personal", oldKey, {
value: valueOverride, value: valueOverride,
newKey: hasKeyChanged ? key : undefined, newKey: hasKeyChanged ? key : undefined,
secretId: orgSecret.idOverride,
skipMultilineEncoding: modSecret.skipMultilineEncoding skipMultilineEncoding: modSecret.skipMultilineEncoding
}); });
} else if (overrideAction) { } else if (overrideAction) {
@@ -218,6 +222,7 @@ export const SecretListView = ({
value, value,
tags: tagIds, tags: tagIds,
comment, comment,
secretId: orgSecret._id,
newKey: hasKeyChanged ? key : undefined, newKey: hasKeyChanged ? key : undefined,
skipMultilineEncoding: modSecret.skipMultilineEncoding skipMultilineEncoding: modSecret.skipMultilineEncoding
}); });
@@ -308,7 +313,6 @@ export const SecretListView = ({
> >
{namespace} {namespace}
</div> </div>
{filteredSecrets.map((secret) => ( {filteredSecrets.map((secret) => (
<SecretItem <SecretItem
environment={environment} environment={environment}

View File

@@ -146,12 +146,13 @@ export const SecretOverviewPage = () => {
} }
}; };
const handleSecretUpdate = async (env: string, key: string, value: string) => { const handleSecretUpdate = async (env: string, key: string, value: string, secretId?: string) => {
try { try {
await updateSecretV3({ await updateSecretV3({
environment: env, environment: env,
workspaceId, workspaceId,
secretPath, secretPath,
secretId,
secretName: key, secretName: key,
secretValue: value, secretValue: value,
type: "shared", type: "shared",
@@ -242,7 +243,6 @@ export const SecretOverviewPage = () => {
); );
const canViewOverviewPage = Boolean(userAvailableEnvs.length); const canViewOverviewPage = Boolean(userAvailableEnvs.length);
const filteredSecretNames = secKeys const filteredSecretNames = secKeys
?.filter((name) => name.toUpperCase().includes(searchFilter.toUpperCase())) ?.filter((name) => name.toUpperCase().includes(searchFilter.toUpperCase()))
.sort((a, b) => (sortDir === "asc" ? a.localeCompare(b) : b.localeCompare(a))); .sort((a, b) => (sortDir === "asc" ? a.localeCompare(b) : b.localeCompare(a)));

View File

@@ -18,7 +18,7 @@ type Props = {
environment: string; environment: string;
secretPath: string; secretPath: string;
onSecretCreate: (env: string, key: string, value: string) => Promise<void>; onSecretCreate: (env: string, key: string, value: string) => Promise<void>;
onSecretUpdate: (env: string, key: string, value: string) => Promise<void>; onSecretUpdate: (env: string, key: string, value: string, secretId?: string) => Promise<void>;
onSecretDelete: (env: string, key: string, secretId?: string) => Promise<void>; onSecretDelete: (env: string, key: string, secretId?: string) => Promise<void>;
}; };
@@ -42,7 +42,7 @@ export const SecretEditRow = ({
formState: { isDirty, isSubmitting } formState: { isDirty, isSubmitting }
} = useForm({ } = useForm({
values: { values: {
value: defaultValue value: defaultValue || null
} }
}); });
const [isDeleting, setIsDeleting] = useToggle(); const [isDeleting, setIsDeleting] = useToggle();
@@ -70,7 +70,7 @@ export const SecretEditRow = ({
if (isCreatable) { if (isCreatable) {
await onSecretCreate(environment, secretName, value); await onSecretCreate(environment, secretName, value);
} else { } else {
await onSecretUpdate(environment, secretName, value); await onSecretUpdate(environment, secretName, value, secretId);
} }
} }
reset({ value }); reset({ value });
@@ -80,7 +80,7 @@ export const SecretEditRow = ({
setIsDeleting.on(); setIsDeleting.on();
try { try {
await onSecretDelete(environment, secretName, secretId); await onSecretDelete(environment, secretName, secretId);
reset({ value: undefined }); reset({ value: null });
} finally { } finally {
setIsDeleting.off(); setIsDeleting.off();
} }

View File

@@ -23,7 +23,7 @@ type Props = {
expandableColWidth: number; expandableColWidth: number;
getSecretByKey: (slug: string, key: string) => DecryptedSecret | undefined; getSecretByKey: (slug: string, key: string) => DecryptedSecret | undefined;
onSecretCreate: (env: string, key: string, value: string) => Promise<void>; onSecretCreate: (env: string, key: string, value: string) => Promise<void>;
onSecretUpdate: (env: string, key: string, value: string) => Promise<void>; onSecretUpdate: (env: string, key: string, value: string, secretId?: string) => Promise<void>;
onSecretDelete: (env: string, key: string, secretId?: string) => Promise<void>; onSecretDelete: (env: string, key: string, secretId?: string) => Promise<void>;
}; };