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

Escape environment variables before pass it to docker run #215

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
buildPlugin(platforms: [
'linux',
'docker',
'maven-windows' // TODO Docker-based tests fail when using Docker on Windows. The maven-windows agents do not have Docker installed so tests that require Docker are skipped.
])
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Node;
import hudson.os.WindowsUtil;
import hudson.util.ArgumentListBuilder;
import hudson.util.VersionNumber;
import java.io.BufferedReader;
Expand Down Expand Up @@ -134,7 +135,7 @@ public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNu
}
for (Map.Entry<String, String> variable : containerEnv.entrySet()) {
argb.add("-e");
argb.addMasked(variable.getKey()+"="+variable.getValue());
argb.addMasked(quoteArgument(variable));
}
argb.add(image).add(command);

Expand Down Expand Up @@ -388,4 +389,14 @@ public List<String> getVolumes(@Nonnull EnvVars launchEnv, String containerID) t
}
return Arrays.asList(volumes.replace("\\", "/").split("\\n"));
}

/**
* Convert key-value pair (variable) to Docker container environment variable argument.
* It handles some difficult cases like a variable containing spaces and quotes.
* @param variable a variable as a key-value pair.
* @return a string representation of the passed variable.
*/
private static String quoteArgument(Map.Entry<String, String> variable) {
return WindowsUtil.quoteArgument(variable.getKey() + "=" + variable.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,21 @@ private static void grep(File dir, String text, String prefix, Set<String> match
}
});
}

@Test public void insideContextOfEnvironmentVariablesWithSpecialCharacters(){
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
assumeDocker();
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" withEnv(['ENV_VAR=\"VARIABLE = VALUE WITH QUOTES \\\" AND SPACES \\\"\"']) {\n" +
" docker.image('busybox').inside { sh 'env' }\n" +
" }\n" +
"}", true));
WorkflowRun r = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("ENV_VAR=\"VARIABLE = VALUE WITH QUOTES \" AND SPACES \"\"", r);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
Expand Down Expand Up @@ -75,6 +76,41 @@ public void test_run() throws IOException, InterruptedException {
Assert.assertNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
}

@Test
public void test_run_with_env_vars() throws IOException, InterruptedException {
EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv();
EnvVars containerEnv = new EnvVars(
"VAR", "A",
"VAR_WITH_SPACE", "A B",
"VAR_WITH_EQUALS", "A=B",
"VAR_WITH_QUOTE", "A\"B",
"VAR_WITH_SPACE_AND_EQUALS", "A=B C",
"VAR_WITH_SPACE_EQUALS_AND_QUOTE", "A=\"B C\""
);
String containerId =
dockerClient.run(launchEnv, "learn/tutorial", null, null, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), containerEnv,
dockerClient.whoAmI(), "cat");
Assert.assertEquals(64, containerId.length());
ContainerRecord containerRecord = dockerClient.getContainerRecord(launchEnv, containerId);
Assert.assertEquals(dockerClient.inspect(launchEnv, "learn/tutorial", ".Id"), containerRecord.getImageId());
Assert.assertTrue(containerRecord.getContainerName().length() > 0);
Assert.assertTrue(containerRecord.getHost().length() > 0);
Assert.assertTrue(containerRecord.getCreated() > 1000000000000L);
Assert.assertEquals(Collections.<String>emptyList(), dockerClient.getVolumes(launchEnv, containerId));

String envVarsString = dockerClient.inspect(launchEnv, containerId, ".Config.Env");
Assert.assertNotNull(envVarsString);

for (Map.Entry<String, String> variable : containerEnv.entrySet()) {
Assert.assertTrue(envVarsString.contains(variable.getKey() + "=" + variable.getValue()));
}

// Also test that the stop works and cleans up after itself
Assert.assertNotNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
dockerClient.stop(launchEnv, containerId);
Assert.assertNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
}

@Test
public void test_valid_version() {
VersionNumber dockerVersion = DockerClient.parseVersionNumber("Docker version 1.5.0, build a8a31ef");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

public class WindowsDockerClientTest {

Expand Down Expand Up @@ -54,4 +55,46 @@ public void test_run() throws IOException, InterruptedException {
dockerClient.stop(launchEnv, containerId);
Assert.assertNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
}

@Test
public void test_run_with_env_vars() throws IOException, InterruptedException {
EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv();
EnvVars containerEnv = new EnvVars(
"VAR", "A",
"VAR_WITH_SPACE", "A B",
"VAR_WITH_EQUALS", "A=B",
"VAR_WITH_QUOTE", "A\"B",
"VAR_WITH_SPACE_AND_EQUALS", "A=B C",
"VAR_WITH_SPACE_EQUALS_AND_QUOTE", "A=\"B C\""
);
String containerId = dockerClient.run(
launchEnv,
"learn/tutorial",
null,
null,
Collections.emptyMap(),
Collections.emptyList(),
containerEnv,
dockerClient.whoAmI(),
"cat");
Assert.assertEquals(64, containerId.length());
ContainerRecord containerRecord = dockerClient.getContainerRecord(launchEnv, containerId);
Assert.assertEquals(dockerClient.inspect(launchEnv, "learn/tutorial", ".Id"), containerRecord.getImageId());
Assert.assertTrue(containerRecord.getContainerName().length() > 0);
Assert.assertTrue(containerRecord.getHost().length() > 0);
Assert.assertTrue(containerRecord.getCreated() > 1000000000000L);
Assert.assertEquals(Collections.<String>emptyList(), dockerClient.getVolumes(launchEnv, containerId));

String envVarsString = dockerClient.inspect(launchEnv, containerId, ".Config.Env");
Assert.assertNotNull(envVarsString);

for (Map.Entry<String, String> variable : containerEnv.entrySet()) {
Assert.assertTrue(envVarsString.contains(variable.getKey() + "=" + variable.getValue()));
}

// Also test that the stop works and cleans up after itself
Assert.assertNotNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
dockerClient.stop(launchEnv, containerId);
Assert.assertNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
}
}