Skip to content

Commit

Permalink
CLeanup
Browse files Browse the repository at this point in the history
  • Loading branch information
praval-microsoft committed Nov 21, 2024
1 parent bf8fba4 commit 72ca44f
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 81 deletions.
48 changes: 1 addition & 47 deletions Tasks/Common/VstsAzureRestHelpers_/VstsAzureRestHelpers_.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function Build-MSALInstance {

Write-Verbose "ServiceConnectionId ${connectedServiceNameARM} and vstsAccessToken ${vstsAccessToken}"

$oidc_token = Get-VstsFederatedTokenModified -serviceConnectionId $connectedServiceNameARM -vstsAccessToken $vstsAccessToken
$oidc_token = Get-VstsFederatedToken -serviceConnectionId $connectedServiceNameARM -vstsAccessToken $vstsAccessToken

$msalClientInstance = $clientBuilder.WithClientAssertion($oidc_token).Build()
}
Expand Down Expand Up @@ -1360,52 +1360,6 @@ function ConvertTo-Pfx {
return $pfxFilePath, $pfxFilePassword
}

function Get-VstsFederatedTokenModified {
param(
[Parameter(Mandatory=$true)]
[string]$serviceConnectionId,
[Parameter(Mandatory=$true)]
[string]$vstsAccessToken
)

$uri = Get-VstsTaskVariable -Name 'System.CollectionUri' -Require
$planId = Get-VstsTaskVariable -Name 'System.PlanId' -Require
$jobId = Get-VstsTaskVariable -Name 'System.JobId' -Require
$hub = Get-VstsTaskVariable -Name 'System.HostType' -Require
$projectId = Get-VstsTaskVariable -Name 'System.TeamProjectId' -Require

Write-Host "URI $uri"
Write-Verbose "URI $uri"


# Construct the API URL
$url = $uri + "$projectId/_apis/distributedtask/hubs/$hub/plans/$planId/jobs/$jobId/oidctoken?serviceConnectionId=$serviceConnectionId&api-version=7.1-preview.1"
Write-Verbose $url

$headers = @{
"Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($vstsAccessToken)"))
"Content-Type" = "application/json"
}

# Make the POST request to generate the OIDC token
$response = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body

Write-Verbose "Response : $response"

# Parse the response content to extract the OIDC token
$responseContent = $response.Content | ConvertFrom-Json
Write-Verbose "responseContent : $responseContent"
$oidcToken = $responseContent.oidcToken # The token field contains the OIDC token


if ($null -eq $oidcToken -or $oidcToken -eq [string]::Empty) {
Write-Verbose "Failed to create OIDC token."
throw (New-Object System.Exception(Get-VstsLocString -Key AZ_CouldNotGenerateOidcToken))
}
Write-Verbose "Token generated $oidcToken"
return $oidcToken
}

function Get-VstsFederatedToken {
param(
[Parameter(Mandatory=$true)]
Expand Down
58 changes: 52 additions & 6 deletions Tasks/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,45 @@ Import-Module Microsoft.PowerShell.Security -Global

$env:SystemAccessTokenPowershellV2 = Get-VstsTaskVariable -Name 'System.AccessToken' -Require

function Get-VstsFederatedToken {
param(
[Parameter(Mandatory=$true)]
[string]$serviceConnectionId,
[Parameter(Mandatory=$true)]
[string]$vstsAccessToken
)


$uri = Get-VstsTaskVariable -Name 'System.CollectionUri' -Require
$planId = Get-VstsTaskVariable -Name 'System.PlanId' -Require
$jobId = Get-VstsTaskVariable -Name 'System.JobId' -Require
$hub = Get-VstsTaskVariable -Name 'System.HostType' -Require
$projectId = Get-VstsTaskVariable -Name 'System.TeamProjectId' -Require

# Construct the API URL
$url = $uri + "$projectId/_apis/distributedtask/hubs/$hub/plans/$planId/jobs/$jobId/oidctoken?serviceConnectionId=$serviceConnectionId&api-version=7.1-preview.1"

$headers = @{
"Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($vstsAccessToken)"))
"Content-Type" = "application/json"
}

# Make the POST request to generate the OIDC token
$response = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body

# Parse the response content to extract the OIDC token
$responseContent = $response.Content | ConvertFrom-Json
$oidcToken = $responseContent.oidcToken # The token field contains the OIDC token


if ($null -eq $oidcToken -or $oidcToken -eq [string]::Empty) {
Write-Debug "Failed to create OIDC token."
throw (New-Object System.Exception(Get-VstsLocString -Key AZ_CouldNotGenerateOidcToken))
}

return $oidcToken
}

function Get-ActionPreference {
param (
[Parameter(Mandatory)]
Expand All @@ -34,6 +73,7 @@ function Get-ActionPreference {
}

class FileBasedToken {

[void] run($filePath) {
$signalFromUserScript = "Global\SignalFromUserScript"
$signalFromTask = "Global\SignalFromTask"
Expand Down Expand Up @@ -431,10 +471,16 @@ catch {
Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow
}
finally {
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
try{
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
}
catch {
Write-Debug "Exception catch while exiting the Task script. Exception Message : $_"
}

}
2 changes: 1 addition & 1 deletion Tasks/PowerShellV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 249,
"Patch": 248
"Patch": 256
},
"releaseNotes": "Script task consistency. Added support for macOS and Linux.",
"minimumAgentVersion": "2.115.0",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/PowerShellV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 249,
"Patch": 248
"Patch": 256
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "2.115.0",
Expand Down
Binary file added _build.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions _generated/PowerShellV2.versionmap.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Default|2.249.248
Node20-225|2.249.249
Default|2.249.256
Node20-225|2.249.257
58 changes: 52 additions & 6 deletions _generated/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,45 @@ Import-Module Microsoft.PowerShell.Security -Global

$env:SystemAccessTokenPowershellV2 = Get-VstsTaskVariable -Name 'System.AccessToken' -Require

function Get-VstsFederatedToken {
param(
[Parameter(Mandatory=$true)]
[string]$serviceConnectionId,
[Parameter(Mandatory=$true)]
[string]$vstsAccessToken
)


$uri = Get-VstsTaskVariable -Name 'System.CollectionUri' -Require
$planId = Get-VstsTaskVariable -Name 'System.PlanId' -Require
$jobId = Get-VstsTaskVariable -Name 'System.JobId' -Require
$hub = Get-VstsTaskVariable -Name 'System.HostType' -Require
$projectId = Get-VstsTaskVariable -Name 'System.TeamProjectId' -Require

# Construct the API URL
$url = $uri + "$projectId/_apis/distributedtask/hubs/$hub/plans/$planId/jobs/$jobId/oidctoken?serviceConnectionId=$serviceConnectionId&api-version=7.1-preview.1"

$headers = @{
"Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($vstsAccessToken)"))
"Content-Type" = "application/json"
}

# Make the POST request to generate the OIDC token
$response = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body

# Parse the response content to extract the OIDC token
$responseContent = $response.Content | ConvertFrom-Json
$oidcToken = $responseContent.oidcToken # The token field contains the OIDC token


if ($null -eq $oidcToken -or $oidcToken -eq [string]::Empty) {
Write-Verbose "Failed to create OIDC token."
throw (New-Object System.Exception(Get-VstsLocString -Key AZ_CouldNotGenerateOidcToken))
}

return $oidcToken
}

function Get-ActionPreference {
param (
[Parameter(Mandatory)]
Expand All @@ -34,6 +73,7 @@ function Get-ActionPreference {
}

class FileBasedToken {

[void] run($filePath) {
$signalFromUserScript = "Global\SignalFromUserScript"
$signalFromTask = "Global\SignalFromTask"
Expand Down Expand Up @@ -431,10 +471,16 @@ catch {
Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow
}
finally {
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
try{
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
}
catch {
Write-Debug "Exception catch while exiting the Task script. Exception Message : $_"
}

}
6 changes: 3 additions & 3 deletions _generated/PowerShellV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 249,
"Patch": 248
"Patch": 256
},
"releaseNotes": "Script task consistency. Added support for macOS and Linux.",
"minimumAgentVersion": "2.115.0",
Expand Down Expand Up @@ -269,7 +269,7 @@
"ScriptArgsSanitized": "Detected characters in arguments that may not be executed correctly by the shell. Please escape special characters using backtick (`). More information is available here: https://aka.ms/ado/75787"
},
"_buildConfigMapping": {
"Default": "2.249.248",
"Node20-225": "2.249.249"
"Default": "2.249.256",
"Node20-225": "2.249.257"
}
}
6 changes: 3 additions & 3 deletions _generated/PowerShellV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 249,
"Patch": 248
"Patch": 256
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "2.115.0",
Expand Down Expand Up @@ -269,7 +269,7 @@
"ScriptArgsSanitized": "ms-resource:loc.messages.ScriptArgsSanitized"
},
"_buildConfigMapping": {
"Default": "2.249.248",
"Node20-225": "2.249.249"
"Default": "2.249.256",
"Node20-225": "2.249.257"
}
}
58 changes: 52 additions & 6 deletions _generated/PowerShellV2_Node20/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,45 @@ Import-Module Microsoft.PowerShell.Security -Global

