Files
linux-cloud-router/web/src/PkiSettingsDialog.tsx
T

143 lines
6.9 KiB
TypeScript

import {
Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography,
} from '@mui/material'
import { useState, type FormEvent } from 'react'
import { PKI_KEYS, type Pki, type PkiSettings } from './api.ts'
import { ROOT_SOURCE_LABELS, daysProblem, made, nameProblem, urlProblem } from './pki.ts'
/**
* A dialog that edits the server's pki section but the general-purpose issuing CAs, the templates and the custom
* extended key usages, which IssuingCaDialog, TemplateDialog and EkuDialog edit: the names of the P2S and site-to-site
* CAs, the lifetimes and key type of the CAs, the validity of the CRLs and, with a Simple CA root, the URL of the
* service. The source of the root is shown and not edited, as it is fixed once
* the root exists. The root and the Servers CA are made by the first setup, so their names are not shown; the P2S and
* site-to-site CAs are made when the VPN and the router are switched on, and their names are fixed from then on. The
* server applies the lifetimes and key type when it creates a CA. onSave sends the settings; the dialog closes when it
* succeeds and shows the server's message when it fails.
*/
export default function PkiSettingsDialog(props: {
pki: Pki
onSave: (settings: Omit<PkiSettings, 'issuing_cas' | 'templates' | 'extended_key_usages'>) => Promise<void>
onCancel: () => void
}) {
const [settings, setSettings] = useState<PkiSettings>(props.pki.settings)
const [saving, setSaving] = useState(false)
const [error, setError] = useState<string | null>(null)
const change = (values: Partial<PkiSettings>) => setSettings({ ...settings, ...values })
const fixed = (name: string) => props.pki.status.authorities.some((authority) => authority.name === name && made(authority))
const blocked = [
nameProblem(settings.p2s_name), nameProblem(settings.s2s_name),
daysProblem(settings.root_days), daysProblem(settings.ca_days), daysProblem(settings.crl_days), daysProblem(settings.root_crl_days),
settings.root === 'simple-ca' ? urlProblem(settings.simple_ca_url) : '',
].some((problem) => problem !== '')
async function submit(event: FormEvent) {
event.preventDefault()
if (blocked) {
return
}
setSaving(true)
setError(null)
const { issuing_cas: _issuingCas, templates: _templates, extended_key_usages: _usages, ...rest } = settings
try {
await props.onSave(rest)
} catch (caught) {
setError((caught as Error).message)
setSaving(false)
}
}
const number = (value: string) => Math.trunc(Number(value) || 0)
return (
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onCancel()} maxWidth="md" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
<DialogTitle>Edit PKI settings</DialogTitle>
<DialogContent>
<Stack sx={{ gap: 2, pt: 1 }}>
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
<Typography variant="body2" color="text.secondary">
Names, lifetimes and the key type apply when the server creates a CA; URLs apply to the certificates signed afterwards.
The P2S and site-to-site CAs are created when the VPN and the router are switched on.
</Typography>
<Typography variant="subtitle2">CAs</Typography>
<TextField label="Root CA source" value={ROOT_SOURCE_LABELS[settings.root]} disabled helperText="Fixed once the root exists." />
{settings.root === 'simple-ca' && (
<TextField
label="Simple CA URL"
value={settings.simple_ca_url}
autoComplete="off"
error={urlProblem(settings.simple_ca_url) !== ''}
helperText={urlProblem(settings.simple_ca_url) || 'The service the server exchanges setup codes with and has its issuing CAs signed by.'}
onChange={(event) => change({ simple_ca_url: event.target.value.trim() })}
/>
)}
{(['p2s', 's2s'] as const).map((name) => {
const option = `${name}_name` as 'p2s_name' | 's2s_name'
return (
<TextField
key={name}
label={{ p2s: 'P2S CA name', s2s: 'Site-to-site CA name' }[name]}
value={settings[option]}
disabled={fixed(name)}
autoComplete="off"
error={nameProblem(settings[option]) !== ''}
helperText={nameProblem(settings[option]) || (fixed(name) ? 'Fixed once the CA exists.' : '')}
onChange={(event) => change({ [option]: event.target.value })}
/>
)
})}
<Stack direction="row" sx={{ gap: 2 }}>
<TextField
fullWidth
label="Root days"
type="number"
value={settings.root_days}
error={daysProblem(settings.root_days) !== ''}
helperText={daysProblem(settings.root_days)}
onChange={(event) => change({ root_days: number(event.target.value) })}
/>
<TextField
fullWidth
label="Issuing CA days"
type="number"
value={settings.ca_days}
error={daysProblem(settings.ca_days) !== ''}
helperText={daysProblem(settings.ca_days)}
onChange={(event) => change({ ca_days: number(event.target.value) })}
/>
<TextField select fullWidth label="CA key" value={settings.key} onChange={(event) => change({ key: event.target.value })}>
{PKI_KEYS.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
</TextField>
</Stack>
<Typography variant="subtitle2">CRLs</Typography>
<Stack direction="row" sx={{ gap: 2 }}>
<TextField
fullWidth
label="Issuing CA CRL days"
type="number"
value={settings.crl_days}
error={daysProblem(settings.crl_days) !== ''}
helperText={daysProblem(settings.crl_days) || 'Signed again at every revocation and daily after half of it.'}
onChange={(event) => change({ crl_days: number(event.target.value) })}
/>
<TextField
fullWidth
label="Root CRL days"
type="number"
value={settings.root_crl_days}
error={daysProblem(settings.root_crl_days) !== ''}
helperText={daysProblem(settings.root_crl_days) || 'When the server signs it, for a root the server created.'}
onChange={(event) => change({ root_crl_days: number(event.target.value) })}
/>
</Stack>
</Stack>
</DialogContent>
<DialogActions>
<Button onClick={props.onCancel}>Cancel</Button>
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
</DialogActions>
</Dialog>
)
}