Skip to content

Commit

Permalink
fix:final review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
HatemMn committed Nov 20, 2024
1 parent 9870096 commit 720a4f9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
6 changes: 3 additions & 3 deletions src/actions/javascript/encryptDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { findexDatabaseEmployee, findexDatabaseEmployeeBytes } from "../../utils/covercryptConfig";
import { findexDatabaseEmployee, findexDatabaseEncryptedEmployees } from "../../utils/covercryptConfig";
import { AesGcm } from "cloudproof_js";

export const encryptDatabase = async (
clearDatabase: findexDatabaseEmployee[],
key: Uint8Array,
nonce: Uint8Array,
authData: Uint8Array
): Promise<findexDatabaseEmployeeBytes[]> => {
): Promise<findexDatabaseEncryptedEmployees[]> => {
const { Aes256Gcm } = await AesGcm();

return Promise.all(
Expand All @@ -22,7 +22,7 @@ export const encryptDatabase = async (
[index]: Aes256Gcm.encrypt(field, key, nonce, authData),
};
},
{ uuid: e.uuid } as findexDatabaseEmployeeBytes
{ uuid: e.uuid } as findexDatabaseEncryptedEmployees
)
)
);
Expand Down
31 changes: 11 additions & 20 deletions src/pages/findex/EncryptDatabase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import { useBoundStore, useFindexStore } from "../../store/store";
import { findCurrentNavigationItem, updateNavigationSteps } from "../../utils/navigationActions";
import { Language } from "../../utils/types";
import { message } from "antd";
import { findexDatabaseEmployee, findexDatabaseEmployeeBytes } from "../../utils/covercryptConfig";
import { findexDatabaseEmployee, findexDatabaseEncryptedEmployees } from "../../utils/covercryptConfig";
import { encryptDatabase } from "../../actions/javascript/encryptDatabase";
import { byteEmployeeToString } from "../../utils/utils";

const activeLanguageList: Language[] = ["javascript"];

