Azure / azure-dev

A developer CLI that reduces the time it takes for you to get started on Azure. The Azure Developer CLI (azd) provides a set of developer-friendly commands that map to key stages in your workflow - code, build, deploy, monitor, repeat.
https://aka.ms/azd
MIT License
405 stars 195 forks source link

`azd login` should fallback to or hint at device code if no browser is found #1250

Open weikanglim opened 1 year ago

weikanglim commented 1 year ago

Currently, azd login fails with the following error if no browser is installed on Linux:

@weikanglim ➜ /workspaces/todo-python-mongo (main ✗) $ azd login

ERROR: logging in: exec: "xdg-open,x-www-browser,www-browser": executable file not found in $PATH

This would be a common scenario for devcontainers without xdg-utils installed.

We should either fallback to device code login or provide user hints to try device code login as a fallback.

KSchlobohm commented 1 year ago

@weikanglim +1 for WSL experience

pdebruin commented 1 year ago

Just tried this. Works smooth in codespaces. Should be like that in WSL too.

vhvb1989 commented 1 year ago

@pdebruin are you getting an error in WSL? Can you share it?

vhvb1989 commented 1 year ago

This would be a common scenario for devcontainers without xdg-utils installed.

@weikanglim we should update azd to use https://github.com/pkg/browser for opening a browser. This is what msal is using for interactive login. It would give us 2 more providers on linux:

func openBrowser(url string) error {
    providers := []string{"xdg-open", "x-www-browser", "www-browser"}

    // There are multiple possible providers to open a browser on linux
    // One of them is xdg-open, another is x-www-browser, then there's www-browser, etc.
    // Look for one that exists and run it
    for _, provider := range providers {
        if _, err := exec.LookPath(provider); err == nil {
            return runCmd(provider, url)
        }
    }

    return &exec.Error{Name: strings.Join(providers, ","), Err: exec.ErrNotFound}
}
vhvb1989 commented 1 year ago

Moving to https://github.com/pkg/browser will also help us during cloudshell interactive login

weikanglim commented 1 year ago

@vhvb1989 Do you see a custom usage in azd somewhere? IIRC, we are relying on msal, which in turn relies on pkg/browser.

pdebruin commented 1 year ago

@pdebruin are you getting an error in WSL? Can you share it?

=== Yes, see below:

$ azd init --template todo-java-mongo-aca

Initializing a new project (azd init)

(✓) Done: Initialized git repository (✓) Done: Downloading template code to: ~/_projects/azdwsl2

? Please enter a new environment name: azdwsl2-dev

SUCCESS: New project initialized! You can view the template code in your directory: ~/_projects/azdwsl2 Learn more about running 3rd party code on our DevHub: https://learn.microsoft.com/azure/developer/azure-developer-cli/azd-templates#guidelines-for-using-azd-templates

$ azd up Error: not logged in, run azd auth login to login

$ azd auth login /usr/bin/xdg-open: 882: x-www-browser: not found /usr/bin/xdg-open: 882: firefox: not found /usr/bin/xdg-open: 882: iceweasel: not found /usr/bin/xdg-open: 882: seamonkey: not found /usr/bin/xdg-open: 882: mozilla: not found /usr/bin/xdg-open: 882: epiphany: not found /usr/bin/xdg-open: 882: konqueror: not found /usr/bin/xdg-open: 882: chromium: not found /usr/bin/xdg-open: 882: chromium-browser: not found /usr/bin/xdg-open: 882: google-chrome: not found /usr/bin/xdg-open: 882: www-browser: not found /usr/bin/xdg-open: 882: links2: not found /usr/bin/xdg-open: 882: elinks: not found /usr/bin/xdg-open: 882: links: not found /usr/bin/xdg-open: 882: lynx: not found /usr/bin/xdg-open: 882: w3m: not found xdg-open: no method available for opening 'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=&code_challenge=&code_challenge_method=S256&prompt=select_account&redirect_uri=http%3A%2F%2Flocalhost%3A38713&response_type=code&scope=https%3A%2F%2Fmanagement.azure.com%2F%2F.default+openid+offline_access+profile&state='

ERROR: logging in: exit status 3

=== Tried to repro on second machine, which responded with

ERROR: logging in: exec: "xdg-open,x-www-browser,www-browser": executable file not found in $PATH

===

Hope that helps

pdebruin commented 1 year ago

Congrats on the release! 🎉 For completeness, anyone visiting this issue should get azd in wsl to work using: azd auth login --use-device-code azd auth login --check-status

weikanglim commented 1 year ago

@vhvb1989 Was WSL working with interactive browser?

vhvb1989 commented 1 year ago

Yes, it works, but it depends on xdg-open + BROWSER config or on installing wslu (https://github.com/wslutilities/wslu)

vhvb1989 commented 1 year ago

Also related: https://github.com/Azure/azure-dev/issues/2291

rajeshkamal5050 commented 1 year ago

@weikanglim @vhvb1989 is this issue still valid with @ellismg MSAL changes to override the package used for browser opening? - https://github.com/Azure/azure-dev/issues/2291#issuecomment-1561666398

GiuseppeGalilei commented 2 months ago

Apparently this isn't yet solved. Today I came across a similar issue (https://github.com/Azure-Samples/azure-search-openai-demo/issues/1855) executing azd auth login from a GitHub codespace terminal. As suggested here (https://github.com/Azure/azure-cli/issues/20315) azd auth login --use-device-code solved the issue.