From 94df31eee9a7771d87acd91f514f650ef38c9e0b Mon Sep 17 00:00:00 2001 From: aksm-ms Date: Wed, 4 Nov 2020 22:10:29 +0530 Subject: [PATCH 1/3] adding params to escape symbols in az cli --- lib/main.js | 30 ++++++++++++++++++++++-------- src/main.ts | 27 ++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/main.js b/lib/main.js index 1527cd26d..8cb6677b9 100644 --- a/lib/main.js +++ b/lib/main.js @@ -46,7 +46,6 @@ function main() { }; yield executeAzCliCommand("--version", true, options); core.debug(`az cli version used:\n${output}`); - let creds = core.getInput('creds', { required: true }); let secrets = new actions_secret_parser_1.SecretParser(creds, actions_secret_parser_1.FormatType.JSON); let servicePrincipalId = secrets.getSecret("$.clientId", false); @@ -55,7 +54,6 @@ function main() { let subscriptionId = secrets.getSecret("$.subscriptionId", false); const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true"; - if (!servicePrincipalId || !servicePrincipalKey || !tenantId) { throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied."); } @@ -64,11 +62,28 @@ function main() { } // Attempting Az cli login if (allowNoSubscriptionsLogin) { - yield executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); + let parameters = [ + "--allow-no-subscriptions", + "--service-principal", + "-u", servicePrincipalId, + "-p", servicePrincipalKey, + "--tenant", tenantId + ]; + yield executeAzCliCommand(`login`, true, {}, parameters); } else { - yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); - yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); + let parameters = [ + "--service-principal", + "-u", servicePrincipalId, + "-p", servicePrincipalKey, + "--tenant", tenantId + ]; + yield executeAzCliCommand(`login`, true, {}, parameters); + parameters = [ + "--subscription", + subscriptionId + ]; + yield executeAzCliCommand(`account set`, true, {}, parameters); } isAzCLISuccess = true; if (enableAzPSSession) { @@ -96,12 +111,11 @@ function main() { } }); } - -function executeAzCliCommand(command, silent, options = {}) { +function executeAzCliCommand(command, silent, options = {}, parameters = []) { return __awaiter(this, void 0, void 0, function* () { options.silent = !!silent; try { - yield exec.exec(`"${azPath}" ${command}`, [], options); + yield exec.exec(`"${azPath}" ${command}`, parameters, options); } catch (error) { throw new Error(error); diff --git a/src/main.ts b/src/main.ts index 3288986e9..4bcf32425 100644 --- a/src/main.ts +++ b/src/main.ts @@ -50,11 +50,28 @@ async function main() { // Attempting Az cli login if (allowNoSubscriptionsLogin) { - await executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); + let parameters = [ + "--allow-no-subscriptions", + "--service-principal", + "-u", servicePrincipalId, + "-p", servicePrincipalKey, + "--tenant", tenantId + ]; + await executeAzCliCommand(`login`, true, {}, parameters); } else { - await executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); - await executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); + let parameters = [ + "--service-principal", + "-u", servicePrincipalId, + "-p", servicePrincipalKey, + "--tenant", tenantId + ]; + await executeAzCliCommand(`login`, true, {}, parameters); + parameters = [ + "--subscription", + subscriptionId + ]; + await executeAzCliCommand(`account set`, true, {}, parameters); } isAzCLISuccess = true; if (enableAzPSSession) { @@ -79,10 +96,10 @@ async function main() { } } -async function executeAzCliCommand(command: string, silent?: boolean, options: any = {}) { +async function executeAzCliCommand(command: string, silent?: boolean, options: any = {}, parameters: any = []) { options.silent = !!silent; try { - await exec.exec(`"${azPath}" ${command}`, [], options); + await exec.exec(`"${azPath}" ${command}`, parameters, options); } catch(error) { throw new Error(error); From 072c92e9263ab95d34ebc6549f5f08b8603d06ce Mon Sep 17 00:00:00 2001 From: aksm-ms Date: Fri, 6 Nov 2020 13:47:39 +0530 Subject: [PATCH 2/3] addressed review comments --- lib/main.js | 6 +++--- src/main.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/main.js b/lib/main.js index 8cb6677b9..30e4c0f04 100644 --- a/lib/main.js +++ b/lib/main.js @@ -111,11 +111,11 @@ function main() { } }); } -function executeAzCliCommand(command, silent, options = {}, parameters = []) { +function executeAzCliCommand(command, silent, execOptions = {}, args = []) { return __awaiter(this, void 0, void 0, function* () { - options.silent = !!silent; + execOptions.silent = !!silent; try { - yield exec.exec(`"${azPath}" ${command}`, parameters, options); + yield exec.exec(`"${azPath}" ${command}`, args, execOptions); } catch (error) { throw new Error(error); diff --git a/src/main.ts b/src/main.ts index 4bcf32425..caf3856db 100644 --- a/src/main.ts +++ b/src/main.ts @@ -96,10 +96,10 @@ async function main() { } } -async function executeAzCliCommand(command: string, silent?: boolean, options: any = {}, parameters: any = []) { - options.silent = !!silent; +async function executeAzCliCommand(command: string, silent?: boolean, execOptions: any = {}, args: any = []) { + execOptions.silent = !!silent; try { - await exec.exec(`"${azPath}" ${command}`, parameters, options); + await exec.exec(`"${azPath}" ${command}`, args, execOptions); } catch(error) { throw new Error(error); From fb74babeb9797a014b3f8f78a42e27ce74625627 Mon Sep 17 00:00:00 2001 From: aksm-ms Date: Fri, 6 Nov 2020 13:49:29 +0530 Subject: [PATCH 3/3] addressed review comments --- lib/main.js | 16 ++++++++-------- src/main.ts | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/main.js b/lib/main.js index 30e4c0f04..c39335195 100644 --- a/lib/main.js +++ b/lib/main.js @@ -37,14 +37,14 @@ function main() { core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv); azPath = yield io.which("az", true); let output = ""; - const options = { + const execOptions = { listeners: { stdout: (data) => { output += data.toString(); } } }; - yield executeAzCliCommand("--version", true, options); + yield executeAzCliCommand("--version", true, execOptions); core.debug(`az cli version used:\n${output}`); let creds = core.getInput('creds', { required: true }); let secrets = new actions_secret_parser_1.SecretParser(creds, actions_secret_parser_1.FormatType.JSON); @@ -62,28 +62,28 @@ function main() { } // Attempting Az cli login if (allowNoSubscriptionsLogin) { - let parameters = [ + let args = [ "--allow-no-subscriptions", "--service-principal", "-u", servicePrincipalId, "-p", servicePrincipalKey, "--tenant", tenantId ]; - yield executeAzCliCommand(`login`, true, {}, parameters); + yield executeAzCliCommand(`login`, true, {}, args); } else { - let parameters = [ + let args = [ "--service-principal", "-u", servicePrincipalId, "-p", servicePrincipalKey, "--tenant", tenantId ]; - yield executeAzCliCommand(`login`, true, {}, parameters); - parameters = [ + yield executeAzCliCommand(`login`, true, {}, args); + args = [ "--subscription", subscriptionId ]; - yield executeAzCliCommand(`account set`, true, {}, parameters); + yield executeAzCliCommand(`account set`, true, {}, args); } isAzCLISuccess = true; if (enableAzPSSession) { diff --git a/src/main.ts b/src/main.ts index caf3856db..3fd4b03a5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,14 +22,14 @@ async function main() { azPath = await io.which("az", true); let output: string = ""; - const options: any = { + const execOptions: any = { listeners: { stdout: (data: Buffer) => { output += data.toString(); } } }; - await executeAzCliCommand("--version", true, options); + await executeAzCliCommand("--version", true, execOptions); core.debug(`az cli version used:\n${output}`); let creds = core.getInput('creds', { required: true }); @@ -50,28 +50,28 @@ async function main() { // Attempting Az cli login if (allowNoSubscriptionsLogin) { - let parameters = [ + let args = [ "--allow-no-subscriptions", "--service-principal", "-u", servicePrincipalId, "-p", servicePrincipalKey, "--tenant", tenantId ]; - await executeAzCliCommand(`login`, true, {}, parameters); + await executeAzCliCommand(`login`, true, {}, args); } else { - let parameters = [ + let args = [ "--service-principal", "-u", servicePrincipalId, "-p", servicePrincipalKey, "--tenant", tenantId ]; - await executeAzCliCommand(`login`, true, {}, parameters); - parameters = [ + await executeAzCliCommand(`login`, true, {}, args); + args = [ "--subscription", subscriptionId ]; - await executeAzCliCommand(`account set`, true, {}, parameters); + await executeAzCliCommand(`account set`, true, {}, args); } isAzCLISuccess = true; if (enableAzPSSession) {