Skip to content

Commit a2ae465

Browse files
committed
Review feedback 1
1 parent 955feeb commit a2ae465

File tree

22 files changed

+86
-129
lines changed

22 files changed

+86
-129
lines changed

cmd/gateway/commands.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ func createStaticModeCommand() *cobra.Command {
172172
LockName: leaderElectionLockName.String(),
173173
Identity: podName,
174174
},
175-
Plus: plus,
176-
TelemetryReportPeriod: period,
177-
Version: version,
178-
EnableExperimentalFeatures: enableExperimental,
175+
Plus: plus,
176+
TelemetryReportPeriod: period,
177+
Version: version,
178+
ExperimentalFeatures: enableExperimental,
179179
}
180180

181181
if err := static.StartManager(conf); err != nil {

examples/backend-tls/README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Backend TLS Policy Example
22

3-
In this example, we will create a Backend TLS Policy, attach it to our
3+
In this example, we will create a Backend TLS Policy, attach it to our application service, and then configure routing
4+
rules. The Backend TLS Policy will be picked up by NGF and the connection between NGF and the upstream server will use
5+
HTTPS.
46

57
## Running the Example
68

@@ -56,18 +58,9 @@ In this example, we will create a Backend TLS Policy, attach it to our
5658
kubectl apply -f policy.yaml
5759
```
5860

59-
## 3. Configure HTTPS Termination and Routing
61+
## 3. Configure HTTP Termination and Routing
6062

61-
1. Create the Secret with a TLS certificate and key:
62-
63-
```shell
64-
kubectl apply -f app-secret.yaml
65-
```
66-
67-
The TLS certificate and key in this Secret are used to terminate the TLS connections for the secure-app application.
68-
> **Important**: This certificate and key are for demo purposes only.
69-
70-
2. Create the Gateway resource:
63+
1. Create the Gateway resource:
7164

7265
```shell
7366
kubectl apply -f gateway.yaml
@@ -77,19 +70,18 @@ In this example, we will create a Backend TLS Policy, attach it to our
7770
- `http` listener for HTTP traffic
7871
- `https` listener for HTTPS traffic. It terminates TLS connections using the `app-secret` we created in step 1.
7972

80-
3. Create the HTTPRoute resources:
73+
2. Create the HTTPRoute resources:
8174

8275
```shell
8376
kubectl apply -f secure-app-routes.yaml
8477
```
8578

8679
## 4. Test the Application
8780

88-
To access the application, we will use `curl` to send requests to the `secure-app` Service over HTTPS. Since our
89-
certificate is self-signed, we will use curl's `--cacert` option to supply the certificate for verification.
81+
To access the application, we will use `curl` to send requests to the `secure-app` Service over HTTP.
9082

9183
```shell
92-
curl --resolve secure-app.example.com:$GW_HTTPS_PORT:$GW_IP https://secure-app.example.com:$GW_HTTPS_PORT/ --cacert ca.cert
84+
curl --resolve secure-app.example.com:$GW_PORT:$GW_IP http://secure-app.example.com:$GW_PORT/
9385
```
9486

9587
```text

examples/backend-tls/app-secret.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/backend-tls/backend-certs-configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
kind: ConfigMap
22
apiVersion: v1
33
metadata:
4-
name: backend-certs
4+
name: backend-cert
55
data:
66
ca.crt: |
77
-----BEGIN CERTIFICATE-----

examples/backend-tls/ca.cert

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/backend-tls/gateway.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,3 @@ spec:
88
- name: http
99
port: 80
1010
protocol: HTTP
11-
- name: https
12-
port: 443
13-
protocol: HTTPS
14-
tls:
15-
mode: Terminate
16-
certificateRefs:
17-
- kind: Secret
18-
name: app-secret

examples/backend-tls/policy.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ apiVersion: gateway.networking.k8s.io/v1alpha2
22
kind: BackendTLSPolicy
33
metadata:
44
name: backend-tls
5-
namespace: default
65
spec:
76
targetRef:
87
group: ''
@@ -11,7 +10,7 @@ spec:
1110
namespace: default
1211
tls:
1312
caCertRefs:
14-
- name: backend-certs
13+
- name: backend-cert
1514
group: ''
1615
kind: ConfigMap
1716
hostname: secure-app.example.com

examples/backend-tls/secure-app-routes.yaml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
apiVersion: gateway.networking.k8s.io/v1
22
kind: HTTPRoute
3-
metadata:
4-
name: secure-app-tls-redirect
5-
spec:
6-
parentRefs:
7-
- name: gateway
8-
sectionName: http
9-
hostnames:
10-
- "secure-app.example.com"
11-
rules:
12-
- filters:
13-
- type: RequestRedirect
14-
requestRedirect:
15-
scheme: https
16-
port: 443
17-
---
18-
apiVersion: gateway.networking.k8s.io/v1
19-
kind: HTTPRoute
203
metadata:
214
name: secure-app
225
spec:
236
parentRefs:
247
- name: gateway
25-
sectionName: https
8+
sectionName: http
269
hostnames:
2710
- "secure-app.example.com"
2811
rules:

examples/backend-tls/secure-app.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ spec:
1414
spec:
1515
containers:
1616
- name: secure-app
17-
image: nginxdemos/nginx-hello:plain-text
17+
image: nginxdemos/nginx-unprivileged:plain-text
1818
ports:
1919
- containerPort: 8443
2020
volumeMounts:
@@ -59,8 +59,6 @@ data:
5959
ssl_certificate /etc/nginx/ssl/tls.crt;
6060
ssl_certificate_key /etc/nginx/ssl/tls.key;
6161
62-
proxy_ssl_server_name on;
63-
6462
default_type text/plain;
6563
6664
location / {

internal/mode/static/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type Config struct {
3838
UpdateGatewayClassStatus bool
3939
// Plus indicates whether NGINX Plus is being used.
4040
Plus bool
41-
// EnableExperimentalFeatures enables experimental features.
42-
EnableExperimentalFeatures bool
41+
// ExperimentalFeatures indicates if experimental features are enabled.
42+
ExperimentalFeatures bool
4343
}
4444

4545
// GatewayPodConfig contains information about this Pod.

internal/mode/static/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func StartManager(cfg config.Config) error {
203203
objects, objectLists := prepareFirstEventBatchPreparerArgs(
204204
cfg.GatewayClassName,
205205
cfg.GatewayNsName,
206-
cfg.EnableExperimentalFeatures,
206+
cfg.ExperimentalFeatures,
207207
)
208208
firstBatchPreparer := events.NewFirstEventBatchPreparerImpl(mgr.GetCache(), objects, objectLists)
209209
eventLoop := events.NewEventLoop(
@@ -384,13 +384,13 @@ func registerControllers(
384384
},
385385
}
386386

387-
if cfg.EnableExperimentalFeatures {
387+
if cfg.ExperimentalFeatures {
388388
backendTLSObjs := []ctlrCfg{
389389
{
390390
objectType: &gatewayv1alpha2.BackendTLSPolicy{},
391391
},
392392
{
393-
objectType: &apiv1.ConfigMap{},
393+
objectType: &apiv1.ConfigMap{}, // TODO(ciarams87): Use only metadata predicate
394394
},
395395
}
396396
controllerRegCfgs = append(controllerRegCfgs, backendTLSObjs...)

internal/mode/static/nginx/config/generator_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ func TestGenerate(t *testing.T) {
9494
g.Expect(configVersion).To(ContainSubstring(fmt.Sprintf("return 200 %d", conf.Version)))
9595

9696
g.Expect(files[3].Path).To(Equal("/etc/nginx/secrets/test-certbundle.crt"))
97+
certBundle := string(files[3].Content)
98+
g.Expect(certBundle).To(Equal("test-cert"))
9799
}

internal/mode/static/nginx/config/http/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ type MapParameter struct {
9090

9191
// ProxySSLVerify holds the proxied HTTPS server verification configuration.
9292
type ProxySSLVerify struct {
93-
CertPath string
94-
Hostname string
95-
VerifyOn bool
93+
TrustedCertificate string
94+
Name string
9695
}

internal/mode/static/nginx/config/servers.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67
"strings"
78
gotemplate "text/template"
89

@@ -254,7 +255,7 @@ func updateLocationsForFilters(
254255
}
255256
}
256257
buildLocations[i].ProxySetHeaders = proxySetHeaders
257-
buildLocations[i].ProxySSLVerify = convertProxyTLSFromBackends(matchRule.BackendGroup.Backends)
258+
buildLocations[i].ProxySSLVerify = createProxyTLSFromBackends(matchRule.BackendGroup.Backends)
258259
proxyPass := createProxyPass(
259260
matchRule.BackendGroup,
260261
matchRule.Filters.RequestURLRewrite,
@@ -266,30 +267,54 @@ func updateLocationsForFilters(
266267
return buildLocations
267268
}
268269

269-
func convertProxyTLSFromBackends(backends []dataplane.Backend) *http.ProxySSLVerify {
270+
func createProxyTLSFromBackends(backends []dataplane.Backend) *http.ProxySSLVerify {
270271
if len(backends) == 0 {
271272
return nil
272273
}
273274
for _, b := range backends {
274-
proxyVerify := convertBackendTLS(b.VerifyTLS)
275+
proxyVerify := createProxySSLVerify(b.VerifyTLS)
275276
if proxyVerify != nil {
276277
// If any backend has a backend TLS policy defined, then we use that for the proxy SSL verification.
277278
// If multiple backends in the group have a backend TLS policy defined, then we use the first one we find.
279+
// TODO(ciarams87): Fix this
278280
return proxyVerify
279281
}
280282
}
281283
return nil
282284
}
283285

284-
func convertBackendTLS(v *dataplane.VerifyTLS) *http.ProxySSLVerify {
286+
func createProxySSLVerify(v *dataplane.VerifyTLS) *http.ProxySSLVerify {
285287
if v == nil || v.Hostname == "" {
286288
return nil
287289
}
290+
var trustedCert string
291+
if v.CertBundleID != "" {
292+
trustedCert = generateCertBundleFileName(v.CertBundleID)
293+
} else {
294+
trustedCert = getRootCAPath()
295+
}
288296
return &http.ProxySSLVerify{
289-
CertPath: generateCertBundleFileName(v.CertBundleID),
290-
Hostname: v.Hostname,
291-
VerifyOn: v.CertBundleID != "",
297+
TrustedCertificate: trustedCert,
298+
Name: v.Hostname,
299+
}
300+
}
301+
302+
// TODO(ciarams87): Move this logic earlier
303+
func getRootCAPath() string {
304+
certFiles := []string{
305+
"/etc/ssl/cert.pem", // Alpine Linux
306+
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
307+
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6
308+
"/etc/ssl/ca-bundle.pem", // OpenSUSE
309+
"/etc/pki/tls/cacert.pem", // OpenELEC
310+
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
311+
}
312+
for _, certFile := range certFiles {
313+
if _, err := os.Stat(certFile); err == nil {
314+
return certFile
315+
}
292316
}
317+
return ""
293318
}
294319

295320
func createReturnValForRedirectFilter(filter *dataplane.HTTPRequestRedirectFilter, listenerPort int32) *http.Return {

internal/mode/static/nginx/config/servers_template.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,11 @@ server {
5353
proxy_http_version 1.1;
5454
proxy_pass {{ $l.ProxyPass }};
5555
{{- if $l.ProxySSLVerify }}
56-
proxy_ssl_name {{ $l.ProxySSLVerify.Hostname }};
57-
{{- if $l.ProxySSLVerify.VerifyOn }}
5856
proxy_ssl_verify on;
59-
proxy_ssl_trusted_certificate {{ $l.ProxySSLVerify.CertPath }};
60-
{{- end }}
57+
proxy_ssl_name {{ $l.ProxySSLVerify.Hostname }};
58+
proxy_ssl_trusted_certificate {{ $l.ProxySSLVerify.TrustedCertificate }};
6159
{{- end }}
6260
{{- end }}
63-
6461
}
6562
{{ end }}
6663
}

internal/mode/static/nginx/config/servers_test.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -621,19 +621,17 @@ func TestCreateServers(t *testing.T) {
621621
ProxyPass: "https://test_btp_80$request_uri",
622622
ProxySetHeaders: baseHeaders,
623623
ProxySSLVerify: &http.ProxySSLVerify{
624-
Hostname: "test-btp.example.com",
625-
CertPath: "/etc/nginx/secrets/test-btp.crt",
626-
VerifyOn: true,
624+
Name: "test-btp.example.com",
625+
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
627626
},
628627
},
629628
{
630629
Path: "= /backend-tls-policy",
631630
ProxyPass: "https://test_btp_80$request_uri",
632631
ProxySetHeaders: baseHeaders,
633632
ProxySSLVerify: &http.ProxySSLVerify{
634-
Hostname: "test-btp.example.com",
635-
CertPath: "/etc/nginx/secrets/test-btp.crt",
636-
VerifyOn: true,
633+
Name: "test-btp.example.com",
634+
TrustedCertificate: "/etc/nginx/secrets/test-btp.crt",
637635
},
638636
},
639637
{
@@ -1860,9 +1858,8 @@ func TestConvertBackendTLSFromGroup(t *testing.T) {
18601858
},
18611859
},
18621860
expected: &http.ProxySSLVerify{
1863-
CertPath: "/etc/nginx/secrets/default-my-cert.crt",
1864-
Hostname: "my-hostname",
1865-
VerifyOn: true,
1861+
TrustedCertificate: "/etc/nginx/secrets/default-my-cert.crt",
1862+
Name: "my-hostname",
18661863
},
18671864
},
18681865
{
@@ -1899,16 +1896,15 @@ func TestConvertBackendTLSFromGroup(t *testing.T) {
18991896
},
19001897
},
19011898
expected: &http.ProxySSLVerify{
1902-
CertPath: "/etc/nginx/secrets/default-my-cert.crt",
1903-
Hostname: "my-hostname",
1904-
VerifyOn: true,
1899+
TrustedCertificate: "/etc/nginx/secrets/default-my-cert.crt",
1900+
Name: "my-hostname",
19051901
},
19061902
},
19071903
}
19081904

19091905
for _, tc := range tests {
19101906
t.Run(tc.msg, func(t *testing.T) {
1911-
result := convertProxyTLSFromBackends(tc.grp)
1907+
result := createProxyTLSFromBackends(tc.grp)
19121908
g.Expect(result).To(Equal(tc.expected))
19131909
})
19141910
}

0 commit comments

Comments
 (0)