你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在 Azure IoT MQ 预览版中使用自动证书管理配置 TLS 以保护 MQTT 通信

重要

Azure IoT 操作预览版(由 Azure Arc 启用)当前处于预览状态。 不应在生产环境中使用此预览版软件。

有关 beta 版本、预览版或尚未正式发布的版本的 Azure 功能所适用的法律条款,请参阅 Microsoft Azure 预览版的补充使用条款

可以使用BrokerListener 资源配置 TLS,以保护 MQTT 代理与客户端之间的 MQTT 通信。 可以使用手动或自动证书管理配置 TLS。

验证证书管理器安装

借助自动证书管理,可以使用证书管理器管理 TLS 服务器证书。 默认情况下,证书管理器已在azure-iot-operations命名空间中与 Azure IoT 操作预览版一起安装。 在继续之前验证安装。

  1. 使用kubectl检查与证书管理器应用标签匹配的 Pod。

    $ kubectl get pods --namespace azure-iot-operations -l 'app in (cert-manager,cainjector,webhook)'
    NAME                                           READY   STATUS    RESTARTS       AGE
    aio-cert-manager-64f9548744-5fwdd              1/1     Running   4 (145m ago)   4d20h
    aio-cert-manager-cainjector-6c7c546578-p6vgv   1/1     Running   4 (145m ago)   4d20h
    aio-cert-manager-webhook-7f676965dd-8xs28      1/1     Running   4 (145m ago)   4d20h
    
  2. 如果看到显示为就绪且正在运行的 Pod,则已安装证书管理器并可供使用。

提示

要进一步验证安装,请查看证书管理器文档验证安装。 请记住使用azure-iot-operations命名空间。

为 TLS 服务器证书创建颁发者

证书管理器颁发者资源定义如何自动颁发证书。 证书管理器支持多种本机颁发者类型。 它还支持外部颁发者类型,用于将功能扩展到本机受支持的颁发者之外。 Azure IoT MQ 预览版可用于任何类型的证书管理器颁发者。

重要

在初始部署期间,Azure IoT 操作随 TLS 服务器证书的默认颁发者一起安装。 可以使用此颁发者进行开发和测试。 有关详细信息,请参阅Azure IoT 操作的默认根 CA 和颁发者。 只有当想要使用不同的颁发者时,才需要执行以下步骤。

创建颁发者的方法因场景而异。 以下部分列出了帮助入门的示例。

CA 颁发者可用于开发和测试。 必须使用 Kubernetes 机密中存储的证书和私钥进行配置。

将根证书设置为 Kubernetes 机密

如果有现有的 CA 证书,请使用 CA 证书和私钥 PEM 文件创建 Kubernetes 机密。 运行以下命令,并将根证书设置为 Kubernetes 机密,并可以跳过下一部分。

kubectl create secret tls test-ca --cert tls.crt --key tls.key -n azure-iot-operations

如果没有 CA 证书,证书管理器可以为你生成根 CA 证书。 使用证书管理器生成根 CA 证书称为使用自签名证书启动 CA 颁发者。

  1. 首先创建ca.yaml

    apiVersion: cert-manager.io/v1
    kind: Issuer
    metadata:
      name: selfsigned-ca-issuer
      namespace: azure-iot-operations
    spec:
      selfSigned: {}
    ---
    apiVersion: cert-manager.io/v1
    kind: Certificate
    metadata:
      name: selfsigned-ca-cert
      namespace: azure-iot-operations
    spec:
      isCA: true
      commonName: test-ca
      secretName: test-ca
      issuerRef:
        # Must match Issuer name above
        name: selfsigned-ca-issuer
        # Must match Issuer kind above
        kind: Issuer
        group: cert-manager.io
      # Override default private key config to use an EC key
      privateKey:
        rotationPolicy: Always
        algorithm: ECDSA
        size: 256
    
  2. 使用以下命令创建自签名 CA 证书:

    kubectl apply -f ca.yaml
    

证书管理器使用其默认值创建 CA 证书。 可以修改证书规范以更改此证书的属性。有关有效选项列表,请参阅证书管理器文档

分发根证书

前面的示例将 CA 证书存储在名为test-ca的 Kubernetes 机密中。 可以使用以下命令从机密检索 PEM 格式的证书并将其存储在文件ca.crt中:

kubectl get secret test-ca -n azure-iot-operations -o json | jq -r '.data["tls.crt"]' | base64 -d > ca.crt

此证书必须由所有客户端分发和信任。 例如,对 mosquitto 客户端使用--cafile标志。

