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

Azure 机密账本 (ACL) 可编程性

可编程性是机密账本中的一项新功能,允许客户在与常规 Azure 机密账本 (ACL) 事务相同的受信任计算基础 (TCB) 中运行自定义代码。 在 TCB 中执行自定义代码和事务的好处是能够为自定义代码及其生成的事务提供相同的保密性和完整性保证。 可编程性还通过 ACL 中定义并在代码中使用的自定义角色来支持基于角色的访问控制 (RBAC)。

可通过可编程性启用的一些方案如下所示:

  • 数据聚合和分析:可以在 TCB 中分析敏感信息,并且只能与利益干系人共享聚合信息。
  • 证明:证明之后,可以与其他机密计算产品(例如 Azure 机密 ACI 和机密 VM)上所运行的工作负载共享敏感信息,例如 PII、信用评分和运行状况信息

先决条件

  • 订阅所有者 - 只有在 Azure 订阅上具有所有者权限的用户才能创建机密账本。 在继续此快速入门之前,请先确认你具有适当的访问权限

本教程假定你已创建机密账本实例。 可以使用 Azure 门户Azure CLIAzure PowerShell 来创建机密账本。

开发和部署应用程序

应用程序是使用 TypeScript 开发的,并汇总到 JavaScript 捆绑包中。 若要详细了解开发过程,请参阅机密联盟框架 (CCF) 文档

重要

只有管理员用户可以在机密账本中部署应用程序和管理自定义 RBAC。 本节的其余部分假定管理员执行以下步骤。

下节假定应用程序已就绪,可供部署。 为了演示部署过程,我们使用 azureconfidentialledger-app-samples 存储库 (https://github.com/microsoft/azureconfidentialledger-app-samples) 中提供的示例银行应用程序。

注意

该应用程序演示了如何通过部署在机密账本实例上的 JavaScript 应用程序实现常见银行用例,例如开户、存款和转移资金。 它还演示了如何使用自定义角色和 RBAC 来授权用户操作。

登录 Azure

注意

机密账本支持 Microsoft Entra 开箱即用。 无需更多配置。 如果应用程序使用其他标识提供者颁发的 JWT,请联系客户支持,以便在机密账本实例上配置它。

获取 Microsoft Entra 令牌,向机密账本实例进行身份验证。

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"

创建自定义角色和用户

银行应用程序涉及两个人员,即“经理”和“出纳”。 我们创建两个角色和用户来表示这两个人员。

注意

用户由唯一证书表示。

注意

可以为应用程序用户分配内置角色,即“管理员”、“参与者”和“读者”。 自定义角色区分大小写,但内置角色不区分大小写。 可以向用户分配多个角色。

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 运行时配置。 为了演示它,我们将最大执行时间设置为 2000 ms。

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 机密账本以及如何将其与应用程序集成,请继续阅读以下文章。