refactor: replace requireVariable and requireInput with task-lib equivalents and improve error handling

This commit is contained in:
2026-02-25 21:12:40 +01:00
parent 8e9224cff9
commit 2c2981d791
10 changed files with 59 additions and 91 deletions

View File

@@ -1,18 +1,26 @@
import * as tl from 'azure-pipelines-task-lib/task';
import {
buildOidcUrl,
exchangeOidcForScopedToken,
getServiceConnectionMetadata,
requestOidcToken
} from './oidc';
import { requireVariable } from './devops-helpers';
export const STORAGE_SCOPE = 'https://storage.azure.com/.default';
export async function requestStorageAccessToken(
endpointId: string
): Promise<string> {
const oidcBaseUrl = requireVariable('System.OidcRequestUri');
const systemAccessToken = requireVariable('System.AccessToken');
const oidcBaseUrl = tl.getVariable('System.OidcRequestUri');
const systemAccessToken = tl.getVariable('System.AccessToken');
if (oidcBaseUrl === undefined) {
throw new Error('Missing required pipeline variable: System.OidcRequestUri.');
}
if (systemAccessToken === undefined) {
throw new Error('Missing required pipeline variable: System.AccessToken.');
}
const metadata = getServiceConnectionMetadata(endpointId);

View File

@@ -1,28 +0,0 @@
type TaskLibBridge = {
getInput: (name: string, required?: boolean) => string | undefined;
getVariable: (name: string) => string | undefined;
};
function getTaskLibBridge(): TaskLibBridge {
return require('azure-pipelines-task-lib/task') as TaskLibBridge;
}
export function requireInput(name: string): string {
const taskLib = getTaskLibBridge();
const value = taskLib.getInput(name, true);
if (!value) {
throw new Error(`Task input ${name} is required.`);
}
return value.trim();
}
export function requireVariable(name: string): string {
const taskLib = getTaskLibBridge();
const value = taskLib.getVariable(name);
if (!value) {
throw new Error(`Missing required pipeline variable: ${name}.`);
}
return value.trim();
}

View File

@@ -1,3 +1,2 @@
export * from './devops-helpers';
export * from './oidc';
export * from './blob';

View File

@@ -1,3 +1,5 @@
import * as tl from 'azure-pipelines-task-lib/task';
export type ServiceConnectionMetadata = {
tenantId: string;
clientId: string;
@@ -11,40 +13,25 @@ export type TokenResponse = {
export const CLIENT_ASSERTION_TYPE = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer';
type TaskLibEndpointBridge = {
getEndpointAuthorizationParameter: (
endpointId: string,
key: string,
optional: boolean
) => string | undefined;
getEndpointDataParameter: (endpointId: string, key: string, optional: boolean) => string | undefined;
};
type OidcResponse = {
oidcToken?: string;
};
function getTaskLibEndpointBridge(): TaskLibEndpointBridge {
return require('azure-pipelines-task-lib/task') as TaskLibEndpointBridge;
}
export function getServiceConnectionMetadata(endpointId: string): ServiceConnectionMetadata {
const taskLib = getTaskLibEndpointBridge();
const tenantId =
taskLib.getEndpointAuthorizationParameter(endpointId, 'tenantid', true) ||
taskLib.getEndpointDataParameter(endpointId, 'tenantid', true);
tl.getEndpointAuthorizationParameter(endpointId, 'tenantid', true) ||
tl.getEndpointDataParameter(endpointId, 'tenantid', true);
const clientId =
taskLib.getEndpointAuthorizationParameter(endpointId, 'serviceprincipalid', true) ||
taskLib.getEndpointAuthorizationParameter(endpointId, 'clientid', true) ||
taskLib.getEndpointDataParameter(endpointId, 'serviceprincipalid', true);
tl.getEndpointAuthorizationParameter(endpointId, 'serviceprincipalid', true) ||
tl.getEndpointAuthorizationParameter(endpointId, 'clientid', true) ||
tl.getEndpointDataParameter(endpointId, 'serviceprincipalid', true);
if (!tenantId) {
if (tenantId === undefined) {
throw new Error('Could not resolve tenant ID from the selected AzureRM service connection.');
}
if (!clientId) {
if (clientId === undefined) {
throw new Error('Could not resolve client ID from the selected AzureRM service connection.');
}