From 0f14e8a1b4a52204d4301bbe1c22e2239f4e6950 Mon Sep 17 00:00:00 2001 From: James Suplizio Date: Tue, 4 Jun 2024 12:55:48 -0700 Subject: [PATCH] Remove PATVAR from PipelineGenerator (#8369) * Remove PATVAR from PipelineGenerator * Add temporarily hacked up yml to test and add output before/after connection auth * indent working directory * add / to beginning of the working directory * change working directory again * trying full path for working directory * try packing/installing separately * try packing/installing separately * try packing/installing separately * Use Azure.Identity and setup a ChainedTokenCred using AzureCLI and AzurePowerShell credentials * do the ConnectAsync in GetConnectionAsync because if there's a failure, it'll happen there not some random place * revert changes from pipeline-generation.yml --- .../Azure.Sdk.Tools.PipelineGenerator.csproj | 9 ++++--- .../CommandParserOptions/DefaultOptions.cs | 5 +--- .../PipelineGenerationContext.cs | 27 +++++++------------ .../Program.cs | 5 +--- 4 files changed, 16 insertions(+), 30 deletions(-) diff --git a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Azure.Sdk.Tools.PipelineGenerator.csproj b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Azure.Sdk.Tools.PipelineGenerator.csproj index f9daf50eae2..46365ac1404 100644 --- a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Azure.Sdk.Tools.PipelineGenerator.csproj +++ b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Azure.Sdk.Tools.PipelineGenerator.csproj @@ -14,13 +14,14 @@ + - - - - + + + + diff --git a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/CommandParserOptions/DefaultOptions.cs b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/CommandParserOptions/DefaultOptions.cs index e52c6c95fc5..38708fb19eb 100644 --- a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/CommandParserOptions/DefaultOptions.cs +++ b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/CommandParserOptions/DefaultOptions.cs @@ -25,10 +25,7 @@ public string Organization [Option('p', "project", Required = false, Default = "internal", HelpText = "Azure DevOps project name. Default: internal")] public string Project { get; set; } - [Option('t', "patvar", Required = false, HelpText = "Environment variable name containing a Personal Access Token.")] - public string Patvar { get; set; } - [Option("whatif", Required = false, HelpText = "Dry Run changes")] public bool WhatIf { get; set; } } -} \ No newline at end of file +} diff --git a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/PipelineGenerationContext.cs b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/PipelineGenerationContext.cs index 9d5588f6963..1ae76ea16fd 100644 --- a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/PipelineGenerationContext.cs +++ b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/PipelineGenerationContext.cs @@ -1,4 +1,5 @@ -using Microsoft.Azure.Services.AppAuthentication; +using Azure.Identity; +using Microsoft.Azure.Services.AppAuthentication; using Microsoft.Extensions.Logging; using Microsoft.TeamFoundation.Build.WebApi; using Microsoft.TeamFoundation.Core.WebApi; @@ -20,7 +21,6 @@ public class PipelineGenerationContext { private string organization; private string project; - private string patvar; private string endpoint; private string agentPool; private int[] variableGroups; @@ -30,7 +30,6 @@ public PipelineGenerationContext( ILogger logger, string organization, string project, - string patvar, string endpoint, string repository, string branch, @@ -46,7 +45,6 @@ public PipelineGenerationContext( this.logger = logger; this.organization = organization; this.project = project; - this.patvar = patvar; this.endpoint = endpoint; this.Repository = repository; this.Branch = branch; @@ -76,20 +74,13 @@ private async Task GetConnectionAsync() { if (cachedConnection == null) { - VssCredentials credentials; - if (string.IsNullOrWhiteSpace(patvar)) - { - var azureTokenProvider = new AzureServiceTokenProvider(); - var authenticationResult = await azureTokenProvider.GetAuthenticationResultAsync("499b84ac-1321-427f-aa17-267ca6975798"); - credentials = new VssAadCredential(new VssAadToken(authenticationResult.TokenType, authenticationResult.AccessToken)); - } - else - { - var pat = Environment.GetEnvironmentVariable(patvar); - credentials = new VssBasicCredential("nobody", pat); - } - - cachedConnection = new VssConnection(new Uri(organization), credentials); + var azureCredential = new ChainedTokenCredential( + new AzureCliCredential(), + new AzurePowerShellCredential() + ); + var devopsCredential = new VssAzureIdentityCredential(azureCredential); + cachedConnection = new VssConnection(new Uri(organization), devopsCredential); + await cachedConnection.ConnectAsync(); } return cachedConnection; diff --git a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Program.cs b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Program.cs index 3c6d7bcf5b1..e46c463ad9a 100644 --- a/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Program.cs +++ b/tools/pipeline-generator/Azure.Sdk.Tools.PipelineGenerator/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -43,7 +43,6 @@ public static async Task Run(object commandObj, CancellationTokenSource cancella g.Project, g.Prefix, g.Path, - g.Patvar, g.Endpoint, g.Repository, g.Branch, @@ -127,7 +126,6 @@ public async Task RunAsync( string project, string prefix, string path, - string patvar, string endpoint, string repository, string branch, @@ -154,7 +152,6 @@ public async Task RunAsync( this.logger, organization, project, - patvar, endpoint, repository, branch,