Skip to content

Commit cae4f72

Browse files
authored
Merge pull request #225536 from EdB-MSFT/refresh-rest-api-walkthrough
Refresh REST API walkthrough
2 parents 30f558f + fe144d4 commit cae4f72

File tree

6 files changed

+261
-257
lines changed

6 files changed

+261
-257
lines changed
39.6 KB
Loading
74.9 KB
Loading

articles/azure-monitor/essentials/rest-api-walkthrough.md

Lines changed: 189 additions & 253 deletions
Large diffs are not rendered by default.

articles/azure-monitor/logs/api/register-app-for-token.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ To access Azure REST APIs such as the Log analytics API, or to send custom metri
1313

1414
## Register an App
1515

16+
Create a service principal and register an app using the Azure portal, Azure CLI, or PowerShell.
17+
### [Azure portal](#tab/portal)
18+
1619
1. To register an app, open the Active Directory Overview page in the Azure portal.
1720

1821
1. Select **App registrations** from the side bar.
@@ -38,18 +41,83 @@ To access Azure REST APIs such as the Log analytics API, or to send custom metri
3841
:::image type="content" source="../media/api-register-app/client-secret.png" alt-text="A screenshot showing the client secrets page.":::
3942

4043

44+
### [Azure CLI](#tab/cli)
45+
46+
47+
Run the following script to create a service principal and app.
48+
49+
```azurecli
50+
az ad sp create-for-rbac -n <Service principal display name>
51+
52+
```
53+
The response looks as follows:
54+
```JSON
55+
{
56+
"appId": "0a123b56-c987-1234-abcd-1a2b3c4d5e6f",
57+
"displayName": "AzMonAPIApp",
58+
"password": "123456.ABCDE.~XYZ876123ABcEdB7169",
59+
"tenant": "a1234bcd-5849-4a5d-a2eb-5267eae1bbc7"
60+
}
61+
62+
```
63+
>[!Important]
64+
> The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control.
65+
66+
Add a role and scope for the resources that you want to access using the API
67+
68+
```azurecli
69+
az role assignment create --assignee <`appId`> --role <Role> --scope <resource URI>
70+
```
71+
72+
The CLI following example assigns the `Reader` role to the service principal for all resources in the `rg-001`resource group:
73+
74+
```azurecli
75+
az role assignment create --assignee 0a123b56-c987-1234-abcd-1a2b3c4d5e6f --role Reader --scope '\/subscriptions/a1234bcd-5849-4a5d-a2eb-5267eae1bbc7/resourceGroups/rg-001'
76+
```
77+
For more information on creating a service principal using Azure CLI, see [Create an Azure service principal with the Azure CLI](https://learn.microsoft.com/cli/azure/create-an-azure-service-principal-azure-cli)
78+
79+
### [PowerShell](#tab/powershell)
80+
The following sample script demonstrates creating an Azure Active Directory service principal via PowerShell. For a more detailed walkthrough, see [using Azure PowerShell to create a service principal to access resources](../../../active-directory/develop/howto-authenticate-service-principal-powershell.md)
81+
82+
```powershell
83+
$subscriptionId = "{azure-subscription-id}"
84+
$resourceGroupName = "{resource-group-name}"
85+
86+
# Authenticate to a specific Azure subscription.
87+
Connect-AzAccount -SubscriptionId $subscriptionId
88+
89+
# Password for the service principal
90+
$pwd = "{service-principal-password}"
91+
$secureStringPassword = ConvertTo-SecureString -String $pwd -AsPlainText -Force
92+
93+
# Create a new Azure Active Directory application
94+
$azureAdApplication = New-AzADApplication `
95+
-DisplayName "My Azure Monitor" `
96+
-HomePage "https://localhost/azure-monitor" `
97+
-IdentifierUris "https://localhost/azure-monitor" `
98+
-Password $secureStringPassword
99+
100+
# Create a new service principal associated with the designated application
101+
New-AzADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
102+
103+
# Assign Reader role to the newly created service principal
104+
New-AzRoleAssignment -RoleDefinitionName Reader `
105+
-ServicePrincipalName $azureAdApplication.ApplicationId.Guid
106+
107+
```
108+
---
109+
41110
## Next steps
42111

43-
Before you can generate a token using your app, client ID, and secret, assign the app to a role using Access control (IAM) for resource that you want to access.
44-
The role will depend on the resource type and the API that you want to use.
112+
Before you can generate a token using your app, client ID, and secret, assign the app to a role using Access control (IAM) for resource that you want to access. The role will depend on the resource type and the API that you want to use.
45113
For example,
46114
- To grant your app read from a Log Analytics Workspace, add your app as a member to the **Reader** role using Access control (IAM) for your Log Analytics Workspace. For more information, see [Access the API](./access-api.md)
47115

48116
- To grant access to send custom metrics for a resource, add your app as a member to the **Monitoring Metrics Publisher** role using Access control (IAM) for your resource. For more information, see [ Send metrics to the Azure Monitor metric database using REST API](../../essentials/metrics-store-custom-rest-api.md)
49117

50-
For more information see [Assign Azure roles using the Azure portal](https://learn.microsoft.com/azure/role-based-access-control/role-assignments-portal)
118+
For more information, see [Assign Azure roles using the Azure portal](../../../role-based-access-control/role-assignments-portal.md)
51119

52-
Once you have assigned a role you can use your app, client ID, and client secret to generate a bearer token to access the REST API.
120+
Once you've assigned a role, you can use your app, client ID, and client secret to generate a bearer token to access the REST API.
53121

54122
> [!NOTE]
55123
> When using Azure AD authentication, it may take up to 60 minutes for the Azure Application Insights REST API to recognize new role-based access control (RBAC) permissions. While permissions are propagating, REST API calls may fail with error code 403.

0 commit comments

Comments
 (0)