const EncryptDatabase = (): JSX.Element => {
// custom hooks
const { loadingCode, codeContent } = useFetchCodeContent("encryptDatabase", activeLanguageList);
// states
const { clearDatabase, encryptedDatabase, setEncryptedDb } = useFindexStore((state) => state);
const { clearDatabase, encryptedDatabase, setEncryptedDatabase } = useFindexStore((state) => state);
const { steps, setSteps } = useBoundStore((state) => state);
const navigate = useNavigate();
const currentItem = findCurrentNavigationItem(steps);
Expand All @@ -34,8 +35,13 @@ const EncryptDatabase = (): JSX.Element => {
const nonce = getRandomBytes(12);
const authenticatedData = getRandomBytes(20);

const encryptedBytesDatabase: findexDatabaseEmployeeBytes[] = await encryptDatabase(clearDatabase, key, nonce, authenticatedData);
setEncryptedDb({
const encryptedBytesDatabase: findexDatabaseEncryptedEmployees[] = await encryptDatabase(
clearDatabase,
key,
nonce,
authenticatedData
);
setEncryptedDatabase({
encryptedBytesDatabase,
key,
nonce,
Expand All @@ -52,22 +58,7 @@ const EncryptDatabase = (): JSX.Element => {

if (loadingCode) return <ContentSkeleton />;

const decodedEmployeeDatabase: findexDatabaseEmployee[] | undefined = encryptedDatabase?.encryptedBytesDatabase
? encryptedDatabase.encryptedBytesDatabase.map((e) =>
Object.keys(e)
.filter((k) => k !== "uuid")
.reduce(
(acc, k) => {
const index = k as keyof Omit<typeof e, "uuid">;
return {
...acc,
[index]: new TextDecoder().decode(e[index]),
};
},
{ uuid: e.uuid } as findexDatabaseEmployee
)
)
: undefined;
const decodedEmployeeDatabase: findexDatabaseEmployee[] | undefined = encryptedDatabase?.encryptedBytesDatabase.map(byteEmployeeToString);
return (
<Split>
<Split.Content>
Expand Down
33 changes: 17 additions & 16 deletions src/pages/findex/SearchInDatabase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ import { findCurrentNavigationItem, updateNavigationSteps } from "../../utils/na
import { Language } from "../../utils/types";

import ContentSkeleton from "../../component/ContentSkeleton";
import { findexDatabaseEmployee, findexDatabaseEmployeeBytes } from "../../utils/covercryptConfig";
import { findexDatabaseEmployee, findexDatabaseEncryptedEmployees } from "../../utils/covercryptConfig";
import { byteEmployeeToString } from "../../utils/utils";
import { AesGcm } from "cloudproof_js";
const activeLanguageList: Language[] = ["java", "javascript", "python"];

const SearchInDatabase = (): JSX.Element => {
const [keyWords, setKeyWords] = useState("France");
const [byteResults, setByteResults] = useState<findexDatabaseEmployeeBytes[] | undefined>(undefined);
const [byteResults, setByteResults] = useState<findexDatabaseEncryptedEmployees[] | undefined>(undefined);
const [decrypted, setDecrypted] = useState<findexDatabaseEmployee[] | undefined>(undefined);

const { loadingCode, codeContent } = useFetchCodeContent("searchWords", activeLanguageList);
const { findexInstance, indexedEntries, resultEmployees, setResultEmployees, encryptedDatabase } = useFindexStore((state) => state);
const { findexInstance, indexedEntries, decryptedSearchResults, setDecryptedSearchResults, encryptedDatabase } = useFindexStore(
(state) => state
);
const { steps, setSteps } = useBoundStore((state) => state);
const navigate = useNavigate();

Expand All @@ -37,20 +39,19 @@ const SearchInDatabase = (): JSX.Element => {
const res = await searchWords(findexInstance, keywordsList);
const resEmployees = res
.map((result) => encryptedDatabase.encryptedBytesDatabase.find((employee) => result === employee.uuid))
.filter((employee): employee is findexDatabaseEmployeeBytes => employee !== undefined);
.filter((employee): employee is findexDatabaseEncryptedEmployees => employee !== undefined);
setByteResults(resEmployees);
if (resEmployees) setResultEmployees(resEmployees.map(byteEmployeeToString));
if (resEmployees) setDecryptedSearchResults(resEmployees.map(byteEmployeeToString));
}
updateNavigationSteps(steps, setSteps);
navigate("#");
} catch (error) {
message.error(typeof error === "string" ? error : (error as Error).message);
console.error(error);
}
};

const handleDecrypt = async (): Promise<void> => {
if (!encryptedDatabase || !resultEmployees || !byteResults) return;
if (!encryptedDatabase || !decryptedSearchResults || !byteResults) return;
const { Aes256Gcm } = await AesGcm();
const { key, nonce, authenticatedData } = encryptedDatabase;

Expand Down Expand Up @@ -80,14 +81,14 @@ const SearchInDatabase = (): JSX.Element => {
<p>Querying the index is performed using the search function.</p>
<p>The result of the search is a map of the searched keywords to the set of associated data found during the search.</p>
<Input defaultValue={keyWords} onChange={(e) => setKeyWords(e.target.value)} />
<Button onClick={() => handleSearch} disabled={indexedEntries == null} style={{ marginTop: 20, width: "100%", marginBottom: 20 }}>
<Button onClick={handleSearch} disabled={indexedEntries == null} style={{ marginTop: 20, width: "100%", marginBottom: 20 }}>
Search in database
</Button>
{resultEmployees && (
{decryptedSearchResults && (
<>
<EmployeeTable style={{ marginTop: 30 }} data={resultEmployees} />
<Button onClick={handleDecrypt} disabled={!resultEmployees} style={{ marginTop: 20, marginBottom: 20, width: "100%" }}>
Decrypt result{resultEmployees.length > 1 && "s"}
<EmployeeTable style={{ marginTop: 30 }} data={decryptedSearchResults} />
<Button onClick={handleDecrypt} disabled={!decryptedSearchResults} style={{ marginTop: 20, marginBottom: 20, width: "100%" }}>
Decrypt result{decryptedSearchResults.length > 1 && "s"}
</Button>
{decrypted && <EmployeeTable data={decrypted} />}
</>
Expand All @@ -100,11 +101,11 @@ const SearchInDatabase = (): JSX.Element => {
codeInputList={codeContent}
runCode={indexedEntries ? () => handleSearch() : undefined}
codeOutputList={
resultEmployees
decryptedSearchResults
? {
java: JSON.stringify(resultEmployees, undefined, 2),
javascript: JSON.stringify(resultEmployees, undefined, 2),
python: JSON.stringify(resultEmployees, undefined, 2),
java: JSON.stringify(decryptedSearchResults, undefined, 2),
javascript: JSON.stringify(decryptedSearchResults, undefined, 2),
python: JSON.stringify(decryptedSearchResults, undefined, 2),
}
: undefined
}
Expand Down
27 changes: 16 additions & 11 deletions src/store/store.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Findex, IndexedEntry, KmsObject, Policy } from "cloudproof_js";
import { StateCreator, create } from "zustand";
import { Employee, employees, findexDatabaseEmployee, findexDatabaseEmployeeBytes } from "../utils/covercryptConfig";
import {
Employee,
employees,
findexDatabaseEmployee,
findexDatabaseEncryptedEmployees as encryptedBytesDatabase,
} from "../utils/covercryptConfig";
import { NavigationConfig, navigationConfig } from "../utils/navigationConfig";
import { EncryptedResult, KeysUid, Language } from "../utils/types";

Expand Down Expand Up @@ -98,7 +103,7 @@ export const useCovercryptStore = create<CovercryptState>()((set) => ({
// FINDEX
type encryptedDatabaseInfo = {
// after encryption, dB is saved as bytes to avoid data loss on conversion to string
encryptedBytesDatabase: findexDatabaseEmployeeBytes[];
encryptedBytesDatabase: encryptedBytesDatabase[];
key: Uint8Array;
nonce: Uint8Array;
authenticatedData: Uint8Array;
Expand All @@ -109,12 +114,12 @@ interface FindexState {
encryptedDatabase?: encryptedDatabaseInfo | undefined;
findexInstance: Findex | undefined;
indexedEntries: IndexedEntry[] | undefined;
resultEmployees: findexDatabaseEmployee[] | undefined;
decryptedSearchResults: findexDatabaseEmployee[] | undefined;
setFindexService: (serviceSetUp: boolean) => void;
setEncryptedDb: (encryptedDatabase?: encryptedDatabaseInfo) => void;
setEncryptedDatabase: (encryptedDatabase?: encryptedDatabaseInfo) => void;
setFindexInstance: (findexInstance?: Findex) => void;
setIndexedEntries: (indexedEntries?: IndexedEntry[]) => void;
setResultEmployees: (resultEmployees?: findexDatabaseEmployee[]) => void;
setDecryptedSearchResults: (resultEmployees?: findexDatabaseEmployee[]) => void;
}
export const useFindexStore = create<FindexState>()((set) => ({
clearDatabase: employees
Expand All @@ -131,31 +136,31 @@ export const useFindexStore = create<FindexState>()((set) => ({
encryptedDatabase: undefined,
findexInstance: undefined,
indexedEntries: undefined,
resultEmployees: undefined,
decryptedSearchResults: undefined,
setFindexService: (findexService: boolean) => {
set((state) => {
state.setEncryptedDb(undefined); // reset next steps
state.setEncryptedDatabase(undefined); // reset next steps
return { findexService };
});
},
setEncryptedDb: (encryptedDatabase?: encryptedDatabaseInfo) => {
setEncryptedDatabase: (encryptedDatabase?: encryptedDatabaseInfo) => {
set((state) => {
state.setFindexInstance(); // reset next steps
return { encryptedDatabase };
});
},
setFindexInstance: (findexInstance?: Findex) =>
set((state) => {
state.setResultEmployees(); // reset next steps
state.setDecryptedSearchResults(); // reset next steps
state.setIndexedEntries();
return { findexInstance };
}),
setIndexedEntries: (indexedEntries?: IndexedEntry[]) =>
set((state) => {
state.setResultEmployees(); // reset next steps
state.setDecryptedSearchResults(); // reset next steps
return { indexedEntries };
}),
setResultEmployees: (resultEmployees?: findexDatabaseEmployee[]) => set(() => ({ resultEmployees })),
setDecryptedSearchResults: (resultEmployees?: findexDatabaseEmployee[]) => set(() => ({ decryptedSearchResults: resultEmployees })),
}));

// PKI
Expand Down
2 changes: 1 addition & 1 deletion src/utils/covercryptConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type Employee = {
// in the findex workflow we need to have the country as a string because it will be encrypted
// using this new type to avoid breaking changes (OCP)
export type findexDatabaseEmployee = Required<Omit<Employee, "country"> & { country: string }>;
export type findexDatabaseEmployeeBytes = {
export type findexDatabaseEncryptedEmployees = {
uuid: number;
first: Uint8Array;
last: Uint8Array;
Expand Down
14 changes: 7 additions & 7 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findexDatabaseEmployeeBytes, findexDatabaseEmployee } from "./covercryptConfig";
import { findexDatabaseEncryptedEmployees, findexDatabaseEmployee } from "./covercryptConfig";

export const convertString = (data: string): Uint8Array => {
const bianryString = atob(data);
Expand All @@ -9,14 +9,14 @@ export const convertString = (data: string): Uint8Array => {
return uint8Array;
};

export const byteEmployeeToString = (employee: findexDatabaseEmployeeBytes): findexDatabaseEmployee => {
export const byteEmployeeToString = (employee: findexDatabaseEncryptedEmployees): findexDatabaseEmployee => {
const textDecoder = new TextDecoder();
return {
...employee,
first: employee.first ? textDecoder.decode(employee.first) : undefined,
last: employee.last ? textDecoder.decode(employee.last) : undefined,
country: employee.country ? textDecoder.decode(employee.country) : undefined,
email: employee.email ? textDecoder.decode(employee.email) : undefined,
salary: employee.salary ? textDecoder.decode(employee.salary) : undefined,
first: textDecoder.decode(employee.first),
last: textDecoder.decode(employee.last),
country: textDecoder.decode(employee.country),
email: textDecoder.decode(employee.email),
salary: textDecoder.decode(employee.salary),
};
};

0 comments on commit 720a4f9

Please sign in to comment.