Azure Service principal name - azure

May I know how in Azure CLI , to get back the name of the service principal name by the client ID >
And CLI command I can do that ?
I would like to see if I can get back the service principal name

I have reproduced from my end, Using the below Cli command you can get the output:
xxx-Clientid
az ad sp show --id xxx
Alternatively, Using below powershell command you can get :
Get-AzureADServicePrincipal
References taken from:
az ad sp | Microsoft Docs
https://learn.microsoft.com/en-us/powershell/module/az.resources/get-azadserviceprincipal?view=azps-8.2.0

Related

Enumerating Azure service principal using cli

I am testing an environment where I have the credentials of a service principal of an application. My next step is to identify the objects owned by the application or the resources that the app can access.
I could get similar details for an AD user through the cli command
ad signed-in-user list-owned-objects
Running the same command when signed-in with the credentials of the sp results in the following error
Resource not found for the segment 'me'.
My use-case is to enumerate the SP account to understand its access rights in the subscription. Can someone help me out with the right set of azure cli commands.
I tried to reproduce the same in my environment and got below results:
I created one service principal with Storage Blob Data Contributor role at storage account scope like below:
az ad sp create-for-rbac --role "Storage Blob Data Contributor" --name <spname> --scopes /subscriptions/<subID>/resourceGroups/<rgname>/providers/Microsoft.Storage/storageAccounts/<storaccname>
Now I logged in to Azure account successfully using above service principal credentials:
az login --service-principal -u appID -p password --tenant tenantID
When I ran the same command to get the resources that the app can access, I got same error as below:
az ad signed-in-user list-owned-objects
To list RBAC roles assigned to a service principal, you can make use of below command:
az role assignment list --assignee <service_principal_ID> --all
If your use case is to list all the resources/objects a service principal can access/own, currently there is no command available particularly for that.
To know more in detail, you can check below reference:
For a given Azure AD Service Principal, Get a list of the Azure Objects and Rights by AlfredoRevilla-MSFT

how to get the object id in azure web app using powershell command

I have enabled the identity in azure web app, it has created one object id. I want to pull the object id using PowerShell, like that I want to pull all the app service using powershell command
You could use this command:
(Get-AzWebApp -Name <Your_WebApp_Name> -ResourceGroupName <Your_ResourceGroup>).Identity.PrincipalId
As per this MSDoc, we have alternate command to get the Azure WebApp Service Principal ID.
Display name must be your WebApp Name
Get-AzADServicePrincipal -DisplayName "Core52022"
To check whether you got the correct ID, you can verify it in Azure Active Directory
Navigate to the Portal => Azure AD => Enterprise applications,remove all the filters and in search box type your Web App name
Finally I got the answer for this. I try to get my identity id in my webapp, so to get this using below command in PowerShell
Command:
az webapp identity show --name yourwebappname --resource-group resrourcegroupname --query principalId
Example:
az webapp identity show --name ph-prod-blue-portal --resource-group test_resource --query principalId
you will get the below result
"abc-def-ghi-ijk-lnmkop"

How to find SPNAME in Azure portal

Trying to access files from Azure Netapps via REST API by following documentation
https://learn.microsoft.com/en-us/azure/azure-netapp-files/azure-netapp-files-develop-with-rest-api
with POSTMAN rest client.
To get appId, password, tenant by executing following command in Azure CLI
az ad sp create-for-rbac --name $YOURSPNAMEGOESHERE --role Contributor --scopes /subscriptions/{subscription-id}
Here what is $YOURSPNAMEGOESHERE? How to fins this value? I am currently using trail account on Azure.
Please note that, the command you are currently using is related to creating service principal and assigning role to it.
In this $YOURSPNAMEGOESHERE , you have to pass the name of the service principal you want to create.
Instead of that, you can also give the name directly in string format like below:
I tried to reproduce the same in my environment like below and got the below results:
az ad sp create-for-rbac --name "TestSP" --role Contributor --scopes /subscriptions/subscriptionId
Output:
Reference:
az ad sp | Microsoft Docs

Assign contributor role to application using az cli

I am trying to assign contributor to app using following command:
az ad sp create-for-rbac -n "Terraform Cloud" --role Contributor --scopes /subscriptions/<id> --sdk-auth
I get following issue:
Changing "Terraform-Cloud" to a valid URI of
"http://Terraform-Cloud", which is the required format used for
service principal names Values of identifierUris property must use a
verified domain of the organization or its subdomain:
'http://Terraform-Cloud'
Any idea how to solve this?
Regarding to this link
Context
According to the announcement of
https://learn.microsoft.com/en-us/azure/active-directory/develop/reference-breaking-changes#appid-uri-in-single-tenant-applications-will-require-use-of-default-scheme-or-verified-domains,
AAD service will block creating single tenant applications with
invalid idefntifierUris after 2021-10-15.
Impact
Azure CLI command az ad sp create-for-rbac before 2.25.0 which
sets invalid idefntifierUris will fails with 400 badrequest, such as
Values of identifierUris property must use a verified domain of the
organization or its subdomain: 'http://azure-cli-2021-10-20-03-53-05'
Solution
az ad sp create-for-rbac has been updated so that now it
doesn't create identifierUris at all (#18312). Please update to Azure
CLI 2.25.0 or newer. We recommend always using the latest version.
So what I did is upgrading my az cli to latest version using az upgrade
When upgrade was done, I ran the same command that is mentioned in the question and all works as expected.

Azure CLI Get Current Service Principal Details in Azure Pipeline

I have Azure Pipeline setup with Azure CLI task
I am using Service Connection. In az cli task I want to retrieve current Service Connection details (like appId name objectId etc..) which I am using to run this task.
az ad sp show required id parameter, which will not work in my case. I want to know that id dynamically
Is there any way to get current SP details?
You can now use a checkbox to expose the service principal id, secret and tenant in the Azure CLI script.
You can get your current session context with
az account show
and then simply get some extra details on the service principal
az ad sp show --id <guid>
and application
az ad app show --id <guid>
I found Service principle is in Azure Active Directory. And i cannot add them to azure devops service connection. What i can add is Azure subscription or azure resource group.
Not sure show how did you add your applications to azure service connection.
If you want to get the connection details. You can try az accout show for azure subscription, or az group show --name for azure resource group
Ran into this same issue, and found out the simplest possible way to get the SP Id in Azure Cli task in the pipeline:
First, define in the task addSpnToEnvironment like this:
- task: AzureCLI#2
continueOnError: true
inputs:
addSpnToEnvironment: true
scriptLocation: inlineScript
After that, you can get the SP Id from $env:servicePrincipalId and use it like this:
az deployment group create ... --parameters SPId=$env:servicePrincipalId
Combine with Adam Marczak's solution with python in one liner,
az ad sp show --id $(az account show| python -c 'import json,sys;print json.load(sys.stdin)["user"]["name"]')

Resources