Skip to content

Commit f133f5e

Browse files
authored
docs: per backend client cert (#7341)
* docs for per backend client cert Signed-off-by: Huabing Zhao <[email protected]>
1 parent cb8a7ca commit f133f5e

File tree

1 file changed

+169
-16
lines changed

1 file changed

+169
-16
lines changed

site/content/en/latest/tasks/security/backend-mtls.md

Lines changed: 169 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ Envoy Gateway supports the Gateway-API defined [BackendTLSPolicy][] to establish
1313

1414
## Installation
1515

16-
Follow the steps from the [Backend TLS][] to install Envoy Gateway and configure TLS to the backend server.
16+
Follow the steps from the [Backend TLS][] to install Envoy Gateway and configure TLS to the backend server.
1717

1818
## TLS Certificates
1919

20-
Generate the certificates and keys used by the Gateway for authentication against the backend.
20+
Generate the client certificate and key used by the Gateway for authentication against the backend.
2121

22-
Create a root certificate and private key to sign certificates:
22+
Create a root certificate and private key to sign the client certificate:
2323

2424
```shell
2525
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout clientca.key -out clientca.crt
2626
```
2727

28-
Create a certificate and a private key for `www.example.com`:
28+
Create the client certificate and a private key for `www.example.com`:
2929

3030
```shell
3131
openssl req -new -newkey rsa:2048 -nodes -keyout client.key -out client.csr -subj "/CN=example-client/O=example organization"
3232
openssl x509 -req -days 365 -CA clientca.crt -CAkey clientca.key -set_serial 0 -in client.csr -out client.crt
3333
```
3434

35-
Store the cert/key in a Secret:
35+
Store the the client cert/key in a Secret:
3636

3737
```shell
3838
kubectl -n envoy-gateway-system create secret tls example-client-cert --key=client.key --cert=client.crt
@@ -46,7 +46,7 @@ kubectl create configmap example-client-ca --from-file=clientca.crt
4646

4747
## Enforce Client Certificate Authentication on the backend
4848

49-
Patch the existing quickstart backend to enforce Client Certificate Authentication. The patch will mount the server certificate and key required for TLS, and the CA certificate into the backend as volumes.
49+
Patch the existing quickstart backend to enforce Client Certificate Authentication. The patch will mount the server certificate and key required for TLS, and the CA certificate into the backend as volumes.
5050

5151
```shell
5252
kubectl patch deployment backend --type=json --patch '
@@ -56,7 +56,7 @@ kubectl patch deployment backend --type=json --patch '
5656
- name: client-certs-volume
5757
mountPath: /etc/client-certs
5858
- name: secret-volume
59-
mountPath: /etc/secret-volume
59+
mountPath: /etc/secret-volume
6060
- op: add
6161
path: /spec/template/spec/volumes
6262
value:
@@ -73,7 +73,7 @@ kubectl patch deployment backend --type=json --patch '
7373
- key: tls.crt
7474
path: crt
7575
- key: tls.key
76-
path: key
76+
path: key
7777
- op: add
7878
path: /spec/template/spec/containers/0/env/-
7979
value:
@@ -82,12 +82,19 @@ kubectl patch deployment backend --type=json --patch '
8282
'
8383
```
8484

85-
## Configure Envoy Proxy to use a client certificate
85+
## Configure Envoy Proxy to use a client certificate
8686

87-
In addition to enablement of backend TLS with the Gateway-API BackendTLSPolicy, Envoy Gateway supports customizing TLS parameters such as TLS Client Certificate.
88-
To achieve this, the [EnvoyProxy][] resource can be used to specify a TLS Client Certificate.
87+
Envoy Gateway supports two ways to configure client certificates for backend mTLS.
88+
* Configure the [EnvoyProxy][] resource to specify a client certificate globally.
89+
* Configure a [Backend][] resource to specify a client certificate per backend.
8990

90-
First, you need to add ParametersRef in GatewayClass, and refer to EnvoyProxy Config:
91+
### Configure EnvoyProxy
92+
93+
The [EnvoyProxy][] resource can be used to specify a client certificate globally for a GatewayClass or Gateway.
94+
95+
The following example shows how to configure a GatewayClass to reference an EnvoyProxy resource with a client certificate.
96+
97+
First, add a parametersRef field in the GatewayClass to reference the EnvoyProxy configuration:
9198

9299
{{< tabpane text=true >}}
93100
{{% tab header="Apply from stdin" %}}
@@ -142,7 +149,7 @@ metadata:
142149
namespace: envoy-gateway-system
143150
spec:
144151
backendTLS:
145-
clientCertificateRef:
152+
clientCertificateRef:
146153
kind: Secret
147154
name: example-client-cert
148155
namespace: envoy-gateway-system
@@ -171,6 +178,151 @@ spec:
171178
{{% /tab %}}
172179
{{< /tabpane >}}
173180
181+
### Configure a Backend Resource
182+
183+
When multiple backends require distinct client certificates, configure each one using a dedicated [Backend][] resource that includes its own client certificate reference.
184+
TLS settings defined in the Backend resource take precedence over the defaults set in EnvoyProxy.backendTLS.
185+
186+
Before creating Backend resources, make sure the Backend API is enabled in the Envoy Gateway configuration (see [Backend Routing](../traffic/backend#enable-backend) for full details). If it is not already enabled, update the `envoy-gateway-config` ConfigMap as shown below:
187+
188+
{{< tabpane text=true >}}
189+
{{% tab header="Apply from stdin" %}}
190+
191+
```shell
192+
cat <<EOF | kubectl apply -f -
193+
apiVersion: v1
194+
kind: ConfigMap
195+
metadata:
196+
name: envoy-gateway-config
197+
namespace: envoy-gateway-system
198+
data:
199+
envoy-gateway.yaml: |
200+
apiVersion: gateway.envoyproxy.io/v1alpha1
201+
kind: EnvoyGateway
202+
provider:
203+
type: Kubernetes
204+
gateway:
205+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
206+
extensionApis:
207+
enableBackend: true
208+
EOF
209+
```
210+
211+
{{% /tab %}}
212+
{{% tab header="Apply from file" %}}
213+
Save and apply the following resource to your cluster:
214+
215+
```yaml
216+
---
217+
apiVersion: v1
218+
kind: ConfigMap
219+
metadata:
220+
name: envoy-gateway-config
221+
namespace: envoy-gateway-system
222+
data:
223+
envoy-gateway.yaml: |
224+
apiVersion: gateway.envoyproxy.io/v1alpha1
225+
kind: EnvoyGateway
226+
provider:
227+
type: Kubernetes
228+
gateway:
229+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
230+
extensionApis:
231+
enableBackend: true
232+
```
233+
234+
{{% /tab %}}
235+
{{< /tabpane >}}
236+
237+
{{< boilerplate rollout-envoy-gateway >}}
238+
239+
Roll out the updated Envoy Gateway deployment:
240+
241+
```shell
242+
kubectl -n envoy-gateway-system rollout restart deployment envoy-gateway
243+
```
244+
245+
Create the client certificate secret in the backend namespace (reusing the same certificate and key generated earlier):
246+
247+
```shell
248+
kubectl -n default create secret tls example-client-cert --key=client.key --cert=client.crt
249+
```
250+
251+
Define the backend and include both the trust anchor and the client credentials:
252+
253+
{{< tabpane text=true >}}
254+
{{% tab header="Apply from stdin" %}}
255+
256+
```shell
257+
cat <<EOF | kubectl apply -f -
258+
apiVersion: gateway.envoyproxy.io/v1alpha1
259+
kind: Backend
260+
metadata:
261+
name: tls-backend-client-cert
262+
namespace: default
263+
spec:
264+
endpoints:
265+
- fqdn:
266+
hostname: tls-backend.default.svc.cluster.local
267+
port: 443
268+
tls:
269+
clientCertificateRef:
270+
kind: Secret
271+
name: example-client-cert
272+
caCertificateRefs:
273+
- group: ""
274+
kind: ConfigMap
275+
name: example-ca
276+
EOF
277+
```
278+
279+
{{% /tab %}}
280+
{{% tab header="Apply from file" %}}
281+
Save and apply the following resource to your cluster:
282+
283+
```yaml
284+
---
285+
apiVersion: gateway.envoyproxy.io/v1alpha1
286+
kind: Backend
287+
metadata:
288+
name: tls-backend-client-cert
289+
namespace: default
290+
spec:
291+
endpoints:
292+
- fqdn:
293+
hostname: tls-backend.default.svc.cluster.local
294+
port: 443
295+
tls:
296+
clientCertificateRef:
297+
kind: Secret
298+
name: example-client-cert
299+
caCertificateRefs:
300+
- group: ""
301+
kind: ConfigMap
302+
name: example-ca
303+
```
304+
305+
{{% /tab %}}
306+
{{< /tabpane >}}
307+
308+
Update the HTTPRoute to reference the backend instead of the Kubernetes Service:
309+
310+
```shell
311+
kubectl patch HTTPRoute backend --type=json --patch '
312+
- op: replace
313+
path: /spec/rules/0/backendRefs/0/group
314+
value: gateway.envoyproxy.io
315+
- op: replace
316+
path: /spec/rules/0/backendRefs/0/kind
317+
value: Backend
318+
- op: replace
319+
path: /spec/rules/0/backendRefs/0/name
320+
value: tls-backend-client-cert
321+
- op: remove
322+
path: /spec/rules/0/backendRefs/0/port
323+
'
324+
```
325+
174326
## Testing mTLS
175327

176328
Query the TLS-enabled backend through Envoy proxy:
@@ -180,8 +332,8 @@ curl -v -HHost:www.example.com --resolve "www.example.com:80:127.0.0.1" \
180332
http://www.example.com:80/get
181333
```
182334

183-
Inspect the output and see that the response contains the details of the TLS handshake between Envoy and the backend.
184-
The response now contains a "peerCertificates" attribute that reflects the client certificate used by the Gateway to establish mTLS with the backend.
335+
Inspect the output and see that the response contains the details of the TLS handshake between Envoy and the backend.
336+
The response now contains a "peerCertificates" attribute that reflects the client certificate used by the Gateway to establish mTLS with the backend.
185337

186338
```shell
187339
< HTTP/1.1 200 OK
@@ -197,4 +349,5 @@ The response now contains a "peerCertificates" attribute that reflects the clien
197349

198350
[Backend TLS]: ./backend-tls
199351
[BackendTLSPolicy]: https://gateway-api.sigs.k8s.io/api-types/backendtlspolicy/
200-
[EnvoyProxy]: ../../api/extension_types#envoyproxy
352+
[EnvoyProxy]: ../../api/extension_types#envoyproxy
353+
[Backend]: ../../api/extension_types#backend

0 commit comments

Comments
 (0)