@@ -6,6 +6,7 @@ package patroni
66
77import (
88 "context"
9+ "strings"
910 "testing"
1011
1112 "gotest.tools/v3/assert"
@@ -92,6 +93,44 @@ func TestReconcileInstanceCertificates(t *testing.T) {
9293 assert .DeepEqual (t , secret , before )
9394}
9495
96+ // TestReconcileInstanceCertificatesWithCABundle covers a custom root CA
97+ // secret whose certificate file is a bundle of an intermediate CA followed
98+ // by its root, as produced by cert-manager when leaf certificates are issued
99+ // through a sub-CA. Every certificate in the bundle must reach
100+ // patroni.ca-roots, or the leaf presented by peers (signed by the
101+ // intermediate) cannot be chained to a trusted root and TLS verification
102+ // fails at bootstrap.
103+ func TestReconcileInstanceCertificatesWithCABundle (t * testing.T ) {
104+ t .Parallel ()
105+
106+ root , err := pki .NewRootCertificateAuthority ()
107+ assert .NilError (t , err , "bug in test" )
108+ intermediate , err := pki .NewRootCertificateAuthority ()
109+ assert .NilError (t , err , "bug in test" )
110+
111+ rootText , err := root .Certificate .MarshalText ()
112+ assert .NilError (t , err , "bug in test" )
113+ intermediateText , err := intermediate .Certificate .MarshalText ()
114+ assert .NilError (t , err , "bug in test" )
115+
116+ bundle := append (append ([]byte {}, intermediateText ... ), rootText ... )
117+
118+ var bundledCA pki.Certificate
119+ assert .NilError (t , bundledCA .UnmarshalText (bundle ))
120+
121+ leaf , err := intermediate .GenerateLeafCertificate ("any" , nil )
122+ assert .NilError (t , err , "bug in test" )
123+
124+ ctx := context .Background ()
125+ secret := new (corev1.Secret )
126+ assert .NilError (t , InstanceCertificates (ctx ,
127+ bundledCA , leaf .Certificate , leaf .PrivateKey , secret ))
128+
129+ assert .DeepEqual (t , secret .Data ["patroni.ca-roots" ], bundle )
130+ assert .Equal (t ,
131+ strings .Count (string (secret .Data ["patroni.ca-roots" ]), "-----BEGIN CERTIFICATE-----" ), 2 )
132+ }
133+
95134func TestInstanceConfigMap (t * testing.T ) {
96135 t .Parallel ()
97136
0 commit comments