|
| 1 | +import { useCallback } from "react"; |
| 2 | +import { type LogEntry, RekorClient, type SearchIndex } from "rekor"; |
| 3 | +import { useRekorClient } from "./context.tsx"; |
| 4 | + |
| 5 | +const PAGE_SIZE = 20; |
| 6 | + |
| 7 | +export const ATTRIBUTES = ["email", "hash", "commitSha", "uuid", "logIndex"] as const; |
| 8 | +const ATTRIBUTES_SET = new Set<string>(ATTRIBUTES); |
| 9 | + |
| 10 | +export type Attribute = (typeof ATTRIBUTES)[number]; |
| 11 | + |
| 12 | +export function isAttribute(input: string): input is Attribute { |
| 13 | + return ATTRIBUTES_SET.has(input); |
| 14 | +} |
| 15 | + |
| 16 | +export type SearchQuery = |
| 17 | + | { |
| 18 | + attribute: "email" | "hash" | "commitSha" | "uuid"; |
| 19 | + query: string; |
| 20 | + } |
| 21 | + | { |
| 22 | + attribute: "logIndex"; |
| 23 | + query: number; |
| 24 | + }; |
| 25 | + |
| 26 | +export interface RekorEntries { |
| 27 | + totalCount: number; |
| 28 | + entries: LogEntry[]; |
| 29 | +} |
| 30 | + |
| 31 | +export function useRekorSearch() { |
| 32 | + const client = useRekorClient(); |
| 33 | + |
| 34 | + return useCallback( |
| 35 | + // eslint-disable-next-line @typescript-eslint/no-inferrable-types |
| 36 | + async (search: SearchQuery, page: number = 1): Promise<RekorEntries> => { |
| 37 | + switch (search.attribute) { |
| 38 | + case "logIndex": |
| 39 | + return { |
| 40 | + totalCount: 1, |
| 41 | + entries: [ |
| 42 | + await client.entries.getLogEntryByIndex({ |
| 43 | + logIndex: search.query, |
| 44 | + }), |
| 45 | + ], |
| 46 | + }; |
| 47 | + case "uuid": |
| 48 | + return { |
| 49 | + totalCount: 1, |
| 50 | + entries: [ |
| 51 | + await client.entries.getLogEntryByUuid({ |
| 52 | + entryUuid: search.query, |
| 53 | + }), |
| 54 | + ], |
| 55 | + }; |
| 56 | + case "email": |
| 57 | + return queryEntries( |
| 58 | + client, |
| 59 | + { |
| 60 | + email: search.query, |
| 61 | + }, |
| 62 | + page |
| 63 | + ); |
| 64 | + case "hash": |
| 65 | + return queryEntries( |
| 66 | + client, |
| 67 | + { |
| 68 | + hash: search.query.startsWith("sha256:") ? search.query : `sha256:${search.query}`, |
| 69 | + }, |
| 70 | + page |
| 71 | + ); |
| 72 | + case "commitSha": |
| 73 | + // eslint-disable-next-line no-case-declarations |
| 74 | + const hash = await digestMessage(search.query); |
| 75 | + return queryEntries(client, { hash }, page); |
| 76 | + } |
| 77 | + }, |
| 78 | + [client] |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +async function queryEntries(client: RekorClient, query: SearchIndex, page: number): Promise<RekorEntries> { |
| 83 | + const logIndexes = await client.index.searchIndex({ query }); |
| 84 | + |
| 85 | + // Preventing entries from jumping between pages on refresh |
| 86 | + logIndexes.sort(); |
| 87 | + |
| 88 | + const startIndex = (page - 1) * PAGE_SIZE; |
| 89 | + const endIndex = startIndex + PAGE_SIZE; |
| 90 | + const uuidToRetrieve = logIndexes.slice(startIndex, endIndex); |
| 91 | + |
| 92 | + const entries = await Promise.all(uuidToRetrieve.map((entryUuid) => client.entries.getLogEntryByUuid({ entryUuid }))); |
| 93 | + return { |
| 94 | + totalCount: logIndexes.length, |
| 95 | + entries, |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +async function digestMessage(message: string): Promise<string> { |
| 100 | + const msgUint8 = new TextEncoder().encode(message); |
| 101 | + const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); |
| 102 | + const hashArray = Array.from(new Uint8Array(hashBuffer)); |
| 103 | + const hash = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); |
| 104 | + return `sha256:${hash}`; |
| 105 | +} |
0 commit comments