Skip to content

Commit

Permalink
Authentication issue in AzurePowerShell task (#19550)
Browse files Browse the repository at this point in the history
* code fixes

* bumped task version

* updated file as per changes

* updating AzurePowerShell.ts file
  • Loading branch information
v-schhabra authored Feb 20, 2024
1 parent acebd30 commit a09e055
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 37 deletions.
9 changes: 5 additions & 4 deletions Tasks/AzurePowerShellV5/AzurePowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ finally {
if ($__vstsAzPSInlineScriptPath -and (Test-Path -LiteralPath $__vstsAzPSInlineScriptPath) ) {
Remove-Item -LiteralPath $__vstsAzPSInlineScriptPath -ErrorAction 'SilentlyContinue'
}

Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
. "$PSScriptRoot\Utility.ps1"
Import-Module "$PSScriptRoot\ps_modules\VstsAzureHelpers_"
Remove-EndpointSecrets
Disconnect-AzureAndClearContext -ErrorAction SilentlyContinue
}
Update-PSModulePathForHostedAgent
Disconnect-AzureAndClearContext -restrictContext 'True' -ErrorAction SilentlyContinue
}
5 changes: 5 additions & 0 deletions Tasks/AzurePowerShellV5/RemoveAzContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
. "$PSScriptRoot/Utility.ps1"
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"

Update-PSModulePathForHostedAgentLinux
Disconnect-AzureAndClearContext -restrictContext 'True'
30 changes: 27 additions & 3 deletions Tasks/AzurePowerShellV5/azurepowershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function convertToNullIfUndefined<T>(arg: T): T|null {
}

async function run() {
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));

Expand All @@ -35,7 +38,6 @@ async function run() {
let customTargetAzurePs: string = convertToNullIfUndefined(tl.getInput('CustomTargetAzurePs', false));
let serviceName = tl.getInput('ConnectedServiceNameARM',/*required*/true);
let endpointObject= await new AzureRMEndpoint(serviceName).getEndpoint();
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
let isDebugEnabled = (process.env['SYSTEM_DEBUG'] || "").toLowerCase() === "true";

// string constants
Expand Down Expand Up @@ -99,8 +101,6 @@ async function run() {

// Write the script to disk.
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.ps1');

await fs.writeFile(
Expand Down Expand Up @@ -160,6 +160,30 @@ async function run() {
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
finally {
try {
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
.arg('-NoLogo')
.arg('-NoProfile')
.arg('-NonInteractive')
.arg('-ExecutionPolicy')
.arg('Unrestricted')
.arg('-Command')
.arg(`. '{path.join(path.resolve(__dirname),'RemoveAzContext.ps1')}'`);

let options = <tr.IExecOptions>{
cwd: input_workingDirectory,
failOnStdErr: false,
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
ignoreReturnCode: true
};
await powershell.exec(options);
}
catch (err) {
tl.debug("Az-clearContext not completed due to an error");
}
}
}

run();
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV5/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 234,
"Minor": 236,
"Patch": 0
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV5/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 234,
"Minor": 236,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
Expand Down
4 changes: 2 additions & 2 deletions _generated/AzurePowerShellV5.versionmap.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Default|5.234.0
Node20_229_2|5.234.1
Default|5.236.0
Node20_229_2|5.236.1
9 changes: 5 additions & 4 deletions _generated/AzurePowerShellV5/AzurePowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ finally {
if ($__vstsAzPSInlineScriptPath -and (Test-Path -LiteralPath $__vstsAzPSInlineScriptPath) ) {
Remove-Item -LiteralPath $__vstsAzPSInlineScriptPath -ErrorAction 'SilentlyContinue'
}

Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
. "$PSScriptRoot\Utility.ps1"
Import-Module "$PSScriptRoot\ps_modules\VstsAzureHelpers_"
Remove-EndpointSecrets
Disconnect-AzureAndClearContext -ErrorAction SilentlyContinue
}
Update-PSModulePathForHostedAgent
Disconnect-AzureAndClearContext -restrictContext 'True' -ErrorAction SilentlyContinue
}
5 changes: 5 additions & 0 deletions _generated/AzurePowerShellV5/RemoveAzContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
. "$PSScriptRoot/Utility.ps1"
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"

Update-PSModulePathForHostedAgentLinux
Disconnect-AzureAndClearContext -restrictContext 'True'
30 changes: 27 additions & 3 deletions _generated/AzurePowerShellV5/azurepowershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function convertToNullIfUndefined<T>(arg: T): T|null {
}

async function run() {
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));

Expand All @@ -35,7 +38,6 @@ async function run() {
let customTargetAzurePs: string = convertToNullIfUndefined(tl.getInput('CustomTargetAzurePs', false));
let serviceName = tl.getInput('ConnectedServiceNameARM',/*required*/true);
let endpointObject= await new AzureRMEndpoint(serviceName).getEndpoint();
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
let isDebugEnabled = (process.env['SYSTEM_DEBUG'] || "").toLowerCase() === "true";

// string constants
Expand Down Expand Up @@ -99,8 +101,6 @@ async function run() {

// Write the script to disk.
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.ps1');

await fs.writeFile(
Expand Down Expand Up @@ -160,6 +160,30 @@ async function run() {
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
finally {
try {
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
.arg('-NoLogo')
.arg('-NoProfile')
.arg('-NonInteractive')
.arg('-ExecutionPolicy')
.arg('Unrestricted')
.arg('-Command')
.arg(`. '{path.join(path.resolve(__dirname),'RemoveAzContext.ps1')}'`);

let options = <tr.IExecOptions>{
cwd: input_workingDirectory,
failOnStdErr: false,
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
ignoreReturnCode: true
};
await powershell.exec(options);
}
catch (err) {
tl.debug("Az-clearContext not completed due to an error");
}
}
}

run();
6 changes: 3 additions & 3 deletions _generated/AzurePowerShellV5/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 234,
"Minor": 236,
"Patch": 0
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
Expand Down Expand Up @@ -204,7 +204,7 @@
"PS_ExitCode": "PowerShell exited with code '{0}'."
},
"_buildConfigMapping": {
"Default": "5.234.0",
"Node20_229_2": "5.234.1"
"Default": "5.236.0",
"Node20_229_2": "5.236.1"
}
}
6 changes: 3 additions & 3 deletions _generated/AzurePowerShellV5/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 234,
"Minor": 236,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
Expand Down Expand Up @@ -204,7 +204,7 @@
"PS_ExitCode": "ms-resource:loc.messages.PS_ExitCode"
},
"_buildConfigMapping": {
"Default": "5.234.0",
"Node20_229_2": "5.234.1"
"Default": "5.236.0",
"Node20_229_2": "5.236.1"
}
}
9 changes: 5 additions & 4 deletions _generated/AzurePowerShellV5_Node20/AzurePowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ finally {
if ($__vstsAzPSInlineScriptPath -and (Test-Path -LiteralPath $__vstsAzPSInlineScriptPath) ) {
Remove-Item -LiteralPath $__vstsAzPSInlineScriptPath -ErrorAction 'SilentlyContinue'
}

Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
. "$PSScriptRoot\Utility.ps1"
Import-Module "$PSScriptRoot\ps_modules\VstsAzureHelpers_"
Remove-EndpointSecrets
Disconnect-AzureAndClearContext -ErrorAction SilentlyContinue
}
Update-PSModulePathForHostedAgent
Disconnect-AzureAndClearContext -restrictContext 'True' -ErrorAction SilentlyContinue
}
5 changes: 5 additions & 0 deletions _generated/AzurePowerShellV5_Node20/RemoveAzContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
. "$PSScriptRoot/Utility.ps1"
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"

