@@ -17,9 +17,14 @@ package pki
1717
1818import (
1919 "bytes"
20+ "os"
21+ "os/exec"
22+ "path/filepath"
2023 "testing"
2124
2225 "gotest.tools/v3/assert"
26+
27+ "github.com/crunchydata/postgres-operator/internal/testing/require"
2328)
2429
2530func TestCertificateTextMarshaling (t * testing.T ) {
@@ -75,6 +80,23 @@ func TestCertificateTextMarshaling(t *testing.T) {
7580 var sink Certificate
7681 assert .ErrorContains (t , sink .UnmarshalText (txt ), "malformed" )
7782 })
83+
84+ t .Run ("ReadByOpenSSL" , func (t * testing.T ) {
85+ openssl := require .OpenSSL (t )
86+ dir := t .TempDir ()
87+
88+ certFile := filepath .Join (dir , "cert.pem" )
89+ certBytes , err := cert .MarshalText ()
90+ assert .NilError (t , err )
91+ assert .NilError (t , os .WriteFile (certFile , certBytes , 0o600 ))
92+
93+ // The "openssl x509" command parses X.509 certificates.
94+ cmd := exec .Command (openssl , "x509" ,
95+ "-in" , certFile , "-inform" , "PEM" , "-noout" , "-text" )
96+
97+ output , err := cmd .CombinedOutput ()
98+ assert .NilError (t , err , "%q\n %s" , cmd .Args , output )
99+ })
78100}
79101
80102func TestPrivateKeyTextMarshaling (t * testing.T ) {
@@ -130,4 +152,29 @@ func TestPrivateKeyTextMarshaling(t *testing.T) {
130152 var sink PrivateKey
131153 assert .ErrorContains (t , sink .UnmarshalText (txt ), "asn1" )
132154 })
155+
156+ t .Run ("ReadByOpenSSL" , func (t * testing.T ) {
157+ openssl := require .OpenSSL (t )
158+ dir := t .TempDir ()
159+
160+ keyFile := filepath .Join (dir , "key.pem" )
161+ keyBytes , err := key .MarshalText ()
162+ assert .NilError (t , err )
163+ assert .NilError (t , os .WriteFile (keyFile , keyBytes , 0o600 ))
164+
165+ // The "openssl pkey" command processes public and private keys.
166+ cmd := exec .Command (openssl , "pkey" ,
167+ "-check" , "-in" , keyFile , "-inform" , "PEM" , "-noout" , "-text" )
168+
169+ output , err := cmd .CombinedOutput ()
170+ assert .NilError (t , err , "%q\n %s" , cmd .Args , output )
171+
172+ assert .Assert (t ,
173+ bytes .Contains (output , []byte ("is valid" )),
174+ "expected valid private key, got:\n %s" , output )
175+
176+ assert .Assert (t ,
177+ bytes .Contains (output , []byte ("\n Private-Key:" )),
178+ "expected valid private key, got:\n %s" , output )
179+ })
133180}
0 commit comments