Skip to content

Commit 8aa288b

Browse files
ndeloofYann Diorcet
authored and
Yann Diorcet
committed
Set --user on exec
1 parent 4e6026a commit 8aa288b

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.jenkinsci.plugins.docker.workflow;
2525

2626
import com.google.common.base.Optional;
27+
import hudson.util.ArgumentListBuilder;
2728
import org.jenkinsci.plugins.docker.workflow.client.DockerClient;
2829
import com.google.inject.Inject;
2930
import hudson.AbortException;
@@ -183,7 +184,8 @@ public static class Execution extends AbstractStepExecutionImpl {
183184
volumes.put(tmp, tmp);
184185
}
185186

186-
container = dockerClient.run(env, step.image, step.args, ws, volumes, volumesFromContainers, envReduced, dockerClient.whoAmI(), /* expected to hang until killed */ "cat");
187+
final String userId = dockerClient.whoAmI();
188+
container = dockerClient.run(env, step.image, step.args, ws, volumes, volumesFromContainers, envReduced, userId, /* expected to hang until killed */ "cat");
187189
final List<String> ps = dockerClient.listProcess(env, container);
188190
if (!ps.contains("cat")) {
189191
listener.error(
@@ -196,7 +198,7 @@ public static class Execution extends AbstractStepExecutionImpl {
196198
DockerFingerprints.addRunFacet(dockerClient.getContainerRecord(env, container), run);
197199
ImageAction.add(step.image, run);
198200
getContext().newBodyInvoker().
199-
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName, dockerVersion))).
201+
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, userId, toolName, dockerVersion))).
200202
withCallback(new Callback(container, toolName)).
201203
start();
202204
return false;
@@ -222,17 +224,19 @@ private static class Decorator extends LauncherDecorator implements Serializable
222224
private final String container;
223225
private final String[] envHost;
224226
private final String ws;
227+
private final String user;
225228
private final @CheckForNull String toolName;
226229
private final boolean hasEnv;
227230
private final boolean hasWorkdir;
228231

