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

auth: Refactor AzureDevOpsSubscriptionProvider so that it accepts values as arguments #1729

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions auth/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion auth/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@microsoft/vscode-azext-azureauth",
"author": "Microsoft Corporation",
"version": "2.4.0",
"version": "2.4.1",
"description": "Azure authentication helpers for Visual Studio Code",
"tags": [
"azure",
Expand Down
48 changes: 31 additions & 17 deletions auth/src/AzureDevOpsSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@

import type { SubscriptionClient, TenantIdDescription } from '@azure/arm-resources-subscriptions';
import type { TokenCredential } from '@azure/core-auth'; // Keep this as `import type` to avoid actually loading the package (at all, this one is dev-only)
import type { PipelineRequest } from '@azure/core-rest-pipeline';
import { Disposable, Event } from 'vscode';
import { AzureAuthentication } from './AzureAuthentication';
import { AzureSubscription } from './AzureSubscription';
import { AzureSubscriptionProvider } from './AzureSubscriptionProvider';
import { getConfiguredAzureEnv } from './utils/configuredAzureEnv';
import type { PipelineRequest } from '@azure/core-rest-pipeline';

export interface AzureDevOpsSubscriptionProviderInitializer {
/**
* The resource ID of the Azure DevOps federated service connection,
* which can be found on the `resourceId` field of the URL at the address bar
* when viewing the service connection in the Azure DevOps portal
*/
serviceConnectionId: string,
/**
* The `Tenant ID` field of the service connection properties
*/
domain: string,
/**
* The `Service Principal Id` field of the service connection properties
*/
clientId: string;
}

let azureDevOpsSubscriptionProvider: AzureDevOpsSubscriptionProvider | undefined;
export function createAzureDevOpsSubscriptionProviderFactory(): () => Promise<AzureDevOpsSubscriptionProvider> {
export function createAzureDevOpsSubscriptionProviderFactory(initializer: AzureDevOpsSubscriptionProviderInitializer): () => Promise<AzureDevOpsSubscriptionProvider> {
return async (): Promise<AzureDevOpsSubscriptionProvider> => {
azureDevOpsSubscriptionProvider ??= new AzureDevOpsSubscriptionProvider();
azureDevOpsSubscriptionProvider ??= new AzureDevOpsSubscriptionProvider(initializer);
return azureDevOpsSubscriptionProvider;
}
}
Expand Down Expand Up @@ -43,22 +60,19 @@ export class AzureDevOpsSubscriptionProvider implements AzureSubscriptionProvide
*/
private _CLIENT_ID: string;

public constructor() {
const SERVICE_CONNECTION_ID = process.env.AzCodeServiceConnectionID;
const DOMAIN = process.env.AzCodeServiceConnectionDomain;
const CLIENT_ID = process.env.AzCodeServiceConnectionClientID;

if (!SERVICE_CONNECTION_ID || !DOMAIN || !CLIENT_ID) {
throw new Error(`Azure DevOps federated service connection is not configured\n
process.env.AzCodeServiceConnectionID: ${SERVICE_CONNECTION_ID ? "✅" : "❌"}\n
process.env.AzCodeServiceConnectionDomain: ${DOMAIN ? "✅" : "❌"}\n
process.env.AzCodeServiceConnectionClientID: ${CLIENT_ID ? "✅" : "❌"}\n
`);
public constructor({ serviceConnectionId, domain, clientId }: AzureDevOpsSubscriptionProviderInitializer) {
if (!serviceConnectionId || !domain || !clientId) {
throw new Error(`Missing initializer values to identify Azure DevOps federated service connection\n
Values provided:\n
serviceConnectionId: ${serviceConnectionId ? "✅" : "❌"}\n
domain: ${domain ? "✅" : "❌"}\n
clientId: ${clientId ? "✅" : "❌"}\n
`);
}

this._SERVICE_CONNECTION_ID = SERVICE_CONNECTION_ID;
this._DOMAIN = DOMAIN;
this._CLIENT_ID = CLIENT_ID;
this._SERVICE_CONNECTION_ID = serviceConnectionId;
this._DOMAIN = domain;
this._CLIENT_ID = clientId;
}

async getSubscriptions(_filter: boolean): Promise<AzureSubscription[]> {
Expand Down
Loading