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

Add APIs to programatically change Tenant ID and Cloud #805

Open
fjakobs opened this issue May 10, 2023 · 21 comments
Open

Add APIs to programatically change Tenant ID and Cloud #805

fjakobs opened this issue May 10, 2023 · 21 comments
Assignees

Comments

@fjakobs
Copy link

fjakobs commented May 10, 2023

For the Databricks Extension for VSCode, we would like to have a programmatic way to change the Cloud and the Tenant ID.

We are currently resorting to these hacks:

    private async setTenantId(): Promise<void> {
        const config: WorkspaceConfiguration =
            workspace.getConfiguration("azure");

        await config.update(
            "tenant",
            this.tenantId,
            ConfigurationTarget.Workspace
        );

        await commands.executeCommand("azure-account.login");
    }
    private async setCloud(): Promise<boolean> {
        const cloud = this.getAzureCloud(this.host);

        const config: WorkspaceConfiguration =
            workspace.getConfiguration("azure");
        if (config.get("cloud") !== cloud) {
            await commands.executeCommand("azure-account.logout", true);
            await config.update("cloud", cloud, ConfigurationTarget.Workspace);
            await commands.executeCommand("azure-account.login");
            return false;
        }
        return true;
    }

This kind of works but it's ugly and we have no way to get any feedback if the command succeeded.

@fjakobs
Copy link
Author

fjakobs commented May 10, 2023

CC @isidorn, @binderjoe

@isidorn
Copy link

isidorn commented May 10, 2023

@fjakobs thanks for the ping.
Looks like a duplicate of microsoft/vscode#115626

fyi @TylerLeonhardt

@TylerLeonhardt
Copy link
Member

The DataBricks extension uses the Azure Account extension for Auth, so I can't help too much here. I'll defer to @bwateratmsft. That said, in the built-in Microsoft auth extension, you can do this with a special scope we introduced. It's something I wanna change in the future for something more supported, but anyway.

@bwateratmsft
Copy link
Contributor

In the near future we plan to deprecate the Azure Account extension in favor of the built-in authentication provider. We intend to create an NPM package that will do essentially the same thing the Azure Account extension is doing, and we'll leave the Azure Account extension available as long as possible (but the ADAL shutdown deadline can't be pushed back forever...).

I'll include some control of tenant selection and cloud in that library's design.

@bwateratmsft bwateratmsft self-assigned this May 10, 2023
@fjakobs
Copy link
Author

fjakobs commented May 16, 2023

@bwateratmsft Do you have a rough timeline for the library that uses the building authentication provider? I might just wait for that if it is not too far out.

@bwateratmsft
Copy link
Contributor

I'm hoping to get it done in the next month or so.

@bwateratmsft
Copy link
Contributor

Here's the PR, by the way: microsoft/vscode-azuretools#1461

@fjakobs
Copy link
Author

fjakobs commented Jul 25, 2023

@bwateratmsft I see that the PR has been merged. Is it ready for me to try out? Also is there any kind of documentation or an example that I can look at?

@bwateratmsft
Copy link
Contributor

The package is available on NPM, it contains documentation on the (relatively few) methods available. https://www.npmjs.com/package/@microsoft/vscode-azext-azureauth

There's a more complex example: microsoft/vscode-azureresourcegroups#707
I'll look around for a simple example or just make one...

@bwateratmsft
Copy link
Contributor

It contains an API for changing the cloud but not tenant. We tried to make it so that when you signed in, all subscriptions across all tenants accessible to that account would be shown.

@bwateratmsft
Copy link
Contributor

A very simple example:

import { AzureSubscription, VSCodeAzureSubscriptionProvider } from '@microsoft/vscode-azext-azureauth';

const subscriptionProvider = new VSCodeAzureSubscriptionProvider();

if (!await subscriptionProvider.isSignedIn()) {
    await subscriptionProvider.signIn();
}

const subscriptions = await subscriptionProvider.getSubscriptions();

for (const subscription of subscriptions) {
    // ...
}

@fjakobs
Copy link
Author

fjakobs commented Jul 25, 2023

Fantastic, thanks a lot.

@fjakobs
Copy link
Author

fjakobs commented Jul 26, 2023

I got a bit further but now I'm stuck configuring an app ID. For Azure Databricks we need to get a token for the app ID 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d and I can't figure out how to do this with that library.

What I need is the equivalent of az account get-access-token --resource 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d.

I've tried

const session = await subscription.authentication.getSession([appId]);

but then I was getting this error when using the Azure Databricks APIs:

'Error 400 io.jsonwebtoken.IncorrectClaimException: Expected aud claim to be: 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d, but was: https://management.azure.com.'

@bwateratmsft
Copy link
Contributor

@TylerLeonhardt is it possible to get a token for a specific app using the auth provider?

@TylerLeonhardt
Copy link
Member

TylerLeonhardt commented Jul 26, 2023

Yeah see this comment:

microsoft/vscode#115626 (comment)

Basically you wanna do VSCODE_CLIENT_ID:<GUID> as one of the scopes

@bwateratmsft
Copy link
Contributor

@fjakobs can you try that?

const session = await subscription.authentication.getSession(['VSCODE_CLIENT_ID:2ff814a6-3304-4ab8-85cb-cd0e6f879c1d']);

@fjakobs
Copy link
Author

fjakobs commented Jul 27, 2023

This doesn't work either. When I call getSession like that is still get a session that doesn't have the required scopes:

'https://management.azure.com/.default'
'VSCODE_TENANT:<XXX>'

@TylerLeonhardt searching for VSCODE_CLIENT_ID in node_modules doesn't give any results. I assume I'm not hitting that code path.

@bwateratmsft
Copy link
Contributor

Since your auth case is somewhat specialized you may want to just directly use the Microsoft auth provider, https://code.visualstudio.com/api/references/vscode-api#authentication and https://github.com/microsoft/vscode/tree/main/extensions/microsoft-authentication have some more information on that.

@fjakobs
Copy link
Author

fjakobs commented Jul 28, 2023

I think I'm stuck. using the Mircosoft auth directly with this code:

const session = await authentication.getSession("microsoft", [`VSCODE_CLIENT_ID:2ff814a6-3304-4ab8-85cb-cd0e6f879c1d`], {forceNewSession: true});

I'm getting

Screenshot 2023-07-28 at 15 30 43

Since AzureDatabricks is an enterprise application I have also no way to change the redirect. Any ideas?

@bwateratmsft
Copy link
Contributor

@TylerLeonhardt this is beyond my expertise, do you know how to configure things?

@fjakobs
Copy link
Author

fjakobs commented Jul 28, 2023

I was able to get it to work with a bit of hacking with the azure-account extension but then I couldn't get China to work.

https://github.com/databricks/databricks-vscode/pull/538/files#diff-21b56f855403d4486bb9dae3c0bd1ce7e8c1d5d6470d05fa9145a530a462036aR287-R329

So in principle it should be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants