Hi JPSac-3841,
Sorry for the late response.
Steps to use SAS token with keyvault and User assigned identity are below
- Create a key vault.
- Create a user-assigned managed identity.
- Assign Azure role-based access control (RBAC) Azure Maps Data Reader role to the user-assigned managed identity.
- Create an Azure Maps account with a Cross Origin Resource Sharing (CORS) configuration, and attach the user-assigned managed identity.
- Create and save a SAS token in the Azure Key Vault.
- Retrieve the SAS token secret from the key vault.
- Create an Azure Maps REST API request that uses the SAS token.
Reference -
- https://github.com/azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.maps/maps-use-sas
- https://free.blessedness.top/en-us/azure/azure-maps/how-to-secure-sas-app#example-scenario-sas-token-secure-storage
- Using CLI
az login
az provider register --namespace Microsoft.KeyVault
az provider register --namespace Microsoft.ManagedIdentity
az provider register --namespace Microsoft.Maps
$id = $(az rest --method GET --url 'https://graph.microsoft.com/v1.0/me?$select=id' --headers 'Content-Type=application/json' --query "id")
az group create --name <group-name> --location "East US"
$outputs = $(az deployment group create --name ExampleDeployment --resource-group <group-name> --template-file "./prereq.azuredeploy.json" --parameters objectId=$id --query "[properties.outputs.keyVaultName.value, properties.outputs.userAssignedIdentityPrincipalId.value, properties.outputs.userIdentityResourceId.value]" --output tsv)
az deployment group create --name ExampleDeployment --resource-group <group-name> --template-file "./azuredeploy.json" --parameters keyVaultName="$($outputs[0])" userAssignedIdentityPrincipalId="$($outputs[1])" userAssignedIdentityResourceId="$($outputs[2])" allowedOrigins="['http://localhost']" allowedRegions="['eastus', 'westus2', 'westcentralus']" maxRatePerSecond="10"
$secretId = $(az keyvault secret list --vault-name $outputs[0] --query "[? contains(name,'map')].id" --output tsv)
$sasToken = $(az keyvault secret show --id "$secretId" --query "value" --output tsv)
az rest --method GET --url 'https://us.atlas.microsoft.com/search/address/json?api-version=1.0&query=1 Microsoft Way, Redmond, WA 98052' --headers "Authorization=jwt-sas $($sasToken)" --query "results[].address"
Using Python Client
from azure.identity import DefaultAzureCredential
from azure.mgmt.maps import AzureMapsManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-maps
# USAGE
python account_list_sas.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = AzureMapsManagementClient(
credential=DefaultAzureCredential(),
subscription_id="21a9967a-e8a9-4656-a70b-96ff1c4d05a0",
)
response = client.accounts.list_sas(
resource_group_name="myResourceGroup",
account_name="myMapsAccount",
maps_account_sas_parameters={
"expiry": "2017-05-24T11:42:03.1567373Z",
"maxRatePerSecond": 500,
"principalId": "e917f87b-324d-4728-98ed-e31d311a7d65",
"regions": ["eastus"],
"signingKey": "primaryKey",
"start": "2017-05-24T10:42:03.1567373Z",
},
)
print(response)
# x-ms-original-file: specification/maps/resource-manager/Microsoft.Maps/stable/2023-06-01/examples/AccountListSAS.json
if __name__ == "__main__":
main()
Sample SAS token usage
async function getData(url = 'https://us.atlas.microsoft.com/search/address/json?api-version=1.0&query=1 Microsoft Way, Redmond, WA 98052') {
const response = await fetch(url, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': 'jwt-sas <your SAS token>',
}
});
return response.json(); // parses JSON response into native JavaScript objects
}
postData('https://us.atlas.microsoft.com/search/address/json?api-version=1.0&query=1 Microsoft Way, Redmond, WA 98052')
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
});
Hope it helps.
Thank you