Back to articles
Security & Anti-DDoS

Comprehensive Microservices Security: Guide to Setting Up mTLS with Traefik v3 and Casdoor

This guide provides a detailed walkthrough for implementing end-to-end security in microservices architectures using mutual TLS (mTLS) with Traefik v3 and Casdoor. It covers...

DPTCloud Engineering4 min read
Comprehensive Microservices Security: Guide to Setting Up mTLS with Traefik v3 and Casdoor

This guide provides a detailed walkthrough for implementing end-to-end security in microservices architectures using mutual TLS (mTLS) with Traefik v3 and Casdoor. It covers certificate configuration, bidirectional authentication setup, integration with Casdoor for user management and role-based access control, and ensures system scalability and security. Step-by-step instructions include real-world configurations, test commands, and troubleshooting for common issues.

Overview of Microservices Security and mTLS

Microservices architectures are widely adopted for their flexibility and scalability, but they introduce significant security challenges. Mutual TLS (mTLS) is a powerful solution that enables both the server and client to authenticate each other using digital certificates. With mTLS, every service in the system must present a valid certificate to establish a secure connection, preventing impersonation attacks and unauthorized access. Traefik v3 offers robust, integrated support for mTLS, allowing easy configuration of authentication rules, routing, and protection. Casdoor, an open-source identity and access management platform, enhances mTLS by providing user authentication, role-based access control, and API protection for sensitive services.

Prerequisites and Environment Setup

To deploy mTLS with Traefik v3 and Casdoor, the following components are required:

  • Microservices applications running on Docker or Kubernetes.
  • A Traefik v3 server installed and operational.
  • A Casdoor server accessible from Traefik.
  • A Certificate Authority (CA) to issue certificates for services.
  • Private keys and public certificates for each service.

We will use Docker Compose for the test environment. Below is a basic configuration in the docker-compose.yml file:

version: '3.8'\n\nservices:\n  traefik:\n    image: traefik:v3.0\n    command:\n      - --api.insecure=true\n      - --providers.docker=true\n      - --providers.docker.exposedbydefault=false\n      - --entrypoints.web.address=:80\n      - --entrypoints.websecure.address=:443\n      - --certificatesresolvers.mtlsresolver.acme.tlschallenge=true\n      - --certificatesresolvers.mtlsresolver.acme.email=your-email@example.com\n      - --certificatesresolvers.mtlsresolver.acme.storage=/letsencrypt/acme.json\n    ports:\n      - \"80:80\"\n      - \"443:443\"\n    volumes:\n      - /var/run/docker.sock:/var/run/docker.sock\n      - ./letsencrypt:/letsencrypt\n    networks:\n      - traefik-network\n\n  casdoor:\n    image: casdoor/casdoor:v1.12.0\n    ports:\n      - \"8000:8000\"\n    environment:\n      - CASDOOR_CONFIG_PATH=/casdoor/config.yaml\n    volumes:\n      - ./casdoor/config.yaml:/casdoor/config.yaml\n    networks:\n      - traefik-network\n\n  app1:\n    image: myapp:latest\n    labels:\n      - traefik.enable=true\n      - traefik.http.routers.app1.rule=Host(`app1.example.com`)\n      - traefik.http.routers.app1.entrypoints=websecure\n      - traefik.http.routers.app1.tls=true\n      - traefik.http.routers.app1.tls.certresolver=mtlsresolver\n      - traefik.http.routers.app1.tls.options=mtls-options\n    networks:\n      - traefik-network\n\nnetworks:\n  traefik-network:\n    driver: bridge\n

Configuring mTLS on Traefik v3

To enable mTLS, configure the following options in the traefik.yml file or via Docker Compose labels:

  • CA Certificate: Create a private CA or use Let's Encrypt. Traefik supports automatic certificate generation with Let's Encrypt.
  • mtlsresolver: Set up a resolver to authenticate client certificates. Example configuration:
certificatesResolvers:\n  mtlsresolver:\n    acme:\n      email: [email protected]\n      storage: /letsencrypt/acme.json\n      httpChallenge: {}\n      tlsChallenge: {}\n

To enforce mTLS, add the tls.options to the router:

traefik.http.routers.app1.tls.options=mtls-options\n

Define the mTLS options:

tls:\n  options:\n    mtls-options:\n      minVersion: TLS1.2\n      cipherSuites:\n        - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256\n        - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384\n      clientAuth:\n        caFiles:\n          - /certs/ca.crt\n        clientAuthType: RequireAndVerifyClientCert\n

Ensure the CA certificate is mounted into the Traefik container and accessible.

Integrating Casdoor with mTLS

Casdoor supports OAuth 2.0 and OpenID Connect authentication. To integrate with mTLS, configure Casdoor to use bidirectional authentication for user management and access control APIs. In Casdoor, create a new application and set scopes such as openid, profile, and email.

In the microservices application, use an OAuth library to call Casdoor APIs. Example with Node.js:

const axios = require('axios');\nconst fs = require('fs');\n\nconst clientCert = fs.readFileSync('/certs/client.crt');\nconst clientKey = fs.readFileSync('/certs/client.key');\nconst caCert = fs.readFileSync('/certs/ca.crt');\n\nconst client = axios.create({\n  baseURL: 'https://casdoor.example.com/api',\n  httpsAgent: new https.Agent({\n    cert: clientCert,\n    key: clientKey,\n    ca: caCert,\n    rejectUnauthorized: true\n  })\n});\n\nclient.get('/users')\n  .then(response => console.log(response.data))\n  .catch(error => console.error(error));\n

Testing and Troubleshooting

After configuration, verify connectivity using the following command:

curl -v https://app1.example.com --cert /certs/client.crt --key /certs/client.key --cacert /certs/ca.crt\n

If you encounter an SSL certificate problem error, verify the certificate is correct and the CA certificate is properly mounted in the container. If you receive a 403 Forbidden error, check Casdoor policies and client access permissions.

To validate mTLS setup, use tools like openssl s_client:

openssl s_client -connect app1.example.com:443 -cert /certs/client.crt -key /certs/client.key -CAfile /certs/ca.crt\n

References

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478