Allow user to pick credentials when using insert resource function
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Bicep version**
`Bicep CLI version 0.4.1124 (66c84c8ee5)`
**Describe the bug**
If I'm signed in to one tenant in Azure CLI and a different tenant in Azure PowerShell and try to use insert resource function it will try to authenticate using the first credential type based on my `credentialPrecedence` in `bicepconfig.json` file. If no custom order is specified it will always use Azure CLI first. If the resource Id that I use with the insert resource function is not from the same tenant as my primary credential type I will get an authentication error although I am authenticated:
```
Caught exception fetching resource: The access token is from the wrong issuer 'https://sts.windows.net/896ecbea-bd27-4a3c-a131-34aa0b46a086/'. It must match the tenant 'https://sts.windows.net/d259a616-4e9d-4615-b83d-2e09a6636fd4/' associated with this subscription. Please use the authority (URL) 'https://login.windows.net/d259a616-4e9d-4615-b83d-2e09a6636fd4' to get the token. Note, if the subscription is transferred to another tenant there is no impact to the services, but information about new tenant could take time to propagate (up to an hour). If you just transferred your subscription and see this error message, please try back later. Status: 401 (Unauthorized) ErrorCode: InvalidAuthenticationTokenTenant Content: {"error":{"code":"InvalidAuthenticationTokenTenant","message":"The access token is from the wrong issuer 'https://sts.windows.net/896ecbea-bd27-4a3c-a131-34aa0b46a086/'. It must match the tenant 'https://sts.windows.net/d259a616-4e9d-4615-b83d-2e09a6636fd4/' associa...
```
As an end user I get no error message telling me which set of credential was used during authentication. And I need to either logout from one scripting tool or create a `bicepconfig.json` file and change the `credentialPrecedence` and retry the command.
**To Reproduce**
1. Create a `bicepconfig.json` file and set the `credentialPrecedence` to:
```
"credentialPrecedence": [
"AzurePowerShell",
"AzureCLI",
"VisualStudioCode"
]
```
2. Sign in with a user account from Tenant A using Azure PowerShell.
3. Sign in with a user account from Tenant B using Azure CLI.
4. Fetch a resource ID from a subscription in Tenant B and use it with the insert resource function.
**Additional context**
I have the following suggestion on improvements:
- When using the Insert resource function in VSCode. Allow the user to pick the credentials to use when a resource ID has been pasted if multiple tokens exist.
- Or if the authentication fails with error `ErrorCode: InvalidAuthenticationTokenTenant` try the next credential type in line.
Contributor guide
Research direction
Start by tracing the VS Code Insert resource flow and how bicepconfig.json credentialPrecedence selects AzurePowerShell, AzureCLI, or VisualStudioCode. Reproduce the cross-tenant authentication case from the listed steps, then determine how the selected credential or fallback behavior should be exposed to the user and verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, powershell, vscode
- Domain
- authentication, developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100