2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-05-04 08:11:58 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-03-16 10:37:45 +00:00
|
|
|
import { Controller, FormProvider, useForm, useWatch } from "react-hook-form";
|
2021-05-04 08:11:58 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
FileUpload,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { StoreSettings } from "./StoreSettings";
|
|
|
|
|
|
|
|
type ImportKeyDialogProps = {
|
|
|
|
toggleDialog: () => void;
|
|
|
|
save: (importFile: ImportFile) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const baseFormats = ["JKS", "PKCS12"];
|
|
|
|
|
|
|
|
const formats = baseFormats.concat([
|
|
|
|
"Certificate PEM",
|
|
|
|
"Public Key PEM",
|
|
|
|
"JSON Web Key Set",
|
|
|
|
]);
|
|
|
|
|
|
|
|
export type ImportFile = {
|
|
|
|
keystoreFormat: string;
|
|
|
|
keyAlias: string;
|
|
|
|
storePassword: string;
|
|
|
|
file: { value: File; filename: string };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ImportKeyDialog = ({
|
|
|
|
save,
|
|
|
|
toggleDialog,
|
|
|
|
}: ImportKeyDialogProps) => {
|
|
|
|
const { t } = useTranslation("clients");
|
2022-03-16 10:37:45 +00:00
|
|
|
const form = useForm<ImportFile>();
|
|
|
|
const { control, handleSubmit } = form;
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
const [openArchiveFormat, setOpenArchiveFormat] = useState(false);
|
|
|
|
|
|
|
|
const format = useWatch<string>({
|
|
|
|
control,
|
|
|
|
name: "keystoreFormat",
|
|
|
|
defaultValue: "JKS",
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.medium}
|
|
|
|
title={t("generateKeys")}
|
|
|
|
isOpen
|
|
|
|
onClose={toggleDialog}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
id="modal-confirm"
|
2022-02-02 11:44:52 +00:00
|
|
|
data-testid="confirm"
|
2021-05-04 08:11:58 +00:00
|
|
|
key="confirm"
|
|
|
|
onClick={() => {
|
|
|
|
handleSubmit((importFile) => {
|
|
|
|
save(importFile);
|
|
|
|
toggleDialog();
|
|
|
|
})();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("import")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
2022-02-02 11:44:52 +00:00
|
|
|
data-testid="cancel"
|
2021-05-04 08:11:58 +00:00
|
|
|
key="cancel"
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={() => {
|
|
|
|
toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<TextContent>
|
|
|
|
<Text>{t("clients-help:generateKeysDescription")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
<Form className="pf-u-pt-lg">
|
|
|
|
<FormGroup
|
|
|
|
label={t("archiveFormat")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:archiveFormat"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:archiveFormat"
|
2021-05-04 08:11:58 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="archiveFormat"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="keystoreFormat"
|
|
|
|
control={control}
|
|
|
|
defaultValue="JKS"
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="archiveFormat"
|
2021-11-30 13:07:44 +00:00
|
|
|
onToggle={setOpenArchiveFormat}
|
2021-05-04 08:11:58 +00:00
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setOpenArchiveFormat(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("archiveFormat")}
|
|
|
|
isOpen={openArchiveFormat}
|
|
|
|
>
|
|
|
|
{formats.map((option) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={option === value}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
{baseFormats.includes(format) && (
|
2022-03-16 10:37:45 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<StoreSettings hidePassword />
|
|
|
|
</FormProvider>
|
2021-05-04 08:11:58 +00:00
|
|
|
)}
|
|
|
|
<FormGroup label={t("importFile")} fieldId="importFile">
|
|
|
|
<Controller
|
|
|
|
name="file"
|
|
|
|
control={control}
|
|
|
|
defaultValue=""
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<FileUpload
|
|
|
|
id="importFile"
|
|
|
|
value={value.value}
|
|
|
|
filename={value.filename}
|
|
|
|
onChange={(value, filename) => onChange({ value, filename })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|