hhdaxia on X asked for mTLS so a shared magpie isn't open to the internet on a key alone; clxsh's #5596 asks Remote Magpie to send a client certificate and key (PEM) to an Nginx doing mTLS, the server still verified, the Gateway Key still sent, for models, the test and streaming. Server side (magpie's own CA, internal/mtls): - Settings -> Network and sharing -> Require a client certificate, or `magpie mtls on|off|issue <device>|list|revoke`. Issue saves client.pem, client-key.pem and ca.pem to ~/Downloads/magpie-<device>; the key is kept nowhere else. - With it on, a peer that isn't this computer or a WSL distro must finish the TLS handshake with a certificate the CA issued and hasn't revoked (RequireAndVerifyClientCert + VerifyConnection -> Admits). The TLS config is chosen per connection, so the switch needs no relisten; without --tls-cert the server certificate is made by the CA in memory. - lanGuard and ClientCertGuard (magpie web's page) refuse 403 what still arrives without a verified one: plain HTTP, a proxy or tunnel on loopback (unless MAGPIE_TRUST_PROXY=1), the page port, a connection from before the switch, a certificate revoked since its handshake. - Loopback, this computer's own address and a WSL agent with its key are answered as before. - A page open from another computer can't shut itself out: without a certificate it can't turn mTLS on, and it can't revoke or reissue the certificate it came with. Client side: a provider with a base URL of the user's own (Remote Magpie included) takes ClientCert, ClientKey and ServerCA paths (editor: Advanced -> Client certificate). netproxy.WithClientCert shows them to that provider's own hosts only, on a transport per identity keyed on the files' mtimes; through an https:// proxy such a request is refused. Save refuses half a pair, a pair that doesn't match and a CA that isn't one. Run (on mote, at 4024df0f9, this commit before its rebase): - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows. - go test -count=1 ./internal/mtls ./internal/netproxy ./internal/provider ./internal/settings ./internal/settingsapi ./internal/cli ./internal/gui ./internal/gateway: ok (gateway 893s). The first run had three reds of this change, each green at base and fixed here: TestEveryRouteHasACommandOrIsNamed (the mtls routes now name `magpie mtls`), TestSettingsSaveKeepsWhatItDoesNotSend (MTLS kept), and TestGoroutineDumpAnswersWhileRequestsWait (mtlsRefuses asks whether the peer is remote before reading settings, so a local request never waits on the settings lock). TestHandedOffGatewayAnswersAConnectionNotedLate was reset because ServeTLS now always wraps the listener: tlsListener hands a connection it took as Close came to the server instead of dropping it. -count=3 of these and every TLS/MTLS/Handover/Relisten test ok. - Every route without a certificate, with a foreign one, a revoked one and a good one: TestMTLSOtherComputerHandshake, TestMTLSGuardEveryRoute, TestMTLSThisComputerAndProxies, TestMTLSToggleWithoutRelisten, TestMTLSRevokeMidConnection, TestClientCertGuardPage. Remote Magpie against an httptest server asking RequireAndVerifyClientCert: models and the connection test fail without the certificate and pass with it (TestRemoteMagpieClientCert). - GUI: the 92 files near the change (settings-*, provider-*, editor-*, lan-*, i18n*, allow-ips, remote-*, mtls-settings new) in Chromium and WebKit: 2904 pass, 74 fail, all in nine files red the same way at base0791c91c5and listed in #5872 (editor-thumbs-batched also #5886; provider-fetch-again #5057 #5232; provider-many-keys, -models-rest, -picks-stay, -typed #5475 #4396 #5233 #4673; remote-magpie #4398; settings-hints, settings-lines-wrap run alone at base and here: the same 8 reds). The whole GUI suite and the whole Go suite were not run: mote's suite lock had about 15 runs queued at load 31-40. Rebased on031938ca9(one conflict, internal/gui/api.go's route list, both kept) and run again there: gofmt clean, vet on three systems, go test ./internal/{gui,cli,mtls,netproxy,provider,settings,settingsapi} ok (cli with the next commit's line), the gateway's TLS, MTLS, Handover, Relisten, HandedOff, GoroutineDump, ClientCert and RemoteMagpie tests ok, TestCoreScriptBudget ok, and mtls-settings, i18n-keys, allow-ips and provider-insecure-tls in Chromium and WebKit: 143 pass, 0 fail. Broken by hand, each red: - lanGuard without refuseWithoutClientCert: TestMTLSGuardEveryRoute, TestMTLSThisComputerAndProxies, TestMTLSRevokeMidConnection. - VerifyClientCertIfGiven and no Admits: TestMTLSOtherComputerHandshake. - netproxy.With keeping another provider's certificate: TestClientCertOnlyItsHost. - The page-lockout and own-serial guards in internal/gui/mtls.go: TestSettingsMTLSPageLockout. Not tried: a real Nginx doing mTLS; a Windows or Linux run (vet only); installing the certificate in a browser (there is no .p12 yet); a proxy that checks certificates itself with MAGPIE_TRUST_PROXY=1.
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// tlsFlags takes --tls-cert <file> and --tls-key <file> (or =<file>) out
|
|
// of args, sets them for the gateway over MAGPIE_TLS_CERT and
|
|
// MAGPIE_TLS_KEY, and checks the pair: one without the other, or files
|
|
// that don't load, refuse to start rather than serve plain HTTP the user
|
|
// didn't ask for. rest is the other arguments.
|
|
func tlsFlags(args []string) (rest []string, err error) {
|
|
cert, key := gateway.TLSFiles()
|
|
for i := 0; i < len(args); i++ {
|
|
switch a := args[i]; {
|
|
case a == "--tls-cert" || a == "--tls-key":
|
|
if i+1 >= len(args) || strings.HasPrefix(args[i+1], "-") {
|
|
return nil, fmt.Errorf("%s takes a file: %s <file>", a, a)
|
|
}
|
|
i++
|
|
if a == "--tls-cert" {
|
|
cert = args[i]
|
|
} else {
|
|
key = args[i]
|
|
}
|
|
case strings.HasPrefix(a, "--tls-cert="):
|
|
cert = strings.TrimPrefix(a, "--tls-cert=")
|
|
case strings.HasPrefix(a, "--tls-key="):
|
|
key = strings.TrimPrefix(a, "--tls-key=")
|
|
default:
|
|
rest = append(rest, a)
|
|
}
|
|
}
|
|
return rest, gateway.SetTLS(cert, key)
|
|
}
|
|
|
|
// tlsLine is the banner's line for TLS, "" with it off.
|
|
func tlsLine() string {
|
|
if !gateway.TLSOn() {
|
|
return ""
|
|
}
|
|
cert, _ := gateway.TLSFiles()
|
|
if cert == "" {
|
|
cert = "magpie's own CA (mTLS)"
|
|
}
|
|
s := muted.Render(" TLS ") + " " + cert
|
|
if sum := gateway.TLSSummary(); sum != "" {
|
|
s += muted.Render(" · " + sum)
|
|
}
|
|
if gateway.MTLSOn() {
|
|
s += "\n" + muted.Render(" other computers show a client certificate magpie issued (magpie mtls)")
|
|
}
|
|
return s + "\n" + muted.Render(" https:// from other computers, read again when the files change; this computer may call plain http://")
|
|
}
|