Option to regenerate olm keys inside client

This commit is contained in:
Pallavi Kumari
2025-10-25 22:42:51 +05:30
parent 3f38080b46
commit c2f607bb9a
5 changed files with 255 additions and 11 deletions

View File

@@ -2095,5 +2095,11 @@
"selectedResources": "Selected Resources",
"enableSelected": "Enable Selected",
"disableSelected": "Disable Selected",
"checkSelectedStatus": "Check Status of Selected"
"checkSelectedStatus": "Check Status of Selected",
"savecredentials": "Save Credentials",
"regeneratecredentials": "Regenerate Credentials",
"regenerateClientCredentials": "Regenerate and save your managed credentials",
"generatedcredentials": "Generated Credentials",
"copyandsavethesecredentials": "Copy and save these credentials",
"copyandsavethesecredentialsdescription": "These credentials will not be shown again after you leave this page. Save them securely now."
}

View File

@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { Client, db, exitNodes, sites } from "@server/db";
import { Client, db, exitNodes, olms, sites } from "@server/db";
import { clients, clientSites } from "@server/db";
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
@@ -18,6 +18,7 @@ import {
deletePeer as olmDeletePeer
} from "../olm/peers";
import { sendToExitNode } from "#dynamic/lib/exitNodes";
import { hashPassword } from "@server/auth/password";
const updateClientParamsSchema = z
.object({
@@ -30,7 +31,10 @@ const updateClientSchema = z
name: z.string().min(1).max(255).optional(),
siteIds: z
.array(z.number().int().positive())
.optional()
.optional(),
olmId: z.string().min(1).optional(),
secret: z.string().min(1).optional(),
})
.strict();
@@ -75,7 +79,7 @@ export async function updateClient(
);
}
const { name, siteIds } = parsedBody.data;
const { name, siteIds, olmId, secret } = parsedBody.data;
const parsedParams = updateClientParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
@@ -89,6 +93,12 @@ export async function updateClient(
const { clientId } = parsedParams.data;
let secretHash = undefined;
if (secret) {
secretHash = await hashPassword(secret);
}
// Fetch the client to make sure it exists and the user has access to it
const [client] = await db
.select()
@@ -136,6 +146,22 @@ export async function updateClient(
.where(eq(clients.clientId, clientId));
}
const [existingOlm] = await trx
.select()
.from(olms)
.where(eq(olms.clientId, clientId))
.limit(1);
if (existingOlm && olmId && secretHash) {
await trx
.update(olms)
.set({
olmId,
secretHash
})
.where(eq(olms.clientId, clientId));
}
// Update site associations if provided
// Remove sites that are no longer associated
for (const siteId of sitesRemoved) {

View File

@@ -111,10 +111,10 @@ export default function GeneralPage() {
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
{t("Generated Credentials")}
{t("generatedcredentials")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t("Regenerate and save your managed credentials")}
{t("regenerateClientCredentials")}
</SettingsSectionDescription>
</SettingsSectionHeader>
@@ -125,7 +125,7 @@ export default function GeneralPage() {
loading={loading}
disabled={loading}
>
{t("Regenerate Credentials")}
{t("regeneratecredentials")}
</Button>
) : (
<>
@@ -138,11 +138,11 @@ export default function GeneralPage() {
<Alert variant="neutral" className="mt-4">
<InfoIcon className="h-4 w-4" />
<AlertTitle className="font-semibold">
{t("Copy and save these credentials")}
{t("copyandsavethesecredentials")}
</AlertTitle>
<AlertDescription>
{t(
"These credentials will not be shown again after you leave this page. Save them securely now."
"copyandsavethesecredentialsdescription"
)}
</AlertDescription>
</Alert>
@@ -152,14 +152,14 @@ export default function GeneralPage() {
variant="outline"
onClick={() => setCredentials(null)}
>
{t("Cancel")}
{t("cancel")}
</Button>
<Button
onClick={handleSave}
loading={saving}
disabled={saving}
>
{t("Save Credentials")}
{t("savecredentials")}
</Button>
</div>
</>

View File

@@ -0,0 +1,208 @@
"use client";
import { useEffect, useState } from "react";
import {
SettingsContainer,
SettingsSection,
SettingsSectionBody,
SettingsSectionDescription,
SettingsSectionHeader,
SettingsSectionTitle
} from "@app/components/Settings";
import { Button } from "@app/components/ui/button";
import { Alert, AlertDescription, AlertTitle } from "@app/components/ui/alert";
import { InfoIcon } from "lucide-react";
import { createApiClient, formatAxiosError } from "@app/lib/api";
import { useEnvContext } from "@app/hooks/useEnvContext";
import { toast } from "@app/hooks/useToast";
import { useParams, useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { InfoSection, InfoSectionContent, InfoSections, InfoSectionTitle } from "@app/components/InfoSection";
import CopyToClipboard from "@app/components/CopyToClipboard";
import { PickClientDefaultsResponse } from "@server/routers/client";
import { useClientContext } from "@app/hooks/useClientContext";
export default function GeneralPage() {
const { env } = useEnvContext();
const api = createApiClient({ env });
const { orgId } = useParams();
const router = useRouter();
const t = useTranslations();
const [olmId, setOlmId] = useState("");
const [olmSecret, setOlmSecret] = useState("");
const { client, updateClient } = useClientContext();
const [clientDefaults, setClientDefaults] =
useState<PickClientDefaultsResponse | null>(null);
const [loading, setLoading] = useState(false);
const [saving, setSaving] = useState(false);
// Clear credentials when user leaves/reloads
useEffect(() => {
const clearCreds = () => {
setOlmId("");
setOlmSecret("");
};
window.addEventListener("beforeunload", clearCreds);
return () => window.removeEventListener("beforeunload", clearCreds);
}, []);
const handleRegenerate = async () => {
try {
setLoading(true);
await api
.get(`/org/${orgId}/pick-client-defaults`)
.then((res) => {
if (res && res.status === 200) {
const data = res.data.data;
setClientDefaults(data);
const olmId = data.olmId;
const olmSecret = data.olmSecret;
setOlmId(olmId);
setOlmSecret(olmSecret);
}
});
} finally {
setLoading(false);
}
};
const handleSave = async () => {
setLoading(true);
try {
await api.post(`/client/${client?.clientId}`, {
olmId: clientDefaults?.olmId,
secret: clientDefaults?.olmSecret,
});
toast({
title: t("clientUpdated"),
description: t("clientUpdatedDescription")
});
router.refresh();
} catch (e) {
toast({
variant: "destructive",
title: t("clientUpdateFailed"),
description: formatAxiosError(
e,
t("clientUpdateError")
)
});
} finally {
setLoading(false);
}
};
return (
<SettingsContainer>
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
{t("generatedcredentials")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t("regenerateClientCredentials")}
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
{!clientDefaults ? (
<Button
onClick={handleRegenerate}
loading={loading}
disabled={loading}
>
{t("regeneratecredentials")}
</Button>
) : (
<>
<SettingsSection>
<SettingsSectionHeader>
<SettingsSectionTitle>
{t("clientOlmCredentials")}
</SettingsSectionTitle>
<SettingsSectionDescription>
{t("clientOlmCredentialsDescription")}
</SettingsSectionDescription>
</SettingsSectionHeader>
<SettingsSectionBody>
<InfoSections cols={3}>
<InfoSection>
<InfoSectionTitle>
{t("olmEndpoint")}
</InfoSectionTitle>
<InfoSectionContent>
<CopyToClipboard
text={
env.app.dashboardUrl
}
/>
</InfoSectionContent>
</InfoSection>
<InfoSection>
<InfoSectionTitle>
{t("olmId")}
</InfoSectionTitle>
<InfoSectionContent>
<CopyToClipboard
text={olmId}
/>
</InfoSectionContent>
</InfoSection>
<InfoSection>
<InfoSectionTitle>
{t("olmSecretKey")}
</InfoSectionTitle>
<InfoSectionContent>
<CopyToClipboard
text={olmSecret}
/>
</InfoSectionContent>
</InfoSection>
</InfoSections>
<Alert variant="neutral" className="mt-4">
<InfoIcon className="h-4 w-4" />
<AlertTitle className="font-semibold">
{t("copyandsavethesecredentials")}
</AlertTitle>
<AlertDescription>
{t(
"copyandsavethesecredentialsdescription"
)}
</AlertDescription>
</Alert>
</SettingsSectionBody>
</SettingsSection>
<div className="flex justify-end mt-6 space-x-2">
<Button
variant="outline"
onClick={() => {
setOlmId("");
setOlmSecret("");
}}
>
{t("cancel")}
</Button>
<Button
onClick={handleSave}
loading={saving}
disabled={saving}
>
{t("savecredentials")}
</Button>
</div>
</>
)}
</SettingsSectionBody>
</SettingsSection>
</SettingsContainer>
);
}

View File

@@ -34,6 +34,10 @@ export default async function SettingsLayout(props: SettingsLayoutProps) {
{
title: "General",
href: `/{orgId}/settings/clients/{clientId}/general`
},
{
title: "Credentials",
href: `/{orgId}/settings/clients/{clientId}/credentials`
}
];