Обновлять сертификат балансировщика без перезапуска процессов
This commit is contained in:
@@ -217,3 +217,88 @@ func TestClientAuthorizationUsesCurrentChainIdentityExpiryAndRevocation(t *testi
|
||||
t.Fatal("missing revocation policy did not fail closed")
|
||||
}
|
||||
}
|
||||
|
||||
// A failed native commit must be reported and aborted so the agent retries;
|
||||
// persisting a new PEM alone does not prove that the native process uses it.
|
||||
func TestHAProxyCertificateTransaction(t *testing.T) {
|
||||
for _, success := range []bool{true, false} {
|
||||
t.Run(fmt.Sprint(success), func(t *testing.T) {
|
||||
ca := root(t)
|
||||
key, err := x509.MarshalECPrivateKey(ca.key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
directory, err := os.MkdirTemp("", "lb-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { os.RemoveAll(directory) })
|
||||
current := filepath.Join(directory, "current")
|
||||
if err = os.Mkdir(current, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bundle := filepath.Join(current, "bundle.pem")
|
||||
raw := append(ca.pem, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: key})...)
|
||||
if err = os.WriteFile(bundle, raw, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
socket := filepath.Join(directory, "admin.sock")
|
||||
listener, err := net.Listen("unix", socket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
commands := []string{"set ssl cert ", "commit ssl cert "}
|
||||
if !success {
|
||||
commands = append(commands, "abort ssl cert ")
|
||||
}
|
||||
for _, prefix := range commands {
|
||||
conn, e := listener.Accept()
|
||||
if e != nil {
|
||||
done <- e
|
||||
return
|
||||
}
|
||||
conn.SetDeadline(time.Now().Add(3 * time.Second))
|
||||
request, e := io.ReadAll(conn)
|
||||
if e != nil {
|
||||
conn.Close()
|
||||
done <- e
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(string(request), prefix+bundle) {
|
||||
conn.Close()
|
||||
done <- fmt.Errorf("unexpected command prefix")
|
||||
return
|
||||
}
|
||||
reply := "Transaction created for certificate\n"
|
||||
if strings.HasPrefix(prefix, "set") && (!strings.HasSuffix(string(request), "\n\n") || !strings.Contains(string(request), "-----BEGIN EC PRIVATE KEY-----")) {
|
||||
conn.Close()
|
||||
done <- fmt.Errorf("missing bounded PEM payload")
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(prefix, "commit") {
|
||||
reply = "Success!\n"
|
||||
if !success {
|
||||
reply = "synthetic failure contains PRIVATE response\n"
|
||||
}
|
||||
}
|
||||
io.WriteString(conn, reply)
|
||||
conn.Close()
|
||||
}
|
||||
done <- nil
|
||||
}()
|
||||
err = installHAProxyCertificate(socket, bundle)
|
||||
if (err == nil) != success {
|
||||
t.Fatal("native commit result was ignored")
|
||||
}
|
||||
if err != nil && strings.Contains(err.Error(), "PRIVATE") {
|
||||
t.Fatal("native response leaked")
|
||||
}
|
||||
if e := <-done; e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user