Sam*_*Sam 7 azure azure-cli azure-functions
我正在尝试使用Azure cli编写环境脚本.我创建了一些功能应用程序,并希望添加主机密钥或至少检索自动创建的默认密钥.天蓝色的cli根本没有任何支持.
似乎有一个api(文档似乎很稀疏)在函数本身允许我获取键,但是你需要一个键来使用它所以..没有帮助那里.
https://github.com/Azure/azure-webjobs-sdk-script/wiki/Key-management-API
例如:https://example-functions.azurewebsites.net/admin/host/keys? code = somecodeyoualreadyknow
我已经看到一些使用webapps scm api下载包含密钥的json文件的其他示例但是我不确定如何使用此API进行身份验证.我有一个服务主体(userid,密码,tenantid),我希望不必在我的脚本中添加另一个身份验证方案.
这是步骤.
这是一个powershell脚本,演示了从Kudu部署信用卡到功能主密钥的确切调用:
# 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
Run Code Online (Sandbox Code Playgroud)
我只能使用以下命令通过 Azure CLI 完成这项工作:
az rest --method post --uri \
"/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Web/sites/$FUNCTION_APP_NAME/host/default/listKeys?api-version=2018-11-01" \
--query functionKeys.default --output tsv
Run Code Online (Sandbox Code Playgroud)
我意识到这比答案晚了几年,但它可能会帮助现在正在搜索的人。
我不知道如何使用服务主体凭据获取“ kudu”凭据
如果可以接受C#代码,则可以使用Microsoft.Azure.Management.ResourceManager.Fluent和Microsoft.Azure.Management.Fluent轻松做到这一点。以下是演示如何获取kudu凭据并运行密钥管理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;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您只想获取密钥而不需要自动化身份验证过程:
Get-AzResource -Name RESOURCE-NAME | Invoke-AzResourceAction -Action host/default/listkeys -Force
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3247 次 |
| 最近记录: |