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

快速入门:使用 Azure SDK for Java 创建 Azure 托管 CCF 资源

Azure 托管 CCF(托管 CCF)是一项用于部署机密应用程序的全新且高度安全的服务。 有关 Azure 托管 CCF 的详细信息,请参阅关于 Azure 托管机密联盟框架

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

API 参考文档 | 库源代码 | 包(maven 中央存储库)

先决条件

安装

本快速入门结合使用 Azure 标识库和 Azure CLI 或 Azure PowerShell,向 Azure 服务验证用户身份。 开发人员还可以在运行 Windows 或 Linux.o Code 的计算机上使用 Visual Studio 或 Visual Studi - OpenSSL 对调用进行身份验证。 有关详细信息,请参阅使用 Azure 标识客户端库对客户端进行身份验证

登录到 Azure

使用 Azure CLI az login 命令或 Azure PowerShell Connect-AzAccount cmdlet 登录到 Azure。

az login

如果 CLI 或 PowerShell 可以打开默认浏览器,它将这样做并加载 Azure 登录页。 否则,请访问 https://aka.ms/devicelogin,然后输入终端中显示的授权代码。

如果出现提示,则在浏览器中使用帐户凭据登录。

安装依赖项

<dependency>
    <groupId>com.azure.resourcemanager</groupId>
    <artifactId>azure-resourcemanager-confidentialledger</artifactId>
    <version>1.0.0-beta.3</version>
</dependency>

创建资源组

资源组是在其中部署和管理 Azure 资源的逻辑容器。 使用 Azure PowerShell New-AzResourceGroup cmdlet 在 southcentralus 位置创建一个名为 myResourceGroup 的资源组。

New-AzResourceGroup -Name "myResourceGroup" -Location "SouthCentralUS"

注册资源提供程序

创建资源之前,必须在订阅中注册 Azure 托管 CCF 资源类型。

az feature registration create --namespace Microsoft.ConfidentialLedger --name ManagedCCF

az provider register --namespace Microsoft.ConfidentialLedger

创建成员

为成员生成密钥对。 以下命令完成后,成员的公钥保存在 member0_cert.pem 中,私钥保存在 member0_privk.pem 中。

openssl ecparam -out "member0_privk.pem" -name "secp384r1" -genkey
openssl req -new -key "member0_privk.pem" -x509 -nodes -days 365 -out "member0_cert.pem" -"sha384" -subj=/CN="member0"

创建 Java 应用程序

Azure SDK for Java 库 (azure-resourcemanager-confidentialledger) 支持对托管 CCF 资源执行操作,例如创建和删除、列出与订阅关联的资源,以及查看特定资源的详细信息。 以下代码片段可创建并查看托管 CCF 资源的属性。

import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.confidentialledger.ConfidentialLedgerManager;
import com.azure.resourcemanager.confidentialledger.fluent.models.ManagedCcfInner;
import com.azure.resourcemanager.confidentialledger.models.DeploymentType;
import com.azure.resourcemanager.confidentialledger.models.LanguageRuntime;
import com.azure.resourcemanager.confidentialledger.models.ManagedCcfProperties;
import com.azure.resourcemanager.confidentialledger.models.MemberIdentityCertificate;
import java.util.*;

public class AzureJavaSdkClient {
    public static void main(String[] args) {
      try {
          AzureProfile profile = new AzureProfile("<tenant id>","<subscription id>", AzureEnvironment.AZURE);
          ConfidentialLedgerManager manager = ConfidentialLedgerManager.authenticate(new DefaultAzureCredentialBuilder().build(), profile);

          MemberIdentityCertificate member0 = new MemberIdentityCertificate()
              .withCertificate("-----BEGIN CERTIFICATE-----\nMIIBvjCCAUSgAwIBAgIUA0YHcPpUCtd...0Yet/xU4G0d71ZtULNWo\n-----END CERTIFICATE-----")
              .withTags(Map.of("Dept", "IT"));
          List<MemberIdentityCertificate> members = new ArrayList<MemberIdentityCertificate>();
          members.add(member0);

          DeploymentType deployment = new DeploymentType().withAppSourceUri("").withLanguageRuntime(LanguageRuntime.JS);
          ManagedCcfProperties properties = new ManagedCcfProperties()
              .withDeploymentType(deployment)
              .withNodeCount(5)
              .withMemberIdentityCertificates(members);

          ManagedCcfInner inner = new ManagedCcfInner().withProperties(properties).withLocation("southcentralus");

          // Send Create request
          manager.serviceClient().getManagedCcfs().create("myResourceGroup", "confidentialbillingapp", inner);

          // Print the Managed CCF resource properties
          ManagedCcfInner app = manager.serviceClient().getManagedCcfs().getByResourceGroup("myResourceGroup", "confidentialbillingapp");
          printAppInfo(app);

          // Delete the resource
          manager.serviceClient().getManagedCcfs().delete("myResourceGroup", "confidentialbillingapp");
        } catch (ManagementException ex) {
            // The x-ms-correlation-request-id is located in the Header
            System.out.println(ex.getResponse().getHeaders().toString());
            System.out.println(ex);
        }
    }

    private static void printAppInfo(ManagedCcfInner app) {
        System.out.println("App Name: " + app.name());
        System.out.println("App Id: " + app.id());
        System.out.println("App Location: " + app.location());
        System.out.println("App type: " + app.type());
        System.out.println("App Properties Uri: " + app.properties().appUri());
        System.out.println("App Properties Language Runtime: " + app.properties().deploymentType().languageRuntime());
        System.out.println("App Properties Source Uri: " + app.properties().deploymentType().appSourceUri());
        System.out.println("App Properties NodeCount: " + app.properties().nodeCount());
        System.out.println("App Properties Identity Uri: " + app.properties().identityServiceUri());
        System.out.println("App Properties Cert 0: " + app.properties().memberIdentityCertificates().get(0).certificate());
        System.out.println("App Properties Cert tags: " + app.properties().memberIdentityCertificates().get(0).tags());
    }
}

清理资源

其他托管 CCF 文章可以根据本快速入门编写。 如果打算继续使用后续的快速入门和教程,则可能需要保留这些资源。

否则,当完成本文中创建的资源后,请使用 Azure CLI az group delete 命令删除资源组及其包含的所有资源。

az group delete --resource-group myResourceGroup

后续步骤

在本快速入门中,你使用用于 Java 的 Azure SDK 创建了托管 CCF 资源。 要详细了解 Azure 托管 CCF 以及如何将其与应用程序集成,请继续阅读以下文章: