24
24
package org .jenkinsci .plugins .docker .workflow ;
25
25
26
26
import com .google .common .base .Optional ;
27
+ import hudson .util .ArgumentListBuilder ;
27
28
import org .jenkinsci .plugins .docker .workflow .client .DockerClient ;
28
29
import com .google .inject .Inject ;
29
30
import hudson .AbortException ;
@@ -183,7 +184,8 @@ public static class Execution extends AbstractStepExecutionImpl {
183
184
volumes .put (tmp , tmp );
184
185
}
185
186
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" );
187
189
final List <String > ps = dockerClient .listProcess (env , container );
188
190
if (!ps .contains ("cat" )) {
189
191
listener .error (
@@ -196,7 +198,7 @@ public static class Execution extends AbstractStepExecutionImpl {
196
198
DockerFingerprints .addRunFacet (dockerClient .getContainerRecord (env , container ), run );
197
199
ImageAction .add (step .image , run );
198
200
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 ))).
200
202
withCallback (new Callback (container , toolName )).
201
203
start ();
202
204
return false ;
@@ -222,17 +224,19 @@ private static class Decorator extends LauncherDecorator implements Serializable
222
224
private final String container ;
223
225
private final String [] envHost ;
224
226
private final String ws ;
227
+ private final String user ;
225
228
private final @ CheckForNull String toolName ;
226
229
private final boolean hasEnv ;
227
230
private final boolean hasWorkdir ;
228
231
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 ) {
230
233
this .container = container ;
231
234
this .envHost = Util .mapToEnv (envHost );
232
235
this .ws = ws ;
233
236
this .toolName = toolName ;
234
237
this .hasEnv = dockerVersion != null && dockerVersion .compareTo (new VersionNumber ("1.13.0" )) >= 0 ;
235
238
this .hasWorkdir = dockerVersion != null && dockerVersion .compareTo (new VersionNumber ("17.12" )) >= 0 ;
239
+ this .user = user ;
236
240
}
237
241
238
242
@ Override public Launcher decorate (final Launcher launcher , final Node node ) {
@@ -244,8 +248,15 @@ private static class Decorator extends LauncherDecorator implements Serializable
244
248
} catch (InterruptedException x ) {
245
249
throw new IOException (x );
246
250
}
251
+
247
252
List <String > prefix = new ArrayList <>(Arrays .asList (executable , "exec" ));
248
253
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
+ }
249
260
if (ws != null ) {
250
261
FilePath cwd = starter .pwd ();
251
262
if (cwd != null ) {
0 commit comments