Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
praval-microsoft committed Dec 11, 2024
1 parent fb17807 commit f411a35
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 283 deletions.
4 changes: 1 addition & 3 deletions Tasks/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ try {
catch {
Write-Host "Something failed ; $_"
}


# Wait for the async runspace to start and get ready to listen to User scripts requests
Start-Sleep 30
Start-Sleep 20


# Get inputs.
$input_errorActionPreference = Get-ActionPreference -VstsInputName 'errorActionPreference' -DefaultAction 'Stop'
$input_warningPreference = Get-ActionPreference -VstsInputName 'warningPreference' -DefaultAction 'Default'
Expand Down
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": 250,
"Patch": 118
"Patch": 124
},
"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": 250,
"Patch": 118
"Patch": 124
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "2.115.0",
Expand Down
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.250.118
Node20-225|2.250.119
Default|2.250.124
Node20-225|2.250.125
129 changes: 6 additions & 123 deletions _generated/PowerShellV2/AccessTokenHelper.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
Import-Module $PSScriptRoot\ps_modules\VstsAzureRestHelpers_
Import-Module $PSScriptRoot\ps_modules\VstsTaskSdk

function Global:Get-VstsFederatedTokenPSV2Task {
param(
[Parameter(Mandatory=$true)]
Expand Down Expand Up @@ -31,12 +28,11 @@ function Global:Get-VstsFederatedTokenPSV2Task {


if ($null -eq $oidcToken -or $oidcToken -eq [string]::Empty) {
#Write-Verbose "Failed to create OIDC token."
$env:praval = $env:praval + "Failed to create OIDC token."
Write-Verbose "Failed to create OIDC token."
throw (New-Object System.Exception("CouldNotGenerateOidcToken"))
}

$env:praval = $env:praval + "OIDC Token generated Successfully"
Write-Verbose "OIDC Token generated Successfully"
return $oidcToken
}
New-Alias -Name 'Get-VstsFederatedToken' -Value 'Global:Get-VstsFederatedTokenPSV2Task' -Scope Global
Expand All @@ -46,21 +42,11 @@ function Global:Get-WiscAccessTokenPSV2Task {
[Parameter(Mandatory=$true)]
[string]$connectedServiceName
)

$env:praval = $env:praval + "`n" + "Inside Get-WiscAccessTokenPSV2Task"

$vstsEndpoint = Get-VstsEndpoint -Name $connectedServiceName -Require

$env:praval = $env:praval + "`n" + "$vstsEndpoint"

try {
$result = Get-AccessTokenMSALWithCustomScope -endpoint $vstsEndpoint `
-connectedServiceNameARM $connectedServiceName `
-scope "499b84ac-1321-427f-aa17-267ca6975798"
} catch {
$env:praval = $env:praval + "`n" + "Get-AccessTokenMSALWithCustomScope failed with $_"
throw $_
}

$token = $null
$expirationTime = $null
Expand All @@ -70,122 +56,19 @@ function Global:Get-WiscAccessTokenPSV2Task {
}

if ($null -eq $token -or $token -eq [string]::Empty) {
$env:praval = $env:praval + "Generated token found to be null, returning the System Access Token"
Write-Verbose "Generated token found to be null, returning the System Access Token"
$token = $env:SystemAccessTokenPowershellV2
} else {
$env:praval = $env:praval + "`n" + "Successfully generated the Azure Access token for Service Connection : $connectedServiceName"
Write-Verbose "Successfully generated the Azure Access token for Service Connection : $connectedServiceName"
if($expirationTime) {
$env:praval = $env:praval + "`n" + "Generated access token with expiration time of $expirationTime minutes."
Write-Host "Generated access token with expiration time of $expirationTime minutes."
}
}

return $token
}
New-Alias -Name 'Get-WiscAccessTokenPSV2Task' -Value 'Global:Get-WiscAccessTokenPSV2Task' -Scope Global

# $tokenHandler = [PSCustomObject]@{

# TokenHandler = {
# param(
# [Parameter(Mandatory=$true)]
# [string]$filePath,
# [Parameter(Mandatory=$true)]
# [string]$signalFromUserScript,
# [Parameter(Mandatory=$true)]
# [string]$signalFromTask,
# [Parameter(Mandatory=$true)]
# [string]$exitSignal
# )

# $eventFromUserScript = $null
# $eventFromTask = $null
# $eventExit = $null

# try {
# $eventFromUserScript = New-Object System.Threading.EventWaitHandle($false, [System.Threading.EventResetMode]::AutoReset, $signalFromUserScript)
# $eventFromTask = New-Object System.Threading.EventWaitHandle($false, [System.Threading.EventResetMode]::AutoReset, $signalFromTask)
# $eventExit = New-Object System.Threading.EventWaitHandle($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)

# # Ensure the output file has restricted permissions
# if (-not (Test-Path $filePath)) {
# New-Item -Path $filePath -ItemType File -Force
# $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

# # Create a new ACL that only grants access to the current user
# $acl = Get-Acl $filePath
# $acl.SetAccessRuleProtection($true, $false) # Disable inheritance
# $rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
# $currentUser, "FullControl", "Allow"
# )
# $acl.SetAccessRule($rule)

# # Apply the ACL to the file
# Set-Acl -Path $filePath -AclObject $acl
# } else {
# $env:praval = $env:praval + "`n" + "Token File not found"
# throw "Token File not found"
# }

# $env:praval = $env:praval + "`n" + "Task: Waiting for signals..."

# # Infinite loop to wait for signals and respond
# while ($true) {
# try {
# # Wait for either UserScript signal or Exit signal
# $index = [System.Threading.WaitHandle]::WaitAny(@($eventFromUserScript, $eventExit))

# if ($index -eq 0) {

# # Signal from UserScript
# $env:SystemAccessTokenPowershellV2 = Get-VstsTaskVariable -Name 'System.AccessToken' -Require
# $token = ""
# try {
# [string]$connectedServiceName = (Get-VstsInput -Name ConnectedServiceName)
# $env:praval = $env:praval + "`n" + "ConnectedServiceName $connectedServiceName"

# if ($null -eq $connectedServiceName -or $connectedServiceName -eq [string]::Empty) {
# $env:praval = $env:praval + "`n" + "No Service connection was found, returning the System Access Token"
# $token = $env:SystemAccessTokenPowershellV2
# } else {
# $token = Get-WiscAccessTokenPSV2Task -connectedServiceName $connectedServiceName
# $env:praval = $env:praval + "`n" + "Successfully generated the Azure Access token for Service Connection : $connectedServiceName"
# }
# }
# catch {
# $env:praval = $env:praval + "`n" + "Failed to generate token with message $_, returning the System Access Token"
# $token = $env:SystemAccessTokenPowershellV2

# } finally {
# $token | Set-Content -Path $filePath
# $env:praval = $env:praval + "`n" + "Task: Wrote access token to file"
# }

# # Signal UserScript to read the file
# $tmp = $eventFromTask.Set()

# } elseif ($index -eq 1) {
# $env:praval = $env:praval + "`n" + "Exiting the loop"
# # Exit signal received
# break
# }
# } catch {
# $env:praval = $env:praval + "`n" + "Error occurred while waiting for signals: $_"
# }
# }
# } catch {
# $env:praval = $env:praval + "`n" + "Critical error in Task: $_"
# } finally {
# try {
# if ($null -ne $eventFromUserScript ) { $eventFromUserScript.Dispose() }
# if ($null -ne $eventFromTask) { $eventFromTask.Dispose() }
# if ($null -ne $eventExit) { $eventExit.Dispose() }
# } catch {
# # do nothing
# }
# }
# }
# }

class TokenHandler {
handle($s)
{
Expand Down
10 changes: 1 addition & 9 deletions _generated/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Add-Type -AssemblyName "System"
$signalFromUserScript = "Global\SignalFromUserScript" + [System.Guid]::NewGuid().ToString()
$signalFromTask = "Global\SignalFromTask" + [System.Guid]::NewGuid().ToString()
$exitSignal = "Global\ExitSignal" + [System.Guid]::NewGuid().ToString()
$env:praval = ""

function Get-ActionPreference {
param (
Expand Down Expand Up @@ -49,10 +48,6 @@ try {
Assert-VstsPath -LiteralPath $tempDirectory -PathType 'Container'
$tokenfilePath = [System.IO.Path]::Combine($tempDirectory, "$([System.Guid]::NewGuid()).txt")

# Create a runspace to handle the Async communication between the Task and User Script for Access Token
# $runspacePool = [runspacefactory]::CreateRunspacePool(1, 1)
# $runspacePool.Open()

$accessTokenHelperFilePath = "$PSScriptRoot\AccessTokenHelper.ps1"
. $accessTokenHelperFilePath

Expand All @@ -68,7 +63,7 @@ try {


# Wait for the async runspace to start and get ready to listen to User scripts requests
Start-Sleep 30
Start-Sleep 20


# Get inputs.
Expand Down Expand Up @@ -339,9 +334,6 @@ finally {
# Signal Task to exit
$eventExit = New-Object System.Threading.EventWaitHandle($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$output = $eventExit.Set()
# $output = $psRunspace.EndInvoke($runspaceOutput)
# Write-Host $output
Write-Host $env:praval
Trace-VstsLeavingInvocation $MyInvocation
} catch {
Write-Host "Full Exception Object: $_"
Expand Down
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": 250,
"Patch": 118
"Patch": 124
},
"releaseNotes": "Script task consistency. Added support for macOS and Linux.",
"minimumAgentVersion": "2.115.0",
Expand Down Expand Up @@ -269,8 +269,8 @@
"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.250.118",
"Default": "2.250.124",
"LocalPackages": "2.249.4",
"Node20-225": "2.250.119"
"Node20-225": "2.250.125"
}
}
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": 250,
"Patch": 118
"Patch": 124
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"minimumAgentVersion": "2.115.0",
Expand Down Expand Up @@ -269,8 +269,8 @@
"ScriptArgsSanitized": "ms-resource:loc.messages.ScriptArgsSanitized"
},
"_buildConfigMapping": {
"Default": "2.250.118",
"Default": "2.250.124",
"LocalPackages": "2.249.4",
"Node20-225": "2.250.119"
"Node20-225": "2.250.125"
}
}
Loading

0 comments on commit f411a35

Please sign in to comment.