可以使用 Azure Key Vault 管理 IoT MQ 的机密,而不是 Kubernetes 机密。 若要了解详细信息,请参阅使用 Azure Key Vault 或 Kubernetes 机密管理机密

基于 CA 证书创建颁发者

证书管理器需要基于在前面的步骤中生成或导入的 CA 证书颁发者。 按issuer-ca.yaml创建以下文件:

apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: my-issuer
  namespace: azure-iot-operations
spec:
  ca:
    # Must match secretName of generated or imported CA cert
    secretName: test-ca

使用以下命令创建颁发者:

kubectl apply -f issuer-ca.yaml

前面的命令创建颁发者以颁发 TLS 服务器证书。 记下颁发者的名称和类型。 在此示例中,名称为my-issuer,类型为Issuer。 这些值稍后在 BrokerListener 资源中设置。

为端口启用 TLS

修改 BrokerListener 资源中的tls设置,以指定前端的 TLS 端口和颁发者。 下面是 BrokerListener 资源示例,该资源使用自动证书管理在端口 8884 上启用 TLS。

apiVersion: mq.iotoperations.azure.com/v1beta1
kind: BrokerListener
metadata:
  name: my-new-tls-listener
  namespace: azure-iot-operations
spec:
  brokerRef: broker
  authenticationEnabled: false # If set to true, a BrokerAuthentication resource must be configured
  authorizationEnabled: false
  serviceType: loadBalancer # Optional; defaults to 'clusterIP'
  serviceName: my-new-tls-listener # Avoid conflicts with default service name 'aio-mq-dmqtt-frontend'
  port: 8884 # Avoid conflicts with default port 8883
  tls:
    automatic:
      issuerRef:
        name: my-issuer
        kind: Issuer

配置 BrokerListener 资源后,IoT MQ 会自动创建已启用指定端口和 TLS 的新服务。

可选:配置服务器证书参数

唯一必需的参数是issuerRef.nameissuerRef.kind。 会自动选择生成的 TLS 服务器证书的所有属性。 但是,IoT MQ 允许在 BrokerListener 资源(tls.automatic.issuerRef下)中指定某些属性以自定义它们。 下面是所有受支持的属性的示例:

# cert-manager issuer for TLS server certificate. Required.
issuerRef:
  # Name of issuer. Required.
  name: my-issuer
  # 'Issuer' or 'ClusterIssuer'. Required.
  kind: Issuer
  # Issuer group. Optional; defaults to 'cert-manager.io'.
  # External issuers may use other groups.
  group: cert-manager.io
# Namespace of certificate. Optional; omit to use default namespace.
namespace: az
# Where to store the generated TLS server certificate. Any existing
# data at the provided secret will be overwritten.
# Optional; defaults to 'my-issuer-{port}'.
secret: my-issuer-8884
# Parameters for the server certificate's private key.
# Optional; defaults to rotationPolicy: Always, algorithm: ECDSA, size: 256.
privateKey:
  rotationPolicy: Always
  algorithm: ECDSA
  size: 256
# Total lifetime of the TLS server certificate. Optional; defaults to '720h' (30 days).
duration: 720h
# When to begin renewing the certificate. Optional; defaults to '240h' (10 days).
renewBefore: 240h
# Any additional SANs to add to the server certificate. Omit if not required.
san:
  dns:
  - iotmq.example.com
  ip:
  - 192.168.1.1

验证部署

使用 kubectl 检查与 BrokerListener 资源关联的服务是否正在运行。 在上面的示例中,服务名称为my-new-tls-listener,命名空间为azure-iot-operations。 以下命令检查服务状态:

$ kubectl get service my-new-tls-listener -n azure-iot-operations
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
my-new-tls-listener    LoadBalancer   10.43.241.171   XXX.XX.X.X    8884:32457/TCP   33s

使用 TLS 连接到代理

配置服务器证书后,会启用 TLS。 要使用 mosquitto 进行测试:

mosquitto_pub -h $HOST -p 8884 -V mqttv5 -i "test" -t "test" -m "test" --cafile ca.crt

--cafile参数在 mosquitto 客户端上启用 TLS,并指定客户端应信任给定文件颁发的所有服务器证书。 必须指定文件,其中包含配置的 TLS 服务器证书的颁发者。

$HOST替换为相应的主机:

  • 如果从同一群集中的进行连接,请替换为给定的服务名称(例如my-new-tls-listener)或服务CLUSTER-IP
  • 如果从群集外部进行连接,请替换为服务EXTERNAL-IP

请记得根据需要指定身份验证方法。 例如,用户名和密码。

