Azure / Azure/azure-cli

Unexpected behaviour creating service principal with same name as managed identity

Open
#23,836 4 comments 1 reaction 1 assignee Claimed by @jiasli View on GitHub
Account act-identity-squad act-platform-engineering-squad Auto-Assign Azure CLI Team customer-reported feature-request Graph Installation
Dominant language
Python
Stars
4.6k
Forks
3.5k
Avg merge
3d 2h
Merged PRs (30d)
60

Description

> ### `az feedback` auto-generates most of the information requested below, as of CLI version 2.0.62

**Related command**

`az ad sp create-for-rbac`

**Describe the bug**

I tried to create a service principal with this command:

```
# az ad sp create-for-rbac --name deleteme-test-service-principal --role Contributor --scopes /subscriptions/a5fc4bf7-7f73-4c88-b291-f17475c9b9f3/resourceGroups/default/providers/Microsoft.ApiManagement/service/deleteme-test-service-principal
Creating 'Contributor' role assignment under scope '/subscriptions/a5fc4bf7-7f73-4c88-b291-f17475c9b9f3/resourceGroups/default/providers/Microsoft.ApiManagement/service/deleteme-test-service-principal'
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. For more information, see https://aka.ms/azadsp-cli
{
"appId": "bf75edc1-84c5-4e50-b1f4-891410cc98da",
"displayName": "deleteme-test-service-principal",
"password": "INl8Q~Ar.ecvgg3~7S5yB5QiAq5K0UBTFLG0wdnW",
"tenant": "eddf9755-4714-489e-b857-bfc6b3149491"
}
```

If a managed identity with the same name already exists, then instead of creating a new service principal it adds the managed identity service principal to the Contributor role for the APIM instance.

But managed identities can't be used to log in (right?) so if I then try to log in with the app registration using the credentials it returned, I get an error:

```
# az login --service-principal --username bf75edc1-84c5-4e50-b1f4-891410cc98da --password INl8Q~Ar.ecvgg3~7S5yB5QiAq5K0UBTFLG0wdnW --tenant eddf9755-4714-489e-b857-bfc6b3149491
(InvalidAuthenticationToken) The received access token is not valid: at least one of the claims 'puid' or 'altsecid' or 'oid' should be present. If you are accessing as application please make sure service principal is properly created in the tenant.
```

**However**, if I follow the exact same steps above when there is no managed identity, a new service principal is created, added to the Contributor role to the APIM instance, and then I'm able to use `az login --service-principal` to log in with the credentials as expected.

Or if the managed identity exists, using a different name works as expected.

**To Reproduce**

1. Create an APIM instance with a managed identity
2. Run `az ad sp create-for-rbac`
- Use the same `name` as the managed identity (same as the APIM instance)
3. Try to log in using the returned credentials and `az login --service-principal`

**Expected behavior**

I'm not sure what should happen ... Some ideas:

- Fail with a message that a managed identity with the same name exists already
- Or create a new service principal (unrelated to the managed identity) that can be used to log in
- Or maybe keep the same behaviour but don't include the password in the output (since it's useless) and add a message to that effect

**Environment summary**

Using Docker (`docker run --rm -it mcr.microsoft.com/azure-cli bash`)

```
# az --version
azure-cli 2.39.0 *

core 2.39.0 *
telemetry 1.0.6 *

Dependencies:
msal 1.18.0b1
azure-mgmt-resource 21.1.0b1

Python location '/usr/local/bin/python'
Extensions directory '/root/.azure/cliextensions'

Python (Linux) 3.10.5 (main, Jul 19 2022, 02:32:29) [GCC 11.2.1 20220219]
```

**Additional context**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.