Skip to content

Commit 716b6da

Browse files
committed
Fix yaml for httpRoute and backendTLSPolicy. Fix CA generation.
1 parent cade4a1 commit 716b6da

File tree

5 files changed

+117
-99
lines changed

5 files changed

+117
-99
lines changed

conformance/base/manifests.yaml

-63
Original file line numberDiff line numberDiff line change
@@ -730,66 +730,3 @@ data:
730730
foo.bar.com:53 {
731731
whoami
732732
}
733-
---
734-
apiVersion: v1
735-
kind: Service
736-
metadata:
737-
name: backendtlspolicy-test
738-
namespace: gateway-conformance-infra
739-
spec:
740-
selector:
741-
app: backendtlspolicy-test
742-
ports:
743-
- protocol: TCP
744-
port: 443
745-
targetPort: 8443
746-
---
747-
apiVersion: apps/v1
748-
kind: Deployment
749-
metadata:
750-
name: backendtlspolicy-test
751-
namespace: gateway-conformance-infra
752-
labels:
753-
app: backendtlspolicy-test
754-
spec:
755-
replicas: 1
756-
selector:
757-
matchLabels:
758-
app: backendtlspolicy-test
759-
template:
760-
metadata:
761-
labels:
762-
app: backendtlspolicy-test
763-
spec:
764-
containers:
765-
- name: backendtlspolicy-test
766-
image: gcr.io/k8s-staging-gateway-api/echo-basic:v20241007-v1.2.0-6-g9f820af9
767-
volumeMounts:
768-
- name: secret-volume
769-
mountPath: /etc/secret-volume
770-
env:
771-
- name: POD_NAME
772-
valueFrom:
773-
fieldRef:
774-
fieldPath: metadata.name
775-
- name: NAMESPACE
776-
valueFrom:
777-
fieldRef:
778-
fieldPath: metadata.namespace
779-
- name: CA_CERT
780-
value: /etc/secret-volume/crt
781-
- name: CA_CERT_KEY
782-
value: /etc/secret-volume/key
783-
resources:
784-
requests:
785-
cpu: 10m
786-
volumes:
787-
- name: secret-volume
788-
secret:
789-
# This secret is generated dynamically by the test suite.
790-
secretName: backend-tls-checks-certificate
791-
items:
792-
- key: tls.crt
793-
path: crt
794-
- key: tls.key
795-
path: key

conformance/tests/backendtlspolicy.yaml

+82-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ spec:
3535
validation:
3636
caCertificateRefs:
3737
- group: ""
38-
kind: Secret
38+
kind: ConfigMap
3939
# This secret is generated dynamically by the test suite.
4040
name: "backend-tls-checks-certificate"
4141
hostname: "abc.example.com"
@@ -52,7 +52,87 @@ spec:
5252
hostnames:
5353
- abc.example.com
5454
rules:
55-
- matches:
55+
- backendRefs:
56+
- group: ""
57+
kind: Service
58+
name: backendtlspolicy-test
59+
port: 443
60+
matches:
5661
- path:
5762
type: Exact
5863
value: /backendTLS
64+
---
65+
apiVersion: v1
66+
kind: Service
67+
metadata:
68+
name: backendtlspolicy-test
69+
namespace: gateway-conformance-infra
70+
spec:
71+
selector:
72+
app: backendtlspolicy-test
73+
ports:
74+
- protocol: TCP
75+
port: 443
76+
targetPort: 8443
77+
---
78+
# Deployment must not be applied until after the secret is generated.
79+
apiVersion: apps/v1
80+
kind: Deployment
81+
metadata:
82+
name: backendtlspolicy-test
83+
namespace: gateway-conformance-infra
84+
labels:
85+
app: backendtlspolicy-test
86+
spec:
87+
replicas: 1
88+
selector:
89+
matchLabels:
90+
app: backendtlspolicy-test
91+
template:
92+
metadata:
93+
labels:
94+
app: backendtlspolicy-test
95+
spec:
96+
containers:
97+
- name: backendtlspolicy-test
98+
image: echo-basic:2.2
99+
volumeMounts:
100+
- name: ca-volume
101+
mountPath: /etc/ca-volume
102+
- name: secret-volume
103+
mountPath: /etc/secret-volume
104+
env:
105+
- name: POD_NAME
106+
valueFrom:
107+
fieldRef:
108+
fieldPath: metadata.name
109+
- name: NAMESPACE
110+
valueFrom:
111+
fieldRef:
112+
fieldPath: metadata.namespace
113+
- name: CA_CERT
114+
value: /etc/ca-volume/crt
115+
- name: TLS_SERVER_CERT
116+
value: /etc/secret-volume/crt
117+
- name: TLS_SERVER_PRIVKEY
118+
value: /etc/secret-volume/key
119+
resources:
120+
requests:
121+
cpu: 10m
122+
volumes:
123+
- name: ca-volume
124+
configMap:
125+
# This configMap is generated dynamically by the test suite.
126+
name: backend-tls-checks-certificate
127+
items:
128+
- key: ca.crt
129+
path: crt
130+
- name: secret-volume
131+
secret:
132+
# This secret is generated dynamically by the test suite.
133+
secretName: tls-checks-certificate
134+
items:
135+
- key: tls.crt
136+
path: crt
137+
- key: tls.key
138+
path: key

conformance/utils/kubernetes/certificate.go

+31-31
Original file line numberDiff line numberDiff line change
@@ -120,34 +120,41 @@ func generateRSACert(hosts []string, keyOut, certOut io.Writer) error {
120120
return nil
121121
}
122122

