共用方式為


Azure 機密總賬中的進階使用者定義函式 (預覽)

進階使用者定義函式 (UDF) 允許自訂程式代碼在與總帳相同的信任執行環境 (TEE) 中執行。 此功能會將機密性和完整性保證的優點延伸至自定義程序代碼。 此外,它也支援自定義角色型訪問控制 (RBAC) 以進行授權。

這很重要

用戶定義函式目前在 API 版本 下處於預覽狀態 2024-08-22-preview。 您可以透過 此註冊表單要求此預覽版的存取權。 請參閱 Microsoft Azure 預覽版增補使用規定,以了解適用於 Azure 功能 (搶鮮版 (Beta)、預覽版,或尚未正式發行的版本) 的法律條款。

小提示

如需較簡單的案例,例如輕量型自定義邏輯或與總帳 API 的直接整合,請參閱 Azure 機密總帳中的簡單使用者定義函式。

使用案例

以下是一些進階使用者定義函式可能有幫助的案例:

  • 數據分析和匯總:敏感性資訊可以在 TEE 中處理,而且匯總的資訊可以與專案關係人共用。
  • 保護機密信息:證明之後,可以與其他機密工作負載共享機密資訊,例如個人資料、信用分數和健康情況資訊。

先決條件

  • Azure CLI 或 Azure PowerShell。
  • 訂用帳戶擁有者 - 只有具備 Azure 訂用帳戶「擁有者」權限的使用者,才能建立機密總帳。 先確認您有適當的存取權,再繼續進行本快速入門。

本教學課程假設您已建立總賬實例。 您可以使用 Azure 入口網站Azure CLIAzure PowerShell 來建立一個。

開發應用程式

總賬應用程式是使用 TypeScript 開發,並匯總成 JavaScript 套件組合。 若要深入了解開發程式,請參閱 機密聯盟架構 (CCF) 檔

這很重要

只有系統管理員可以在總賬中部署應用程式及管理自定義 RBAC。 區段的其餘部分假設系統管理員會執行命令。

我們會使用 azureconfidentialledger-app-samples 存放庫 (https://github.com/microsoft/azureconfidentialledger-app-samples) 提供的銀行應用程式來示範此功能。

備註

銀行應用程式會針對常用的銀行案例公開 API,例如使用自定義角色和動作來開立帳戶、存款和轉移資金。

登入 Azure

備註

Azure 機密總賬支援Microsoft現用的 Entra ID。 如果您的應用程式與其他識別提供者整合,請連絡客戶支援以在總賬中設定。

取得Microsoft Entra ID 令牌。

az login --use-device-code
az account get-access-token --resource https://confidential-ledger.azure.com

從輸出複製原始令牌值。

下載總賬身分識別

總賬是由稱為服務憑證的憑證唯一識別。 它用來建立與區塊鏈帳本的安全連線。 從已知的端點下載它,並將其儲存至 servicer_cert.pem。

備註

contoso 是總賬的名稱。 將它取代為適當的總賬名稱。

curl https://identity.confidential-ledger.core.azure.com/ledgerIdentity/contoso --silent | jq ' .ledgerTlsCertificate' | xargs echo -e > service_cert.pem

部署應用程式

叫用 /app/userDefinedEndpoints 端點,以部署JavaScript應用程式套件組合。

apiVersion="2024-08-22-preview"
content_type_application_json="Content-Type: application/json"
bundle="/path/to/bundle.json"
authorization="Authorization: Bearer raw_token_value"
server_identity="--cacert service_cert.pem"

# Deploy the application
#
curl $server_identity -X PUT "https://contoso.confidential-ledger.azure.com/app/userDefinedEndpoints?api-version=$apiVersion" -H "$content_type_application_json" -H "$authorization" -d @$bundle

# View the application
#
curl $server_identity "https://contoso.confidential-ledger.azure.com/app/userDefinedEndpoints?api-version=$apiVersion" -H "$authorization"

備註

進階使用者定義函式和 簡單的使用者定義函 式是互斥的功能。 如果已定義進階 UDF,則您無法建立或執行簡單的 UDF,反之亦然,當已定義簡單的 UDF時,您也無法建立或執行進階 UDF。 若要在兩者之間切換,請遵循 UDF概觀頁面中的指示。

建立角色和使用者

銀行應用程式使用兩個角色,即 經理出納員。 我們會建立角色和用戶來代表他們。

備註

每個用戶都會以唯一的憑證表示。

備註

應用程式使用者可以指派內建角色,也就是系統管理員、參與者和讀者。 自定義角色區分大小寫,且內建角色不區分大小寫。 用戶可以指派多個角色。

apiVersion="2024-08-22-preview"
content_type_application_json="Content-Type: application/json"
content_type_merge_patch_json="Content-Type: application/merge-patch+json"
authorization="Authorization: Bearer raw_token_value"
curve="secp384r1"
server_identity="--cacert service_cert.pem"

# These actions must match (case-sensitive) the values defined in the application.
#
role_actions='{"roles":[{"role_name":"manager","role_actions":["/banking/accounts/post","/banking/accounts/put","/banking/accounts/get","/banking/accounts/patch"]},{"role_name":"teller","role_actions":["/banking/accounts/put","/banking/accounts/get","/banking/accounts/patch"]}]}'

# Create the roles.
#
curl $server_identity -X PUT "https://contoso.confidential-ledger.azure.com/app/roles?api-version=$apiVersion" -H "$content_type_application_json" -H "$authorization" -d $role_actions

# View the roles
#
curl $server_identity "https://contoso.confidential-ledger.azure.com/app/roles?api-version=$apiVersion" -H "$authorization"

# Create a certificate for the manager user.
#
openssl ecparam -out "manager_privk.pem" -name "$curve" -genkey
openssl req -new -key "manager_privk.pem" -x509 -nodes -days 365 -out "manager_cert.pem" -sha384 -subj=/CN="manager"
manager_cert_fingerprint=$(openssl x509 -in "manager_cert.pem" -noout -fingerprint -sha256 | cut -d "=" -f 2)
manager_user="{\"user_id\":\"$manager_cert_fingerprint\",\"assignedRoles\":[\"manager\"]}"

# Create a certificate for the teller user.
#
openssl ecparam -out "teller_privk.pem" -name "$curve" -genkey
openssl req -new -key "teller_privk.pem" -x509 -nodes -days 365 -out "teller_cert.pem" -sha384 -subj=/CN="teller"
teller_cert_fingerprint=$(openssl x509 -in "teller_cert.pem" -noout -fingerprint -sha256 | cut -d "=" -f 2)
teller_user="{\"user_id\":\"$teller_cert_fingerprint\",\"assignedRoles\":[\"teller\"]}"

# Create the manager user.
#
curl $server_identity -X PATCH "https://contoso.confidential-ledger.azure.com/app/ledgerUsers/$manager_cert_fingerprint?api-version=$apiVersion" -H "$content_type_merge_patch_json" -H "$authorization" -d $manager_user

# Create the teller user.
#
curl $server_identity -X PATCH "https://contoso.confidential-ledger.azure.com/app/ledgerUsers/$teller_cert_fingerprint?api-version=$apiVersion" -H "$content_type_merge_patch_json" -H "$authorization" -d $teller_user

# View the users
#
curl $server_identity "https://contoso.confidential-ledger.azure.com/app/ledgerUsers?api-version=$apiVersion" -H "$authorization"

更新執行時間群組態 (選擇性)

呼叫 /app/userDefinedEndpoints/runTimeOptions 端點,即可更新 JavaScript 運行時間組態。 為了示範,讓我們將運行時間上限設定為 2,000 毫秒。

apiVersion="2024-08-22-preview"
content_type_merge_patch_json="Content-Type: application/merge-patch+json"
authorization="Authorization: Bearer raw_token_value"
runtime_options="{\"max_heap_bytes\":1024,\"max_stack_bytes\":1024,\"max_execution_time_ms\":2000,\"log_exception_details\":false,\"return_exception_details\":false,\"max_cached_interpreters\":1024}"
server_identity="--cacert service_cert.pem"

# Patch the runtime options
#
curl $server_identity -X PATCH "https://contoso.confidential-ledger.azure.com/app/userDefinedEndpoints/runTimeOptions?api-version=$apiVersion" -H "$content_type_merge_patch_json" -H "$authorization" -d $runtime_options

# View the runtime options
#
curl $server_identity "https://contoso.confidential-ledger.azure.com/app/userDefinedEndpoints/runTimeOptions?api-version=$apiVersion" -H "$authorization"

現在您已準備好呼叫應用程式端點並提交交易。

清理資源

此集合中的其他快速入門和教學課程會以本快速入門為基礎。 如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。

若不再需要,您可以使用 Azure CLI az group delete 命令來移除資源群組和所有相關資源:

az group delete --name "myResourceGroup"

後續步驟

在本教學課程中,您已將自定義 JavaScript 應用程式部署至機密總賬實例。 若要深入瞭解 Azure 機密總帳,以及如何將其與您的應用程式整合,請繼續閱讀下列文章: