目录

Istio 中 mTLS 设置方法

在默认情况下,istio中sidecar之间的相互访问,是采用automtls模式,即如果client和server端的pod都有sidecar的话,会采用mtls来通信,当client没有sidecar,而server中有sidecar,则也可以采用明文通信。

然而,有的客户为了提高通信性能,希望在mesh内部(部分)服务之间互访全部用明文,无论是否有sidecar。而也有客户处于安全性考虑,希望(部分)服务之间互访都通过mtls方式访问(前提是服务pod都注入了sidecar)。

istio主要提供了多种配置方式,来供用户使用:

  • 修改全局配置
  • 通过DestinationRule配置
  • 通过PeerAuthentication配置

全局配置是通过全局配置 configmap istio中的 enableAutoMtls 来配置,配置方法如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
apiVersion: v1
data:
  mesh: |
    accessLogEncoding: JSON
    accessLogFile: /dev/stdout
    accessLogFormat: "{…… ……}"
    defaultConfig:
      discoveryAddress: istiod.istio-system.svc:15012
      holdApplicationUntilProxyStarts: false
      proxyMetadata:
        DNS_AGENT: ""
        EXIT_ON_ZERO_ACTIVE_CONNECTIONS: "true"
        ISTIO_META_DNS_AUTO_ALLOCATE: "true"
        ISTIO_META_DNS_CAPTURE: "true"
      tracing:
        zipkin:
          address: zipkin.istio-system.svc:9441
    outboundTrafficPolicy:
      mode: ALLOW_ANY
    rootNamespace: istio-system
    trustDomain: cluster.local
    enableAutoMtls: false            # 将全局自适应 mtls 关闭
    。。。 。。。    
  meshNetworks: 'networks: {}'
kind: ConfigMap
metadata:
  labels:
    istio.io/rev: default
  name: istio
  namespace: istio-system

在istio中,默认情况下 enableAutoMtls 的值为 true,上述配置中将其设置为 false,将全局 mtls 关闭。下面是 enableAutoMtls 字段的官方介绍:

enabelAutoMtls:

This flag is used to enable mutual TLS automatically for service to service communication within the mesh, default true. If set to true, and a given service does not have a corresponding DestinationRule configured, or its DestinationRule does not have ClientTLSSettings specified, Istio configures client side TLS configuration appropriately. More specifically, If the upstream authentication policy is in STRICT mode, use Istio provisioned certificate for mutual TLS to connect to upstream. If upstream service is in plain text mode, use plain text. If the upstream authentication policy is in PERMISSIVE mode, Istio configures clients to use mutual TLS when server sides are capable of accepting mutual TLS traffic. If service DestinationRule exists and has ClientTLSSettings specified, that is always used instead.

官方文档见:Istio / Global Mesh Options

主要设置workload作为client时的 mTLS 的配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: helloworld
spec:
  host: "helloworld.default.svc.cluster.local" # 设置访问什么服务时生效
  trafficPolicy:
    tls:
      mode: DISABLE 
      # 取值为 DISABLE(禁用)、SIMPLE(单向C->S)、MUTUAL(双向,证书自定义)、ISTIO_MUTUAL(双向,istio自动生成证书)

mode 的取值:

Name Description
DISABLE Do not setup a TLS connection to the upstream endpoint.
SIMPLE Originate a TLS connection to the upstream endpoint.
MUTUAL Secure connections to the upstream using mutual TLS by presenting client certificates for authentication.
ISTIO_MUTUAL Secure connections to the upstream using mutual TLS by presenting client certificates for authentication. Compared to Mutual mode, this mode uses certificates generated automatically by Istio for mTLS authentication. When this mode is used, all other fields in ClientTLSSettings should be empty.

同样以default 命名空间中 helloworld 服务为例,如果针对 helloworld 服务设置 trafficPolicy 后,会在访问helloworld的所有client中的 cds 配置中发生变化。

./_images/mtls-dr-mode-diff-of-disable-and-permissive-cds.png
mode 设置为 DISABLE 时 CDS 的变化

