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
393 stars 187 forks source link

[Issue] azd auth login in local vscode dev container fails to redirect back to vscode after auth #3230

Closed dfberry closed 7 months ago

dfberry commented 7 months ago

Output from azd version Run azd version and copy and paste the output here: azd version 1.5.1 (commit 3856d1e98281683b8d112e222c0a7c7b3e148e96)

Describe the bug azd auth login fails to redirect back to vscode dev container after auth.

To Reproduce

  1. open dev container (on mac m1)
  2. run azd auth login
  3. Select user identity
  4. URL changes to a redirect with a code param to vscode but screen shows

image

Expected behavior Redirect successfully to dev container

Environment Information on your environment:

image

image

Additional context Add any other context about the problem here.

ellismg commented 7 months ago

@vhvb1989 - I thought that VS Code was supposed to open the port on the local machine and forward it into the container so that the redirect would get forwarded back into the container. Am I remembering this right? I know there were some gaps in some cases, but I thought for local dev containers this Just Worked - do you know if this stopped working for some reason?

@dfberry - You might be able to use azd auth login --use-device-code since there we don't have to worry about the OAuth return back into the container where azd is running.

dfberry commented 7 months ago

Yes, I'm having to revert to device code but haven't had to use it in a while. @pamelafox is also having this issue. I run in to this where its devcontainers local or Codespaces. It does seem like vscode updated a few days ago which is when it started happening. If device code is required, the change will affect documentation across a wide variety of training and documentation because dev containers are moving toward day 1 experience for procedural docs.

vhvb1989 commented 7 months ago

Just tried now on Windows using the same VSCode version and WSL and it works fine.

@dfberry , as soon as you run azd auth login from VSCode terminal, you should see the PORT redirect is created like:

image

If you go to the PORTS tab, you should see the info about the port:

image

If this is not happening, there might be a bug on VSCode for Mac.

You can try creating the port by yourself (just to see if you get some error you can use to submit an issue to VSCode). There's 2 ways of doing:

vhvb1989 commented 7 months ago

@dfberry , can you also share your devcontainer? I can give it a try. sometimes the base container can be missing something required by VSCode for doing the re-direction

dfberry commented 7 months ago

@vhvb1989 - steps that worked

1) azd auth login (no params) 2) continue in browser, get the failed redirect, find the port in the url 3) go back to vscode and manually open port 4) go back to browser while still on failed redirect and rerequest that url 5) success

dev container "image": "mcr.microsoft.com/devcontainers/typescript-node:1-18-bullseye",

// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node
{
    "name": "Node.js & TypeScript",
    // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
    "image": "mcr.microsoft.com/devcontainers/typescript-node:1-18-bullseye",
    // Features to add to the dev container. More info: https://containers.dev/features.
    "features": {
        "ghcr.io/devcontainers/features/node:1": {},
        "ghcr.io/devcontainers/features/azure-cli:1": {},
        "ghcr.io/azure/azure-dev/azd:0": {},
        "ghcr.io/devcontainers/features/docker-in-docker:2": {},
        "ghcr.io/devcontainers/features/powershell:1": {},
        "ghcr.io/devcontainers/features/github-cli:1": {}
    },
    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    "forwardPorts": [
        3000,
        80,
        27017
    ],
    // add labels to ports
    "portsAttributes": {
        "3000": {
            "label": "API",
            "onAutoForward": "notify"
        },
        "80": {
            "label": "CLIENT",
            "onAutoForward": "notify"
        },
        "27017": {
            "label": "MONGO",
            "onAutoForward": "notify"
        }
    },
    // Use 'postCreateCommand' to run commands after the container is created but before prebuilds.
    "onCreateCommand": "bash .devcontainer/on-create-command.sh",
    // Use 'postAttachCommand" to run commands after the user is attached.
    "postAttachCommand": "bash .devcontainer/post-attach-command.sh",
    // Configure tool-specific properties.
    "customizations": {
        "vscode": {
            "extensions": [
                "GitHub.copilot-chat",
                "ms-playwright.playwright",
                "ms-azuretools.vscode-bicep",
                "ms-vscode.powershell",
                "ms-azuretools.vscode-docker",
                "mongodb.mongodb-vscode",
                "esbenp.prettier-vscode",
                "github.vscode-github-actions",
                "me-dutour-mathieu.vscode-github-actions"
            ]
        }
    },
    // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
    "remoteUser": "root"
}
# on create command
#! /bin/bash
chmod -R +x ./scripts

# dev container feature isn't working as expected - i do understand this shouldn't be necessary
curl -fsSL https://aka.ms/install-azd.sh | bash
# post-attach-command

#! /bin/bash

echo 'Installed Tool Versions:'
echo '------------------------'

echo 'Node ' $(node -v)
echo 'NPM version ' $(npm -v)
echo $(git -v)
echo $(docker --version)
vhvb1989 commented 7 months ago

So it means this is VSCode failing to auto-forward ports.

You might want to follow up with VSCode team about this. Here's some info about auto-redirect (including configuration to disable it which you might want to check if you have it) https://stackoverflow.com/questions/73741583/why-automatic-port-forwarding-how-to-prevent-vs-code-from-doing-it