使用 TLS 來透過 Azure Machine Learning 保護 Web 服務

適用於:Python SDK azureml v1

本文將說明如何保護透過 Azure Machine Learning 部署的 Web 服務。

您可以使用 HTTPS 來限制對 Web 服務的存取,並保護用戶端所提交的資料。 HTTPS 可以加密用戶端與 Web 服務的通訊,藉此協助保護兩者之間的通訊安全。 加密方法使用的是傳輸層安全性 (TLS)。 TLS 有時仍被稱為 安全通訊端層 (SSL),也就是 TLS 的前身。

提示

Azure Machine Learning SDK 會針對與安全通訊相關的屬性使用 "SSL" 一詞。 這並不表示您的 Web 服務不是使用 TLS。 SSL 只是較容易辨識的詞彙。

具體而言,透過 Azure Machine Learning 部署的 Web 服務支援對 AKS 和 ACI 使用 TLS 1.2 版。 針對 ACI 部署,如果您使用較舊的 TLS 版本,建議您重新部署以取得最新的 TLS 版本。

不支援對 Azure Machine Learning - AKS 推斷使用 TLS 1.3 版。

TLS 和 SSL 都依賴數位憑證,其可協助進行加密和身分識別驗證。 如需有關數位憑證如何運作的詳細資訊,請參閱 Wikipedia 主題:公開金鑰基礎結構

警告

如果您的 Web 服務未使用 HTTPS,則網際網路上的其他人可能會看到來往該服務的資料。

HTTPS 也可讓用戶端驗證其所連線之伺服器的真確性。 這項功能可保護用戶端抵禦中間人攻擊。

這是保護 Web 服務的一般程序:

  1. 取得網域名稱。

  2. 取得數位憑證。

  3. 部署或更新啟用 TLS 的 Web 服務。

  4. 更新您的 DNS 以指向 Web 服務。

重要

如果您要部署至 Azure Kubernetes Service (AKS),您可以購買自己的憑證,或使用 Microsoft 提供的憑證。 如果您使用來自 Microsoft 的憑證,則不需要取得網域名稱或 TLS/SSL 憑證。 如需詳細資訊,請參閱本文的啟用 TLS 並部署一節。

部署目標的保護會有些微差異。

重要

本文中的 Azure CLI 命令使用 azure-cli-ml 或 v1 (Azure Machine Learning 的擴充功能)。 v1 擴充功能的支援將於 2025 年 9 月 30 日終止。 您將能安裝並使用 v1 擴充功能,直到該日期為止。

建議您在 2025 年 9 月 30 日之前轉換至 ml 或 v2 擴充功能。 如需有關 v2 擴充功能的詳細資訊,請參閱 Azure ML CLI 擴充功能和 Python SDK v2

取得網域名稱

如果您還沒有自己的網域名稱,請向網域名稱註冊機構購買。 註冊機構之間的流程和價格會有所不同。 註冊機構會提供管理網域名稱的工具。 您可以使用這些工具將完整網域名稱 (FQDN) (例如 www.contoso.com) 對應到裝載您 Web 服務的 IP 位址。

取得 TLS/SSL 憑證

有許多方法可取得 TLS/SSL 憑證 (數位憑證)。 最常見的作法是向憑證授權單位 (CA) 購買。 不論您在哪裡取得憑證,都需要下列檔案:

  • 憑證。 憑證必須包含完整的憑證鏈結,而且必須「以 PEM 編碼」。
  • 金鑰。 金鑰也必須是 PEM 編碼。

當您要求憑證時,您必須提供您打算用於 Web 服務的位址 FQDN (例如 www.contoso.com)。 驗證 Web 服務的身分識別時,會比較在憑證上加上戳記的位址和用戶端所使用的位址。 如果這些位址不相符,用戶端會收到錯誤訊息。

提示

如果憑證授權單位無法以 PEM 編碼的檔案來提供憑證和金鑰,您可以使用 OpenSSL 之類的公用程式來變更格式。

警告

自我簽署憑證應只用於開發。 請勿在實際執行環境中使用。 自我簽署的憑證可能會造成用戶端應用程式的問題。 如需詳細資訊,請參閱用戶端應用程式所用網路程式庫的文件。

啟用 TLS 並部署

針對 AKS 部署,您可以在 Azure Machine Learning 工作區中建立或附加 AKS 叢集時,啟用 TLS 終止。 您可以在 AKS 模型部署期間,使用部署設定物件停止 TLS 終止,否則所有 AKS 模型部署都會預設為在 AKS 叢集建立或附加時啟用 TLS 終止。

針對 ACI 部署,您可以使用部署設定物件在模型部署時啟用 TLS 終止。

在 Azure Kubernetes Service 上部署

注意

當您為設計工具部署安全的 Web 服務時,也適用本節中的資訊。 如果您不熟悉如何使用 Python SDK,請參閱什麼是適用於 Python 的 Azure Machine Learning SDK?

當您在 Azure Machine Learning 工作區中建立或連結 AKS 叢集時,您可以使用 AksCompute.provisioning_configuration()AksCompute.attach_configuration() 設定物件來啟用 TLS 終止。 這兩種方法都會傳回具有 enable_ssl 方法的設定物件,而您可以使用 enable_ssl 方法來啟用 TLS。

您可以使用 Microsoft 憑證或從 CA 購買的自訂憑證來啟用 TLS。

  • 當您使用來自 Microsoft 的憑證時,您必須使用 leaf_domain_label 參數。 此參數會產生服務的 DNS 名稱。 例如,"contoso" 的值會建立 "contoso<six-random-characters>.<azureregion>.cloudapp.azure.com" 的網域名稱,其中 <azureregion> 是包含服務的區域。 (選擇性) 您可以使用 overwrite_existing_domain 參數來覆寫現有的 leaf_domain_label。 下列範例示範如何建立可使用 Microsoft 憑證啟用 TLS 的設定:

    from azureml.core.compute import AksCompute
    
    # Config used to create a new AKS cluster and enable TLS
    provisioning_config = AksCompute.provisioning_configuration()
    
    # Leaf domain label generates a name using the formula
    #  "<leaf-domain-label>######.<azure-region>.cloudapp.azure.com"
    #  where "######" is a random series of characters
    provisioning_config.enable_ssl(leaf_domain_label = "contoso")
    
    
    # Config used to attach an existing AKS cluster to your workspace and enable TLS
    attach_config = AksCompute.attach_configuration(resource_group = resource_group,
                                          cluster_name = cluster_name)
    
    # Leaf domain label generates a name using the formula
    #  "<leaf-domain-label>######.<azure-region>.cloudapp.azure.com"
    #  where "######" is a random series of characters
    attach_config.enable_ssl(leaf_domain_label = "contoso")
    

    重要

    當您使用來自 Microsoft 的憑證時,您不需要購買自己的憑證或網域名稱。

  • 當您使用您所購買的自訂憑證時,您會使用 ssl_cert_pem_filessl_key_pem_filessl_cname 參數。 不支援使用複雜密碼保護的 PEM 檔案。 下列範例示範如何使用 .pem 檔案來建立使用您所購買 TLS/SSL 憑證的設定:

    from azureml.core.compute import AksCompute
    
    # Config used to create a new AKS cluster and enable TLS
    provisioning_config = AksCompute.provisioning_configuration()
    provisioning_config.enable_ssl(ssl_cert_pem_file="cert.pem",
                                        ssl_key_pem_file="key.pem", ssl_cname="www.contoso.com")
    
    # Config used to attach an existing AKS cluster to your workspace and enable SSL
    attach_config = AksCompute.attach_configuration(resource_group = resource_group,
                                         cluster_name = cluster_name)
    attach_config.enable_ssl(ssl_cert_pem_file="cert.pem",
                                        ssl_key_pem_file="key.pem", ssl_cname="www.contoso.com")
    

如需有關 enable_ssl 的詳細資訊,請參閱 AksProvisioningConfiguration.enable_ssl()AksAttachConfiguration.enable_ssl()

在 Azure 容器執行個體上部署

當您部署至 Azure 容器執行個體時,您應提供 TLS 相關參數的值,如下列程式碼片段所示:

from azureml.core.webservice import AciWebservice

aci_config = AciWebservice.deploy_configuration(
    ssl_enabled=True, ssl_cert_pem_file="cert.pem", ssl_key_pem_file="key.pem", ssl_cname="www.contoso.com")

如需詳細資訊,請參閱 AciWebservice.deploy_configuration()

更新您的 DNS

針對使用自訂憑證的 AKS 部署或 ACI 部署,您必須更新您的 DNS 記錄,以指向評分端點的 IP 位址。

重要

當您使用 Microsoft 的憑證進行 AKS 部署時,您不需要手動更新叢集的 DNS 值。 該值應該會自動設定。

您可以遵循下列步驟來更新自訂網域名稱的 DNS 記錄:

  1. 從評分端點 URI 取得評分端點 IP 位址,其格式通常為 http://104.214.29.152:80/api/v1/service/<service-name>/score。 在此範例中,IP 位址是 104.214.29.152。

  2. 使用來自網域名稱註冊機構的工具來更新您網域名稱的 DNS 記錄。 記錄會將 FQDN (例如 www.contoso.com) 對應至 IP 位址。 記錄必須指向評分端點的 IP 位址。

    提示

    Microsoft 不負責更新您自訂 DNS 名稱或憑證的 DNS。 您必須使用您的網域註冊機構來進行更新。

  3. 更新 DNS 記錄之後,您可以使用 nslookup custom-domain-name 命令驗證 DNS 解析。 如果 DNS 記錄已正確更新,自訂網域名稱將會指向評分端點的 IP 位址。

    可能會經過幾分鐘或幾小時的延遲之後,用戶端才能解析網域名稱,這取決於註冊機構及針對網域名稱所設定的存留時間 (TTL)。

若要深入了解 Azure Machine Learning 的 DNS 解析,請參閱如何搭配自訂 DNS 伺服器使用工作區

更新 TLS/SSL 憑證

TLS/SSL 憑證過期,必須更新。 通常每年都需要更新。 使用下列各節中的資訊,針對部署至 Azure Kubernetes Service 的模型更新和續訂憑證:

更新 Microsoft 產生的憑證

如果憑證原本是由 Microsoft 產生 (使用 leaf_domain_label 建立服務時),則會在必要時自動更新。 如果您想要以手動方式進行更新,請使用下列其中一個範例來更新憑證:

重要

  • 如果現有的憑證仍然有效,請使用renew=True (SDK) 或 --ssl-renew (CLI) 來強制設定更新憑證。 此作業大約需要 5 小時才會生效。
  • 最初部署服務時,leaf_domain_label 會用來以 <leaf-domain-label>######.<azure-region>.cloudapp.azure.com 模式建立 DNS 名稱。 若要保留現有的名稱 (包括原本產生的 6 個數字),請使用原始的 leaf_domain_label 值。 請勿包含已產生的 6 個數字。

使用 SDK

from azureml.core.compute import AksCompute
from azureml.core.compute.aks import AksUpdateConfiguration
from azureml.core.compute.aks import SslConfiguration

# Get the existing cluster
aks_target = AksCompute(ws, clustername)

# Update the existing certificate by referencing the leaf domain label
ssl_configuration = SslConfiguration(leaf_domain_label="myaks", overwrite_existing_domain=True, renew=True)
update_config = AksUpdateConfiguration(ssl_configuration)
aks_target.update(update_config)

使用 CLI

適用於:Azure CLI ml 延伸模組 v1

az ml computetarget update aks -g "myresourcegroup" -w "myresourceworkspace" -n "myaks" --ssl-leaf-domain-label "myaks" --ssl-overwrite-domain True --ssl-renew

如需詳細資訊,請參閱下列參考文件:

更新自訂憑證

如果憑證原本是由憑證授權單位單位所產生,請使用下列步驟:

  1. 使用憑證授權單位提供的文件來更新憑證。 此程序會建立新的憑證檔案。

  2. 使用 SDK 或 CLI,以新的憑證更新服務:

    使用 SDK

    from azureml.core.compute import AksCompute
    from azureml.core.compute.aks import AksUpdateConfiguration
    from azureml.core.compute.aks import SslConfiguration
    
    # Read the certificate file
    def get_content(file_name):
        with open(file_name, 'r') as f:
            return f.read()
    
    # Get the existing cluster
    aks_target = AksCompute(ws, clustername)
    
    # Update cluster with custom certificate
    ssl_configuration = SslConfiguration(cname="myaks", cert=get_content('cert.pem'), key=get_content('key.pem'))
    update_config = AksUpdateConfiguration(ssl_configuration)
    aks_target.update(update_config)
    

    使用 CLI

    適用於:Azure CLI ml 延伸模組 v1

    az ml computetarget update aks -g "myresourcegroup" -w "myresourceworkspace" -n "myaks" --ssl-cname "myaks"--ssl-cert-file "cert.pem" --ssl-key-file "key.pem"
    

如需詳細資訊,請參閱下列參考文件:

停用 TLS

若要針對部署至 Azure Kubernetes Service 的模型停用 TLS,請使用 status="Disabled" 建立 SslConfiguration,然後執行更新:

from azureml.core.compute import AksCompute
from azureml.core.compute.aks import AksUpdateConfiguration
from azureml.core.compute.aks import SslConfiguration

# Get the existing cluster
aks_target = AksCompute(ws, clustername)

# Disable TLS
ssl_configuration = SslConfiguration(status="Disabled")
update_config = AksUpdateConfiguration(ssl_configuration)
aks_target.update(update_config)

下一步

了解如何: