Share via


快速入門:使用 Azure SDK for JavaScript 和 TypeScript 建立 Azure 受控 CCF 資源

Microsoft Azure 受控 CCF (受控 CCF) 是部署機密應用程式新型且高度安全的服務。 如需 Azure 受控 CCF 的詳細資訊,請參閱關於 Azure 受控機密聯盟架構

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

API 參考文件 | 程式庫原始程式碼 | 套件 (npm)

必要條件

設定

本快速入門使用 Azure 身分識別程式庫,搭配 Azure CLI 或 Azure PowerShell,向 Azure 服務驗證使用者。 開發人員也可以使用 Visual Studio 或 Visual Studio Code 來驗證其呼叫。 如需詳細資訊,請參閱使用 Azure 身分識別用戶端程式庫驗證用戶端

登入 Azure

使用 Azure CLI az login 命令或 Azure PowerShell Connect-AzAccount Cmdlet 來登入 Azure。

az login

如果 CLI 或 PowerShell 可以開啟您的預設瀏覽器,其會執行這項操作,並載入 Azure 登入頁面。 否則,請造訪 https://aka.ms/devicelogin,並輸入顯示在終端機中的授權碼。

如果出現提示,請在瀏覽器中使用您的帳戶認證登入。

初始化新的 npm 專案

在終端機或命令提示字元中,建立適當的專案資料夾並初始化 npm 專案。 如果您有現有的節點專案,則可以略過此步驟。

cd <work folder>
npm init -y

安裝套件

安裝 Azure Active Directory 用戶端程式庫。

npm install --save @azure/identity

安裝 Azure 機密總帳管理平面用戶端程式庫。

npm install -save @azure/arm-confidentialledger@1.3.0-beta.1

全域安裝 TypeScript 編譯器和工具

npm install -g typescript

建立資源群組

資源群組是在其中部署與管理 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"

建立 JavaScript 應用程式

使用管理平面用戶端程式庫

Azure SDK for JavaScript 和 TypeScript 程式庫 azure/arm-confidentialledger 允許管理 CCF 資源的作業,例如建立及刪除、列出與訂用帳戶相關聯的資源,以及檢視特定資源的詳細資料。

若要執行下列範例,請將程式碼片段儲存到專案資料夾中具有 .ts 副檔名的檔案中,並將其編譯為您的 TypeScript 專案的一部分,或透過執行下列命令將指令碼個別編譯為 JavaScript:

tsc <filename.ts>

編譯後的 JavaScript 檔案將具有相同的名稱,但副檔名為 *.js。 然後在 nodeJS 中執行該指令碼:

node <filename.js>

下列範例 TypeScript 程式碼會建立及檢視受控 CCF 資源的屬性。

import  { ConfidentialLedgerClient, ManagedCCFProperties, ManagedCCF, KnownLanguageRuntime, DeploymentType, MemberIdentityCertificate } from "@azure/arm-confidentialledger";
import { DefaultAzureCredential } from "@azure/identity";

// Please replace these variables with appropriate values for your project
const subscriptionId = "0000000-0000-0000-0000-000000000001";
const rgName = "myResourceGroup";
const ledgerId = "testApp";
const memberCert0 = "-----BEGIN CERTIFICATE-----\nMIIBvjCCAUSgAwIBAg...0d71ZtULNWo\n-----END CERTIFICATE-----";
const memberCert1 = "-----BEGIN CERTIFICATE-----\nMIIBwDCCAUagAwIBAgI...2FSyKIC+vY=\n-----END CERTIFICATE-----";

async function main() {
    console.log("Creating a new instance.")
    const client = new ConfidentialLedgerClient(new DefaultAzureCredential(), subscriptionId);

    const properties = <ManagedCCFProperties> {
        deploymentType: <DeploymentType> {
            appSourceUri: "",
            languageRuntime: KnownLanguageRuntime.JS
        },
        memberIdentityCertificates: [
            <MemberIdentityCertificate>{
                certificate: memberCert0,
                encryptionkey: "",
                tags: { 
                    "owner":"member0"
                }
            },
            <MemberIdentityCertificate>{
                certificate: memberCert1,
                encryptionkey: "",
                tags: { 
                    "owner":"member1"
                }
            },
        ],
        nodeCount: 3,
    };

    const mccf = <ManagedCCF> {
        location: "SouthCentralUS",
        properties: properties,
    }

    const createResponse = await client.managedCCFOperations.beginCreateAndWait(rgName, ledgerId, mccf);
    console.log("Created. Instance id: " +  createResponse.id);

    // Get details of the instance
    console.log("Getting instance details.");
    const getResponse = await client.managedCCFOperations.get(rgName, ledgerId);
    console.log(getResponse.properties?.identityServiceUri);
    console.log(getResponse.properties?.nodeCount);

    // List mccf instances in the RG
    console.log("Listing the instances in the resource group.");
    const instancePages = await client.managedCCFOperations.listByResourceGroup(rgName).byPage();
    for await(const page of instancePages){
        for(const instance of page)
        {
            console.log(instance.name + "\t" + instance.location + "\t" + instance.properties?.nodeCount);
        }
    }

    console.log("Delete the instance.");
    await client.managedCCFOperations.beginDeleteAndWait(rgName, ledgerId);
    console.log("Deleted.");
}

(async () => {
    try {
        await main();
    } catch(err) {
        console.error(err);
    }
})();

刪除受控 CCF 資源

下列程式碼片段會刪除受控 CCF 資源。 其他受控 CCF 文章可以建置在本快速入門的基礎上。 如果您打算繼續進行後續的快速入門和教學課程,可以讓這些資源留在原處。

import  { ConfidentialLedgerClient, ManagedCCFProperties, ManagedCCF, KnownLanguageRuntime, DeploymentType, MemberIdentityCertificate } from "@azure/arm-confidentialledger";
import { DefaultAzureCredential } from "@azure/identity";

const subscriptionId = "0000000-0000-0000-0000-000000000001"; // replace
const rgName = "myResourceGroup";
const ledgerId = "confidentialbillingapp";

async function deleteManagedCcfResource() {
    const client = new ConfidentialLedgerClient(new DefaultAzureCredential(), subscriptionId);

    console.log("Delete the instance.");
    await client.managedCCFOperations.beginDeleteAndWait(rgName, ledgerId);
    console.log("Deleted.");
}

(async () => {
    try {
        await deleteManagedCcfResource();
    } catch(err) {
        console.error(err);
    }
})();

清除資源

其他受控 CCF 文章可以建置在本快速入門的基礎上。 如果您打算繼續進行後續的快速入門和教學課程,可以讓這些資源留在原處。

否則,當您完成本文中建立的資源時,請使用 Azure CLI az group delete 命令來刪除資源群組及其包含的所有資源。

az group delete --resource-group myResourceGroup

下一步

在本快速入門中,您使用適用於機密總帳的 Azure Python SDK 建立了受控 CCF 資源。 若要深入了解 Azure 受控 CCF 及如何與應用程式整合,請繼續閱讀下列文章: