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

Changes to scripts to deal with PATs and AccessTokens #8340

Merged
merged 9 commits into from
May 28, 2024
13 changes: 9 additions & 4 deletions eng/common/scripts/Add-RetentionLease.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ param(
[string]$OwnerId = "azure-sdk-pipeline-automation",

[Parameter(Mandatory = $false)]
[string]$AccessToken = $env:DEVOPS_PAT
JimSuplizio marked this conversation as resolved.
Show resolved Hide resolved
[string]$AccessToken = $env:DEVOPS_PAT,

[Parameter(Mandatory = $false)]
[string]$AuthToken=$null
)

Set-StrictMode -Version 3

. (Join-Path $PSScriptRoot common.ps1)

$encodedAuthToken = Get-Base64EncodedToken $AccessToken
if (![string]::IsNullOrWhiteSpace($AccessToken)) {
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
$encodedAuthToken = Get-Base64EncodedToken $AccessToken
}

LogDebug "Checking for existing leases on run: $RunId"
$existingLeases = Get-RetentionLeases -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -Base64EncodedAuthToken $encodedAuthToken
Expand All @@ -36,11 +41,11 @@ if ($existingLeases.count -ne 0) {

foreach ($lease in $existingLeases.value) {
LogDebug "Deleting lease: $($lease.leaseId)"
Delete-RetentionLease -Organization $Organization -Project $Project -LeaseId $lease.leaseId -Base64EncodedAuthToken $encodedAuthToken
Delete-RetentionLease -Organization $Organization -Project $Project -LeaseId $lease.leaseId -Base64EncodedAuthToken $encodedAuthToken -AccessToken $AuthToken
}

}

LogDebug "Creating new lease on run: $RunId"
$lease = Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64EncodedAuthToken $encodedAuthToken
$lease = Add-RetentionLease -Organization $Organization -Project $Project -DefinitionId $DefinitionId -RunId $RunId -OwnerId $OwnerId -DaysValid $DaysValid -Base64EncodedAuthToken $encodedAuthToken -AccessToken $AuthToken
LogDebug "Lease ID is: $($lease.value.leaseId)"
74 changes: 52 additions & 22 deletions eng/common/scripts/Invoke-DevOpsAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@ function Get-Base64EncodedToken([string]$AuthToken)
return $encodedAuthToken
}

function Get-DevOpsApiHeaders ($Base64EncodedToken) {
$headers = @{
Authorization = "Basic $Base64EncodedToken"
# The Base64EncodedToken would be from a PAT that was passed in and the header requires Basic authorization
# The AccessToken would be the querying the Azure resource with the following command:
# az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" --query "accessToken" --output tsv
# The header for an AccessToken requires Bearer authorization
function Get-DevOpsApiHeaders ($Base64EncodedToken, $AccessToken) {
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
$headers = $null
if (![string]::IsNullOrWhiteSpace($Base64EncodedToken) -and
![string]::IsNullOrWhiteSpace($AccessToken)) {
LogError "Get-DevOpsApiHeaders::Unable to set the Authentication in the header because Base64EncodedToken and AccessToken are both set and only one should be."
exit 1
}
weshaggard marked this conversation as resolved.
Show resolved Hide resolved
if (![string]::IsNullOrWhiteSpace($Base64EncodedToken)) {
$headers = @{
Authorization = "Basic $Base64EncodedToken"
}
} elseif (![string]::IsNullOrWhiteSpace($AccessToken)) {
$headers = @{
Authorization = "Bearer $AccessToken"
}
} else {
LogError "Get-DevOpsApiHeaders::Unable to set the Authentication in the header because neither Base64EncodedToken nor AccessToken are set."
exit 1
}
return $headers
}
Expand All @@ -30,9 +49,8 @@ function Start-DevOpsBuild {
$SourceBranch,
[Parameter(Mandatory = $true)]
$DefinitionId,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$Base64EncodedAuthToken,
$Base64EncodedAuthToken=$null,
$AccessToken=$null,
[Parameter(Mandatory = $false)]
[string]$BuildParametersJson
)
Expand All @@ -45,11 +63,13 @@ function Start-DevOpsBuild {
parameters = $BuildParametersJson
}

$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken -AccessToken $AccessToken)

return Invoke-RestMethod `
-Method POST `
-Body ($parameters | ConvertTo-Json) `
-Uri $uri `
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
-Headers $headers `
-MaximumRetryCount 3 `
-ContentType "application/json"
}
Expand All @@ -62,21 +82,22 @@ function Update-DevOpsBuild {
[Parameter(Mandatory = $true)]
$BuildId,
$Status, # pass canceling to cancel build
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$Base64EncodedAuthToken
$Base64EncodedAuthToken,
$AccessToken
)

$uri = "$DevOpsAPIBaseURI" -F $Organization, $Project, "build", "builds/$BuildId", ""
$parameters = @{}

if ($Status) { $parameters["status"] = $Status}

$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken -AccessToken $AccessToken)

return Invoke-RestMethod `
-Method PATCH `
-Body ($parameters | ConvertTo-Json) `
-Uri $uri `
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
-Headers $headers `
-MaximumRetryCount 3 `
-ContentType "application/json"
}
Expand All @@ -88,9 +109,8 @@ function Get-DevOpsBuilds {
$BranchName, # Should start with 'refs/heads/'
$Definitions, # Comma seperated string of definition IDs
$StatusFilter, # Comma seperated string 'cancelling, completed, inProgress, notStarted'
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$Base64EncodedAuthToken
$Base64EncodedAuthToken,
$AccessToken
)

