Skip to content

Commit 040bdea

Browse files
committed
Fix broken tigron testca
Signed-off-by: apostasie <[email protected]>
1 parent 62477fb commit 040bdea

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

mod/tigron/test/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (tp *temp) SaveToWriter(writer func(file io.Writer) error, key ...string) s
126126
silentT := assertive.WithSilentSuccess(tp.t)
127127

128128
//nolint:gosec // it is fine
129-
file, err := os.OpenFile(pth, os.O_CREATE, FilePermissionsDefault)
129+
file, err := os.OpenFile(pth, os.O_CREATE|os.O_WRONLY, FilePermissionsDefault)
130130
assertive.ErrorIsNil(
131131
silentT,
132132
err,

mod/tigron/utils/testca/ca.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ func (ca *Cert) GenerateCustomX509(
107107
template *x509.Certificate,
108108
) *Cert {
109109
silentT := assertive.WithSilentSuccess(helpers.T())
110-
key, certPath, keyPath := createCert(silentT, data, underDirectory, template, ca.cert, ca.key)
110+
111+
var (
112+
cert *x509.Certificate
113+
key *rsa.PrivateKey
114+
)
115+
116+
if ca != nil {
117+
cert = ca.cert
118+
key = ca.key
119+
}
120+
121+
key, certPath, keyPath := createCert(silentT, data, underDirectory, template, cert, key)
111122

112123
return &Cert{
113124
CertPath: certPath,
@@ -124,16 +135,16 @@ func createCert(
124135
template, caCert *x509.Certificate,
125136
caKey *rsa.PrivateKey,
126137
) (key *rsa.PrivateKey, certPath, keyPath string) {
127-
if caCert == nil {
128-
caCert = template
129-
}
138+
key, err := rsa.GenerateKey(rand.Reader, keyLength)
139+
assertive.ErrorIsNil(testing, err, "key generation should succeed")
130140

131141
if caKey == nil {
132142
caKey = key
133143
}
134144

135-
key, err := rsa.GenerateKey(rand.Reader, keyLength)
136-
assertive.ErrorIsNil(testing, err, "key generation should succeed")
145+
if caCert == nil {
146+
caCert = template
147+
}
137148

138149
signedCert, err := x509.CreateCertificate(rand.Reader, template, caCert, &key.PublicKey, caKey)
139150
assertive.ErrorIsNil(testing, err, "certificate creation should succeed")
@@ -144,16 +155,17 @@ func createCert(
144155
}
145156

146157
data.Temp().Dir(dir)
147-
certPath = data.Temp().Path(dir, serial.String()+".cert")
148-
keyPath = data.Temp().Path(dir, serial.String()+".key")
149158

150159
data.Temp().SaveToWriter(func(writer io.Writer) error {
151160
return pem.Encode(writer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
152-
}, keyPath)
161+
}, dir, serial.String()+".key")
153162

154163
data.Temp().SaveToWriter(func(writer io.Writer) error {
155164
return pem.Encode(writer, &pem.Block{Type: "CERTIFICATE", Bytes: signedCert})
156-
}, keyPath)
165+
}, dir, serial.String()+".cert")
166+
167+
certPath = data.Temp().Path(dir, serial.String()+".cert")
168+
keyPath = data.Temp().Path(dir, serial.String()+".key")
157169

158170
return key, certPath, keyPath
159171
}

0 commit comments

Comments
 (0)