Skip to content

Commit

Permalink
Deployed to Eric and Silviu
Browse files Browse the repository at this point in the history
  • Loading branch information
praval-microsoft committed Nov 21, 2024
1 parent d045fca commit bf8fba4
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 91 deletions.
25 changes: 9 additions & 16 deletions Tasks/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ class FileBasedToken {
$eventFromA.Set()
} elseif ($index -eq 1) {
# Exit signal received
Write-Host "Task: Exit signal received. Exiting loop..."
Write-Debug "Task: Exit signal received. Exiting loop..."
break
}
} catch {
Write-Host "Error occurred while waiting for signals: $_"
Write-Debug "Error occurred while waiting for signals: $_"
}
}
} catch {
Write-Host "Critical error in Task: $_"
Write-Debug "Critical error in Task: $_"
} finally {
# Cleanup resources
if ($null -ne $eventFromB ) { $eventFromB.Dispose() }
if ($null -ne $eventFromA) { $eventFromA.Dispose() }
if ($null -ne $eventExit) { $eventExit.Dispose() }
Write-Host "Task: Resources cleaned up. Exiting."
Write-Debug "Task: Resources cleaned up. Exiting."
}
}
}
Expand Down Expand Up @@ -278,15 +278,15 @@ try {
$eventFromB = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $signalFromB)
$eventFromA = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $signalFromA)
function Get-AzDoTokenHelper {
function Get-AzDoToken {
Write-Debug "User Script: Starting process to notify Task and read output."
[string]$tokenResponse = $env:SystemAccessTokenPowershellV2
try {
# Signal Task to generate access token
$eventFromB.Set()
Write-Debug "User Script: Notified Task to generate access token."
$tmp = $eventFromB.Set()
Write-Debug "User Script: Notified Task to generate access token $tmp."
# Wait for Task to finish processing
$receivedResponseBool = $eventFromA.WaitOne(60000) # Wait for up to 60 seconds
Expand All @@ -310,13 +310,6 @@ try {
return $tokenResponse
}
function Get-AzDoToken {
$token = Get-AzDoTokenHelper
$token = $token | Out-String
$token = $token.Substring(4).Trim()
return $token
}
' + $joinedContents

Expand Down Expand Up @@ -441,7 +434,7 @@ finally {
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$eventExit.Set()
Write-Host "Exit signal sent to Task."
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
}
22 changes: 13 additions & 9 deletions Tasks/PowerShellV2/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,34 @@ async function startNamedPiped() {

pipeStream.on('data', async (data) => {
const trimmedData = data.toString('utf8').trim();
console.log(`Received from PowerShell: ${trimmedData}`);
console.debug(`Received from PowerShell: ${trimmedData}`);
var systemAccessToken = tl.getVariable('System.AccessToken');
process.env.System_Access_Token_PSV2_Task = systemAccessToken;

if (trimmedData == 'Get-AzDoToken') {
try {
if(connectedServiceName && connectedServiceName.trim().length > 0) {
const token = await getAccessTokenViaWorkloadIdentityFederation(connectedServiceName);
console.log(`Successfully fetched the ADO access token for ${connectedServiceName}`);
console.debug(`Successfully fetched the ADO access token for ${connectedServiceName}`);
writeStream.write(token + "\n");
} else {
console.log(`No Service Connection found, returning empty token`);
console.debug(`No Service Connection found, returning empty token`);
writeStream.write(systemAccessToken + "\n");
}
} catch(err) {
console.log(`Token generation failed with error message ${err.message}`);
console.debug(`Token generation failed with error message ${err.message}`);
writeStream.write(systemAccessToken + "\n");
}
} else {
console.log('Pipe reading ended');
writeStream.close();
pipeStream.close();
fs.unlinkSync(ts2PsPipePath);
fs.unlinkSync(ps2TsPipePath);
try {
console.debug('Pipe reading ended');
writeStream.close();
pipeStream.close();
fs.unlinkSync(ts2PsPipePath);
fs.unlinkSync(ps2TsPipePath);
} catch(err) {
console.debug(`Cleanup failed : ${err.message}`);
}
}
});

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": 249,
"Patch": 234
"Patch": 248
},
"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": 234
"Patch": 248
},
"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.249.234
Node20-225|2.249.235
Default|2.249.248
Node20-225|2.249.249
25 changes: 9 additions & 16 deletions _generated/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ class FileBasedToken {
$eventFromA.Set()
} elseif ($index -eq 1) {
# Exit signal received
Write-Host "Task: Exit signal received. Exiting loop..."
Write-Debug "Task: Exit signal received. Exiting loop..."
break
}
} catch {
Write-Host "Error occurred while waiting for signals: $_"
Write-Debug "Error occurred while waiting for signals: $_"
}
}
} catch {
Write-Host "Critical error in Task: $_"
Write-Debug "Critical error in Task: $_"
} finally {
# Cleanup resources
if ($null -ne $eventFromB ) { $eventFromB.Dispose() }
if ($null -ne $eventFromA) { $eventFromA.Dispose() }
if ($null -ne $eventExit) { $eventExit.Dispose() }
Write-Host "Task: Resources cleaned up. Exiting."
Write-Debug "Task: Resources cleaned up. Exiting."
}
}
}
Expand Down Expand Up @@ -278,15 +278,15 @@ try {
$eventFromB = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $signalFromB)
$eventFromA = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $signalFromA)
function Get-AzDoTokenHelper {
function Get-AzDoToken {
Write-Debug "User Script: Starting process to notify Task and read output."
[string]$tokenResponse = $env:SystemAccessTokenPowershellV2
try {
# Signal Task to generate access token
$eventFromB.Set()
Write-Debug "User Script: Notified Task to generate access token."
$tmp = $eventFromB.Set()
Write-Debug "User Script: Notified Task to generate access token $tmp."
# Wait for Task to finish processing
$receivedResponseBool = $eventFromA.WaitOne(60000) # Wait for up to 60 seconds
Expand All @@ -310,13 +310,6 @@ try {
return $tokenResponse
}
function Get-AzDoToken {
$token = Get-AzDoTokenHelper
$token = $token | Out-String
$token = $token.Substring(4).Trim()
return $token
}
' + $joinedContents

Expand Down Expand Up @@ -441,7 +434,7 @@ finally {
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$eventExit.Set()
Write-Host "Exit signal sent to Task."
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
}
22 changes: 13 additions & 9 deletions _generated/PowerShellV2/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,34 @@ async function startNamedPiped() {

pipeStream.on('data', async (data) => {
const trimmedData = data.toString('utf8').trim();
console.log(`Received from PowerShell: ${trimmedData}`);
console.debug(`Received from PowerShell: ${trimmedData}`);
var systemAccessToken = tl.getVariable('System.AccessToken');
process.env.System_Access_Token_PSV2_Task = systemAccessToken;

if (trimmedData == 'Get-AzDoToken') {
try {
if(connectedServiceName && connectedServiceName.trim().length > 0) {
const token = await getAccessTokenViaWorkloadIdentityFederation(connectedServiceName);
console.log(`Successfully fetched the ADO access token for ${connectedServiceName}`);
console.debug(`Successfully fetched the ADO access token for ${connectedServiceName}`);
writeStream.write(token + "\n");
} else {
console.log(`No Service Connection found, returning empty token`);
console.debug(`No Service Connection found, returning empty token`);
writeStream.write(systemAccessToken + "\n");
}
} catch(err) {
console.log(`Token generation failed with error message ${err.message}`);
console.debug(`Token generation failed with error message ${err.message}`);
writeStream.write(systemAccessToken + "\n");
}
} else {
console.log('Pipe reading ended');
writeStream.close();
pipeStream.close();
fs.unlinkSync(ts2PsPipePath);
fs.unlinkSync(ps2TsPipePath);
try {
console.debug('Pipe reading ended');
writeStream.close();
pipeStream.close();
fs.unlinkSync(ts2PsPipePath);
fs.unlinkSync(ps2TsPipePath);
} catch(err) {
console.debug(`Cleanup failed : ${err.message}`);
}
}
});

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": 249,
"Patch": 234
"Patch": 248
},
"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.234",
"Node20-225": "2.249.235"
"Default": "2.249.248",
"Node20-225": "2.249.249"
}
}
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": 234
"Patch": 248
},
"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.234",
"Node20-225": "2.249.235"
"Default": "2.249.248",
"Node20-225": "2.249.249"
}
}
25 changes: 9 additions & 16 deletions _generated/PowerShellV2_Node20/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ class FileBasedToken {
$eventFromA.Set()
} elseif ($index -eq 1) {
# Exit signal received
Write-Host "Task: Exit signal received. Exiting loop..."
Write-Debug "Task: Exit signal received. Exiting loop..."
break
}
} catch {
Write-Host "Error occurred while waiting for signals: $_"
Write-Debug "Error occurred while waiting for signals: $_"
}
}
} catch {
Write-Host "Critical error in Task: $_"
Write-Debug "Critical error in Task: $_"
} finally {
# Cleanup resources
if ($null -ne $eventFromB ) { $eventFromB.Dispose() }
if ($null -ne $eventFromA) { $eventFromA.Dispose() }
if ($null -ne $eventExit) { $eventExit.Dispose() }
Write-Host "Task: Resources cleaned up. Exiting."
Write-Debug "Task: Resources cleaned up. Exiting."
}
}
}
Expand Down Expand Up @@ -278,15 +278,15 @@ try {
$eventFromB = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $signalFromB)
$eventFromA = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $signalFromA)
function Get-AzDoTokenHelper {
function Get-AzDoToken {
Write-Debug "User Script: Starting process to notify Task and read output."
[string]$tokenResponse = $env:SystemAccessTokenPowershellV2
try {
# Signal Task to generate access token
$eventFromB.Set()
Write-Debug "User Script: Notified Task to generate access token."
$tmp = $eventFromB.Set()
Write-Debug "User Script: Notified Task to generate access token $tmp."
# Wait for Task to finish processing
$receivedResponseBool = $eventFromA.WaitOne(60000) # Wait for up to 60 seconds
Expand All @@ -310,13 +310,6 @@ try {
return $tokenResponse
}
function Get-AzDoToken {
$token = Get-AzDoTokenHelper
$token = $token | Out-String
$token = $token.Substring(4).Trim()
return $token
}
' + $joinedContents

Expand Down Expand Up @@ -441,7 +434,7 @@ finally {
# Signal Script A to exit
$exitSignal = "Global\ExitSignal"
$eventExit = [System.Threading.EventWaitHandle]::new($false, [System.Threading.EventResetMode]::AutoReset, $exitSignal)
$eventExit.Set()
Write-Host "Exit signal sent to Task."
$tmp = $eventExit.Set()
Write-Debug "Exit signal sent to Task $tmp."
Trace-VstsLeavingInvocation $MyInvocation
}
Loading

0 comments on commit bf8fba4

Please sign in to comment.