Azure IoT 操作预览版的默认根 CA 和颁发者

为了帮助入门,Azure IoT 操作使用 TLS 服务器证书的默认“快速入门”根 CA 和颁发者部署。 可以使用此颁发者进行开发和测试。

  • CA 证书是自签名证书,不受 Azure IoT 操作外部的任何客户端信任。 CA 证书的使用者是CN = Azure IoT Operations Quickstart Root CA - Not for Production,从安装之日起 30 天内过期。

  • 根 CA 证书存储在名为aio-ca-key-pair-test-only的 Kubernetes 机密中。

  • 根 CA 证书的公共部分存储在名为aio-ca-trust-bundle-test-only的 ConfigMap 中。 可以从 ConfigMap 检索 CA 证书,并使用 kubectl 和 openssl 对其进行检查。

    $ kubectl get configmap aio-ca-trust-bundle-test-only -n azure-iot-operations -o json | jq -r '.data["ca.crt"]' | openssl x509 -text -noout
    Certificate:
        Data:
            Version: 3 (0x2)
            Serial Number:
                74:d8:b6:fe:63:5a:7d:24:bd:c2:c0:25:c2:d2:c7:94:66:af:36:89
            Signature Algorithm: ecdsa-with-SHA256
            Issuer: CN = Azure IoT Operations Quickstart Root CA - Not for Production
            Validity
                Not Before: Nov  2 00:34:31 2023 GMT
                Not After : Dec  2 00:34:31 2023 GMT
            Subject: CN = Azure IoT Operations Quickstart Root CA - Not for Production
            Subject Public Key Info:
                Public Key Algorithm: id-ecPublicKey
                    Public-Key: (256 bit)
                    pub:
                        04:51:43:93:2c:dd:6b:7e:10:18:a2:0f:ca:2e:7b:
                        bb:ba:5c:78:81:7b:06:99:b5:a8:11:4f:bb:aa:0d:
                        e0:06:4f:55:be:f9:5f:9e:fa:14:75:bb:c9:01:61:
                        0f:20:95:cd:9b:69:7c:70:98:f8:fa:a0:4c:90:da:
                        5b:1a:d7:e7:6b
                    ASN1 OID: prime256v1
                    NIST CURVE: P-256
            X509v3 extensions:
                X509v3 Basic Constraints: critical
                    CA:TRUE
                X509v3 Key Usage: 
                    Certificate Sign
                X509v3 Subject Key Identifier: 
                    B6:DD:8A:42:77:05:38:7A:51:B4:8D:8E:3F:2A:D1:79:32:E9:43:B9
        Signature Algorithm: ecdsa-with-SHA256
            30:44:02:20:21:cd:61:d7:21:86:fd:f8:c3:6d:33:36:53:e3:
            a6:06:3c:a6:80:14:13:55:16:f1:19:a8:85:4b:e9:5d:61:eb:
            02:20:3e:85:8a:16:d1:0f:0b:0d:5e:cd:2d:bc:39:4b:5e:57:
            38:0b:ae:12:98:a9:8f:12:ea:95:45:71:bd:7c:de:9d
    
  • 默认情况下,azure-iot-operations命名空间中已配置了名为aio-ca-issuer的 CA 颁发者。 它用作 IoT 操作的所有 TLS 服务器证书的通用 CA 颁发者。 IoT MQ 使用从同一 CA 证书创建的颁发者为端口 8883 上的默认 TLS 侦听器颁发 TLS 服务器证书。 可以使用以下命令检查颁发者:

    $ kubectl get issuer aio-ca-issuer -n azure-iot-operations -o yaml
    apiVersion: cert-manager.io/v1
    kind: Issuer
    metadata:
      annotations:
        meta.helm.sh/release-name: azure-iot-operations
        meta.helm.sh/release-namespace: azure-iot-operations
      creationTimestamp: "2023-11-01T23:10:24Z"
      generation: 1
      labels:
        app.kubernetes.io/managed-by: Helm
      name: aio-ca-issuer
      namespace: azure-iot-operations
      resourceVersion: "2036"
      uid: c55974c0-e0c3-4d35-8c07-d5a0d3f79162
    spec:
      ca:
        secretName: aio-ca-key-pair-test-only
    status:
      conditions:
      - lastTransitionTime: "2023-11-01T23:10:59Z"
        message: Signing CA verified
        observedGeneration: 1
        reason: KeyPairVerified
        status: "True"
        type: Ready
    

对于生产,必须使用来自受信任 CA 的证书配置 CA 颁发者,如前面的部分所述。