mcpField
M
mtls-configuration

mtls-configuration

github.com/sickn33安全48.7k176 次安装1 天前更新
代码仓库

关于 mtls-configuration

面向保护服务间通信和服务网格的团队,提供 mTLS 配置指南,涵盖证书生命周期、配置边界和故障排查,并强调验证版本、信任链和身份。

主要功能

  1. 01mTLS 流程与证书层级
  2. 02Istio 和 Linkerd 配置模板
  3. 03证书轮换指导
  4. 04握手与策略故障排查
  5. 05信任和授权防护

适用场景

  1. 01保护服务间流量
  2. 02规划证书轮换
  3. 03排查 TLS 握手故障
  4. 04验证多集群信任

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill mtls-configuration

安装后在 Claude Code 中输入/mtls-configuration确认已加载。

详细介绍

mTLS 配置

关于如何实现相互 TLS,以支持零信任服务网格通信的综合指南。

以下情况请勿使用此技能

  • 任务与 mTLS 配置无关
  • 你需要使用本范围之外的其他领域或工具

说明

  • 明确目标、约束和所需输入。
  • 应用相关最佳实践并验证结果。
  • 提供可执行的步骤和验证方法。

以下情况请使用此技能

  • 实现零信任网络
  • 保护服务间通信
  • 证书轮换和管理
  • 调试 TLS 握手问题
  • 满足合规要求(PCI-DSS、HIPAA)
  • 实现多集群安全通信

核心概念

1. mTLS 流程
┌─────────┐                              ┌─────────┐
│ Service │                              │ Service │
│    A    │                              │    B    │
└────┬────┘                              └────┬────┘
     │                                        │
┌────┴────┐      TLS Handshake          ┌────┴────┐
│  Proxy  │◄───────────────────────────►│  Proxy  │
│(Sidecar)│  1. ClientHello             │(Sidecar)│
│         │  2. ServerHello + Cert      │         │
│         │  3. Client Cert             │         │
│         │  4. Verify Both Certs       │         │
│         │  5. Encrypted Channel       │         │
└─────────┘                              └─────────┘
2. 证书层级
Root CA (Self-signed, long-lived)
    │
    ├── Intermediate CA (Cluster-level)
    │       │
    │       ├── Workload Cert (Service A)
    │       └── Workload Cert (Service B)
    │
    └── Intermediate CA (Multi-cluster)
            │
            └── Cross-cluster certs

配置边界

这些片段与特定版本相关,并非完整的服务网格/PKI 部署方案。请先验证已安装的 CRD 以及代理/ambient 模式。仅有 cert-manager Certificate/Issuer 资源并不会接入 Istio 的签名器;请使用经过审核且受支持的集成。Linkerd Server 用于管理入站策略,而非出站外部 TLS;请将受支持的出站跳过注解添加到工作负载 Pod 模板,而不是 Service。排除指标/端口会削弱保护,需经过审核的网络/身份验证策略。mTLS 可验证对等方身份,但授权仍需指定允许的身份。请将 CA 密钥保存在版本控制之外,规划信任关系重叠的轮换/回滚,并在强制启用严格模式前验证允许和拒绝的身份。

模板

模板 1:Istio mTLS(严格模式)
# Enable strict mTLS mesh-wide
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: STRICT
---
# Namespace-level override (permissive for migration)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: legacy-namespace
spec:
  mtls:
    mode: PERMISSIVE
---
# Workload-specific policy
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: payment-service
  namespace: production
spec:
  selector:
    matchLabels:
      app: payment-service
  mtls:
    mode: STRICT
  portLevelMtls:
    8080:
      mode: STRICT
    9090:
      mode: DISABLE  # Metrics port, no mTLS
模板 2:用于 mTLS 的 Istio Destination Rule
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: default
  namespace: istio-system
spec:
  host: "*.local"
  trafficPolicy:
    tls:
      mode: ISTIO_MUTUAL
---
# TLS to external service
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: external-api
spec:
  host: api.external.com
  trafficPolicy:
    tls:
      mode: SIMPLE
      caCertificates: /etc/certs/external-ca.pem
---
# Mutual TLS to external service
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: partner-api
spec:
  host: api.partner.com
  trafficPolicy:
    tls:
      mode: MUTUAL
      clientCertificate: /etc/certs/client.pem
      privateKey: /etc/certs/client-key.pem
      caCertificates: /etc/certs/partner-ca.pem
模板 3:将 Cert-Manager 与 Istio 配合使用
# Install cert-manager issuer for Istio
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: istio-ca
spec:
  ca:
    secretName: istio-ca-secret
---
# Create Istio CA secret
apiVersion: v1
kind: Secret
metadata:
  name: istio-ca-secret
  namespace: cert-manager
type: kubernetes.io/tls
data:
  tls.crt: <base64-encoded-ca-cert>
  tls.key: <base64-encoded-ca-key>