123-
// MustCreateCASignedCertSecret will create a secret using a CA Certificate, and public and private key for that certificate.
124-
func MustCreateCASignedCertSecret(t *testing.T, namespace, secretName string, hosts []string) *corev1.Secret {
123+
// MustCreateCASignedCertConfigMap will create a ConfigMap containing a CA Certificate, given a TLS Secret
124+
// for that CA certificate.
125+
func MustCreateCASignedCertConfigMap(t *testing.T, namespace, configMapName string, hosts []string) *corev1.ConfigMap {
125126
require.NotEmpty(t, hosts, "require a non-empty hosts for Subject Alternate Name values")
126127

127-
var serverKey, serverCert bytes.Buffer
128-
129-
require.NoError(t, generateCACert(hosts, &serverKey, &serverCert), "failed to generate CA certificate")
128+
caBytes, err := generateCACert(hosts)
129+
if err != nil {
130+
t.Errorf("failed to generate CA certificate: %v", err)
131+
return nil
132+
}
130133

131-
data := map[string][]byte{
132-
corev1.TLSCertKey: serverCert.Bytes(),
133-
corev1.TLSPrivateKeyKey: serverKey.Bytes(),
134+
var certData bytes.Buffer
135+
if err := pem.Encode(&certData, &pem.Block{Type: "CERTIFICATE", Bytes: caBytes}); err != nil {
136+
t.Errorf("failed creating cert: %w", err)
137+
return nil
134138
}
135139

136-
newSecret := &corev1.Secret{
140+
// Store the certificate in a ConfigMap.
141+
caConfigMap := &corev1.ConfigMap{
137142
ObjectMeta: metav1.ObjectMeta{
138143
Namespace: namespace,
139-
Name: secretName,
144+
Name: configMapName,
145+
},
146+
Data: map[string]string{
147+
"ca.crt": certData.String(),
140148
},
141-
Type: corev1.SecretTypeTLS,
142-
Data: data,
143149
}
144-
145-
return newSecret
150+
return caConfigMap
146151
}
147152

148-
// generateCACert generates a CA Certificate signed certificate valid for a year.
149-
func generateCACert(hosts []string, keyOut, certOut io.Writer) error {
150-
// Create the CA certificate.
153+
// generateCACert generates a ConfigMap containing a CA Certificate signed certificate valid for a year.
154+
func generateCACert(hosts []string) ([]byte, error) {
155+
var caBytes []byte
156+
157+
// Create the CA certificate template.
151158
ca := &x509.Certificate{
152159
SerialNumber: big.NewInt(2024),
153160
Subject: pkix.Name{
@@ -162,7 +169,7 @@ func generateCACert(hosts []string, keyOut, certOut io.Writer) error {
162169
NotAfter: time.Now().AddDate(1, 0, 0),
163170
IsCA: true, // Indicates this is a CA Certificate.
164171
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
165-
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
172+
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign | x509.KeyUsageKeyEncipherment,
166173
BasicConstraintsValid: true,
167174
}
168175

@@ -175,26 +182,19 @@ func generateCACert(hosts []string, keyOut, certOut io.Writer) error {
175182
}
176183
}
177184

178-
// Generate the private key.
185+
// Generate the private key to sign certificates.
179186
caPrivKey, err := rsa.GenerateKey(rand.Reader, rsaBits)
180187
if err != nil {
181-
return err
188+
return caBytes, fmt.Errorf("error generating key for CA: %v", err)
182189
}
183190

184-
// Generate the certificate using the CA certificate.
185-
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
191+
// Create the self-signed certificate using the CA certificate.
192+
caBytes, err = x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
186193
if err != nil {
187-
return err
188-
}
189-
190-
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: caBytes}); err != nil {
191-
return fmt.Errorf("failed creating cert: %w", err)
194+
return caBytes, fmt.Errorf("error creating CA: %v", err)
192195
}
193196

194-
if err := pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey)}); err != nil {
195-
return fmt.Errorf("failed creating key: %w", err)
196-
}
197-
return nil
197+
return caBytes, nil
198198
}
199199

200200
// validateHost ensures that the host name length is no more than 253 characters.

conformance/utils/roundtripper/roundtripper.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ func (d *DefaultRoundTripper) defaultRoundTrip(request Request, transport http.R
229229
return nil, nil, err
230230
}
231231
tlog.Logf(request.T, "Error sending request:\n%s\n\n", formatDump(dump, "< "))
232+
} else {
233+
tlog.Logf(request.T, "Error sending request: %v (no response)\n", err)
232234
}
233-
tlog.Log(request.T, "Error sending request: no response\n")
234235
}
235236
return nil, nil, err
236237
}

conformance/utils/suite/suite.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ func (suite *ConformanceTestSuite) Setup(t *testing.T, tests []ConformanceTest)
361361
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{secret}, suite.Cleanup)
362362
secret = kubernetes.MustCreateSelfSignedCertSecret(t, "gateway-conformance-app-backend", "tls-passthrough-checks-certificate", []string{"abc.example.com"})
363363
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{secret}, suite.Cleanup)
364-
caSecret := kubernetes.MustCreateCASignedCertSecret(t, "gateway-conformance-infra", "backend-tls-checks-certificate", []string{"abc.example.com"})
365-
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{caSecret}, suite.Cleanup)
364+
caConfigMap := kubernetes.MustCreateCASignedCertConfigMap(t, "gateway-conformance-infra", "backend-tls-checks-certificate", []string{"abc.example.com"})
365+
suite.Applier.MustApplyObjectsWithCleanup(t, suite.Client, suite.TimeoutConfig, []client.Object{caConfigMap}, suite.Cleanup)
366366

367367
tlog.Logf(t, "Test Setup: Ensuring Gateways and Pods from base manifests are ready")
368368
namespaces := []string{

0 commit comments

Comments
 (0)