Skip to content

Commit

Permalink
CHE-3715: fix parsing of dockerfile's ENV instruction (#4016)
Browse files Browse the repository at this point in the history
In UD there was the bug in dockerfile's parser
which caused incorrect value of environment variable.

Signed-off-by: Oleksii Kurinnyi <[email protected]>
  • Loading branch information
Oleksii Kurinnyi authored Feb 13, 2017
1 parent 8ba7e73 commit 76de6e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/components/api/environment/docker-file-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Simper dockerfile parser', () => {
});

describe('method _parseArgument()', () => {
it('should parse ENV argument with single environment variable', () => {
it('should parse ENV argument with single environment variable #1', () => {
let instruction = 'ENV',
argument = 'name environment variable value';

Expand All @@ -38,6 +38,19 @@ describe('Simper dockerfile parser', () => {
expect(result).toEqual(expectedResult);
});

it('should parse ENV argument with single environment variable #2', () => {
let instruction = 'ENV',
argument = 'SBT_OPTS \'-Dhttp.proxyHost=proxy.wdf.sap.corp -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.wdf.sap.corp -Dhttps.proxyPort=8080 -Dhttp.nonProxyHosts=nexus.wdf.sap.corp\'';

let result = parser._parseArgument(instruction, argument);

let expectedResult = [{
instruction: 'ENV',
argument: ['SBT_OPTS', '\'-Dhttp.proxyHost=proxy.wdf.sap.corp -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.wdf.sap.corp -Dhttps.proxyPort=8080 -Dhttp.nonProxyHosts=nexus.wdf.sap.corp\'']
}];
expect(result).toEqual(expectedResult);
});

it('should parse ENV argument with several environment variables', () => {
let instruction = 'ENV',
argument = 'myName="John Doe" myDog=Rex\ The\ Dog myCat=fluffy';
Expand All @@ -56,6 +69,7 @@ describe('Simper dockerfile parser', () => {
}];
expect(result).toEqual(expectedResult);
});

});

it('should parse a dockerfile', () => {
Expand Down
18 changes: 12 additions & 6 deletions src/components/api/environment/docker-file-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class DockerfileParser {
commentLineRE: RegExp;
instructionRE: RegExp;
envVariablesRE: RegExp;
quotesRE: RegExp;
quotesTestRE: RegExp;
quoteAtStartReplaceRE: RegExp;
quoteAtEndReplaceRE: RegExp;
backslashSpaceRE: RegExp;

constructor() {
Expand All @@ -43,7 +45,9 @@ export class DockerfileParser {
// | | \- variable value
// | \- variable name
// \- start of line or spaces before variable name
this.quotesRE = /^["]|["]$/g;
this.quotesTestRE = /^(["']).+(\1)$/;
this.quoteAtStartReplaceRE = /^["']/g;
this.quoteAtEndReplaceRE = /["']$/g;
this.backslashSpaceRE = /\\\s/g;
}

Expand Down Expand Up @@ -116,14 +120,17 @@ export class DockerfileParser {

switch (instruction) {
case 'ENV':
if (argumentStr.indexOf('=') >= 0) {
let firstSpaceIndex = argumentStr.indexOf(' '),
firstEqualIndex = argumentStr.indexOf('=');
if (firstEqualIndex > -1 && firstEqualIndex < firstSpaceIndex) {
// this argument string contains one or more environment variables
let match;
while (match = this.envVariablesRE.exec(argumentStr)) {
let name: string = match[1],
value: string = match[2];
if (this.quotesRE.test(value)) {
value = value.replace(this.quotesRE, '');
if (this.quotesTestRE.test(value)) {
value = value.replace(this.quoteAtStartReplaceRE, '');
value = value.replace(this.quoteAtEndReplaceRE, '');
}
if (this.backslashSpaceRE.test(value)) {
value = value.replace(this.backslashSpaceRE, ' ');
Expand All @@ -136,7 +143,6 @@ export class DockerfileParser {
}
} else {
// this argument string contains only one environment variable
let firstSpaceIndex = argumentStr.indexOf(' ');
results.push({
instruction: instruction,
argument: [argumentStr.slice(0, firstSpaceIndex), argumentStr.slice(firstSpaceIndex + 1)]
Expand Down

0 comments on commit 76de6e6

Please sign in to comment.