Address greptile comments

This commit is contained in:
Carlos Monastyrski
2025-11-10 22:15:16 -03:00
parent 76645ccd23
commit ff07bb643a
5 changed files with 82 additions and 47 deletions

View File

@@ -139,28 +139,38 @@ export const useUpdateRenewalConfig = () => {
export const useDownloadCertPkcs12 = () => {
return useMutation<void, object, TDownloadPkcs12DTO>({
mutationFn: async ({ serialNumber, projectSlug, password, alias }) => {
const response = await apiRequest.post(
`/api/v1/pki/certificates/${serialNumber}/pkcs12`,
{
password,
alias
},
{
params: { projectSlug },
responseType: "arraybuffer"
}
);
try {
const response = await apiRequest.post(
`/api/v1/pki/certificates/${serialNumber}/pkcs12`,
{
password,
alias
},
{
params: { projectSlug },
responseType: "arraybuffer"
}
);
// Create blob and trigger download
const blob = new Blob([response.data], { type: "application/octet-stream" });
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `certificate-${serialNumber}.p12`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
// Create blob and trigger download
const blob = new Blob([response.data], { type: "application/octet-stream" });
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `certificate-${serialNumber}.p12`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
} catch (error: any) {
if (error.response?.data instanceof ArrayBuffer) {
const decoder = new TextDecoder();
const errorText = decoder.decode(error.response.data);
const errorData = JSON.parse(errorText);
throw new Error(errorData.message);
}
throw error;
}
}
});
};

View File

@@ -19,13 +19,17 @@ type Props = {
popUpName: keyof UsePopUpState<["certificateExport"]>,
state?: boolean
) => void;
onFormatSelected: (format: "pem" | "jks", serialNumber: string, options?: ExportOptions) => void;
onFormatSelected: (
format: "pem" | "pkcs12",
serialNumber: string,
options?: ExportOptions
) => void;
};
export type CertificateExportFormat = "pem" | "jks";
export type CertificateExportFormat = "pem" | "pkcs12";
export type ExportOptions = {
jks?: {
pkcs12?: {
password: string;
alias: string;
};
@@ -33,7 +37,7 @@ export type ExportOptions = {
export const CertificateExportModal = ({ popUp, handlePopUpToggle, onFormatSelected }: Props) => {
const [selectedFormat, setSelectedFormat] = useState<CertificateExportFormat>("pem");
const [jksOptions, setJksOptions] = useState({
const [pkcs12Options, setPkcs12Options] = useState({
password: "",
alias: ""
});
@@ -45,7 +49,7 @@ export const CertificateExportModal = ({ popUp, handlePopUpToggle, onFormatSelec
useEffect(() => {
if (popUp?.certificateExport?.isOpen) {
setSelectedFormat("pem");
setJksOptions({
setPkcs12Options({
password: "",
alias: ""
});
@@ -53,8 +57,8 @@ export const CertificateExportModal = ({ popUp, handlePopUpToggle, onFormatSelec
}, [popUp?.certificateExport?.isOpen]);
const isFormValid = () => {
if (selectedFormat === "jks") {
return jksOptions.password.trim() !== "" && jksOptions.alias.trim() !== "";
if (selectedFormat === "pkcs12") {
return pkcs12Options.password.length >= 6 && pkcs12Options.alias.trim() !== "";
}
return true;
};
@@ -63,8 +67,8 @@ export const CertificateExportModal = ({ popUp, handlePopUpToggle, onFormatSelec
if (serialNumber && isFormValid()) {
const options: ExportOptions = {};
if (selectedFormat === "jks") {
options.jks = jksOptions;
if (selectedFormat === "pkcs12") {
options.pkcs12 = pkcs12Options;
}
onFormatSelected(selectedFormat, serialNumber, options);
@@ -97,20 +101,28 @@ export const CertificateExportModal = ({ popUp, handlePopUpToggle, onFormatSelec
onValueChange={(value) => setSelectedFormat(value as CertificateExportFormat)}
>
<SelectItem value="pem">PEM Format</SelectItem>
<SelectItem value="jks">PKCS12 Format</SelectItem>
<SelectItem value="pkcs12">PKCS12 Format</SelectItem>
</Select>
</FormControl>
{selectedFormat === "jks" && (
{selectedFormat === "pkcs12" && (
<>
<FormControl
label="Keystore Password"
helperText="Password to protect the PKCS12 keystore"
helperText={
pkcs12Options.password.length > 0 && pkcs12Options.password.length < 6
? undefined
: "Password to protect the PKCS12 keystore (minimum 6 characters)"
}
isError={pkcs12Options.password.length > 0 && pkcs12Options.password.length < 6}
errorText="Password must be at least 6 characters long"
>
<Input
placeholder="Enter keystore password"
value={jksOptions.password}
onChange={(e) => setJksOptions((prev) => ({ ...prev, password: e.target.value }))}
value={pkcs12Options.password}
onChange={(e) =>
setPkcs12Options((prev) => ({ ...prev, password: e.target.value }))
}
type="password"
/>
</FormControl>
@@ -121,8 +133,8 @@ export const CertificateExportModal = ({ popUp, handlePopUpToggle, onFormatSelec
>
<Input
placeholder="Enter certificate alias"
value={jksOptions.alias}
onChange={(e) => setJksOptions((prev) => ({ ...prev, alias: e.target.value }))}
value={pkcs12Options.alias}
onChange={(e) => setPkcs12Options((prev) => ({ ...prev, alias: e.target.value }))}
/>
</FormControl>
</>

View File

@@ -53,16 +53,16 @@ export const CertificatesSection = () => {
};
const handleCertificateExport = async (
format: "pem" | "jks",
format: "pem" | "pkcs12",
serialNumber: string,
options?: ExportOptions
) => {
if (format === "pem") {
handlePopUpOpen("certificateCert", { serialNumber });
} else if (format === "jks") {
} else if (format === "pkcs12") {
if (!currentProject?.slug) return;
if (!options?.jks?.password || !options?.jks?.alias) {
if (!options?.pkcs12?.password || !options?.pkcs12?.alias) {
createNotification({
text: "Password and alias are required for PKCS12 export",
type: "error"
@@ -74,17 +74,17 @@ export const CertificatesSection = () => {
await downloadCertPkcs12({
serialNumber,
projectSlug: currentProject.slug,
password: options.jks.password,
alias: options.jks.alias
password: options.pkcs12.password,
alias: options.pkcs12.alias
});
createNotification({
text: "PKCS12 certificate downloaded successfully",
type: "success"
});
} catch {
} catch (error: any) {
createNotification({
text: "Failed to download PKCS12 certificate",
text: error?.message || "Failed to download PKCS12 certificate",
type: "error"
});
}