Skip to content

Commit ed5567b

Browse files
MakFlyclaude
andcommitted
feat(tls): verify a certificate issued for a name the endpoint does not carry
A node reached at its LAN address while holding a certificate issued for a routable name cannot satisfy standard verification: the address is right and the name does not match. That is the ordinary shape of a homelab once Tailscale, an ACME domain or a reverse proxy is in front of pveproxy — and until now the only way through was to pin the certificate. Pinning answers it, then stops answering it. An ACME certificate is renewed, so the empreinte changes on a schedule, and every renewal resurfaces as "someone is between you and it" — the one message that must never become routine. The operator learns to retype `config trust` without reading it, which is exactly the reflex the pinning was there to prevent. tls.server_name names the identity the certificate actually carries while the connection still goes to the configured address. Verification returns to a real CA, the route stays local, and a renewal changes nothing. It is ignored in pinned mode: pinning already answers the identity question, and more strictly, so honouring the override there could only weaken it. The CertChanged message also pointed at the wrong file. pveproxy serves pveproxy-ssl.pem when it exists and ignores pve-ssl.pem, so an operator verifying "from the node console" as instructed reads the certificate that is not being served, sees the empreinte they expected, and concludes they are under attack. It now names both files, says which one wins, and offers server_name before pinning when the certificate came from an ACME provider. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVhTKvLaAPfxyhDpLo6ufY
1 parent e4c1aa1 commit ed5567b

7 files changed

Lines changed: 79 additions & 6 deletions

File tree

‎cmd/client.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func newClient(cmd *cobra.Command) (*pve.Client, error) {
6363
Trust: pve.TrustOptions{
6464
Fingerprint: eff.TLS.Fingerprint,
6565
CAFile: eff.TLS.CAFile,
66+
ServerName: eff.TLS.ServerName,
6667
Insecure: eff.Insecure,
6768
},
6869
})

‎cmd/completion.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func loadCompletionInventory(cmd *cobra.Command) (*completionInventory, error) {
121121
return inv, nil
122122
}
123123

