Certificates: reuse the installer's HTTP-01 account, and ask port 80 or 443 #209

Merged
jcoffey-dev merged 1 commits from fix/certs-http01 into main 2026-11-15 05:19:36 +00:00
6 changed files with 173 additions and 24 deletions

No files matched your search

@@ -8,7 +8,8 @@
* inbuxa: guided setup, "Certificates, automatically" (settings-reorg, first
* wave). Picks domains, checks every name the certificate will cover before
* anything is ordered, chooses how Let's Encrypt checks them (through DNS
* when the domain's DNS is connected, otherwise on port 443), switches the
* when the domain's DNS is connected, otherwise on port 80 or 443: an
* existing HTTP-01 account, as the installer makes, picks port 80), switches the
* domains to automatic, and watches the certificates arrive.
*/
@@ -29,13 +30,15 @@ import type { JmapSetResponse } from '@/types/jmap';
import { RESOLVER_NAME } from '@/features/dns/liveCheck';
import { cn } from '@/lib/utils';
import { classify, orderNames, type Method } from './names';
import { DirectChoice } from './DirectChoice';
import {
addProvider,
CHALLENGE,
type CertRow,
type Check,
coveringCert,
currentCert,
defaultDirect,
type Direct,
type DomainRow,
fmtDate,
LE_PRODUCTION,
@@ -43,6 +46,8 @@ import {
type Loaded,
load,
pollOrders,
PORT,
providerFor,
resolveAddresses,
} from './shared';
import { StateIcon } from './StateIcon';
@@ -72,6 +77,7 @@ export function CertificateSetupPage() {
const [anyway, setAnyway] = useState<Set<string>>(new Set());
const [staging, setStaging] = useState(false);
const [contact, setContact] = useState('');
const [direct, setDirect] = useState<Direct>('TlsAlpn01');
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [startedAt, setStartedAt] = useState<number | null>(null);
@@ -87,6 +93,7 @@ export function CertificateSetupPage() {
setData(d);
setPicked(new Set(d.domains.filter((x) => !x.automatic).map((x) => x.id)));
setContact(d.providers.find((p) => p.contact.length > 0)?.contact[0] ?? '');
setDirect(defaultDirect(d.providers));
})
.catch((e: unknown) => live && setLoadError(e instanceof Error ? e.message : String(e)));
return () => {
@@ -157,9 +164,9 @@ export function CertificateSetupPage() {
const directory = staging ? LE_STAGING : LE_PRODUCTION;
const methodsNeeded = [...new Set(going.map(methodOf))];
const providerFor = (m: Method) =>
data?.providers.find((p) => p.directory === directory && p.challengeType === CHALLENGE[m]);
const needsNewProvider = methodsNeeded.some((m) => !providerFor(m));
const existingFor = (m: Method) => (data ? providerFor(data.providers, directory, m, direct) : undefined);
const needsNewProvider = methodsNeeded.some((m) => !existingFor(m));
const port = PORT[direct];
const poll = useCallback(async () => {
setWatch(await pollOrders());
@@ -189,12 +196,12 @@ export function CertificateSetupPage() {
try {
const ids: Partial<Record<Method, string>> = {};
for (const m of methodsNeeded) {
const existing = providerFor(m);
const existing = existingFor(m);
if (existing) {
ids[m] = existing.id;
continue;
}
const id = await addProvider(m, contact, staging);
const id = await addProvider(m, direct, contact, staging);
ids[m] = id;
made.push(id);
}
@@ -272,7 +279,8 @@ export function CertificateSetupPage() {
<p>
{t(
'certSetup.howTls',
'Otherwise Let’s Encrypt connects to this server on port 443 for each name. Those names must point straight at this server, not through a proxy.',
'Otherwise Let’s Encrypt connects to this server on port {{port}} for each name. Those names must point straight at this server, not through a proxy.',
{ port },
)}
</p>
</WizardNote>
@@ -316,7 +324,7 @@ export function CertificateSetupPage() {
: t('certSetup.noCert', 'No certificate yet.')}{' '}
{methodOf(d) === 'dns'
? t('certSetup.viaDns', 'Checked through DNS.')
: t('certSetup.viaTls', 'Checked on port 443.')}
: t('certSetup.viaTls', 'Checked on port {{port}}.', { port })}
</span>
</label>
</li>
@@ -467,6 +475,7 @@ export function CertificateSetupPage() {
</WizardNote>
}
>
{methodsNeeded.includes('tls') && <DirectChoice value={direct} onChange={setDirect} />}
{needsNewProvider ? (
<div className="space-y-1.5">
<Label htmlFor="acme-contact">{t('certSetup.contact', 'Your email address')}</Label>
@@ -553,7 +562,7 @@ export function CertificateSetupPage() {
how:
methodOf(d) === 'dns'
? t('certSetup.howDnsWord', 'through DNS')
: t('certSetup.howTlsWord', 'on port 443'),
: t('certSetup.howTlsWord', 'on port {{port}}', { port }),
names: namesOf(d).join(', '),
},
)}
@@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
/**
* inbuxa: how Let’s Encrypt reaches this server for names checked without
* DNS, port 80 or port 443 (#206). Both certificate guides ask it.
*/
import { useTranslation } from 'react-i18next';
import type { Direct } from './shared';
export function DirectChoice({ value, onChange }: { value: Direct; onChange: (v: Direct) => void }) {
const { t } = useTranslation();
const choice = (v: Direct, title: string, body: string) => (
<label
className={`flex cursor-pointer gap-3 rounded-lg border p-3 text-sm ${value === v ? 'border-primary bg-primary/5' : ''}`}
>
<input type="radio" name="acme-direct" className="mt-1" checked={value === v} onChange={() => onChange(v)} />
<span>
<span className="block font-medium">{title}</span>
<span className="text-muted-foreground">{body}</span>
</span>
</label>
);
return (
<fieldset className="space-y-2">
<legend className="mb-1.5 text-sm font-medium">
{t('certSetup.directTitle', 'How Let’s Encrypt reaches this server')}
</legend>
{choice(
'Http01',
t('certSetup.directHttp', 'Port 80 (HTTP-01)'),
t(
'certSetup.directHttpHint',
'A web server in front of this one holds port 443 and passes /.well-known/acme-challenge/ on port 80 to it. The installer sets it up this way.',
),
)}
{choice(
'TlsAlpn01',
t('certSetup.directTls', 'Port 443 (TLS-ALPN-01)'),
t('certSetup.directTlsHint', 'This server answers HTTPS on port 443 itself.'),
)}
</fieldset>
);
}
@@ -41,12 +41,14 @@ import {
sameNames,
suggestDomain,
} from './orders';
import { DirectChoice } from './DirectChoice';
import {
addProvider,
CHALLENGE,
type CertRow,
type Check,
coveringCert,
defaultDirect,
type Direct,
type DomainRow,
fmtDate,
LE_PRODUCTION,
@@ -54,6 +56,8 @@ import {
type Loaded,
load,
pollOrders,
PORT,
providerFor,
refusal,
resolveAddresses,
} from './shared';
@@ -79,6 +83,7 @@ export function OrderCertificatePage() {
const [anyway, setAnyway] = useState(false);
const [staging, setStaging] = useState(false);
const [contact, setContact] = useState('');
const [direct, setDirect] = useState<Direct>('TlsAlpn01');
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [startedAt, setStartedAt] = useState<number | null>(null);
@@ -101,6 +106,7 @@ export function OrderCertificatePage() {
if (!live) return;
setData(d);
setContact(d.providers.find((p) => p.contact.length > 0)?.contact[0] ?? '');
setDirect(defaultDirect(d.providers));
})
.catch((e: unknown) => live && setLoadError(e instanceof Error ? e.message : String(e)));
return () => {
@@ -135,7 +141,8 @@ export function OrderCertificatePage() {
const replaced = domain?.automatic && !unchanged ? currentNames.filter((n) => !names.includes(n)) : [];
const directory = staging ? LE_STAGING : LE_PRODUCTION;
const provider = data?.providers.find((p) => p.directory === directory && p.challengeType === CHALLENGE[method]);
const provider = data ? providerFor(data.providers, directory, method, direct) : undefined;
const port = PORT[direct];
const runChecks = useCallback(async () => {
if (!data || method !== 'tls') return;
@@ -223,7 +230,7 @@ export function OrderCertificatePage() {
try {
let providerId = provider?.id;
if (!providerId) {
providerId = await addProvider(method, contact, staging);
providerId = await addProvider(method, direct, contact, staging);
made = providerId;
}
const [res] = await jmapSet('x:Domain', getAccountId('x:Domain'), {
@@ -290,7 +297,8 @@ export function OrderCertificatePage() {
{error}
</p>
);
const how = method === 'dns' ? t('certSetup.howDnsWord', 'through DNS') : t('certSetup.howTlsWord', 'on port 443');
const how =
method === 'dns' ? t('certSetup.howDnsWord', 'through DNS') : t('certSetup.howTlsWord', 'on port {{port}}', { port });
// ── Step 1: names (also where it goes back to if the domain stops fitting) ──
if (step === 0 || !domain) {
@@ -319,7 +327,8 @@ export function OrderCertificatePage() {
<p>
{t(
'certOrder.howDns',
'When the domain’s DNS is connected, the names are checked through DNS, and wildcards work. Otherwise Let’s Encrypt connects to this server on port 443 for each name.',
'When the domain’s DNS is connected, the names are checked through DNS, and wildcards work. Otherwise Let’s Encrypt connects to this server on port {{port}} for each name.',
{ port },
)}
</p>
</WizardNote>
@@ -371,7 +380,7 @@ export function OrderCertificatePage() {
<p className="text-xs text-muted-foreground">
{method === 'dns'
? t('certSetup.viaDns', 'Checked through DNS.')
: t('certSetup.viaTls', 'Checked on port 443.')}
: t('certSetup.viaTls', 'Checked on port {{port}}.', { port })}
</p>
</div>
{wildcardBlocked && (
@@ -575,6 +584,7 @@ export function OrderCertificatePage() {
</WizardNote>
}
>
{method === 'tls' && <DirectChoice value={direct} onChange={setDirect} />}
{provider ? (
<p className="text-sm">
{t('certSetup.reuse', 'The Let’s Encrypt account already set up on this server will be used.')}
+42
View File
@@ -0,0 +1,42 @@
/*
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { describe, expect, it } from 'vitest';
import { challengeOf, defaultDirect, LE_PRODUCTION, LE_STAGING, PORT, providerFor, type ProviderRow } from './shared';
const row = (id: string, challengeType: string, directory = LE_PRODUCTION): ProviderRow => ({
id,
directory,
challengeType,
contact: [],
});
describe('certificate accounts (#206)', () => {
it('prefers port 80 when the server has an HTTP-01 account, as an installer setup does', () => {
expect(defaultDirect([row('a', 'Http01')])).toBe('Http01');
expect(defaultDirect([row('a', 'TlsAlpn01'), row('b', 'Http01', LE_STAGING)])).toBe('Http01');
expect(defaultDirect([row('a', 'TlsAlpn01'), row('b', 'Dns01')])).toBe('TlsAlpn01');
expect(defaultDirect([])).toBe('TlsAlpn01');
expect(PORT.Http01).toBe(80);
expect(PORT.TlsAlpn01).toBe(443);
});
it('reuses the installer’s HTTP-01 account instead of adding one', () => {
const providers = [row('installer', 'Http01'), row('dns', 'Dns01')];
expect(providerFor(providers, LE_PRODUCTION, 'tls', 'Http01')?.id).toBe('installer');
expect(providerFor(providers, LE_PRODUCTION, 'dns', 'Http01')?.id).toBe('dns');
// Port 443 picked: an HTTP-01 account doesn't do, so one is added.
expect(providerFor(providers, LE_PRODUCTION, 'tls', 'TlsAlpn01')).toBeUndefined();
// Staging is its own account.
expect(providerFor(providers, LE_STAGING, 'tls', 'Http01')).toBeUndefined();
});
it('asks for the challenge that matches the way names are checked', () => {
expect(challengeOf('dns', 'Http01')).toBe('Dns01');
expect(challengeOf('tls', 'Http01')).toBe('Http01');
expect(challengeOf('tls', 'TlsAlpn01')).toBe('TlsAlpn01');
});
});
+38 -3
View File
@@ -14,7 +14,37 @@ import type { Method, NameState } from './names';
export const LE_PRODUCTION = 'https://acme-v02.api.letsencrypt.org/directory';
export const LE_STAGING = 'https://acme-staging-v02.api.letsencrypt.org/directory';
export const CHALLENGE: Record<Method, string> = { dns: 'Dns01', tls: 'TlsAlpn01' };
/**
* How Let’s Encrypt reaches this server for names checked without DNS:
* HTTP-01 on port 80, or TLS-ALPN-01 on port 443. The server answers either.
* Behind a web server that holds 443, only HTTP-01 works: the installer's
* Caddy forwards /.well-known/acme-challenge/ on port 80 to the server, and
* the installer makes an HTTP-01 account for it (#206).
*/
export type Direct = 'Http01' | 'TlsAlpn01';
export const PORT: Record<Direct, number> = { Http01: 80, TlsAlpn01: 443 };
/** HTTP-01 when the server already has an account for it, as an installer setup does; otherwise TLS-ALPN-01. */
export function defaultDirect(providers: ProviderRow[]): Direct {
return providers.some((p) => p.challengeType === 'Http01') ? 'Http01' : 'TlsAlpn01';
}
/** The challenge type an account needs for one way of checking names. */
export function challengeOf(method: Method, direct: Direct): string {
return method === 'dns' ? 'Dns01' : direct;
}
/** The existing Let’s Encrypt account for one way of checking names, if there is one. */
export function providerFor(
providers: ProviderRow[],
directory: string,
method: Method,
direct: Direct,
): ProviderRow | undefined {
return providers.find((p) => p.directory === directory && p.challengeType === challengeOf(method, direct));
}
export interface DomainRow {
id: string;
@@ -163,12 +193,17 @@ export async function pollOrders(): Promise<{ certs: CertRow[]; tasks: Record<st
* Adds a Let’s Encrypt account for one way of checking names, and returns its
* id. The server writes the description itself, and refuses one from a client.
*/
export async function addProvider(method: Method, contact: string, staging: boolean): Promise<string> {
export async function addProvider(
method: Method,
direct: Direct,
contact: string,
staging: boolean,
): Promise<string> {
const [res] = await jmapSet('x:AcmeProvider', getAccountId('x:AcmeProvider'), {
create: {
acme: {
directory: staging ? LE_STAGING : LE_PRODUCTION,
challengeType: CHALLENGE[method],
challengeType: challengeOf(method, direct),
contact: { [contact.trim()]: true },
},
},
+10 -5
View File
@@ -659,7 +659,7 @@
"subtitle": "Free certificates from Let’s Encrypt for your domains, renewed by the server before they expire.",
"howTitle": "How the server proves it’s yours",
"howDns": "A domain whose DNS is connected is checked through DNS: the certificate covers the domain and everything under it (*.domain), and nothing needs to reach this server.",
"howTls": "Otherwise Let’s Encrypt connects to this server on port 443 for each name. Those names must point straight at this server, not through a proxy.",
"howTls": "Otherwise Let’s Encrypt connects to this server on port {{port}} for each name. Those names must point straight at this server, not through a proxy.",
"noDomains": "There are no domains yet.",
"addDomain": "Add one first",
"alreadyAuto": "Already automatic.",
@@ -667,7 +667,7 @@
"validUntil": "Current certificate valid until {{date}}.",
"noCert": "No certificate yet.",
"viaDns": "Checked through DNS.",
"viaTls": "Checked on port 443.",
"viaTls": "Checked on port {{port}}.",
"checkedWith": "How this is checked",
"checkedHow": "Each name is looked up in public DNS through {{resolver}}, and compared with where {{server}} points. Nothing is ordered yet.",
"thisServer": "this server",
@@ -701,7 +701,7 @@
"stagingWord": "staging ",
"domainAuto": "{{domain}} gets its certificate automatically, checked {{how}}: {{names}}.",
"howDnsWord": "through DNS",
"howTlsWord": "on port 443",
"howTlsWord": "on port {{port}}",
"skipped": "Left as they are until their names are fixed: {{names}}.",
"issued": "Issued. Valid until {{date}}.",
"failed": "Failed.",
@@ -712,7 +712,12 @@
"after": "The server renews each certificate well before it expires. Failed renewals show under Management › Tasks › Failed.",
"keepWatching": "Keep watching",
"watchingHint": "Checking every few seconds. Most certificates arrive within a minute.",
"tabTitle": "Certificates"
"tabTitle": "Certificates",
"directTitle": "How Let’s Encrypt reaches this server",
"directHttp": "Port 80 (HTTP-01)",
"directHttpHint": "A web server in front of this one holds port 443 and passes /.well-known/acme-challenge/ on port 80 to it. The installer sets it up this way.",
"directTls": "Port 443 (TLS-ALPN-01)",
"directTlsHint": "This server answers HTTPS on port 443 itself."
},
"overview": {
"first": "First record",
@@ -2718,7 +2723,7 @@
"subtitle": "A free Let’s Encrypt certificate for the names you choose, renewed by the server before it expires.",
"oneDomainTitle": "One certificate per domain",
"oneDomain": "The server orders each certificate for a domain, and renews it for as long as the domain says so. The names you enter become that domain’s certificate hostnames.",
"howDns": "When the domain’s DNS is connected, the names are checked through DNS, and wildcards work. Otherwise Let’s Encrypt connects to this server on port 443 for each name.",
"howDns": "When the domain’s DNS is connected, the names are checked through DNS, and wildcards work. Otherwise Let’s Encrypt connects to this server on port {{port}} for each name.",
"namesLabel": "Names the certificate covers",
"namesHint": "One per line. A wildcard (*.example.com) covers every name one level under it.",
"invalid": "Not a name a certificate can cover: {{names}}",