Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit 4908203

Browse files
authored
Merge pull request #31 from raydouglass/current-user
Current user looks up remote user
2 parents bafa700 + 892ed99 commit 4908203

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/main/java/com/gpuopenanalytics/jenkins/remotedocker/config/UserConfigItem.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525
package com.gpuopenanalytics.jenkins.remotedocker.config;
2626

2727
import com.gpuopenanalytics.jenkins.remotedocker.AbstractDockerLauncher;
28-
import com.gpuopenanalytics.jenkins.remotedocker.DockerLauncher;
2928
import com.gpuopenanalytics.jenkins.remotedocker.Utils;
3029
import hudson.Extension;
31-
import hudson.model.AbstractBuild;
30+
import hudson.Launcher;
3231
import hudson.model.Descriptor;
3332
import hudson.util.ArgumentListBuilder;
3433
import net.sf.json.JSONObject;
@@ -39,7 +38,10 @@
3938

4039
import javax.annotation.Nonnull;
4140
import javax.annotation.Nullable;
41+
import java.io.ByteArrayOutputStream;
4242
import java.io.IOException;
43+
import java.nio.charset.StandardCharsets;
44+
import java.util.Arrays;
4345

4446
public class UserConfigItem extends ConfigItem {
4547

@@ -145,15 +147,33 @@ public void postCreate(AbstractDockerLauncher launcher) throws IOException, Inte
145147
}
146148
}
147149

150+
private String executeWithOutput(Launcher launcher, String... args) {
151+
try {
152+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
153+
int status = launcher.launch()
154+
.cmds(args)
155+
.stdout(baos)
156+
.stderr(launcher.getListener().getLogger())
157+
.join();
158+
if (status != 0) {
159+
throw new RuntimeException(
160+
"Non-zero status " + status + ": " + Arrays
161+
.toString(args));
162+
}
163+
return baos.toString(StandardCharsets.UTF_8.name()).trim();
164+
} catch (InterruptedException | IOException e) {
165+
throw new RuntimeException(e);
166+
}
167+
}
168+
148169
@Override
149170
public void addRunArgs(AbstractDockerLauncher launcher,
150171
ArgumentListBuilder args) {
151172
if (!isCurrentUser()) {
152173
args.add("--user", Utils.resolveVariables(launcher, username));
153174
} else {
154-
com.sun.security.auth.module.UnixSystem unix = new com.sun.security.auth.module.UnixSystem();
155-
long uid = unix.getUid();
156-
long gid = unix.getGid();
175+
String uid = executeWithOutput(launcher.getInner(), "id", "-u");
176+
String gid = executeWithOutput(launcher.getInner(), "id", "-g");
157177
args.add("--user", uid + ":" + gid);
158178
}
159179
}

0 commit comments

Comments
 (0)