---
# Certificate for workload
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: my-service-cert
  namespace: my-namespace
spec:
  secretName: my-service-tls
  duration: 24h
  renewBefore: 8h
  issuerRef:
    name: istio-ca
    kind: ClusterIssuer
  commonName: my-service.my-namespace.svc.cluster.local
  dnsNames:
    - my-service
    - my-service.my-namespace
    - my-service.my-namespace.svc
    - my-service.my-namespace.svc.cluster.local
  usages:
    - server auth
    - client auth
模板 4:SPIFFE/SPIRE 集成
# SPIRE Server configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: spire-server
  namespace: spire
data:
  server.conf: |
    server {
      bind_address = "0.0.0.0"
      bind_port = "8081"
      trust_domain = "example.org"
      data_dir = "/run/spire/data"
      log_level = "INFO"
      ca_ttl = "168h"
      default_x509_svid_ttl = "1h"
    }

    plugins {
      DataStore "sql" {
        plugin_data {
          database_type = "sqlite3"
          connection_string = "/run/spire/data/datastore.sqlite3"
        }
      }

      NodeAttestor "k8s_psat" {
        plugin_data {
          clusters = {
            "demo-cluster" = {
              service_account_allow_list = ["spire:spire-agent"]
            }
          }
        }
      }

      KeyManager "memory" {
        plugin_data {}
      }

      UpstreamAuthority "disk" {
        plugin_data {
          key_file_path = "/run/spire/secrets/bootstrap.key"
          cert_file_path = "/run/spire/secrets/bootstrap.crt"
        }
      }
    }
---
# SPIRE Agent DaemonSet (abbreviated)
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: spire-agent
  namespace: spire
spec:
  selector:
    matchLabels:
      app: spire-agent
  template:
    metadata:
      labels:
        app: spire-agent
    spec:
      containers:
        - name: spire-agent
          image: ghcr.io/spiffe/spire-agent:1.8.0
          volumeMounts:
            - name: spire-agent-socket
              mountPath: /run/spire/sockets
      volumes:
        - name: spire-agent-socket
          hostPath:
            path: /run/spire/sockets
            type: DirectoryOrCreate
模板 5:Linkerd mTLS(自动)
# Linkerd enables mTLS automatically
# Verify with:
# linkerd viz edges deployment -n my-namespace

# For external services without mTLS
apiVersion: policy.linkerd.io/v1beta1
kind: Server
metadata:
  name: external-api
  namespace: my-namespace
spec:
  podSelector:
    matchLabels:
      app: my-app
  port: external-api
  proxyProtocol: HTTP/1  # or TLS for passthrough
---
# Skip TLS for specific port
apiVersion: v1
kind: Service
metadata:
  name: my-service
  annotations:
    config.linkerd.io/skip-outbound-ports: "3306"  # MySQL

证书轮换

# Istio - Check certificate expiry
istioctl proxy-config secret deploy/my-app -o json | \
  jq '.dynamicActiveSecrets[0].secret.tlsCertificate.certificateChain.inlineBytes' | \
  tr -d '"' | base64 -d | openssl x509 -text -noout # security-allowlist: local certificate inspection

# An authorized restart may obtain a new workload certificate, but is not
# a general CA/certificate rotation procedure and disrupts workloads.
kubectl rollout restart deployment/my-app

# Check Linkerd identity
linkerd identity -n my-namespace

调试 mTLS 问题

# Istio - Check if mTLS is enabled
istioctl authn tls-check my-service.my-namespace.svc.cluster.local

# Verify peer authentication
kubectl get peerauthentication --all-namespaces

# Check destination rules
kubectl get destinationrule --all-namespaces

# Debug TLS handshake
istioctl proxy-config log deploy/my-app --level debug
kubectl logs deploy/my-app -c istio-proxy | grep -i tls

# Linkerd - Check mTLS status
linkerd viz edges deployment -n my-namespace
linkerd viz tap deploy/my-app --to deploy/my-backend

最佳实践

建议事项
  • 从 PERMISSIVE 模式开始 - 逐步迁移到 STRICT 模式
  • 监控证书有效期 - 设置告警
  • 使用短期证书 - 工作负载证书有效期不超过 24 小时
  • 定期轮换 CA - 做好 CA 轮换计划
  • 记录 TLS 错误 - 便于调试和审计
禁止事项
  • 不要禁用 mTLS - 不要为了方便而在生产环境中禁用
  • 不要忽略证书过期 - 自动执行轮换
  • 不要信任任意自签名叶证书 - 受管根 CA 可以是自签名的;请验证预期的信任链和工作负载身份
  • 不要跳过验证 - 验证完整证书链

资源

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为针对特定环境进行验证、测试或专家审核的替代品。
  • 如果缺少必要输入、权限、安全边界或成功标准,请暂停并请求澄清。

评论

支持 Markdown