次の方法で共有


Azure Stack Hub でアプリに Key Vault に格納されているシークレットへのアクセスを許可する

この記事の手順は、Azure Stack Hub のキー コンテナーからキーとシークレットを取得するサンプル アプリ HelloKeyVault を実行する方法を説明するものです。

前提条件

Azure Stack Development Kit から、または VPN 経由で接続している場合は Windows ベースの外部クライアントから、次の前提条件をインストールできます。

Key Vault の作成とアプリの登録

サンプル アプリケーションを準備するには:

  • Azure Stack Hub でキー コンテナーを作成します。
  • Microsoft Entra ID でアプリを登録します。

Azure portal または PowerShell を使用して、サンプル アプリを準備します。

Note

既定では、この PowerShell スクリプトによって、Active Directory に新しいアプリが作成されます。 ただし、既存のアプリケーションのいずれかを登録できます。

次のスクリプトを実行する前に、必ず aadTenantName 変数と applicationPassword 変数の値を指定します。 applicationPassword の値を指定しないと、このスクリプトによりランダムなパスワードが生成されます。

$vaultName           = 'myVault'
$resourceGroupName   = 'myResourceGroup'
$applicationName     = 'myApp'
$location            = 'local'

# Password for the application. If not specified, this script generates a random password during app creation.
$applicationPassword = ''

# Function to generate a random password for the application.
Function GenerateSymmetricKey()
{
    $key = New-Object byte[](32)
    $rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
    $rng.GetBytes($key)
    return [System.Convert]::ToBase64String($key)
}

Write-Host 'Please log into your Azure Stack Hub user environment' -foregroundcolor Green

$tenantARM = "https://management.local.azurestack.external"
$aadTenantName = "FILL THIS IN WITH YOUR AAD TENANT NAME. FOR EXAMPLE: myazurestack.onmicrosoft.com"

# Configure the Azure Stack Hub operator's PowerShell environment.
Add-AzEnvironment `
  -Name "AzureStackUser" `
  -ArmEndpoint $tenantARM

$TenantID = Get-AzsDirectoryTenantId `
  -AADTenantName $aadTenantName `
  -EnvironmentName AzureStackUser

# Sign in to the user portal.
Connect-AzAccount `
  -EnvironmentName "AzureStackUser" `
  -TenantId $TenantID `

$now = [System.DateTime]::Now
$oneYearFromNow = $now.AddYears(1)

$applicationPassword = GenerateSymmetricKey

# Create a new Azure AD application.
$identifierUri = [string]::Format("http://localhost:8080/{0}",[Guid]::NewGuid().ToString("N"))
$homePage = "https://contoso.com"

Write-Host "Creating a new AAD Application"
$ADApp = New-AzADApplication `
  -DisplayName $applicationName `
  -HomePage $homePage `
  -IdentifierUris $identifierUri `
  -StartDate $now `
  -EndDate $oneYearFromNow `
  -Password $applicationPassword

Write-Host "Creating a new AAD service principal"
$servicePrincipal = New-AzADServicePrincipal `
  -ApplicationId $ADApp.ApplicationId

# Create a new resource group and a key vault in that resource group.
New-AzResourceGroup `
  -Name $resourceGroupName `
  -Location $location

Write-Host "Creating vault $vaultName"
$vault = New-AzKeyVault -VaultName $vaultName `
  -ResourceGroupName $resourceGroupName `
  -Sku standard `
  -Location $location

# Specify full privileges to the vault for the application.
Write-Host "Setting access policy"
Set-AzKeyVaultAccessPolicy -VaultName $vaultName `
  -ObjectId $servicePrincipal.Id `
  -PermissionsToKeys all `
  -PermissionsToSecrets all

Write-Host "Paste the following settings into the app.config file for the HelloKeyVault project:"
'<add key="VaultUrl" value="' + $vault.VaultUri + '"/>'
'<add key="AuthClientId" value="' + $servicePrincipal.ApplicationId + '"/>'
'<add key="AuthClientSecret" value="' + $applicationPassword + '"/>'
Write-Host

次の画像は、キー コンテナーの作成に使用されたスクリプトからの出力を示しています。

キー コンテナーとアクセス キー

上のスクリプトによって返される VaultUrlAuthClientIdAuthClientSecret の値をメモしておきます。 これらの値は、HelloKeyVault アプリケーションを実行するために使用します。

サンプル アプリケーションのダウンロードと構成

Azure Key Vault client samples (Azure Key Vault クライアントのサンプル)」ページから、キー コンテナーのサンプルをダウンロードします。 .zip ファイルの内容を自分の開発ワークステーションに抽出します。 samples フォルダーには 2 つのアプリがあります。この記事では、HelloKeyVault を使用します。

HelloKeyVault サンプルを読み込むには:

  1. Microsoft.Azure.KeyVault.Samples>samples>HelloKeyVault フォルダーを参照します。
  2. Visual Studio でHelloKeyVault アプリを開きます。

サンプル アプリケーションを構成する

Visual Studio で次の操作を行います。

  1. HelloKeyVault\App.config ファイルを開き、<appSettings> 要素を見つけます。

  2. キー コンテナーの作成時に返された値を使用して VaultUrlAuthClientId、および AuthCertThumbprint キーを更新します。 既定では、App.config ファイルには AuthCertThumbprint のプレースホルダーが含まれます。 このプレースホルダーを AuthClientSecret に置き換えます。

    <appSettings>
     <!-- Update these settings for your test environment -->
     <add key="VaultUrl" value="URL to your Vault" />
     <add key="AuthClientId" value="Client Id of your Service Principal" />
     <add key="AuthCertThumbprint" value="Thumbprint of the certificate used for authentication" />
     <add key="TracingEnabled" value="false" />
    </appSettings>
    
  3. ソリューションをリビルドします。

アプリを実行する

HelloKeyVault を実行すると、アプリは Microsoft Entra ID にサインインし、トークンをAuthClientSecret使用して Azure Stack Hub のキー コンテナーに対して認証を行います。

HelloKeyVault サンプルを使用して、以下の操作を行います。

  • キーおよびシークレットで作成、暗号化、ラップ、削除などの基本操作を行います。
  • encryptdecrypt などのパラメーターを HelloKeyVault に渡して、指定した変更をキー コンテナーに適用します。

次のステップ