Skip to content

Commit 2d18846

Browse files
Sync eng/common directory with azure-sdk-tools for PR 5951 (Azure#35478)
* Switch to using standard PAT tokens instead of base 64 For most of these we can use the standard System.AccessToken given to the build instead of maintaining a specific token. However that token isn't base 64 encoded so we need to encode it. With this we can stop explicitly passing PAT's unless we need to access another DevOps org and we also don't have to remember to keep the PAT's in KV base 64 encoded. Add error detection for queue build script to fail if we get login response. * PR Feedback --------- Co-authored-by: Wes Haggard <[email protected]>
1 parent b773efd commit 2d18846

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

eng/common/scripts/Add-RetentionLease.ps1

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,7 @@ Set-StrictMode -Version 3
2626

2727
. (Join-Path $PSScriptRoot common.ps1)
2828

29-
$unencodedAuthToken = "nobody:$AccessToken"
30-
$unencodedAuthTokenBytes = [System.Text.Encoding]::UTF8.GetBytes($unencodedAuthToken)
31-
$encodedAuthToken = [System.Convert]::ToBase64String($unencodedAuthTokenBytes)
32-
33-
if ($isDevOpsRun) {
34-
# We are doing this here so that there is zero chance that this token is emitted in Azure Pipelines
35-
# build logs. Azure Pipelines will see this text and register the secret as a value it should *** out
36-
# before being transmitted to the server (and shown in logs). It means if the value is accidentally
37-
# leaked anywhere else that it won't be visible. The downside is that when the script is executed
38-
# on a local development box, it will be visible.
39-
Write-Host "##vso[task.setvariable variable=_throwawayencodedaccesstoken;issecret=true;]$($encodedAuthToken)"
40-
}
41-
29+
$encodedAuthToken = Get-Base64EncodedToken $AccessToken
4230

4331
LogDebug "Checking for existing leases on run: $RunId"
4432
$existingLeases = Get-RetentionLeases -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -Base64EncodedAuthToken $encodedAuthToken

eng/common/scripts/Invoke-DevOpsAPI.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
$DevOpsAPIBaseURI = "https://dev.azure.com/{0}/{1}/_apis/{2}/{3}?{4}api-version=6.0"
44

5+
function Get-Base64EncodedToken([string]$AuthToken)
6+
{
7+
$unencodedAuthToken = "nobody:$AuthToken"
8+
$unencodedAuthTokenBytes = [System.Text.Encoding]::UTF8.GetBytes($unencodedAuthToken)
9+
$encodedAuthToken = [System.Convert]::ToBase64String($unencodedAuthTokenBytes)
10+
11+
if (Test-SupportsDevOpsLogging) {
12+
# Mark the encoded value as a secret so that DevOps will star any references to it that might end up in the logs
13+
Write-Host "##vso[task.setvariable variable=_throwawayencodedaccesstoken;issecret=true;]$($encodedAuthToken)"
14+
}
15+
16+
return $encodedAuthToken
17+
}
18+
519
function Get-DevOpsApiHeaders ($Base64EncodedToken) {
620
$headers = @{
721
Authorization = "Basic $Base64EncodedToken"

eng/common/scripts/Queue-Pipeline.ps1

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pipeline.
1717
Pipline definition ID
1818
1919
.PARAMETER CancelPreviousBuilds
20-
Requires a value for SourceBranch. Cancel previous builds before queuing the new
20+
Requires a value for SourceBranch. Cancel previous builds before queuing the new
2121
build.
2222
2323
.PARAMETER VsoQueuedPipelines
@@ -55,18 +55,25 @@ param(
5555

5656
[boolean]$CancelPreviousBuilds=$false,
5757

58-
[Parameter(Mandatory = $false)]
5958
[string]$VsoQueuedPipelines,
6059

61-
[Parameter(Mandatory = $true)]
60+
# Already base 64 encoded authentication token
6261
[string]$Base64EncodedAuthToken,
6362

63+
# Unencoded authentication token
64+
[string]$AuthToken,
65+
6466
[Parameter(Mandatory = $false)]
6567
[string]$BuildParametersJson
6668
)
6769

6870
. (Join-Path $PSScriptRoot common.ps1)
6971

72+
if (!$Base64EncodedAuthToken)
73+
{
74+
$Base64EncodedAuthToken = Get-Base64EncodedToken $AuthToken
75+
}
76+
7077
# Skip if SourceBranch is empty because it we cannot generate a target branch
7178
# name from an empty string.
7279
if ($CancelPreviousBuilds -and $SourceBranch)
@@ -105,11 +112,16 @@ catch {
105112
exit 1
106113
}
107114

115+
if (!$resp.definition) {
116+
LogError "Invalid queue build response: $resp"
117+
exit 1
118+
}
119+
108120
LogDebug "Pipeline [ $($resp.definition.name) ] queued at [ $($resp._links.web.href) ]"
109121

110122
if ($VsoQueuedPipelines) {
111123
$enVarValue = [System.Environment]::GetEnvironmentVariable($VsoQueuedPipelines)
112-
$QueuedPipelineLinks = if ($enVarValue) {
124+
$QueuedPipelineLinks = if ($enVarValue) {
113125
"$enVarValue<br>[$($resp.definition.name)]($($resp._links.web.href))"
114126
}else {
115127
"[$($resp.definition.name)]($($resp._links.web.href))"

eng/common/scripts/logging.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
$isDevOpsRun = ($null -ne $env:SYSTEM_TEAMPROJECTID)
1+
function Test-SupportsDevOpsLogging()
2+
{
3+
return ($null -ne $env:SYSTEM_TEAMPROJECTID)
4+
}
25

36
function LogWarning
47
{
5-
if ($isDevOpsRun)
8+
if (Test-SupportsDevOpsLogging)
69
{
710
Write-Host "##vso[task.LogIssue type=warning;]$args"
811
}
@@ -14,23 +17,23 @@ function LogWarning
1417

1518
function LogError
1619
{
17-
if ($isDevOpsRun)
20+
if (Test-SupportsDevOpsLogging)
1821
{
1922
Write-Host "##vso[task.LogIssue type=error;]$args"
2023
}
21-
else
24+
else
2225
{
2326
Write-Error "$args"
2427
}
2528
}
2629

2730
function LogDebug
2831
{
29-
if ($isDevOpsRun)
32+
if (Test-SupportsDevOpsLogging)
3033
{
3134
Write-Host "[debug]$args"
3235
}
33-
else
36+
else
3437
{
3538
Write-Debug "$args"
3639
}

0 commit comments

Comments
 (0)