-
Notifications
You must be signed in to change notification settings - Fork 4
feat(aurora-portal): clavis create certificates #859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
018be2d
feat(aurora-portal): clavis create certificates
vlad-schur-external-sap 579e1ed
chore(docs): update clavis documentation
vlad-schur-external-sap 43bbdcb
fix(config): allow vitest to run specific file from server directory
vlad-schur-external-sap e68d11d
fix(aurora-portal): resolve copilot and coderabbit pr comments
vlad-schur-external-sap 4848c4e
fix(ci): temporarily return server line as ceph tests are failing
vlad-schur-external-sap 18b1280
Merge branch 'main' into vlad-clavis-create-ca
vlad-schur-external-sap d442a12
chore(ci): generate changeset
vlad-schur-external-sap 270cc36
Merge branch 'main' into vlad-clavis-create-ca
vlad-schur-external-sap fdbc62e
Merge branch 'main' into vlad-clavis-create-ca
andypf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cobaltcore-dev/aurora": patch | ||
| --- | ||
|
|
||
| added clavis CA create certificates functionality |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...projectId/services/pca/$pcaId/-components/-modals/IssueEndEntityCertificateModal.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" | ||
| import { act, render, screen, waitFor } from "@testing-library/react" | ||
| import userEvent from "@testing-library/user-event" | ||
| import { I18nProvider } from "@lingui/react" | ||
| import { i18n } from "@lingui/core" | ||
| import { PortalProvider } from "@cloudoperators/juno-ui-components" | ||
| import { IssueEndEntityCertificateModal } from "./IssueEndEntityCertificateModal" | ||
|
|
||
| const mockProjectId = "project-123" | ||
| const mockMutateAsync = vi.fn().mockResolvedValue({}) | ||
| const mockReset = vi.fn() | ||
| const mockInvalidate = vi.fn() | ||
|
|
||
| vi.mock("@/client/hooks", () => ({ | ||
| useProjectId: () => mockProjectId, | ||
| })) | ||
|
|
||
| vi.mock("@/client/trpcClient", () => ({ | ||
| trpcReact: { | ||
| useUtils: () => ({ | ||
| services: { | ||
| pca: { | ||
| listCertificates: { | ||
| invalidate: mockInvalidate, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| services: { | ||
| pca: { | ||
| createCertificate: { | ||
| useMutation: (options?: { onSettled?: () => void }) => ({ | ||
| isPending: false, | ||
| mutateAsync: async (input: unknown) => { | ||
| const result = await mockMutateAsync(input) | ||
| options?.onSettled?.() | ||
| return result | ||
| }, | ||
| reset: mockReset, | ||
| error: null, | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| const renderModal = (onClose = vi.fn()) => | ||
| render( | ||
| <I18nProvider i18n={i18n}> | ||
| <PortalProvider> | ||
| <IssueEndEntityCertificateModal open={true} onClose={onClose} pcaId="ca-1" /> | ||
| </PortalProvider> | ||
| </I18nProvider> | ||
| ) | ||
|
|
||
| describe("IssueEndEntityCertificateModal", () => { | ||
| beforeEach(async () => { | ||
| vi.clearAllMocks() | ||
| await act(async () => { | ||
| i18n.activate("en") | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it("does not submit when csr is empty", async () => { | ||
| const user = userEvent.setup() | ||
|
|
||
| renderModal() | ||
| await user.click(screen.getByRole("button", { name: "Save" })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockMutateAsync).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| it("submits normalized csr payload and closes modal", async () => { | ||
| const user = userEvent.setup() | ||
| const onClose = vi.fn() | ||
| vi.spyOn(Date, "now").mockReturnValue(1_700_000_000_000) | ||
|
|
||
| renderModal(onClose) | ||
|
|
||
| await user.type(screen.getByPlaceholderText("Paste CSR code"), "line1\\nline2") | ||
| await user.click(screen.getByRole("button", { name: "Save" })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockMutateAsync).toHaveBeenCalledWith({ | ||
| project_id: "project-123", | ||
| certificate_authority_id: "ca-1", | ||
| csr: "line1\nline2", | ||
| configuration: { | ||
| validity: { | ||
| not_after: 1_700_028_800, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| expect(mockInvalidate).toHaveBeenCalledTimes(1) | ||
| expect(mockReset).toHaveBeenCalledTimes(1) | ||
| expect(onClose).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
114 changes: 114 additions & 0 deletions
114
...cts/$projectId/services/pca/$pcaId/-components/-modals/IssueEndEntityCertificateModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { z } from "zod" | ||
| import { useForm, useStore } from "@tanstack/react-form" | ||
| import { Trans, useLingui } from "@lingui/react/macro" | ||
| import { Modal, Form, FormSection, Spinner, Message, Textarea } from "@cloudoperators/juno-ui-components" | ||
| import { trpcReact } from "@/client/trpcClient" | ||
| import { useProjectId } from "@/client/hooks" | ||
|
|
||
| export interface IssueEndEntityCertificateModalProps { | ||
| open: boolean | ||
| onClose: () => void | ||
| pcaId: string | ||
| } | ||
|
|
||
| export const IssueEndEntityCertificateModal = ({ open, onClose, pcaId }: IssueEndEntityCertificateModalProps) => { | ||
| const { t } = useLingui() | ||
| const projectId = useProjectId() | ||
| const utils = trpcReact.useUtils() | ||
|
|
||
| const { isPending, ...createCertificateMutation } = trpcReact.services.pca.createCertificate.useMutation({ | ||
| onSettled: () => utils.services.pca.listCertificates.invalidate(), | ||
| }) | ||
|
|
||
| const formSchema = z.object({ | ||
| csr: z.string().trim().min(1), | ||
| }) | ||
|
vlad-schur-external-sap marked this conversation as resolved.
vlad-schur-external-sap marked this conversation as resolved.
|
||
|
|
||
| const form = useForm({ | ||
| defaultValues: { | ||
| csr: "", | ||
| }, | ||
| validators: { | ||
| onSubmit: formSchema, | ||
| }, | ||
| onSubmit: async ({ value }) => { | ||
| if (isPending) return | ||
|
|
||
| await createCertificateMutation.mutateAsync({ | ||
| project_id: projectId, | ||
| certificate_authority_id: pcaId, | ||
| // Normalize to one format so users can paste raw multi-line CSRs with \n along with already formatted ones | ||
| csr: value.csr.replace(/\\n/g, "\n"), | ||
| configuration: { validity: { not_after: Math.floor(Date.now() / 1000) + 8 * 60 * 60 } }, | ||
| }) | ||
| handleClose() | ||
| }, | ||
| }) | ||
|
|
||
| const handleClose = () => { | ||
| if (isPending) return | ||
|
|
||
| form.reset() | ||
| createCertificateMutation.reset() | ||
| onClose() | ||
| } | ||
|
|
||
| const currentCsr = useStore(form.store, (state) => state.values.csr) | ||
|
|
||
| return ( | ||
| <Modal | ||
| open={open} | ||
| size="large" | ||
| title={t`Issue End Entity Certificate`} | ||
| onCancel={handleClose} | ||
| cancelButtonLabel={t`Cancel`} | ||
| confirmButtonLabel={t`Save`} | ||
| onConfirm={form.handleSubmit} | ||
| disableConfirmButton={isPending || !(currentCsr.trim().length > 0)} | ||
| > | ||
| {createCertificateMutation.error && ( | ||
| <Message dismissible={false} variant="error" className="mb-4"> | ||
| {createCertificateMutation.error?.message} | ||
| </Message> | ||
| )} | ||
|
|
||
| {isPending && ( | ||
| <div className="mb-4 flex items-center justify-center gap-2"> | ||
| <Spinner variant="primary" /> | ||
| <span className="text-theme-high text-sm"> | ||
| <Trans>Issuing End Entity Certificate...</Trans> | ||
| </span> | ||
| </div> | ||
| )} | ||
|
|
||
| {!isPending && ( | ||
| <Form | ||
| className="mb-0" | ||
| id="issue-end-entity-certificate-form" | ||
| onSubmit={(e) => { | ||
| e.preventDefault() | ||
| form.handleSubmit() | ||
| }} | ||
| > | ||
| <FormSection> | ||
| <form.Field | ||
| name="csr" | ||
| children={(field) => ( | ||
| <Textarea | ||
| id={field.name} | ||
| name={field.name} | ||
| value={field.state.value} | ||
| onBlur={field.handleBlur} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| placeholder={t`Paste CSR code`} | ||
| errortext={field.state.meta.errors.map((e) => e?.message).join(", ")} | ||
| disabled={isPending} | ||
| /> | ||
| )} | ||
| /> | ||
| </FormSection> | ||
| </Form> | ||
| )} | ||
| </Modal> | ||
| ) | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
...rojectId/services/pca/$pcaId/-components/-modals/IssueSelfSignedCertificateModal.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" | ||
| import { act, render, screen, waitFor } from "@testing-library/react" | ||
| import userEvent from "@testing-library/user-event" | ||
| import { I18nProvider } from "@lingui/react" | ||
| import { i18n } from "@lingui/core" | ||
| import { PortalProvider } from "@cloudoperators/juno-ui-components" | ||
| import type { CertificateAuthority } from "@/server/Services/types/pca" | ||
| import { IssueSelfSignedCertificateModal } from "./IssueSelfSignedCertificateModal" | ||
|
|
||
| const mockProjectId = "project-123" | ||
| const mockMutateAsync = vi.fn().mockResolvedValue({}) | ||
| const mockReset = vi.fn() | ||
| const mockInvalidate = vi.fn() | ||
|
|
||
| vi.mock("@/client/hooks", () => ({ | ||
| useProjectId: () => mockProjectId, | ||
| })) | ||
|
|
||
| vi.mock("@/client/trpcClient", () => ({ | ||
| trpcReact: { | ||
| useUtils: () => ({ | ||
| services: { | ||
| pca: { | ||
| listCertificates: { | ||
| invalidate: mockInvalidate, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| services: { | ||
| pca: { | ||
| createCertificate: { | ||
| useMutation: (options?: { onSettled?: () => void }) => ({ | ||
| isPending: false, | ||
| mutateAsync: async (input: unknown) => { | ||
| const result = await mockMutateAsync(input) | ||
| options?.onSettled?.() | ||
| return result | ||
| }, | ||
| reset: mockReset, | ||
| error: null, | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| const createPca = (csr?: string): CertificateAuthority => ({ | ||
| id: "ca-1", | ||
| project_id: "project-123", | ||
| state: "AWAITING_CERTIFICATE", | ||
| csr, | ||
| }) | ||
|
|
||
| const renderModal = (pca: CertificateAuthority, onClose = vi.fn()) => | ||
| render( | ||
| <I18nProvider i18n={i18n}> | ||
| <PortalProvider> | ||
| <IssueSelfSignedCertificateModal open={true} onClose={onClose} pca={pca} /> | ||
| </PortalProvider> | ||
| </I18nProvider> | ||
| ) | ||
|
|
||
| describe("IssueSelfSignedCertificateModal", () => { | ||
| beforeEach(async () => { | ||
| vi.clearAllMocks() | ||
| await act(async () => { | ||
| i18n.activate("en") | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it("does not submit when pca csr is missing", async () => { | ||
| renderModal(createPca()) | ||
| expect(screen.getByRole("button", { name: "Issue Certificate" })).toBeDisabled() | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockMutateAsync).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| it("submits self-signed certificate payload and closes modal", async () => { | ||
| const user = userEvent.setup() | ||
| const onClose = vi.fn() | ||
| vi.spyOn(Date, "now").mockReturnValue(1_700_000_000_000) | ||
|
|
||
| renderModal(createPca("-----BEGIN CERTIFICATE REQUEST-----\nabc\n-----END CERTIFICATE REQUEST-----"), onClose) | ||
| await user.click(screen.getByRole("button", { name: "Issue Certificate" })) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockMutateAsync).toHaveBeenCalledWith({ | ||
| project_id: "project-123", | ||
| certificate_authority_id: "ca-1", | ||
| csr: "-----BEGIN CERTIFICATE REQUEST-----\nabc\n-----END CERTIFICATE REQUEST-----", | ||
| configuration: { | ||
| validity: { | ||
| not_after: 1_700_086_400, | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| expect(mockInvalidate).toHaveBeenCalledTimes(1) | ||
| expect(onClose).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.