-
Notifications
You must be signed in to change notification settings - Fork 137
Add mTLS auth modal and validation #628
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
Open
marcschwaiger
wants to merge
2
commits into
netbirdio:main
Choose a base branch
from
marcschwaiger:feature/mtls-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,241 @@ | ||
| import Button from "@components/Button"; | ||
| import HelpText from "@components/HelpText"; | ||
| import { Label } from "@components/Label"; | ||
| import { Modal, ModalClose, ModalContent } from "@components/modal/Modal"; | ||
| import ModalHeader from "@components/modal/ModalHeader"; | ||
| import { Textarea } from "@components/Textarea"; | ||
| import { GradientFadedBackground } from "@components/ui/GradientFadedBackground"; | ||
| import { cn } from "@utils/helpers"; | ||
| import { FileUp } from "lucide-react"; | ||
| import React, { useMemo, useRef, useState } from "react"; | ||
|
|
||
| const MASKED_VALUE = "••••••••"; | ||
|
|
||
| function validateCertificatePEM(value: string): string | undefined { | ||
| const trimmed = value.trim(); | ||
| if (!trimmed) return "CA certificate PEM is required"; | ||
|
|
||
| const matches = trimmed.match( | ||
| /-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g, | ||
| ); | ||
|
|
||
| if (!matches || matches.length === 0) { | ||
| return "Enter a valid PEM certificate"; | ||
| } | ||
|
|
||
| for (const cert of matches) { | ||
| const base64Body = cert | ||
| .replace(/-----BEGIN CERTIFICATE-----/g, "") | ||
| .replace(/-----END CERTIFICATE-----/g, "") | ||
| .replace(/\s+/g, ""); | ||
|
|
||
| if (!base64Body) { | ||
| return "Enter a valid PEM certificate"; | ||
| } | ||
|
|
||
| let decoded = ""; | ||
| try { | ||
| decoded = atob(base64Body); | ||
| } catch { | ||
| return "Certificate PEM contains invalid base64 data"; | ||
| } | ||
|
|
||
| if (!decoded || decoded.charCodeAt(0) !== 0x30) { | ||
| return "Certificate PEM does not contain a valid X.509 certificate"; | ||
| } | ||
| } | ||
|
|
||
| const leftover = trimmed | ||
| .replace(/-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----/g, "") | ||
| .replace(/^\s*#.*$/gm, "") | ||
| .trim(); | ||
| if (leftover.length > 0) { | ||
| return "Only PEM certificate data is allowed"; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| type Props = { | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| currentCACertPEM: string; | ||
| isEnabled: boolean; | ||
| onSave: (caCertPEM: string) => void; | ||
| onRemove: () => void; | ||
| }; | ||
|
|
||
| export default function AuthMTLSModal({ | ||
| open, | ||
| onOpenChange, | ||
| currentCACertPEM, | ||
| isEnabled, | ||
| onSave, | ||
| onRemove, | ||
| }: Readonly<Props>) { | ||
| const [caCertPEM, setCACertPEM] = useState(currentCACertPEM); | ||
| const [isMasked, setIsMasked] = useState(isEnabled && currentCACertPEM === ""); | ||
| const isEditing = isEnabled; | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const validationError = useMemo(() => { | ||
| if (isMasked) return undefined; | ||
| if (!caCertPEM.trim()) return undefined; | ||
| return validateCertificatePEM(caCertPEM); | ||
| }, [caCertPEM, isMasked]); | ||
|
|
||
| const handleSave = () => { | ||
| if (isMasked) { | ||
| onOpenChange(false); | ||
| onSave(""); | ||
| return; | ||
| } | ||
|
|
||
| const error = validateCertificatePEM(caCertPEM); | ||
| if (error) return; | ||
|
|
||
| onOpenChange(false); | ||
| onSave(caCertPEM); | ||
| }; | ||
|
|
||
| const handleRemove = () => { | ||
| onOpenChange(false); | ||
| setCACertPEM(""); | ||
| setIsMasked(false); | ||
| onRemove(); | ||
| }; | ||
|
|
||
| const handleFileText = (text: string) => { | ||
| setIsMasked(false); | ||
| setCACertPEM(text); | ||
| }; | ||
|
|
||
| const handleFileUpload = (files: FileList | null) => { | ||
| if (!files || files.length === 0) return; | ||
| const file = files[0]; | ||
| const fileReader = new FileReader(); | ||
| fileReader.readAsText(file, "UTF-8"); | ||
| fileReader.onload = (e) => { | ||
| if (e.target === null) return; | ||
| handleFileText(e.target.result as string); | ||
| }; | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal open={open} onOpenChange={onOpenChange}> | ||
| <ModalContent maxWidthClass="max-w-2xl"> | ||
| <ModalHeader | ||
| title="mTLS" | ||
| description="Require clients to present a certificate signed by your trusted CA." | ||
| /> | ||
|
|
||
| <GradientFadedBackground /> | ||
|
|
||
| <div className="px-8"> | ||
| <div className="flex flex-col gap-4"> | ||
| <div className="flex flex-col gap-2"> | ||
| <Label htmlFor="mtls-ca-cert-pem">Client CA Certificate PEM</Label> | ||
| <Textarea | ||
| id="mtls-ca-cert-pem" | ||
| aria-label="Client CA certificate PEM" | ||
| placeholder="-----BEGIN CERTIFICATE-----" | ||
| value={isMasked ? MASKED_VALUE : caCertPEM} | ||
| onChange={(e) => { | ||
| if (isMasked) { | ||
| setIsMasked(false); | ||
| setCACertPEM(e.target.value.replace(/•/g, "")); | ||
| } else { | ||
| setCACertPEM(e.target.value); | ||
| } | ||
| }} | ||
| error={validationError} | ||
| className="min-h-[160px] font-mono text-xs" | ||
| resize | ||
| /> | ||
| <HelpText margin={false}> | ||
| Paste one PEM certificate or a PEM certificate bundle for the client CA. | ||
| </HelpText> | ||
| </div> | ||
|
|
||
| <div | ||
| className={cn( | ||
| "flex gap-5 border border-dashed hover:border-nb-gray-600/50 rounded-md border-nb-gray-600/40 items-center justify-center group/upload", | ||
| "bg-nb-gray-930/50 hover:bg-nb-gray-930/40 cursor-pointer transition-all px-4 pb-8 pt-6", | ||
| )} | ||
| onClick={() => inputRef.current?.click()} | ||
| onKeyDown={(e) => { | ||
| if (e.key === "Enter" || e.key === " ") { | ||
| e.preventDefault(); | ||
| inputRef.current?.click(); | ||
| } | ||
| }} | ||
| role="button" | ||
| tabIndex={0} | ||
| aria-label="Upload client CA certificate file" | ||
| > | ||
| <input | ||
| ref={inputRef} | ||
| type="file" | ||
| className="sr-only" | ||
| accept=".pem,.crt,.cer,.txt" | ||
| onChange={(e) => handleFileUpload(e.target.files)} | ||
| /> | ||
| <div className="bg-nb-gray-930 p-2.5 rounded-md mt-0.5 group-hover/upload:bg-nb-gray-930/80 transition-all"> | ||
| <FileUp size={20} className="text-netbird" /> | ||
| </div> | ||
| <div> | ||
| <p className="text-[14px] font-medium text-nb-gray-100"> | ||
| Upload certificate file | ||
| </p> | ||
| <p className="text-xs !text-nb-gray-300 mt-1"> | ||
| <span className="underline underline-offset-4 group-hover/upload:text-nb-gray-200 transition-all"> | ||
| Click to upload | ||
| </span>{" "} | ||
| or paste the PEM directly above | ||
| </p> | ||
| </div> | ||
| </div> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| </div> | ||
|
|
||
| <div className="flex gap-3 w-full justify-between mt-6"> | ||
| {isEditing ? ( | ||
| <> | ||
| <Button variant="danger-text" onClick={handleRemove}> | ||
| Remove | ||
| </Button> | ||
| <div className="flex gap-3"> | ||
| <ModalClose asChild> | ||
| <Button variant="secondary">Cancel</Button> | ||
| </ModalClose> | ||
| <Button | ||
| variant="primary" | ||
| onClick={handleSave} | ||
| disabled={(!isMasked && !caCertPEM.trim()) || !!validationError} | ||
| > | ||
| Save | ||
| </Button> | ||
| </div> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <div /> | ||
| <div className="flex gap-3"> | ||
| <ModalClose asChild> | ||
| <Button variant="secondary">Cancel</Button> | ||
| </ModalClose> | ||
| <Button | ||
| variant="primary" | ||
| onClick={handleSave} | ||
| disabled={!caCertPEM.trim() || !!validationError} | ||
| > | ||
| Add mTLS | ||
| </Button> | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </ModalContent> | ||
| </Modal> | ||
| ); | ||
| } | ||
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.