229-
Decorator(String container, EnvVars envHost, String ws, String toolName, VersionNumber dockerVersion) {
232+
Decorator(String container, EnvVars envHost, String ws, String user, String toolName, VersionNumber dockerVersion) {
230233
this.container = container;
231234
this.envHost = Util.mapToEnv(envHost);
232235
this.ws = ws;
233236
this.toolName = toolName;
234237
this.hasEnv = dockerVersion != null && dockerVersion.compareTo(new VersionNumber("1.13.0")) >= 0;
235238
this.hasWorkdir = dockerVersion != null && dockerVersion.compareTo(new VersionNumber("17.12")) >= 0;
239+
this.user = user;
236240
}
237241

238242
@Override public Launcher decorate(final Launcher launcher, final Node node) {
@@ -244,8 +248,15 @@ private static class Decorator extends LauncherDecorator implements Serializable
244248
} catch (InterruptedException x) {
245249
throw new IOException(x);
246250
}
251+
247252
List<String> prefix = new ArrayList<>(Arrays.asList(executable, "exec"));
248253
List<Boolean> masksPrefixList = new ArrayList<>(Arrays.asList(false, false));
254+
if (user != null) {
255+
prefix.add("-u");
256+
masksPrefixList.add(false);
257+
prefix.add(user);
258+
masksPrefixList.add(false);
259+
}
249260
if (ws != null) {
250261
FilePath cwd = starter.pwd();
251262
if (cwd != null) {

src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import hudson.util.ArgumentListBuilder;
3333
import hudson.util.VersionNumber;
3434
import org.jenkinsci.plugins.docker.commons.fingerprint.ContainerRecord;
35+
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
36+
import org.kohsuke.accmod.Restricted;
37+
import org.kohsuke.accmod.restrictions.NoExternalUse;
3538

3639
import javax.annotation.CheckForNull;
3740
import javax.annotation.Nonnull;
@@ -44,21 +47,18 @@
4447
import java.text.ParseException;
4548
import java.text.SimpleDateFormat;
4649
import java.util.ArrayList;
50+
import java.util.Arrays;
4751
import java.util.Collection;
4852
import java.util.Collections;
4953
import java.util.Date;
50-
import java.util.Map;
5154
import java.util.List;
52-
import java.util.Arrays;
55+
import java.util.Map;
5356
import java.util.StringTokenizer;
5457
import java.util.concurrent.TimeUnit;
5558
import java.util.logging.Level;
5659
import java.util.logging.Logger;
5760
import java.util.regex.Matcher;
5861
import java.util.regex.Pattern;
59-
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
60-
import org.kohsuke.accmod.Restricted;
61-
import org.kohsuke.accmod.restrictions.NoExternalUse;
6262

6363
/**
6464
* Simple docker client for Pipeline.
@@ -103,7 +103,7 @@ public DockerClient(@Nonnull Launcher launcher, @CheckForNull Node node, @CheckF
103103
* @param command The command to execute in the image container being run.
104104
* @return The container ID.
105105
*/
106-
public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNull String args, @CheckForNull String workdir, @Nonnull Map<String, String> volumes, @Nonnull Collection<String> volumesFromContainers, @Nonnull EnvVars containerEnv, @Nonnull String user, @Nonnull String... command) throws IOException, InterruptedException {
106+
public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNull String args, @CheckForNull String workdir, @Nonnull Map<String, String> volumes, @Nonnull Collection<String> volumesFromContainers, @Nonnull EnvVars containerEnv, @CheckForNull String user, @Nonnull String... command) throws IOException, InterruptedException {
107107
ArgumentListBuilder argb = new ArgumentListBuilder();
108108

109109
argb.add("run", "-t", "-d", "-u", user);

src/test/java/org/jenkinsci/plugins/docker/workflow/WithContainerStepTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,42 @@ public class WithContainerStepTest {
243243
});
244244
}
245245

246+
@Test public void withInitAndExecAsUser() throws Exception {
247+
story.addStep(new Statement() {
248+
@Override
249+
public void evaluate() throws Throwable {
250+
DockerTestUtil.assumeDocker();
251+
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
252+
p.setDefinition(new CpsFlowDefinition(
253+
"node {" +
254+
" withDockerContainer(args: '-e BUILDER_UID=1000 -e BUILDER_GID=1000 -e BUILDER_USER=jenkins -e BUILDER_GROUP=jenkins -e HOME=/home/jenkins', image: 'dockcross/manylinux-x64') {" +
255+
" sh 'stat -c \"%U %G\" /opt/python/cp36-cp36m/share/man'\n" +
256+
" }" +
257+
"}", true));
258+
WorkflowRun b = story.j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0));
259+
}
260+
});
261+
}
262+
263+
@Test public void withInitAsRootAndExecAsUser() throws Exception {
264+
story.addStep(new Statement() {
265+
@Override
266+
public void evaluate() throws Throwable {
267+
DockerTestUtil.assumeDocker();
268+
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "prj");
269+
p.setDefinition(new CpsFlowDefinition(
270+
"node {\n" +
271+
" withDockerContainer(args: '--user root:root -e BUILDER_UID=1000 -e BUILDER_GID=1000 -e BUILDER_USER=jenkins -e BUILDER_GROUP=jenkins -e HOME=/home/jenkins', image: 'dockcross/manylinux-x64') {\n" +
272+
" sh 'stat -c \"%U %G\" /opt/python/cp36-cp36m/share/man'\n" +
273+
" }\n" +
274+
"}\n", true));
275+
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
276+
story.j.assertLogContains("jenkins jenkins", b);
277+
}
278+
});
279+
}
280+
281+
246282
@Issue("JENKINS-27152")
247283
@Test public void configFile() throws Exception {
248284
story.addStep(new Statement() {

0 commit comments

Comments
 (0)