Update-PSModulePathForHostedAgentLinux
Disconnect-AzureAndClearContext -restrictContext 'True'
30 changes: 27 additions & 3 deletions _generated/AzurePowerShellV5_Node20/azurepowershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function convertToNullIfUndefined<T>(arg: T): T|null {
}

async function run() {
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));

Expand All @@ -35,7 +38,6 @@ async function run() {
let customTargetAzurePs: string = convertToNullIfUndefined(tl.getInput('CustomTargetAzurePs', false));
let serviceName = tl.getInput('ConnectedServiceNameARM',/*required*/true);
let endpointObject= await new AzureRMEndpoint(serviceName).getEndpoint();
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
let isDebugEnabled = (process.env['SYSTEM_DEBUG'] || "").toLowerCase() === "true";

// string constants
Expand Down Expand Up @@ -99,8 +101,6 @@ async function run() {

// Write the script to disk.
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.ps1');

await fs.writeFile(
Expand Down Expand Up @@ -160,6 +160,30 @@ async function run() {
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
finally {
try {
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
.arg('-NoLogo')
.arg('-NoProfile')
.arg('-NonInteractive')
.arg('-ExecutionPolicy')
.arg('Unrestricted')
.arg('-Command')
.arg(`. '{path.join(path.resolve(__dirname),'RemoveAzContext.ps1')}'`);

let options = <tr.IExecOptions>{
cwd: input_workingDirectory,
failOnStdErr: false,
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
ignoreReturnCode: true
};
await powershell.exec(options);
}
catch (err) {
tl.debug("Az-clearContext not completed due to an error");
}
}
}

run();
6 changes: 3 additions & 3 deletions _generated/AzurePowerShellV5_Node20/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 234,
"Minor": 236,
"Patch": 1
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
Expand Down Expand Up @@ -208,7 +208,7 @@
"PS_ExitCode": "PowerShell exited with code '{0}'."
},
"_buildConfigMapping": {
"Default": "5.234.0",
"Node20_229_2": "5.234.1"
"Default": "5.236.0",
"Node20_229_2": "5.236.1"
}
}
6 changes: 3 additions & 3 deletions _generated/AzurePowerShellV5_Node20/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 234,
"Minor": 236,
"Patch": 1
},
"releaseNotes": "ms-resource:loc.releaseNotes",
Expand Down Expand Up @@ -208,7 +208,7 @@
"PS_ExitCode": "ms-resource:loc.messages.PS_ExitCode"
},
"_buildConfigMapping": {
"Default": "5.234.0",
"Node20_229_2": "5.234.1"
"Default": "5.236.0",
"Node20_229_2": "5.236.1"
}
}

0 comments on commit a09e055

Please sign in to comment.