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

Fix #103: Dealing with un-escaped environment variables by using args #105

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

45 changes: 23 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ const cpExec = util.promisify(require('child_process').exec);

import { createScriptFile, TEMP_DIRECTORY, NullOutstreamStringWritable, deleteFile, getCurrentTime, checkIfEnvironmentVariableIsOmitted } from './utils';

const START_SCRIPT_EXECUTION_MARKER: string = `Starting script execution via docker image mcr.microsoft.com/azure-cli:`;
const BASH_ARG: string = `bash --noprofile --norc -e `;
const START_SCRIPT_EXECUTION_MARKER: string = "Starting script execution via docker image mcr.microsoft.com/azure-cli:";
const AZ_CLI_VERSION_DEFAULT_VALUE = 'agentazcliversion'

export async function main(){
export async function main() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any linter tool for TypeScript? A linter can help us normalize the code style and avoid distracting diffs like this.

Just a reminder. No need to do that right now.

var scriptFileName: string = '';
const CONTAINER_NAME = `MICROSOFT_AZURE_CLI_${getCurrentTime()}_CONTAINER`;
try {
Expand Down Expand Up @@ -49,14 +48,6 @@ export async function main(){
}
inlineScript = ` set -e >&2; echo '${START_SCRIPT_EXECUTION_MARKER}' >&2; ${inlineScript}`;
scriptFileName = await createScriptFile(inlineScript);
let startCommand: string = ` ${BASH_ARG}${TEMP_DIRECTORY}/${scriptFileName} `;
let environmentVariables = '';
for (let key in process.env) {
// if (key.toUpperCase().startsWith("GITHUB_") && key.toUpperCase() !== 'GITHUB_WORKSPACE' && process.env[key]){
if (!checkIfEnvironmentVariableIsOmitted(key) && process.env[key]) {
environmentVariables += ` -e "${key}=${process.env[key]}" `;
}
}

/*
For the docker run command, we are doing the following
Expand All @@ -65,15 +56,25 @@ export async function main(){
- voulme mount .azure session token file between host and container,
- volume mount temp directory between host and container, inline script file is created in temp directory
*/
let command: string = `run --workdir ${process.env.GITHUB_WORKSPACE} -v ${process.env.GITHUB_WORKSPACE}:${process.env.GITHUB_WORKSPACE} `;
command += ` -v ${process.env.HOME}/.azure:/root/.azure -v ${TEMP_DIRECTORY}:${TEMP_DIRECTORY} `;
command += ` ${environmentVariables} `;
command += `--name ${CONTAINER_NAME} `;
command += ` mcr.microsoft.com/azure-cli:${azcliversion} ${startCommand}`;
let args: string[] = ["run", "--workdir", `${process.env.GITHUB_WORKSPACE}`,
"-v", `${process.env.GITHUB_WORKSPACE}:${process.env.GITHUB_WORKSPACE}`,
"-v", `${process.env.HOME}/.azure:/root/.azure`,
"-v", `${TEMP_DIRECTORY}:${TEMP_DIRECTORY}`
];
for (let key in process.env) {
if (!checkIfEnvironmentVariableIsOmitted(key) && process.env[key]) {
args.push("-e", `${key}=${process.env[key]}`);
}
}
args.push("--name", CONTAINER_NAME,
`mcr.microsoft.com/azure-cli:${azcliversion}`,
"bash", "--noprofile", "--norc", "-e", `${TEMP_DIRECTORY}/${scriptFileName}`);

console.log(`${START_SCRIPT_EXECUTION_MARKER}${azcliversion}`);
await executeDockerCommand(command);
await executeDockerCommand(args);
console.log("az script ran successfully.");
} catch (error) {
}
catch (error) {
core.error(error);
throw error;
}
Expand All @@ -82,7 +83,7 @@ export async function main(){
const scriptFilePath: string = path.join(TEMP_DIRECTORY, scriptFileName);
await deleteFile(scriptFilePath);
console.log("cleaning up container...");
await executeDockerCommand(` container rm --force ${CONTAINER_NAME} `, true);
await executeDockerCommand(["rm", "--force", CONTAINER_NAME], true);
}
};

Expand All @@ -104,7 +105,7 @@ const getAllAzCliVersions = async (): Promise<Array<string>> => {
};

try {
await exec.exec(`curl --location -s https://mcr.microsoft.com/v2/azure-cli/tags/list`, [], execOptions)
await exec.exec("curl", ["--location", "-s", "https://mcr.microsoft.com/v2/azure-cli/tags/list"], execOptions)
if (outStream && JSON.parse(outStream).tags) {
return JSON.parse(outStream).tags;
}
Expand All @@ -115,7 +116,7 @@ const getAllAzCliVersions = async (): Promise<Array<string>> => {
return [];
}

const executeDockerCommand = async (dockerCommand: string, continueOnError: boolean = false): Promise<void> => {
const executeDockerCommand = async (args: string[], continueOnError: boolean = false): Promise<void> => {

const dockerTool: string = await io.which("docker", true);
var errorStream: string = '';
Expand All @@ -140,7 +141,7 @@ const executeDockerCommand = async (dockerCommand: string, continueOnError: bool
};
var exitCode;
try {
exitCode = await exec.exec(`"${dockerTool}" ${dockerCommand}`, [], execOptions);
exitCode = await exec.exec(dockerTool, args, execOptions);
} catch (error) {
if (!continueOnError) {
throw error;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const deleteFile = async (filePath: string) => {
}
}

export const giveExecutablePermissionsToFile = async (filePath: string): Promise<number> => await exec.exec(`chmod +x ${filePath}`, [], { silent: true })
export const giveExecutablePermissionsToFile = async (filePath: string): Promise<number> => await exec.exec('chmod', ['+x', filePath], { silent: true })

export const getCurrentTime = (): number => {
return new Date().getTime();
Expand Down