设置workload中 mtls 的配置,主要设置workload作为server时的场景

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
# 设置 "foo" namespace下的mtls,未知道具体的workload的选择
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default-ns
  namespace: foo
spec:
  mtls:
    mode: PERMISSIVE
---
# 设置 "foo" namespace下,label为"app: finance"的workload的mtls
# 该配置会覆盖namespace级别配置
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: finance
  namespace: foo
spec:
  selector:
    matchLabels:
      app: finance
  mtls:
    mode: STRICT
---
# "foo" namespace下,label为"app: finance"的workload的继承上一级(如ns级别)
# 为某个port(8080) 设置独立的规则,覆盖所有上级配置
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: finance-with-port-level
  namespace: foo
spec:
  selector:
    matchLabels:
      app: finance
  mtls:
    mode: UNSET
  portLevelMtls:
    8080:
      mode: DISABLE
      # 取值为 UNSET(继承范围更大的设置值),DISABLE(禁用),PERMISSIVE(可明文,可mtls),STRICT(强制mtls)

mode的取值

取值 功能描述
UNSET 如果父级配置中已经设置该的值,继承父级的值, 否则默认为 PERMISSIVE.
DISABLE 禁用 mTLS
PERMISSIVE 连接可以根据具体情况,选择plaintext 或 mTLS 默认通信.
STRICT 采用mTLS 隧道通信 (client必须有证书).

下面分析一下通过 PeerAuthentication 设置禁用mTLS后,在envoy的config_dump中的变化。

以default 命名空间中 helloworld 服务为例,如果全局服务设置取消(DISABLE)mtls 后,会在访问helloworld的所有client中的 CDS客户端生效) 和 LDS服务端生效)的配置中发生变化。

1
2
3
4
5
6
7
8
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: DISABLE # 取值为 UNSET(继承范围更大的设置值),DISABLE(禁用),PERMISSIVE(可明文,可mtls),STRICT(强制mtls)   

CDS 中(客户端生效),会去掉tls相关的配置,如下图左侧所示:

./_images/mtls-peerauthn-diff-of-disable-and-permissive-cds.png
mode 设置为 DISABLE 后 CDS 的变化

LDS 中(服务端生效),默认情况下,在 VirtualInbound 中会配置 mTLS 相关的配置,识别请求数据是否为 mTLS,并采用相应的方式解码数据。当 mode 设置为 DISABLE 时,LDS 中会去掉TLS相关的配置,如下图左侧所示:

./_images/mtls-peerauthn-diff-of-disable-and-permissive-lds-1.png
mode 设置为DISABLE 时 VirtualInbound 的 LDS 变化一

./_images/mtls-peerauthn-diff-of-disable-and-permissive-lds-2.png
mode 设置为DISABLE 时 VirtualInbound 的 LDS 变化二

通过 PeerAuthentication 配置 mTLS 时,在client端和server端的envoy中都会有相应的配置变更,主要变更点是 transportSocketMatches 配置项 ,在CDS和LDS中,都有该配置项,下面是两处配置的对比:

./_images/mtls-peerauthn-permissive-diff-of-cds-and-lds.png
mode 设置为 DISABLE 时 transportSocket 配置项的 CDS 和 LDS 变化

DestinationRule, 主要针对有sidecar的client端,设置tls规则,只在client端生效,告诉client端,用何种方式来访问客户端。服务端可以是网格内的服务,也可以是网格外的服务。另外,DestinationRule 主要作用在 CDS 中生效,所以只在 client 端生效。

PeerAuthentication,则针对网格内所有sidecar的client端和server端都会生效。PeerAuthentication 在 CDS 和LDS中都会有相应的配置,所以在client端和server端都会有相应的配置。

  1. 如果用PeerAuthentication设置全局严格 mtls后,访问外部网址如何设置?

    client端,默认情况下,是既支持明文,也支持tls,默认tls,如果访问外部服务,还需要配置DestinationRule来配置。

  2. DestinationRule 和 PeerAuthentication 同时设置,client端的设置以谁为准呢?

    client端,以DestinationRule为准。