124-
inv, err := fetchCompletionInventory(cmd, eff.Endpoint, eff.TokenID, eff.TokenSecret, eff.Insecure, eff.TLS.Fingerprint, eff.TLS.CAFile)
124+
inv, err := fetchCompletionInventory(cmd, eff.Endpoint, eff.TokenID, eff.TokenSecret, eff.Insecure, eff.TLS.Fingerprint, eff.TLS.CAFile, eff.TLS.ServerName)
125125
if err != nil {
126126
return nil, err
127127
}
@@ -136,7 +136,7 @@ func loadCompletionInventory(cmd *cobra.Command) (*completionInventory, error) {
136136
// That line is right for a command and catastrophic for a completion: it would
137137
// land in the middle of the prompt the operator is typing. Here stderr goes to
138138
// io.Discard, and the trace is never enabled.
139-
func fetchCompletionInventory(cmd *cobra.Command, endpoint, tokenID, secret string, insecure bool, fingerprint, caFile string) (*completionInventory, error) {
139+
func fetchCompletionInventory(cmd *cobra.Command, endpoint, tokenID, secret string, insecure bool, fingerprint, caFile, serverName string) (*completionInventory, error) {
140140
client, err := pve.New(pve.Options{
141141
Endpoint: endpoint,
142142
TokenID: tokenID,
@@ -145,6 +145,7 @@ func fetchCompletionInventory(cmd *cobra.Command, endpoint, tokenID, secret stri
145145
Trust: pve.TrustOptions{
146146
Fingerprint: fingerprint,
147147
CAFile: caFile,
148+
ServerName: serverName,
148149
Insecure: insecure,
149150
},
150151
})

‎cmd/login.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Endpoints : POST /access/ticket, POST /access/users, POST /access/users/{id}/tok
112112
Trust: pve.TrustOptions{
113113
Fingerprint: eff.TLS.Fingerprint,
114114
CAFile: eff.TLS.CAFile,
115+
ServerName: eff.TLS.ServerName,
115116
Insecure: eff.Insecure,
116117
},
117118
}

‎internal/config/config.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ type Context struct {
8484
type TLS struct {
8585
Fingerprint string `yaml:"fingerprint,omitempty"`
8686
CAFile string `yaml:"ca_file,omitempty"`
87+
88+
// ServerName is the name the certificate is verified against, when it is
89+
// not the one in the endpoint. A node reached by LAN address but holding a
90+
// certificate issued for a routable name — Tailscale, an ACME domain, a
91+
// reverse proxy — cannot satisfy standard verification: the address is
92+
// right and the name does not match. Setting the name the certificate
93+
// actually carries restores real CA verification without giving up the
94+
// direct address, and without pinning a certificate that will be renewed.
95+
ServerName string `yaml:"server_name,omitempty"`
8796
}
8897

8998
// DefaultManagedTag is the tag the lab repository puts on everything Terraform
@@ -125,6 +134,7 @@ var WritableKeys = []string{
125134
"insecure",
126135
"tls.fingerprint",
127136
"tls.ca_file",
137+
"tls.server_name",
128138
"iac.terraform_dir",
129139
"iac.ansible_dir",
130140
"iac.managed_tag",
@@ -152,6 +162,8 @@ func SetKey(c *Context, key, value string) error {
152162
c.TLS.Fingerprint = value
153163
case "tls.ca_file":
154164
c.TLS.CAFile = value
165+
case "tls.server_name":
166+
c.TLS.ServerName = value
155167

156168
// Absolutised on the way in. A relative path in a config file resolves
157169
// against whatever directory the operator happened to be in, so the same

‎internal/config/resolve.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func Resolve(fl *pflag.FlagSet, f *File) (*Effective, error) {
111111
e.Node = set("node", pick(fl, "node", "", c.Node, ""))
112112
e.TLS.Fingerprint = set("tls.fingerprint", pick(fl, "", "", c.TLS.Fingerprint, ""))
113113
e.TLS.CAFile = set("tls.ca_file", pick(fl, "", "", c.TLS.CAFile, ""))
114+
e.TLS.ServerName = set("tls.server_name", pick(fl, "", "", c.TLS.ServerName, ""))
114115

115116
// --terraform-dir and --ansible-dir are declared by the `iac` commands only,
116117
// so pick() is handed a flag name that most command lines do not have. It

‎internal/pve/tls.go‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ type TrustOptions struct {
3434
Fingerprint string
3535
CAFile string
3636
Insecure bool
37+
38+
// ServerName overrides the name the certificate is checked against, while
39+
// the connection still goes to the configured address. It is what lets a
40+
// node be reached at 192.168.1.23 while presenting a certificate issued
41+
// for pve.example.ts.net: the route stays local, the verification stays
42+
// real. Ignored when an empreinte is pinned — pinning already answers the
43+
// identity question, and it answers it more strictly.
44+
ServerName string
3745
}
3846

3947
// Mode reports which of the four behaviours the options select. Order matters:
@@ -81,10 +89,10 @@ func tlsConfig(t TrustOptions, host string) (*tls.Config, error) {
8189
if !pool.AppendCertsFromPEM(pem) {
8290
return nil, fmt.Errorf("%s ne contient aucun certificat PEM exploitable", t.CAFile)
8391
}
84-
return &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}, nil
92+
return &tls.Config{RootCAs: pool, ServerName: t.ServerName, MinVersion: tls.VersionTLS12}, nil
8593

8694
default:
87-
return &tls.Config{MinVersion: tls.VersionTLS12}, nil
95+
return &tls.Config{ServerName: t.ServerName, MinVersion: tls.VersionTLS12}, nil
8896
}
8997
}
9098

@@ -137,9 +145,22 @@ Soit le nœud a été réinstallé ou son certificat régénéré, soit quelqu'u
137145
s'est intercalé. Ne mets pas à jour l'empreinte sans avoir vérifié laquelle
138146
des deux hypothèses est vraie — depuis la console du nœud :
139147
140-
openssl x509 -in /etc/pve/local/pve-ssl.pem -noout -fingerprint -sha256
148+
openssl x509 -in /etc/pve/local/pveproxy-ssl.pem -noout -fingerprint -sha256 -subject
149+
openssl x509 -in /etc/pve/local/pve-ssl.pem -noout -fingerprint -sha256
150+
151+
Le premier fichier est celui qui compte : quand il existe, pveproxy le sert et
152+
ignore pve-ssl.pem. Un certificat ACME (Tailscale, Let's Encrypt, un proxy
153+
devant le nœud) atterrit là, et se renouvelle tout seul — auquel cas épingler
154+
la nouvelle empreinte ne fait que repousser cette erreur au prochain
155+
renouvellement. Vérifie le nom dans le sujet et préfère :
156+
157+
pvecli config set tls.server_name <le CN du certificat>
158+
pvecli config set tls.fingerprint ""
159+
160+
La connexion continue d'aller à l'adresse configurée, la vérification redevient
161+
celle d'une vraie CA, et un renouvellement ne casse plus rien.
141162
142-
Si le changement est légitime :
163+
Si tu tiens à l'épinglage et que le changement est légitime :
143164
pvecli config trust`, e.Host, formatFingerprint(e.Want), formatFingerprint(e.Got))
144165

145166
case CertAbsent:

‎internal/pve/tls_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,39 @@ func TestFetchCertificate(t *testing.T) {
150150
t.Errorf("Fingerprint = %q, want %q", got, fp)
151151
}
152152
}
153+
154+
// A node reached by address while holding a certificate issued for a routable
155+
// name is the Tailscale/ACME case: the route must stay local and the
156+
// verification must stay real, which is exactly what ServerName buys.
157+
func TestTLSConfigServerNameOverride(t *testing.T) {
158+
t.Run("système", func(t *testing.T) {
159+
cfg, err := tlsConfig(TrustOptions{ServerName: "pve.example.ts.net"}, "192.168.1.23:8006")
160+
if err != nil {
161+
t.Fatalf("tlsConfig: %v", err)
162+
}
163+
if cfg.ServerName != "pve.example.ts.net" {
164+
t.Fatalf("ServerName = %q, attendu pve.example.ts.net", cfg.ServerName)
165+
}
166+
if cfg.InsecureSkipVerify {
167+
t.Fatal("la vérification doit rester active")
168+
}
169+
})
170+
171+
t.Run("empreinte ignore le nom", func(t *testing.T) {
172+
// Pinning already answers the identity question, and more strictly.
173+
// Honouring ServerName here would only add a way to weaken it.
174+
cfg, err := tlsConfig(TrustOptions{
175+
Fingerprint: strings.Repeat("ab", 32),
176+
ServerName: "pve.example.ts.net",
177+
}, "192.168.1.23:8006")
178+
if err != nil {
179+
t.Fatalf("tlsConfig: %v", err)
180+
}
181+
if cfg.ServerName != "" {
182+
t.Fatalf("ServerName = %q, attendu vide en mode épinglé", cfg.ServerName)
183+
}
184+
if cfg.VerifyPeerCertificate == nil {
185+
t.Fatal("le vérificateur épinglé doit rester installé")
186+
}
187+
})
188+
}

0 commit comments

Comments
 (0)