$env:SystemAccessTokenPowershellV2 = Get-VstsTaskVariable -Name 'System.AccessToken' -Require

function Get-VstsFederatedToken {
param(
[Parameter(Mandatory=$true)]
[string]$serviceConnectionId,
[Parameter(Mandatory=$true)]
[string]$vstsAccessToken
)


$uri = Get-VstsTaskVariable -Name 'System.CollectionUri' -Require
$planId = Get-VstsTaskVariable -Name 'System.PlanId' -Require
$jobId = Get-VstsTaskVariable -Name 'System.JobId' -Require
$hub = Get-VstsTaskVariable -Name 'System.HostType' -Require
$projectId = Get-VstsTaskVariable -Name 'System.TeamProjectId' -Require

# Construct the API URL
$url = $uri + "$projectId/_apis/distributedtask/hubs/$hub/plans/$planId/jobs/$jobId/oidctoken?serviceConnectionId=$serviceConnectionId&api-version=7.1-preview.1"

$headers = @{
"Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($vstsAccessToken)"))
"Content-Type" = "application/json"
}

# Make the POST request to generate the OIDC token
$response = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body

# Parse the response content to extract the OIDC token
$responseContent = $response.Content | ConvertFrom-Json
$oidcToken = $responseContent.oidcToken # The token field contains the OIDC token


if ($null -eq $oidcToken -or $oidcToken -eq [string]::Empty) {
Write-Verbose "Failed to create OIDC token."
throw (New-Object System.Exception(Get-VstsLocString -Key AZ_CouldNotGenerateOidcToken))
}

return $oidcToken
}

function Get-ActionPreference {
param (
[Parameter(Mandatory)]
Expand All @@ -34,6 +73,7 @@ function Get-ActionPreference {
}

class FileBasedToken {

[void] run($filePath) {
$signalFromUserScript = "Global\SignalFromUserScript"
$signalFromTask = "Global\SignalFromTask"
Expand Down Expand Up @@ -431,10 +471,16 @@ catch {
Write-VstsSetResult -Result 'Failed' -Message "Error detected" -DoNotThrow
}
finally {
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
try{
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
}
catch {
Write-Debug "Exception catch while exiting the Task script. Exception Message : $_"
}

}
6 changes: 3 additions & 3 deletions _generated/PowerShellV2_Node20/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 249,
"Patch": 249
"Patch": 257
},
"releaseNotes": "Script task consistency. Added support for macOS and Linux.",
"minimumAgentVersion": "2.115.0",
Expand Down Expand Up @@ -273,7 +273,7 @@
"ScriptArgsSanitized": "Detected characters in arguments that may not be executed correctly by the shell. Please escape special characters using backtick (`). More information is available here: https://aka.ms/ado/75787"
},
"_buildConfigMapping": {
"Default": "2.249.248",
"Node20-225": "2.249.249"
"Default": "2.249.256",
"Node20-225": "2.249.257"
}
}
6 changes: 3 additions & 3 deletions _generated/PowerShellV2_Node20/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 249,
"Patch": 249
"Patch": 257
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "2.115.0",
Expand Down Expand Up @@ -273,7 +273,7 @@
"ScriptArgsSanitized": "ms-resource:loc.messages.ScriptArgsSanitized"
},
"_buildConfigMapping": {
"Default": "2.249.248",
"Node20-225": "2.249.249"
"Default": "2.249.256",
"Node20-225": "2.249.257"
}
}

0 comments on commit 72ca44f

Please sign in to comment.