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";
|
2023-01-18 10:12:32 +00:00
|
|
|
import {
|
|
|
|
Controller,
|
|
|
|
FormProvider,
|
|
|
|
useForm,
|
|
|
|
useWatch,
|
|
|
|
} from "react-hook-form-v7";
|
2021-05-04 08:11:58 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { StoreSettings } from "./StoreSettings";
|
2022-09-12 09:05:27 +00:00
|
|
|
import { FileUpload } from "../../components/json-file-upload/patternfly/FileUpload";
|
2022-11-24 16:59:02 +00:00
|
|
|
import { useServerInfo } from "../../context/server-info/ServerInfoProvider";
|
2021-05-04 08:11:58 +00:00
|
|
|
|
|
|
|
type ImportKeyDialogProps = {
|
|
|
|
toggleDialog: () => void;
|
|
|
|
save: (importFile: ImportFile) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type ImportFile = {
|
|
|
|
keystoreFormat: string;
|
|
|
|
keyAlias: string;
|
|
|
|
storePassword: string;
|
2023-01-18 10:12:32 +00:00
|
|
|
file: { value?: string; filename: string };
|
2021-05-04 08:11:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-11-24 16:59:02 +00:00
|
|
|
const baseFormats = useServerInfo().cryptoInfo?.supportedKeystoreTypes ?? [];
|
|
|
|
|
|
|
|
const formats = baseFormats.concat([
|
|
|
|
"Certificate PEM",
|
|
|
|
"Public Key PEM",
|
|
|
|
"JSON Web Key Set",
|
|
|
|
]);
|
|
|
|
|
2023-01-02 15:34:51 +00:00
|
|
|
const format = useWatch({
|
|
|
|
control,
|
|
|
|
name: "keystoreFormat",
|
|
|
|
defaultValue: formats[0],
|
|
|
|
});
|
|
|
|
|
2021-05-04 08:11:58 +00:00
|
|
|
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}
|
2023-01-02 15:34:51 +00:00
|
|
|
defaultValue={formats[0]}
|
2023-01-18 10:12:32 +00:00
|
|
|
render={({ field }) => (
|
2021-05-04 08:11:58 +00:00
|
|
|
<Select
|
|
|
|
toggleId="archiveFormat"
|
2021-11-30 13:07:44 +00:00
|
|
|
onToggle={setOpenArchiveFormat}
|
2021-05-04 08:11:58 +00:00
|
|
|
onSelect={(_, value) => {
|
2023-01-18 10:12:32 +00:00
|
|
|
field.onChange(value as string);
|
2021-05-04 08:11:58 +00:00
|
|
|
setOpenArchiveFormat(false);
|
|
|
|
}}
|
2023-01-18 10:12:32 +00:00
|
|
|
selections={field.value}
|
2021-05-04 08:11:58 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("archiveFormat")}
|
|
|
|
isOpen={openArchiveFormat}
|
|
|
|
>
|
|
|
|
{formats.map((option) => (
|
|
|
|
<SelectOption
|
2023-01-18 10:12:32 +00:00
|
|
|
selected={option === field.value}
|
2021-05-04 08:11:58 +00:00
|
|
|
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}
|
2023-01-18 10:12:32 +00:00
|
|
|
defaultValue={{ filename: "" }}
|
|
|
|
render={({ field }) => (
|
2021-05-04 08:11:58 +00:00
|
|
|
<FileUpload
|
|
|
|
id="importFile"
|
2023-01-18 10:12:32 +00:00
|
|
|
value={field.value.value}
|
|
|
|
filename={field.value.filename}
|
|
|
|
onChange={(value, filename) =>
|
|
|
|
field.onChange({ value, filename })
|
|
|
|
}
|
2021-05-04 08:11:58 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|