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: fixed fromStatement parsing to parse quotes in variable expressions #356

Merged
merged 1 commit into from
Jan 4, 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
16 changes: 13 additions & 3 deletions src/spec-node/dockerfileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export { CLIHostDocuments, Documents, createDocuments, Edit, fileDocuments, Remo


const findFromLines = new RegExp(/^(?<line>\s*FROM.*)/, 'gm');
const parseFromLine = /FROM\s+(?<platform>--platform=\S+\s+)?"?(?<image>[^\s"]+)"?(\s+[Aa][Ss]\s+(?<label>[^\s]+))?/;
const parseFromLine = /FROM\s+(?<platform>--platform=\S+\s+)?(?<image>"?[^\s]+"?)(\s+[Aa][Ss]\s+(?<label>[^\s]+))?/;

const fromStatement = /^\s*FROM\s+(?<platform>--platform=\S+\s+)?"?(?<image>[^\s"]+)"?(\s+[Aa][Ss]\s+(?<label>[^\s]+))?/m;
const fromStatement = /^\s*FROM\s+(?<platform>--platform=\S+\s+)?(?<image>"?[^\s]+"?)(\s+[Aa][Ss]\s+(?<label>[^\s]+))?/m;
const argEnvUserStatements = /^\s*(?<instruction>ARG|ENV|USER)\s+(?<name>[^\s=]+)([ =]+("(?<value1>\S+)"|(?<value2>\S+)))?/gm;
const directives = /^\s*#\s*(?<name>\S+)\s*=\s*(?<value>.+)/;

Expand Down Expand Up @@ -46,13 +46,23 @@ export interface Instruction {
value: string | undefined;
}

function parseFromStatement(line: string): From {
const match = fromStatement.exec(line);
if (!match) {
return { image: 'unknown' };
}
let { platfrom, image, label } = match.groups as unknown as From;
image = image.replace(/^['"]|['"]$/g, ''); // remove quotes
return { platfrom, image, label };
}

export function extractDockerfile(dockerfile: string): Dockerfile {
const fromStatementsAhead = /(?=^[\t ]*FROM)/gm;
const parts = dockerfile.split(fromStatementsAhead);
const preambleStr = fromStatementsAhead.test(parts[0] || '') ? '' : parts.shift()!;
const stageStrs = parts;
const stages = stageStrs.map(stageStr => ({
from: fromStatement.exec(stageStr)?.groups as unknown as From || { image: 'unknown' },
from: parseFromStatement(stageStr),
instructions: extractInstructions(stageStr),
}));
const directives = extractDirectives(preambleStr);
Expand Down
61 changes: 61 additions & 0 deletions src/test/dockerfileUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,67 @@ FROM \${cloud:-mcr.microsoft.com/}azure-cli:latest
const image = findBaseImage(extracted, {}, undefined);
assert.strictEqual(image, 'mcr.microsoft.com/azure-cli:latest');
});

describe('Quoted', async () => {
it('Positive variable expression with value specified', async () => {
const dockerfile = `
ARG cloud
FROM \${cloud:+"mcr.microsoft.com/"}azure-cli:latest"
`;
const extracted = extractDockerfile(dockerfile);
assert.strictEqual(extracted.stages.length, 1);
const image = findBaseImage(
extracted,
{
cloud: 'true',
},
undefined
);
assert.strictEqual(image, 'mcr.microsoft.com/azure-cli:latest');
});

it('Positive variable expression with no value specified', async () => {
const dockerfile = `
ARG cloud
FROM "\${cloud:+"mcr.microsoft.com/"}azure-cli:latest"
`;

const extracted = extractDockerfile(dockerfile);
assert.strictEqual(extracted.stages.length, 1);
const image = findBaseImage(extracted, {}, undefined);
assert.strictEqual(image, 'azure-cli:latest');
});

it('Negative variable expression with value specified', async () => {
const dockerfile = `
ARG cloud
FROM "\${cloud:-"mcr.microsoft.com/"}azure-cli:latest"
`;

const extracted = extractDockerfile(dockerfile);
assert.strictEqual(extracted.stages.length, 1);
const image = findBaseImage(
extracted,
{
cloud: 'ghcr.io/',
},
undefined
);
assert.strictEqual(image, 'ghcr.io/azure-cli:latest');
});

it('Negative variable expression with no value specified', async () => {
const dockerfile = `
ARG cloud
FROM \${cloud:-"mcr.microsoft.com/"}azure-cli:latest as label
`;

const extracted = extractDockerfile(dockerfile);
assert.strictEqual(extracted.stages.length, 1);
const image = findBaseImage(extracted, {}, undefined);
assert.strictEqual(image, 'mcr.microsoft.com/azure-cli:latest');
});
});
});
});

Expand Down