Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

az login does not work in codespaces in browser #1006

Closed
zedy-wj opened this issue Oct 26, 2022 · 8 comments
Closed

az login does not work in codespaces in browser #1006

zedy-wj opened this issue Oct 26, 2022 · 8 comments
Labels

Comments

@zedy-wj
Copy link
Member

zedy-wj commented Oct 26, 2022

Describe the issue:
The command az login doesn't work in codespaces running in VSCode browser.

Repro Steps:

  1. Open any one of the templates.
  2. Click <> Code button and choose Codespaces.
  3. Click Create codespace on main and wait for setting up your codespace to complete.
  4. Install the latest daily version of azd with the command curl -fsSL https://aka.ms/install-azd.sh | bash -s -- --version daily.
  5. Execute the command az login and enter your account. Display error: This site can't be reached. localhost refused to connect.

Error Message:
image

Environment:
OS: Codespace.
Templates: All templates
azd version: azd version 0.3.0-beta.4-daily.1943817 (commit 75d070a7099a1691a96c2ea930b50b6de9fa0c78)

Expected behavior:
az login can work in codespaces.

@weikanglim, @rajeshkamal5050, @danieljurek for notification.

@pamelafox
Copy link
Member

I experience this as well, and that's why I assume it prompts the device code route when using azd. It'd be nice if az login did work, as it's a shorter flow than the device code flow.

@rajeshkamal5050
Copy link
Contributor

rajeshkamal5050 commented Oct 26, 2022

@zedy-wj have you tried azd login after step 4 and was able to use the device code flow?

@pamelafox just to clarify,

  • azd login detects its running in codespaces and triggers the device code flow? and able to get authenticated?
  • az login doesn't work from codespaces unless specified explicitly to use device code flow?

@pamelafox
Copy link
Member

@rajeshkamal5050 Yes, the logic seems to be here to detect codespace and using device code flow:

useDeviceCode := forceDeviceCode || os.Getenv(CodespacesEnvVarName) == "true" ||

Here's the relevant issue about az login not working in Codespaces:
Azure/azure-cli#20315

@zedy-wj
Copy link
Member Author

zedy-wj commented Oct 27, 2022

@rajeshkamal5050 - Yes, as you said, when logging in with azd login, the device code flow was triggered when it detected running in code space. Besides, during our testing, we have been using the two login methods az login --use-device-code and azd login, both of them can get authenticated successfully.

@weikanglim , @danieljurek - Do you have any ideas on this issue?

@vhvb1989
Copy link
Member

The issue is that az cli doesn't know that it is running on codespaces, and after authenticating the user, it is trying to use localhost to return a response to the device.

Here's a workaround for how to make it work:

  • After getting the page not found error (after entering the user credentials in the browser), look at the url address in the browsert and take note of the port number that is after the localhost:XXXX.:

image

Then go back to the codespaces window and go to the ports tab:

image

There should be an entry in the port list with the same port number as the one you saw in the URL address:

image

Note: The codespaces editor creates the port forwarding automatically.

  • Now, copy the local address from that record. You can do right click on the record and then select copy url path:

image

// Or you can also hover over the url and click on the copy button that pops up:

image

  • Now go back to the page with not found error (where you saw the localhost:XXX path) and replace the locahost:XXX for the value on your clipboard. Something like:

image

Then click enter. This should redirect the call to the codespaces container and the az cli would receive the response from the log in. The page should change to something like:

image

Then, go back to codespaces and to the terminal tab. The az cli should be now logged in. And the forwarded port is automatically removed.

// This has nothing to do with azd. I suggest we follow up with the az cli team to see if they can support codespaces by setting the login callback to use the local url address that is created by codespaces.

@vhvb1989
Copy link
Member

Note: The previous workaround is also applicable to:

  • Running VSCode with ssh-remote connection to a Linux VM. The only difference is that, on that scenario, the port forwarding is not created automatically, and there is not a local url address. Instead, we need to manually create the port forwarding with the port number and then just go back to the url and click enter (using locahost:).

@weikanglim
Copy link
Contributor

@zedy-wj I'm closing this as azd login does work now with codespaces, as you have verified, -- and we have the linked issue created for az cli.

@zedy-wj
Copy link
Member Author

zedy-wj commented Oct 31, 2022

@vhvb1989 - We follow the steps you provided to verify and az login can work, thanks!

@ghost ghost added the needs-triage For new issues label Nov 1, 2022
@weikanglim weikanglim changed the title [codespaces] login issue - az login does not work in codespaces az login does not work in codespaces in browser Dec 12, 2022
weikanglim added a commit that referenced this issue Dec 14, 2022
For users running Codespaces in browsers, interactive login does not work due to the localhost server being launched remotely with no forwarding. See #1006.

This switches the default login on Codespaces to device-code based which circumvents the need for the login redirect. User can still opt-out of decide-code via `azd login --use-device-code=false`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants