@@ -171,7 +171,7 @@ func (t *TLS) generateCert(secret *v1.Secret, cn ...string) (*v1.Secret, bool, e
171171 return nil , false , err
172172 }
173173
174- certBytes , keyBytes , err := Marshal ( newCert , privateKey )
174+ keyBytes , certBytes , err := MarshalChain ( privateKey , newCert , t . CACert )
175175 if err != nil {
176176 return nil , false , err
177177 }
@@ -250,14 +250,33 @@ func getPrivateKey(secret *v1.Secret) (crypto.Signer, error) {
250250 return NewPrivateKey ()
251251}
252252
253+ // MarshalChain returns given key and certificates as byte slices.
254+ func MarshalChain (privateKey crypto.Signer , certs ... * x509.Certificate ) (keyBytes , certChainBytes []byte , err error ) {
255+ keyBytes , err = cert .MarshalPrivateKeyToPEM (privateKey )
256+ if err != nil {
257+ return nil , nil , err
258+ }
259+
260+ for _ , cert := range certs {
261+ if cert != nil {
262+ certBlock := pem.Block {
263+ Type : CertificateBlockType ,
264+ Bytes : cert .Raw ,
265+ }
266+ certChainBytes = append (certChainBytes , pem .EncodeToMemory (& certBlock )... )
267+ }
268+ }
269+ return keyBytes , certChainBytes , nil
270+ }
271+
253272// Marshal returns the given cert and key as byte slices.
254- func Marshal (x509Cert * x509.Certificate , privateKey crypto.Signer ) ([] byte , []byte , error ) {
273+ func Marshal (x509Cert * x509.Certificate , privateKey crypto.Signer ) (certBytes , keyBytes []byte , err error ) {
255274 certBlock := pem.Block {
256275 Type : CertificateBlockType ,
257276 Bytes : x509Cert .Raw ,
258277 }
259278
260- keyBytes , err : = cert .MarshalPrivateKeyToPEM (privateKey )
279+ keyBytes , err = cert .MarshalPrivateKeyToPEM (privateKey )
261280 if err != nil {
262281 return nil , nil , err
263282 }
0 commit comments