Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Websites Kudu REST API - Authentication

I'm trying to use PowerShell to put an updated content file onto an Azure Website via the REST API. However, when supplying my credentials into Invoke-RestMethod -Credentials I am returned the HTML of the standard Azure login page.

How can I authenticate with Kudu from PowerShell? Thanks.

like image 231
Luke Puplett Avatar asked Dec 12 '14 12:12

Luke Puplett


People also ask

How do I access Kudu Azure Web App?

Access Kudu for your app This Kudu app is accessible at: App not in Isolated tier: https://<app-name>.scm.azurewebsites.net. Internet-facing app in Isolated tier (App Service Environment): https://<app-name>.scm.<ase-name>.p.azurewebsites.net.

Which essential elements can we access using Azure kudu?

The Environment tab in Kudu Dash shows some very valuable information including System Information, App Settings, Connection Strings, Environment Variables, PATH, HTTP Headers and Server Variables.

What is the URL for the Azure App Service Kudu companion app?

We can access the Kudu service through the portal by navigating to Web App dashboard > Advanced Tools > Click on Go. If you've mapped your own public DNS name to your web app, then you'll still need to use the original *. azurewebsites.net DNS name to access Kudu.


2 Answers

You can first get the website via Powershell and then use the publish credentials from the website to call the Kudu REST API. The example below will get the Kudu version.

$website = Get-AzureWebsite -Name "WebsiteName"

$username = $website.PublishingUsername
$password = $website.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"

$kuduVersion = Invoke-RestMethod -Uri "$apiBaseUrl/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
like image 179
Seth Avatar answered Sep 28 '22 04:09

Seth


In the new ARM world and with the latest PowerShell, you'll need to make some adjustments to @Seth's answer.

Specifically, the way you obtain the publishing creds is different, which is the first 3 lines. The rest I shamelessly copied from @Seth to complete the snippet.

Make sure to replace YourResourceGroup/YourWebApp as appropriate:

$creds = Invoke-AzureRmResourceAction -ResourceGroupName YourResourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName YourWebApp/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force

$username = $creds.Properties.PublishingUserName
$password = $creds.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"

$kuduVersion = Invoke-RestMethod -Uri "$apiBaseUrl/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
like image 41
David Ebbo Avatar answered Sep 28 '22 03:09

David Ebbo