feat: ui removed all private key except secret rotation to raw endpoints version

This commit is contained in:
=
2024-07-30 23:19:05 +05:30
parent d4747abba8
commit b7b0e60b1d
12 changed files with 66 additions and 200 deletions
@@ -13,15 +13,13 @@ import {
import { apiRequest } from "@app/config/request";
import { UserWsKeyPair } from "../keys/types";
import { EncryptedSecret, SecretType,SecretV3RawSanitized } from "../secrets/types";
import { EncryptedSecret, SecretType, SecretV3RawSanitized } from "../secrets/types";
import {
CommitType,
TGetSecretApprovalRequestCount,
TGetSecretApprovalRequestDetails,
TGetSecretApprovalRequestList,
TSecretApprovalRequest,
TSecretApprovalRequestCount,
TSecretApprovalSecChangeData
TSecretApprovalRequestCount
} from "./types";
export const secretApprovalRequestKeys = {
@@ -117,48 +115,6 @@ export const decryptSecrets = (
return secrets;
};
export const decryptSecretApprovalSecret = (
encSecret: TSecretApprovalSecChangeData,
decryptFileKey: UserWsKeyPair
) => {
const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY") as string;
const key = decryptAssymmetric({
ciphertext: decryptFileKey.encryptedKey,
nonce: decryptFileKey.nonce,
publicKey: decryptFileKey.sender.publicKey,
privateKey: PRIVATE_KEY
});
const secretKey = decryptSymmetric({
ciphertext: encSecret.secretKeyCiphertext,
iv: encSecret.secretKeyIV,
tag: encSecret.secretKeyTag,
key
});
const secretValue = decryptSymmetric({
ciphertext: encSecret.secretValueCiphertext,
iv: encSecret.secretValueIV,
tag: encSecret.secretValueTag,
key
});
const secretComment = decryptSymmetric({
ciphertext: encSecret.secretCommentCiphertext,
iv: encSecret.secretCommentIV,
tag: encSecret.secretCommentTag,
key
});
return {
id: encSecret.id,
version: encSecret.version,
secretKey,
secretValue,
secretComment,
tags: encSecret.tags
};
};
const fetchSecretApprovalRequestList = async ({
workspaceId,
environment,
@@ -245,7 +201,7 @@ export const useGetSecretApprovalRequestDetails = ({
UseQueryOptions<
TSecretApprovalRequest,
unknown,
TSecretApprovalRequest<SecretV3RawSanitized>,
TSecretApprovalRequest,
ReturnType<typeof secretApprovalRequestKeys.detail>
>,
"queryKey" | "queryFn"
@@ -254,16 +210,6 @@ export const useGetSecretApprovalRequestDetails = ({
useQuery({
queryKey: secretApprovalRequestKeys.detail({ id }),
queryFn: () => fetchSecretApprovalRequestDetails({ id }),
select: (data) => ({
...data,
commits: data.commits.map(({ secretVersion, op, secret, ...newVersion }) => ({
op,
secret,
secretVersion: secretVersion ? decryptSecrets([secretVersion], decryptKey)[0] : undefined,
newVersion:
op !== CommitType.DELETE ? decryptSecretApprovalSecret(newVersion, decryptKey) : undefined
}))
}),
enabled: Boolean(id && decryptKey) && (options?.enabled ?? true)
});
@@ -1,6 +1,6 @@
import { UserWsKeyPair } from "../keys/types";
import { TSecretApprovalPolicy } from "../secretApproval/types";
import { EncryptedSecret } from "../secrets/types";
import { SecretV3Raw } from "../secrets/types";
import { WsTag } from "../tags/types";
export enum ApprovalStatus {
@@ -17,15 +17,9 @@ export enum CommitType {
export type TSecretApprovalSecChangeData = {
id: string;
secretKeyCiphertext: string;
secretKeyIV: string;
secretKeyTag: string;
secretValueCiphertext: string;
secretValueIV: string;
secretValueTag: string;
secretCommentIV: string;
secretCommentTag: string;
secretCommentCiphertext: string;
secretKey: string;
secretValue?: string;
secretComment?: string;
skipMultilineEncoding?: boolean;
algorithm: "aes-256-gcm";
keyEncoding: "utf8" | "base64";
@@ -37,12 +31,12 @@ export type TSecretApprovalSecChange = {
id: string;
version: number;
secretKey: string;
secretValue: string;
secretComment: string;
secretValue?: string;
secretComment?: string;
tags?: string[];
};
export type TSecretApprovalRequest<J extends unknown = EncryptedSecret> = {
export type TSecretApprovalRequest = {
id: string;
isReplicated?: boolean;
slug: string;
@@ -90,7 +84,7 @@ export type TSecretApprovalRequest<J extends unknown = EncryptedSecret> = {
commits: ({
// if there is no secret means it was creation
secret?: { version: number };
secretVersion: J;
secretVersion: SecretV3Raw;
// if there is no new version its for Delete
op: CommitType;
} & TSecretApprovalSecChangeData)[];
@@ -1,13 +1,9 @@
/* eslint-disable no-param-reassign */
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
decryptAssymmetric,
decryptSymmetric
} from "@app/components/utilities/cryptography/crypto";
import { apiRequest } from "@app/config/request";
import { SecretType,SecretV3RawSanitized } from "../secrets/types";
import { SecretType, SecretV3RawSanitized } from "../secrets/types";
import {
TGetSecretSnapshotsDTO,
TSecretRollbackDTO,
@@ -65,55 +61,33 @@ const fetchSnapshotEncSecrets = async (snapshotId: string) => {
return res.data.secretSnapshot;
};
export const useGetSnapshotSecrets = ({ decryptFileKey, snapshotId }: TSnapshotDataProps) =>
export const useGetSnapshotSecrets = ({ snapshotId }: TSnapshotDataProps) =>
useQuery({
queryKey: secretSnapshotKeys.snapshotData(snapshotId),
enabled: Boolean(snapshotId && decryptFileKey),
enabled: Boolean(snapshotId),
queryFn: () => fetchSnapshotEncSecrets(snapshotId),
select: (data) => {
const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY") as string;
const latestKey = decryptFileKey;
const key = decryptAssymmetric({
ciphertext: latestKey.encryptedKey,
nonce: latestKey.nonce,
publicKey: latestKey.sender.publicKey,
privateKey: PRIVATE_KEY
});
const sharedSecrets: SecretV3RawSanitized[] = [];
const personalSecrets: Record<string, { id: string; value: string }> = {};
data.secretVersions.forEach((encSecret) => {
const secretKey = decryptSymmetric({
ciphertext: encSecret.secretKeyCiphertext,
iv: encSecret.secretKeyIV,
tag: encSecret.secretKeyTag,
key
});
const secretValue = decryptSymmetric({
ciphertext: encSecret.secretValueCiphertext,
iv: encSecret.secretValueIV,
tag: encSecret.secretValueTag,
key
});
const secretComment = "";
data.secretVersions.forEach((secretVersion) => {
const decryptedSecret = {
id: encSecret.secretId,
id: secretVersion.secretId,
env: data.environment.slug,
key: secretKey,
value: secretValue,
tags: encSecret.tags,
comment: secretComment,
createdAt: encSecret.createdAt,
updatedAt: encSecret.updatedAt,
key: secretVersion.secretKey,
value: secretVersion.secretValue || "",
tags: secretVersion.tags,
comment: secretVersion.secretComment,
createdAt: secretVersion.createdAt,
updatedAt: secretVersion.updatedAt,
type: "modified",
version: encSecret.version
version: secretVersion.version
};
if (encSecret.type === SecretType.Personal) {
personalSecrets[decryptedSecret.key] = { id: encSecret.secretId, value: secretValue };
if (secretVersion.type === SecretType.Personal) {
personalSecrets[decryptedSecret.key] = {
id: secretVersion.secretId,
value: secretVersion.secretValue || ""
};
} else {
sharedSecrets.push(decryptedSecret);
}
@@ -1,5 +1,4 @@
import { UserWsKeyPair } from "../keys/types";
import { EncryptedSecretVersion } from "../secrets/types";
import { SecretVersions } from "../secrets/types";
import { WorkspaceEnv } from "../types";
export type TSecretSnapshot = {
@@ -12,7 +11,7 @@ export type TSecretSnapshot = {
export type TSnapshotData = Omit<TSecretSnapshot, "secretVersions"> & {
id: string;
secretVersions: EncryptedSecretVersion[];
secretVersions: SecretVersions[];
folderVersion: Array<{ name: string; id: string }>;
environment: WorkspaceEnv;
};
@@ -20,7 +19,6 @@ export type TSnapshotData = Omit<TSecretSnapshot, "secretVersions"> & {
export type TSnapshotDataProps = {
snapshotId: string;
env: string;
decryptFileKey: UserWsKeyPair;
};
export type TGetSecretSnapshotsDTO = {
+6 -33
View File
@@ -2,19 +2,15 @@
import { useCallback, useMemo } from "react";
import { useQueries, useQuery, UseQueryOptions } from "@tanstack/react-query";
import {
decryptAssymmetric,
decryptSymmetric
} from "@app/components/utilities/cryptography/crypto";
import { apiRequest } from "@app/config/request";
import {
EncryptedSecretVersion,
GetSecretVersionsDTO,
SecretType,
SecretV3Raw,
SecretV3RawResponse,
SecretV3RawSanitized,
SecretVersions,
TGetProjectSecretsAllEnvDTO,
TGetProjectSecretsDTO,
TGetProjectSecretsKey
@@ -166,7 +162,7 @@ export const useGetProjectSecretsAllEnv = ({
};
const fetchEncryptedSecretVersion = async (secretId: string, offset: number, limit: number) => {
const { data } = await apiRequest.get<{ secretVersions: EncryptedSecretVersion[] }>(
const { data } = await apiRequest.get<{ secretVersions: SecretVersions[] }>(
`/api/v1/secret/${secretId}/secret-versions`,
{
params: {
@@ -180,33 +176,10 @@ const fetchEncryptedSecretVersion = async (secretId: string, offset: number, lim
export const useGetSecretVersion = (dto: GetSecretVersionsDTO) =>
useQuery({
enabled: Boolean(dto.secretId && dto.decryptFileKey),
enabled: Boolean(dto.secretId),
queryKey: secretKeys.getSecretVersion(dto.secretId),
queryFn: () => fetchEncryptedSecretVersion(dto.secretId, dto.offset, dto.limit),
select: useCallback(
(data: EncryptedSecretVersion[]) => {
const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY") as string;
const latestKey = dto.decryptFileKey;
const key = decryptAssymmetric({
ciphertext: latestKey.encryptedKey,
nonce: latestKey.nonce,
publicKey: latestKey.sender.publicKey,
privateKey: PRIVATE_KEY
});
return data
.map((el) => ({
createdAt: el.createdAt,
id: el.id,
value: decryptSymmetric({
ciphertext: el.secretValueCiphertext,
iv: el.secretValueIV,
tag: el.secretValueTag,
key
})
}))
.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
},
[dto.decryptFileKey]
)
select: useCallback((data: SecretVersions[]) => {
return data.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
}, [])
});
+4 -9
View File
@@ -1,4 +1,3 @@
import type { UserWsKeyPair } from "../keys/types";
import type { WsTag } from "../tags/types";
export enum SecretType {
@@ -79,7 +78,7 @@ export type SecretV3RawResponse = {
}[];
};
export type EncryptedSecretVersion = {
export type SecretVersions = {
id: string;
secretId: string;
version: number;
@@ -87,12 +86,9 @@ export type EncryptedSecretVersion = {
type: SecretType;
isDeleted: boolean;
envId: string;
secretKeyCiphertext: string;
secretKeyIV: string;
secretKeyTag: string;
secretValueCiphertext: string;
secretValueIV: string;
secretValueTag: string;
secretKey: string;
secretValue?: string;
secretComment?: string;
tags: WsTag[];
__v: number;
skipMultilineEncoding?: boolean;
@@ -123,7 +119,6 @@ export type GetSecretVersionsDTO = {
secretId: string;
limit: number;
offset: number;
decryptFileKey: UserWsKeyPair;
};
export type TCreateSecretsV3DTO = {