Use cryptoInfo to populate key type dropdowns (#3818)

This commit is contained in:
Douglas Palmer 2022-11-24 08:59:02 -08:00 committed by GitHub
parent 0e04f088e2
commit 2ed958b4d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 13 deletions

View file

@ -4,7 +4,7 @@ import { Button, Modal, Form } from "@patternfly/react-core";
import { saveAs } from "file-saver";
import KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig";
import { KeyForm } from "./GenerateKeyDialog";
import { KeyForm, getFileExtension } from "./GenerateKeyDialog";
import { useRealm } from "../../context/realm-context/RealmContext";
import { useAdminClient } from "../../context/auth/AdminClient";
import { useAlerts } from "../../components/alert/Alerts";
@ -39,7 +39,7 @@ export const ExportSamlKeyDialog = ({
);
saveAs(
new Blob([keyStore], { type: "application/octet-stream" }),
`keystore.${config.format == "PKCS12" ? "p12" : "jks"}`
`keystore.${getFileExtension(config.format ?? "")}`
);
addAlert(t("samlKeysExportSuccess"));
close();

View file

@ -24,6 +24,7 @@ import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keysto
import { HelpItem } from "../../components/help-enabler/HelpItem";
import { StoreSettings } from "./StoreSettings";
import { FileUpload } from "../../components/json-file-upload/patternfly/FileUpload";
import { useServerInfo } from "../../context/server-info/ServerInfoProvider";
type GenerateKeyDialogProps = {
clientId: string;
@ -39,6 +40,14 @@ type KeyFormProps = {
const CERT_PEM = "Certificate PEM" as const;
const extensions = new Map([
["PKCS12", "p12"],
["JKS", "jks"],
["BCFKS", "bcfks"],
]);
export const getFileExtension = (format: string) => extensions.get(format);
export const KeyForm = ({
isSaml = false,
hasPem = false,
@ -52,6 +61,8 @@ export const KeyForm = ({
const { control, watch } = useFormContext<KeyStoreConfig>();
const format = watch("format");
const { cryptoInfo } = useServerInfo();
return (
<Form className="pf-u-pt-lg">
<FormGroup
@ -81,7 +92,7 @@ export const KeyForm = ({
aria-label={t("archiveFormat")}
isOpen={openArchiveFormat}
>
{["JKS", "PKCS12"]
{cryptoInfo?.supportedKeystoreTypes
.concat(hasPem ? CERT_PEM : [])
.map((option) => (
<SelectOption

View file

@ -17,20 +17,13 @@ import {
import { HelpItem } from "../../components/help-enabler/HelpItem";
import { StoreSettings } from "./StoreSettings";
import { FileUpload } from "../../components/json-file-upload/patternfly/FileUpload";
import { useServerInfo } from "../../context/server-info/ServerInfoProvider";
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;
@ -53,6 +46,15 @@ export const ImportKeyDialog = ({
name: "keystoreFormat",
defaultValue: "JKS",
});
const baseFormats = useServerInfo().cryptoInfo?.supportedKeystoreTypes ?? [];
const formats = baseFormats.concat([
"Certificate PEM",
"Public Key PEM",
"JSON Web Key Set",
]);
return (
<Modal
variant={ModalVariant.medium}

View file

@ -23,7 +23,7 @@ import { HelpItem } from "../../components/help-enabler/HelpItem";
import { FormAccess } from "../../components/form-access/FormAccess";
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
import { Controller, useFormContext, useWatch } from "react-hook-form";
import { GenerateKeyDialog } from "./GenerateKeyDialog";
import { GenerateKeyDialog, getFileExtension } from "./GenerateKeyDialog";
import { useFetch, useAdminClient } from "../../context/auth/AdminClient";
import { useAlerts } from "../../components/alert/Alerts";
import useToggle from "../../utils/useToggle";
@ -80,7 +80,7 @@ export const Keys = ({ clientId, save, hasConfigureAccess }: KeysProps) => {
);
saveAs(
new Blob([keyStore], { type: "application/octet-stream" }),
`keystore.${config.format == "PKCS12" ? "p12" : "jks"}`
`keystore.${getFileExtension(config.format ?? "")}`
);
addAlert(t("generateSuccess"), AlertVariant.success);
refresh();