$query = ""
Expand All @@ -100,10 +120,12 @@ function Get-DevOpsBuilds {
if ($StatusFilter) { $query += "statusFilter=$StatusFilter&" }
$uri = "$DevOpsAPIBaseURI" -F $Organization, $Project , "build" , "builds", $query

$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken -AccessToken $AccessToken)

return Invoke-RestMethod `
-Method GET `
-Uri $uri `
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
-Headers $headers `
-MaximumRetryCount 3
}

Expand All @@ -112,15 +134,18 @@ function Delete-RetentionLease {
$Organization,
$Project,
$LeaseId,
$Base64EncodedAuthToken
$Base64EncodedAuthToken,
$AccessToken
)

$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ids=$LeaseId&api-version=6.0-preview.1"

$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken -AccessToken $AccessToken)

return Invoke-RestMethod `
-Method DELETE `
-Uri $uri `
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
-Headers $headers `
-MaximumRetryCount 3
}

Expand All @@ -131,15 +156,18 @@ function Get-RetentionLeases {
$DefinitionId,
$RunId,
$OwnerId,
$Base64EncodedAuthToken
$Base64EncodedAuthToken,
$AccessToken
)

$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?ownerId=$OwnerId&definitionId=$DefinitionId&runId=$RunId&api-version=6.0-preview.1"

$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken -AccessToken $AccessToken)

return Invoke-RestMethod `
-Method GET `
-Uri $uri `
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
-Headers $headers `
-MaximumRetryCount 3
}

Expand All @@ -151,7 +179,8 @@ function Add-RetentionLease {
$RunId,
$OwnerId,
$DaysValid,
$Base64EncodedAuthToken
$Base64EncodedAuthToken,
$AccessToken
)

$parameter = @{}
Expand All @@ -165,12 +194,13 @@ function Add-RetentionLease {

$uri = "https://dev.azure.com/$Organization/$Project/_apis/build/retention/leases?api-version=6.0-preview.1"

$headers = (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken -AccessToken $AccessToken)

return Invoke-RestMethod `
-Method POST `
-Body "[$body]" `
-Uri $uri `
-Headers (Get-DevOpsApiHeaders -Base64EncodedToken $Base64EncodedAuthToken) `
-Headers $headers `
-MaximumRetryCount 3 `
-ContentType "application/json"

}
14 changes: 10 additions & 4 deletions eng/common/scripts/Queue-Pipeline.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ param(
[string]$VsoQueuedPipelines,

# Already base 64 encoded authentication token
[string]$Base64EncodedAuthToken,
[string]$Base64EncodedAuthToken=$null,
weshaggard marked this conversation as resolved.
Show resolved Hide resolved

# Unencoded authentication token
[string]$AuthToken,
# Unencoded authentication token from a PAT
[string]$AuthToken=$null,

# Temp access token from the logged in az cli user for azure devops resource
[string]$AccessToken=$null,
weshaggard marked this conversation as resolved.
Show resolved Hide resolved

[Parameter(Mandatory = $false)]
[string]$BuildParametersJson
Expand All @@ -71,7 +74,9 @@ param(

if (!$Base64EncodedAuthToken)
{
$Base64EncodedAuthToken = Get-Base64EncodedToken $AuthToken
if (![string]::IsNullOrWhiteSpace($AuthToken)) {
$Base64EncodedAuthToken = Get-Base64EncodedToken $AuthToken
}
}

# Skip if SourceBranch is empty because it we cannot generate a target branch
Expand Down Expand Up @@ -105,6 +110,7 @@ try {
-SourceBranch $SourceBranch `
-DefinitionId $DefinitionId `
-Base64EncodedAuthToken $Base64EncodedAuthToken `
-AccessToken $AccessToken `
-BuildParametersJson $BuildParametersJson
}
catch {
Expand Down
57 changes: 57 additions & 0 deletions eng/pipelines/pipeline-queue-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
trigger: none

pr: none

jobs:
- job: Run
pool:
name: azsdk-pool-mms-ubuntu-2204-general
vmImage: ubuntu-22.04
variables:
ToolsCODEOWNERSLinterId: 6597
steps:
- template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
- task: AzureCLI@2
displayName: Test Authenticate to OpenSource API and queue pipeline
inputs:
azureSubscription: opensource-api-connection
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$accessToken = az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" --query "accessToken" --output tsv
eng/common/scripts/Queue-Pipeline.ps1 `
-Organization "azure-sdk" `
-Project "public" `
-DefinitionId "$(ToolsCODEOWNERSLinterId)" `
-AccessToken $accessToken

# # This task is going to become obsolete once the PATs go away
# # the queueing PAT will be gone first but there's another PAT
# # for queuing docs and this task is just testing the pipeline
# # scripts. This task will need to be commented out or removed.
# - task: PowerShell@2
# displayName: Test Queue Pipeline with PAT
# inputs:
# pwsh: true
# filePath: eng/common/scripts/Queue-Pipeline.ps1
# arguments: >
# -Organization "azure-sdk"
# -Project "public"
# -DefinitionId "$(ToolsCODEOWNERSLinterId)"
# -AuthToken "$(azuresdk-azure-sdk-devops-build-queuing-pat)"

- task: PowerShell@2
displayName: Test Retain pipeline run
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
pwsh: true
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Add-RetentionLease.ps1
arguments: >
-Organization azure-sdk
-Project $(System.TeamProject)
-DefinitionId $(System.DefinitionId)
-RunId $(Build.BuildId)
-DaysValid 7
-AccessToken $env:SYSTEM_ACCESSTOKEN
-Debug