working on edge key with public server key.

This commit is contained in:
charlesgauthereau
2025-10-31 17:55:16 +01:00
parent e3239639f3
commit 027928c29f
5 changed files with 38 additions and 17 deletions
@@ -8,7 +8,7 @@ import {Card, CardContent, CardHeader, CardTitle} from "@/components/ui/card";
import {CardsWithPagination} from "@/components/wrappers/common/cards-with-pagination"; import {CardsWithPagination} from "@/components/wrappers/common/cards-with-pagination";
import {DatabaseCard} from "@/components/wrappers/dashboard/projects/project-card/project-database-card"; import {DatabaseCard} from "@/components/wrappers/dashboard/projects/project-card/project-database-card";
import {AgentCardKey} from "@/components/wrappers/dashboard/agent/agent-card-key/agent-card-key"; import {AgentCardKey} from "@/components/wrappers/dashboard/agent/agent-card-key/agent-card-key";
import { db } from "@/db"; import {db} from "@/db";
import * as drizzleDb from "@/db"; import * as drizzleDb from "@/db";
import {and, eq} from "drizzle-orm"; import {and, eq} from "drizzle-orm";
import {notFound} from "next/navigation"; import {notFound} from "next/navigation";
@@ -18,6 +18,8 @@ import {
import {ButtonDeleteAgent} from "@/components/wrappers/dashboard/agent/button-delete-agent/button-delete-agent"; import {ButtonDeleteAgent} from "@/components/wrappers/dashboard/agent/button-delete-agent/button-delete-agent";
import {capitalizeFirstLetter} from "@/utils/text"; import {capitalizeFirstLetter} from "@/utils/text";
import {Server} from "lucide-react"; import {Server} from "lucide-react";
import {generateEdgeKey} from "@/utils/edge_key";
import {getServerUrl} from "@/utils/get-server-url";
export default async function RoutePage(props: PageParams<{ agentId: string }>) { export default async function RoutePage(props: PageParams<{ agentId: string }>) {
@@ -36,6 +38,7 @@ export default async function RoutePage(props: PageParams<{ agentId: string }>)
notFound() notFound()
} }
const edgeKey = await generateEdgeKey(getServerUrl(), agent.id);
return ( return (
<Page> <Page>
@@ -48,7 +51,7 @@ export default async function RoutePage(props: PageParams<{ agentId: string }>)
</Link> </Link>
</PageTitle> </PageTitle>
<PageActions className="justify-between"> <PageActions className="justify-between">
<ButtonDeleteAgent agentId={agentId} text={"Delete Agent"} /> <ButtonDeleteAgent agentId={agentId} text={"Delete Agent"}/>
</PageActions> </PageActions>
</div> </div>
<PageDescription className="mt-5 sm:mt-0">{agent.description}</PageDescription> <PageDescription className="mt-5 sm:mt-0">{agent.description}</PageDescription>
@@ -58,7 +61,7 @@ export default async function RoutePage(props: PageParams<{ agentId: string }>)
<Card className="w-full sm:w-auto flex-1"> <Card className="w-full sm:w-auto flex-1">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Databases</CardTitle> <CardTitle className="text-sm font-medium">Databases</CardTitle>
<Server className="h-4 w-4 text-muted-foreground" /> <Server className="h-4 w-4 text-muted-foreground"/>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<div className="text-2xl font-bold">{agent.databases.length}</div> <div className="text-2xl font-bold">{agent.databases.length}</div>
@@ -69,7 +72,7 @@ export default async function RoutePage(props: PageParams<{ agentId: string }>)
<Card className="w-full sm:w-auto flex-1"> <Card className="w-full sm:w-auto flex-1">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Last contact</CardTitle> <CardTitle className="text-sm font-medium">Last contact</CardTitle>
<Server className="h-4 w-4 text-muted-foreground" /> <Server className="h-4 w-4 text-muted-foreground"/>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<div className="text-2xl font-bold">{formatDateLastContact(agent.lastContact)}</div> <div className="text-2xl font-bold">{formatDateLastContact(agent.lastContact)}</div>
@@ -83,7 +86,9 @@ export default async function RoutePage(props: PageParams<{ agentId: string }>)
Edge Key Edge Key
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<AgentCardKey agent={agent}/> <AgentCardKey
edgeKey={edgeKey}
/>
</CardContent> </CardContent>
</Card> </Card>
<CardsWithPagination cardsPerPage={2} data={agent.databases} cardItem={DatabaseCard}/> <CardsWithPagination cardsPerPage={2} data={agent.databases} cardItem={DatabaseCard}/>
@@ -1,25 +1,20 @@
"use client"; "use client";
import {generateEdgeKey} from "@/utils/edge_key";
import {getServerUrl} from "@/utils/get-server-url";
import {PasswordInput} from "@/components/wrappers/auth/password-input/password-input"; import {PasswordInput} from "@/components/wrappers/auth/password-input/password-input";
import {useState} from "react"; import {useState} from "react";
import {CopyButton} from "@/components/wrappers/common/button/copy-button"; import {CopyButton} from "@/components/wrappers/common/button/copy-button";
import {Agent} from "@/db/schema/08_agent";
export type AgentCardKeyProps = { export type AgentCardKeyProps = {
agent: Agent; edgeKey: string;
}; };
export const AgentCardKey = (props: AgentCardKeyProps) => { export const AgentCardKey = ({edgeKey}: AgentCardKeyProps) => {
const edge_key = generateEdgeKey(getServerUrl(), props.agent.id); const [code, setCode] = useState<string>(`${edgeKey}`);
const [code, setCode] = useState<string>(`${edge_key}`);
return ( return (
<> <>
<PasswordInput <PasswordInput
value={code} value={code}
onChange={() => { onChange={() => {
setCode(edge_key); setCode(edgeKey);
}} }}
/> />
<CopyButton className="mt-5" value={code}/> <CopyButton className="mt-5" value={code}/>
+17
View File
@@ -0,0 +1,17 @@
import fs from "node:fs";
/**
* Get Public server key content
*/
export function getPublicServerKeyContent() {
try {
return fs.readFileSync("private/keys/server_public.pem", "utf8");
} catch (error: any) {
console.error("Error :", error);
return {
success: false,
message: `An error occurred while getting public server key`,
};
}
}
+6 -3
View File
@@ -1,14 +1,17 @@
"use server"
import {getPublicServerKeyContent} from "@/features/keys/keys.action";
export function generateEdgeKey(serverUrl: string, agentId: string): string { export async function generateEdgeKey(serverUrl: string, agentId: string): Promise<string> {
const publicKey = getPublicServerKeyContent()
const edgeKeyData = { const edgeKeyData = {
serverUrl, serverUrl,
agentId, agentId,
publicKey
}; };
console.log(edgeKeyData); console.log(edgeKeyData);
const edgeKeyJson = JSON.stringify(edgeKeyData); const edgeKeyJson = JSON.stringify(edgeKeyData);
const edgeKeyBuffer = Buffer.from(edgeKeyJson, 'utf-8'); const edgeKeyBuffer = Buffer.from(edgeKeyJson, 'utf-8');
const edgeKeyBase64 = edgeKeyBuffer.toString('base64').replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_'); return edgeKeyBuffer.toString('base64').replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_');
return edgeKeyBase64;
} }
function decodeEdgeKey(edgeKey: string): object { function decodeEdgeKey(edgeKey: string): object {
+1
View File
@@ -4,6 +4,7 @@ import {eq} from "drizzle-orm";
import * as drizzleDb from "@/db"; import * as drizzleDb from "@/db";
import {retentionJob} from "@/lib/tasks"; import {retentionJob} from "@/lib/tasks";
import {generateRSAKeys} from "@/utils/rsa-keys"; import {generateRSAKeys} from "@/utils/rsa-keys";
import {getPublicServerKeyContent} from "@/features/keys/keys.action";
export async function init() { export async function init() {