功能级别授权在Azure中授权密钥我们可以通过CICD管理这些密钥吗

时间:2019-02-22 12:52:29

标签: azure devops

我们可以通过CICD管理密钥吗? 意味着我需要通过CICD而不是门户网站或Rest Srevice来管理这些文件?

1 个答案:

答案 0 :(得分:1)

一切(最终)都通过rest api进行管理,所以这个问题没有什么意义。您只能使用rest calls(据我所知)进行管理。

function Add-AzureFunctionKey {

    Param(
        [string]$appName,
        [string]$resourceGroup,
        [string]$funcKeyName,
        [string]$funcKeyValue
    )

    $AzureContext = Get-AzureRmContext
    if(!$AzureContext){
        Write-Output "Please login to your Azure Account"
        Login-AzureRmAccount
    }
    $SubscriptionId = (Get-AzureRmSubscription | select Name, State, SubscriptionId, TenantId | Out-GridView -Title "Azure Subscription Selector" -PassThru).SubscriptionId
    Get-AzureRmSubscription -SubscriptionId $SubscriptionId | Select-AzureRmSubscription

    $PublishingProfile = (Get-AzureRmWebAppPublishingProfile -ResourceGroupName $resourceGroup -Name $appName)

    $user = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userName").Node.Value
    $pass = (Select-Xml -Xml $PublishingProfile -XPath "//publishData/publishProfile[contains(@profileName,'Web Deploy')]/@userPWD").Node.Value

    $pair = "$($user):$($pass)"
    $kuduCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

    $authToken = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization = ("Basic {0}" -f $kuduCredentials)} -Method GET

    $Functions = Invoke-RestMethod -Method GET -Headers @{Authorization = ("Bearer {0}" -f $authToken)} -Uri "https://$appName.azurewebsites.net/admin/functions"
    $Functions = $Functions.Name
    ForEach ($functionName in $Functions) {
        $data = @{ 
            "name"  = "$funcKeyName"
            "value" = "$funcKeyValue"
        }
        $json = $data | ConvertTo-Json;

        $keys = Invoke-RestMethod -Method PUT -Headers @{Authorization = ("Bearer {0}" -f $authToken)} -ContentType "application/json" -Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys/$funcKeyName" -body $json
        Write-Output "Function $FunctionName Key updated $keys"
    }
}

这是在网上找到的样本,我没有对其进行测试。网上有一些例子或多或少像上面的例子。

来源:https://www.powershellbros.com/add-azure-function-key/