# Pre-defined client certificate to backend request

**URL:** <https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772>\
**Category:** Traefik v2\
**Tags:** rest-api, cli, middleware\
**Created:** [December 14, 2023, 2:01pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772 "2023-12-14T14:01:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 14, 2023, 2:01pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/1 "2023-12-14T14:01:20Z")

</div>

Hello, i have one client that i need to auth the connection with already generated client certificate.

How i can insert while i make requests for their backend?

The certificate is signed by his CA on the backend (their side)

But i was wondering how i can insert the .pfx file and use it to auth that connection to the client backend?

Regards,

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [December 14, 2023, 2:17pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/2 "2023-12-14T14:17:39Z")

</div>

You can load mTLS and TLS certs in a dynamic config file ([doc](https://doc.traefik.io/traefik/https/tls/#user-defined)), which you load via `providers.file` in static config.

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 14, 2023, 2:48pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/3 "2023-12-14T14:48:46Z")

</div>

If i import from tls.certificates part, they will be forwarded to the load balancer backend? And acting like client certificate ?

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [December 14, 2023, 2:55pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/4 "2023-12-14T14:55:30Z")

</div>

If you need to use custom TLS between Traefik and your target service, check this [doc](https://doc.traefik.io/traefik/routing/services/#serverstransport_1).

When using a TCP router, you can tell Traefik to forward the TLS-encrypted request with `tls.passthrough`. But then Traefik and the target service both need the certs to decrypt. If Traefik doesn’t have the cert, you can only use `rule=HostSNI(`*`)`.

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 14, 2023, 3:29pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/5 "2023-12-14T15:29:31Z")

</div>

Thanks, here is what i'm trying but i'm not getting any responses, getting 421 error.

```auto
[http.routers.test-1]
rule = "Host(`domain.com`)"
entryPoints = ["web2"]
service = "test-1"

[http.services]
[http.services.test-1]
    [http.services.test-1.loadBalancer]
    serversTransport = "test-1"
    [[http.services.test-1.loadBalancer.servers]]
    url = "https://thehostwhoisrequiringclientcertificate"

[http.serversTransports.test-1]
insecureSkipVerify = true
[[http.serversTransports.test-1.certificates]]
	certFile = "/etc/nginx/ssl/client-cert/domain.crt"
	keyFile = "/etc/nginx/ssl/client-cert/domain.rsa"
[http.serversTransports.test-1.forwardingTimeouts]
  responseHeaderTimeout = "40s"
  dialTimeout = "40s"

[[tls.certificates]]
  certFile = "/opt/zero-downtime/config/ssls/cert.pem"
  keyFile = "/opt/zero-downtime/config/ssls/certkey.pem"

```

Here is the error that i'm gettting

 ![image](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/1/1e9822e2ceaad2e5ffb92f9dcfbeb0d04f1596da.png)

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [December 14, 2023, 5:13pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/6 "2023-12-14T17:13:19Z")

</div>

Enable access log in JSON format to see where the error is coming from. `OriginStatus` is the code coming from the target service.

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 14, 2023, 5:27pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/7 "2023-12-14T17:27:20Z")

</div>

I have been adding this :

```auto

[tls.options]
  [tls.options.test-1]
    sniStrict = false

```

And

```auto
[http.services.test-1]
    [http.services.test-1.loadBalancer]
    passHostHeader = false

```

then i was adding the :

```auto
[http.middlewares.test-1.headers.customrequestheaders]
    Host = "remotedomain that i'm connecting (client backend)"

```

But now receiving new error :

```auto
level=debug msg="'502 Bad Gateway' caused by: local error: tls: no renegotiation"

```

And after i disable the `insecureSkipVerify = true`

i'm getting :

```auto
500 Internal Server Error' caused by: tls: failed to verify certificate: x509: certificate signed by unknown authority

```

Thanks!

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [December 14, 2023, 5:29pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/8 "2023-12-14T17:29:10Z")

</div>

502 happens when the target IP can’t be reached, usually when using Docker and not both are on the same Docker network.

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 14, 2023, 5:57pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/9 "2023-12-14T17:57:25Z")

</div>

The target backend it's not hosted on same network, it's on external network, which it's working fine with Postman (by inserting client certificate)

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [December 14, 2023, 5:58pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/10 "2023-12-14T17:58:56Z")

</div>

Go into Traefik container and try a `wget` to the target.

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 15, 2023, 10:13am UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/11 "2023-12-15T10:13:24Z")

</div>

```auto
[root@serv1]# wget https://externalbackendtogetconnected/
--2023-12-15 08:17:23-- https://externalbackendtogetconnected/
Resolving externalbackendtogetconnected (externalbackendtogetconnected)... x.x.x.x
Connecting to externalbackendtogetconnected (externalbackendtogetconnected)|x.x.x.x|:443... connected.
ERROR: cannot verify externalbackendtogetconnected's certificate, issued by ‘/O=Company1/CN=Company1 - RSP2 Root CI1’:
  Self-signed certificate encountered.
To connect to externalbackendtogetconnected insecurely, use `--no-check-certificate'.
[root@serv1]# wget https://externalbackendtogetconnected/ --no-check-certificate
--2023-12-15 08:17:32-- https://externalbackendtogetconnected/
Resolving externalbackendtogetconnected (externalbackendtogetconnected)... x.x.x.x
Connecting to externalbackendtogetconnected (externalbackendtogetconnected)|x.x.x.x|:443... connected.
WARNING: cannot verify externalbackendtogetconnected's certificate, issued by ‘/O=Company1/CN=Company1 - RSP2 Root CI1’:
  Self-signed certificate encountered.
HTTP request sent, awaiting response... 403 Forbidden
2023-12-15 08:17:33 ERROR 403: Forbidden.

```

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [December 15, 2023, 11:30am UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/12 "2023-12-15T11:30:03Z")

</div>

There seem to be 3 options:

1. Use Traefik `insecureSkipVerify` on Traefik service definition to ignore unknown cert
2. Set custom client cert on Traefik service definition
3. Use TCP router and pass plain TCP traffic with `tls.passthrough` in service

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [December 15, 2023, 3:07pm UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/13 "2023-12-15T15:07:27Z")

</div>

Thanks bluepuma,

Actually

1. I have tried this option and getting

```auto
level=debug msg="'502 Bad Gateway' caused by: local error: tls: no renegotiation"

```

1. I'm already forwarding the cert via transports section
2. 

Thanks will try that.

---

<div class="post-metadata">

**Author:** ![xmanag3r](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xmanag3r](https://community.traefik.io/u/xmanag3r)\
**Post date:** [January 2, 2024, 11:42am UTC](https://community.traefik.io/t/pre-defined-client-certificate-to-backend-request/20772/14 "2024-01-02T11:42:33Z")

</div>

Tried the third option, however cannot offer any connectivity because it's listened on https port.
