Retrieve the host keys from an azure function app

后端 未结 6 1710
梦如初夏
梦如初夏 2020-12-01 19:03

I am trying to script an environment using the Azure cli. I have created a few function apps and would like to add a host key or at least retrieve the default one that is cr

6条回答
  •  旧巷少年郎
    2020-12-01 20:07

    Here are the steps.

    1. Assuming you already have your Kudu deployment credentials. (it sounds like you already know how to do this. You can get it via an ARM call from your service principle, etc)
    2. From kudu deployment creds, you can get a JWT that lets you call the Functions key API.
    3. From the Functions API, you can get all your keys (including your master).

    Here's a powershell script that demonstrates the exact calls to go from Kudu deployment creds to Function Master key:

    # 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
    

提交回复
热议问题