Skip to content

Commit 3ffbbaf

Browse files
committed
add more tests to certificate bundle test
1 parent e0220ae commit 3ffbbaf

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

internal/mode/static/state/graph/certificate_bundle.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ import (
1111
v1 "sigs.k8s.io/gateway-api/apis/v1"
1212
)
1313

14-
// CAKey is the key that is stored in a secret or configmap to grab its data from.
15-
// This follows the convention setup by kubernetes service account root ca
16-
// key for optional root certificate authority
14+
// key for optional root certificate authority.
1715
const CAKey = "ca.crt"
1816

19-
// CertificateBundle is used to submit certificate data to nginx that is kubernetes aware
17+
// CertificateBundle is used to submit certificate data to nginx that is kubernetes aware.
2018
type CertificateBundle struct {
2119
Cert *Certificate
2220

2321
Name types.NamespacedName
2422
Kind v1.Kind
2523
}
2624

27-
// Certificate houses the real certificate data that is sent to the configurator
25+
// Certificate houses the real certificate data that is sent to the configurator.
2826
type Certificate struct {
2927
// TLSCert is the SSL certificate used to send to CA
3028
TLSCert []byte
@@ -34,7 +32,7 @@ type Certificate struct {
3432
CACert []byte
3533
}
3634

37-
// NewCertificateBundle generates a kubernetes aware certificate that is used during the configurator for nginx
35+
// NewCertificateBundle generates a kubernetes aware certificate that is used during the configurator for nginx.
3836
func NewCertificateBundle(name types.NamespacedName, kind string, cert *Certificate) *CertificateBundle {
3937
return &CertificateBundle{
4038
Name: name,
@@ -43,11 +41,11 @@ func NewCertificateBundle(name types.NamespacedName, kind string, cert *Certific
4341
}
4442
}
4543

46-
// validateTLS checks to make sure a ssl certificate key pair is valid
44+
// validateTLS checks to make sure a ssl certificate key pair is valid.
4745
func validateTLS(tlsCert, tlsPrivateKey []byte) error {
4846
_, err := tls.X509KeyPair(tlsCert, tlsPrivateKey)
4947
if err != nil {
50-
return fmt.Errorf("TLS secret is invalid: %w", err)
48+
return fmt.Errorf("tls secret is invalid: %w", err)
5149
}
5250

5351
return nil

internal/mode/static/state/graph/certificate_bundle_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,55 @@ import (
77
. "github.com/onsi/gomega"
88
)
99

10+
func TestValidateTLS(t *testing.T) {
11+
t.Parallel()
12+
tests := []struct {
13+
expectedErr string
14+
name string
15+
tlsCert []byte
16+
tlsPrivateKey []byte
17+
}{
18+
{
19+
name: "valid tls key pair",
20+
tlsCert: cert,
21+
tlsPrivateKey: key,
22+
},
23+
{
24+
name: "invalid tls cert valid key",
25+
tlsCert: invalidCert,
26+
tlsPrivateKey: key,
27+
expectedErr: "tls secret is invalid: x509: malformed certificate",
28+
},
29+
{
30+
name: "invalid tls private key valid cert",
31+
tlsCert: cert,
32+
tlsPrivateKey: invalidKey,
33+
expectedErr: "tls secret is invalid: tls: failed to parse private key",
34+
},
35+
{
36+
name: "invalid tls cert key pair",
37+
tlsCert: invalidCert,
38+
tlsPrivateKey: invalidKey,
39+
expectedErr: "tls secret is invalid: x509: malformed certificate",
40+
},
41+
}
42+
43+
for _, test := range tests {
44+
t.Run(test.name, func(t *testing.T) {
45+
t.Parallel()
46+
47+
g := NewWithT(t)
48+
err := validateTLS(test.tlsCert, test.tlsPrivateKey)
49+
if test.expectedErr != "" {
50+
g.Expect(err).To(HaveOccurred())
51+
g.Expect(err).To(MatchError(test.expectedErr))
52+
} else {
53+
g.Expect(err).ToNot(HaveOccurred())
54+
}
55+
})
56+
}
57+
}
58+
1059
func TestValidateCA(t *testing.T) {
1160
t.Parallel()
1261
base64Data := make([]byte, base64.StdEncoding.EncodedLen(len(caBlock)))

internal/mode/static/state/graph/secret_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ func TestSecretResolver(t *testing.T) {
207207
{
208208
name: "invalid secret cert",
209209
nsname: client.ObjectKeyFromObject(invalidSecretCert),
210-
expectedErrMsg: "TLS secret is invalid: x509: malformed certificate",
210+
expectedErrMsg: "tls secret is invalid: x509: malformed certificate",
211211
},
212212
{
213213
name: "invalid secret key",
214214
nsname: client.ObjectKeyFromObject(invalidSecretKey),
215-
expectedErrMsg: "TLS secret is invalid: tls: failed to parse private key",
215+
expectedErrMsg: "tls secret is invalid: tls: failed to parse private key",
216216
},
217217
{
218218
name: "invalid secret ca cert",

0 commit comments

Comments
 (0)