Working with the REST API
Working with the REST APIs
Sometimes I need a zero-install way to interact with Azure. I have no specific Azure utilities at hand, no Python, no nothing. Usually, Azure management is done using PowerShell, the az cli or, if you want raw REST calls, the armclient. But for my customer, even can be too much ceremony.
So the question was how can I get going with purely bash, cURL and jq for JSON parsing, and potentially yq and xq for YAML/XML parsing.
#!/bin/bash
# Proper install
sudo apt-get -y install jq
sudo pip install yq
# YOLO
curl \
--silent \
--url https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 \
--location \
--output ./jq
chmod +x ./jq
sudo mv ./jq /usr/local/bin
sudo chown root.root /usr/local/bin/jq
If you're running inside a VM, with Managed Identity enabled, you can easily fetch a token. But unfortunately the VM wasn't authorized to hit the resource I care about.
Next stop service principals. Problem is customer's AD admin team running a tough regime, and don't hand out service principals.
So ultimately, how can I get my actual AAD user identity avail in the shell? In the end, all I need is a bearer token.
Let's dive right in:
A few variables first
I want to authN against 'my' Azure AD tenant, and want to hit the Azure ARM REST API.
Doing a device login (AAD v2)
For the full user login, i.e. device authN, here's what happens under the hood: The code needs to fetch a device code, and then use that code to poll and validate whether the user authenticated.
Using a service principal (AAD v1)
Assuming we have a 'real' service principal, we can do this:
Create an AAD app with a specified password
The underlying GraphAPI call for creating an app with a given password
Even though this says that Adding passwordCredential when creating applications is not supported., and the sample shows an empty "passwordCredentials": [] array, the call to az ad app create --display-name "${display_name}" --password "${client_secret}" exactly populates that property.
Using managed VM identity (running inside an Azure VM) (AAD v1)
Fetch the subscription ID, from the Azure VM's instance metadata endpoint
Invoke the ARM API, for example with a listing of resource groups
Fetching a secret from Azure KeyVault using a managed identity
This little script demonstrates how to fetch a secret from an Azure KeyVault, using a managed identity on an Azure VM. Just adapt key_vault_name and secret_name accordingly, and of course ensure that the managed identity can actually read the secret.
Force the instance metadata service to skip the token cache
Use the bypass_cache=true parameter when fetching a token from IMDS.
Shutdown a VM, quite radically (skip graceful shutdown, just turn it off)
The skipShutdown=true below is useful in STONITH scenarios.
Talking to Azure Blob Storage
Uploading a blob
Commit suicide using managed identity
Last updated