2017-09-21 10 views
4

Azure cliを使用して環境をスクリプト化しようとしています。私はいくつかの機能のアプリケーションを作成し、ホストキーを追加するか、または自動的に作成されたデフォルトのものを少なくとも取得したいと考えています。紺碧のcliはこれを全くサポートしていません。azure関数appからホスト鍵を取得する

私はキーを取得することができる関数自体にAPI(それは疎そうです)があるようですが、それを使用するにはキーが必要です。

https://github.com/Azure/azure-webjobs-sdk-script/wiki/Key-management-API

例:https://example-functions.azurewebsites.net/admin/host/keys?code=somecodeyoualreadyknow

私はこのAPIを使用して認証するかどうかはわかりませんが、キーが含まれているJSONファイルをダウンロードするにはWebアプリケーションのSCM APIを使用する他のいくつかの例を見てきました。私はサービスプリンシパル(ユーザーID、パスワード、テナントード)を持っていて、スクリプトに別の認証方式を追加する必要はないと考えていました。

答えて

3

ここに手順があります。

  1. すでにKuduの展開資格情報があるとします。 (これはあなたが既にこれを行う方法を知っているように聞こえます。あなたはサービスの原則からのARM呼び出しによって得ることができます)
  2. kuduデプロイメントクレジットから、関数キーAPIを呼び出すことができるJWTを取得できます。
  3. 関数APIから、すべてのキー(マスターを含む)を取得できます。

ここでマスターキーを機能するクーズー展開credsをから行くには、正確な呼び出しを示していPowerShellスクリプトです:

# You need to start with these: 
$site = "YourSiteName" 
$username='YourDeploymentUserName' 
$password='YourDeploymentPassword' 

# Now... 
$apiBaseUrl = "https://$($site).scm.azurewebsites.net/api" 
$siteBaseUrl = "https://$($site).azurewebsites.net" 

# For authenticating to Kudu 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password))) 


# Call Kudu /api/functions/admin/token to get a JWT that can be used with the Functions Key API 
$jwt = Invoke-RestMethod -Uri "$apiBaseUrl/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET 

# Call Functions Key API to get the master key 
$x = Invoke-RestMethod -Uri "$siteBaseUrl/admin/host/systemkeys/_master" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method GET 

$masterKey = $x.value 
+0

こんにちはから取り戻すだろうか、私はサービスプリンシパルの資格情報を持っているが、私はそれを理解すると、彼らは同じではありません。私はサービスプリンシパルの資格情報で "kudo"資格情報を取得する方法を知らない。 – Sam

1

私は私のサービスプリンシパルで「クーズー」の資格情報を取得する方法がわかりませんC#のコードが許容される場合は、資格情報

、我々は簡単にそれを行うためにMicrosoft.Azure.Management.ResourceManager.FluentMicrosoft.Azure.Management.Fluentを使用することができます。以下は、kuduの資格情報を取得し、Key management APIを実行する方法を示すデモです。ローカルでテストすると、正しく動作します。

string clientId = "client id"; 
string secret = "secret key"; 
string tenant = "tenant id"; 
var functionName ="functionName"; 
var webFunctionAppName = "functionApp name"; 
string resourceGroup = "resource group name"; 
var credentials = new AzureCredentials(new ServicePrincipalLoginInformation { ClientId = clientId, ClientSecret = secret}, tenant, AzureEnvironment.AzureGlobalCloud); 
var azure = Azure 
      .Configure() 
      .Authenticate(credentials) 
      .WithDefaultSubscription(); 

var webFunctionApp = azure.AppServices.FunctionApps.GetByResourceGroup(resourceGroup, webFunctionAppName); 
var ftpUsername = webFunctionApp.GetPublishingProfile().FtpUsername; 
var username = ftpUsername.Split('\\').ToList()[1]; 
var password = webFunctionApp.GetPublishingProfile().FtpPassword; 
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}")); 
var apiUrl = new Uri($"https://{webFunctionAppName}.scm.azurewebsites.net/api"); 
var siteUrl = new Uri($"https://{webFunctionAppName}.azurewebsites.net"); 
string JWT; 
using (var client = new HttpClient()) 
    { 
    client.DefaultRequestHeaders.Add("Authorization", $"Basic {base64Auth}"); 

    var result = client.GetAsync($"{apiUrl}/functions/admin/token").Result; 
    JWT = result.Content.ReadAsStringAsync().Result.Trim('"'); //get JWT for call funtion key 
    } 
using (var client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + JWT); 
    var key = client.GetAsync($"{siteUrl}/admin/functions/{functionName}/keys").Result.Content.ReadAsStringAsync().Result; 
    } 

enter image description here

0

おかげであなたの応答のための両方。あなたの答えであるMike Sを使い、csharpの流暢なソースコード(Tom Sunに感謝する)の周りをうかがい、私はこれで終わりました。もちろん、たくさんのトークンが必要です!私はで始まる資格情報を使用すると、az ad sp create-for-rbac -n $name --role contributor

$credentials = (ConvertFrom-Json $env:AzureCliLogin) 

$tenant = $credentials.tenant 
$clientId = $credentials.appId 
$clientSecret = $credentials.password 
$subscriptionId = "<subscription id>" 

$body = @{ 
    "grant_type"="client_credentials"; 
    "client_id"=$clientId; 
    "client_secret"=$clientSecret; 
    "resource"="https://management.azure.com/" 
} 

$authInfo = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenant/oauth2/token" -Body $body -Method Post -Headers @{"Content-Type"="application/x-www-form-urlencoded"} 

$publishData = Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$name/publishxml?api-version=2016-08-01" -Method Post -Headers @{"Authorization"="Bearer $($authInfo.access_token)"} 

$userName = $publishData.publishData.publishProfile[0].userName 
$password = $publishData.publishData.publishProfile[0].userPWD 

$apiBaseUrl = "https://$name.scm.azurewebsites.net/api" 
$siteBaseUrl = "https://$name.azurewebsites.net" 

# For authenticating to Kudu 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))  

# Call Kudu /api/functions/admin/token to get a JWT that can be used with the Functions Key API 
$jwt = Invoke-RestMethod -Uri "$apiBaseUrl/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET 

# Call Functions Key API to get the master key 
$x = Invoke-RestMethod -Uri "$siteBaseUrl/admin/host/systemkeys/_master" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method GET 

$masterKey = $x.value 
関連する問題