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

Authentication issue in AzurePowerShell task #19550

Merged
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
onetocny marked this conversation as resolved.
Show resolved Hide resolved
}
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'
43 changes: 40 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,43 @@ async function run() {
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
finally {
let RemoveScriptContent: string[] = [];
let RemoveAzContextPath = path.join(path.resolve(__dirname), 'RemoveAzContext.ps1');
let removeScripts = `${RemoveAzContextPath} -ErrorAction continue`
onetocny marked this conversation as resolved.
Show resolved Hide resolved
RemoveScriptContent.push(removeScripts);
const removeScriptFilePath = path.join(tempDirectory, uuidV4() + '.ps1');
onetocny marked this conversation as resolved.
Show resolved Hide resolved
try {
await fs.writeFile(
removeScriptFilePath,
'\ufeff' + RemoveScriptContent.join(os.EOL), // Prepend the Unicode BOM character.
{ encoding: 'utf8' }, // Since UTF8 encoding is specified, node will
// encode the BOM into its UTF8 binary sequence.
function (err) {
if (err) throw err;
});
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(`. '${removeScriptFilePath.replace(/'/g, "''")}'`);
v-schhabra marked this conversation as resolved.
Show resolved Hide resolved

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": 235,
"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": 235,
"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.235.0
Node20_229_2|5.235.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'
43 changes: 40 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,43 @@ async function run() {
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
finally {
let RemoveScriptContent: string[] = [];
let RemoveAzContextPath = path.join(path.resolve(__dirname), 'RemoveAzContext.ps1');
let removeScripts = `${RemoveAzContextPath} -ErrorAction continue`
RemoveScriptContent.push(removeScripts);
const removeScriptFilePath = path.join(tempDirectory, uuidV4() + '.ps1');
try {
await fs.writeFile(
removeScriptFilePath,
'\ufeff' + RemoveScriptContent.join(os.EOL), // Prepend the Unicode BOM character.
{ encoding: 'utf8' }, // Since UTF8 encoding is specified, node will
// encode the BOM into its UTF8 binary sequence.
function (err) {
if (err) throw err;
});
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(`. '${removeScriptFilePath.replace(/'/g, "''")}'`);

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": 235,
"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.235.0",
"Node20_229_2": "5.235.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": 235,
"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.235.0",
"Node20_229_2": "5.235.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'
43 changes: 40 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,43 @@ async function run() {
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
finally {
let RemoveScriptContent: string[] = [];
let RemoveAzContextPath = path.join(path.resolve(__dirname), 'RemoveAzContext.ps1');
let removeScripts = `${RemoveAzContextPath} -ErrorAction continue`
RemoveScriptContent.push(removeScripts);
const removeScriptFilePath = path.join(tempDirectory, uuidV4() + '.ps1');
try {
await fs.writeFile(
removeScriptFilePath,
'\ufeff' + RemoveScriptContent.join(os.EOL), // Prepend the Unicode BOM character.
{ encoding: 'utf8' }, // Since UTF8 encoding is specified, node will
// encode the BOM into its UTF8 binary sequence.
function (err) {
if (err) throw err;
});
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(`. '${removeScriptFilePath.replace(/'/g, "''")}'`);

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": 235,
"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.235.0",
"Node20_229_2": "5.235.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": 235,
"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.235.0",
"Node20_229_2": "5.235.1"
}
}