Skip to content

Commit d2449fb

Browse files
authored
Merge pull request #32 from rust-lang/docker-user
Allow using an arbitrary Docker image instead of forcing crates-build-env
2 parents 4b82a3a + 9744cf8 commit d2449fb

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- **BREAKING**: all functions and methods inside `cmd` now return `CommandError`.
2424
- `winapi` is no longer required on unix; `nix` is no longer required on windows.
2525
- Relaxed lifetime restrictions of `Build::cmd` and `Build::cargo`.
26+
- The requirement of using an image similar to `crates-build-env` has been
27+
lifted, and it's now possible to use any Docker image for the sandbox.
2628

2729
## [0.9.0] - 2020-07-01
2830

src/cmd/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ impl<'w, 'pl> Command<'w, 'pl> {
388388
.workdir(container_dirs::WORK_DIR.to_str().unwrap())
389389
.cmd(cmd);
390390

391-
if let Some(user_id) = native::current_user() {
392-
builder = builder.env("MAP_USER_ID", user_id.to_string());
391+
if let Some(user) = native::current_user() {
392+
builder = builder.user(user.user_id, user.group_id);
393393
}
394394

395395
for (key, value) in self.env {

src/cmd/sandbox.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub struct SandboxBuilder {
143143
memory_limit: Option<usize>,
144144
cpu_limit: Option<f32>,
145145
workdir: Option<String>,
146+
user: Option<String>,
146147
cmd: Vec<String>,
147148
enable_networking: bool,
148149
}
@@ -156,6 +157,7 @@ impl SandboxBuilder {
156157
workdir: None,
157158
memory_limit: None,
158159
cpu_limit: None,
160+
user: None,
159161
cmd: Vec::new(),
160162
enable_networking: true,
161163
}
@@ -216,6 +218,11 @@ impl SandboxBuilder {
216218
self
217219
}
218220

221+
pub(super) fn user(mut self, user: u32, group: u32) -> Self {
222+
self.user = Some(format!("{}:{}", user, group));
223+
self
224+
}
225+
219226
fn create(self, workspace: &Workspace) -> Result<Container<'_>, CommandError> {
220227
let mut args: Vec<String> = vec!["create".into()];
221228

@@ -253,6 +260,11 @@ impl SandboxBuilder {
253260
args.push(limit.to_string());
254261
}
255262

263+
if let Some(user) = self.user {
264+
args.push("--user".into());
265+
args.push(user);
266+
}
267+
256268
if !self.enable_networking {
257269
args.push("--network".into());
258270
args.push("none".into());

src/native/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ pub(crate) use self::unix::*;
77
mod windows;
88
#[cfg(windows)]
99
pub(crate) use self::windows::*;
10+
11+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
12+
pub(crate) struct CurrentUser {
13+
pub(crate) user_id: u32,
14+
pub(crate) group_id: u32,
15+
}

src/native/unix.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::CurrentUser;
12
use crate::cmd::KillFailedError;
23
use failure::Error;
34
use nix::{
@@ -24,20 +25,21 @@ pub(crate) fn kill_process(id: u32) -> Result<(), KillFailedError> {
2425
}
2526
}
2627

27-
pub(crate) fn current_user() -> Option<u32> {
28-
Some(Uid::effective().into())
29-
}
30-
31-
fn current_group() -> u32 {
32-
Gid::effective().into()
28+
pub(crate) fn current_user() -> Option<CurrentUser> {
29+
Some(CurrentUser {
30+
user_id: Uid::effective().into(),
31+
group_id: Gid::effective().into(),
32+
})
3333
}
3434

3535
fn executable_mode_for(path: &Path) -> Result<u32, Error> {
3636
let metadata = path.metadata()?;
3737

38-
if metadata.uid() == current_user().unwrap() {
38+
let user = current_user().unwrap();
39+
40+
if metadata.uid() == user.user_id {
3941
Ok(EXECUTABLE_BITS << 6)
40-
} else if metadata.gid() == current_group() {
42+
} else if metadata.gid() == user.group_id {
4143
Ok(EXECUTABLE_BITS << 3)
4244
} else {
4345
Ok(EXECUTABLE_BITS)
@@ -65,6 +67,7 @@ pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Result<(), Error> {
6567

6668
#[cfg(test)]
6769
mod tests {
70+
use super::CurrentUser;
6871
use nix::unistd::{Gid, Uid};
6972
use std::fs::File;
7073
use std::os::unix::process::ExitStatusExt;
@@ -82,12 +85,13 @@ mod tests {
8285

8386
#[test]
8487
fn test_current_user() {
85-
assert_eq!(super::current_user(), Some(u32::from(Uid::effective())));
86-
}
87-
88-
#[test]
89-
fn test_current_group() {
90-
assert_eq!(super::current_group(), u32::from(Gid::effective()));
88+
assert_eq!(
89+
super::current_user(),
90+
Some(CurrentUser {
91+
user_id: u32::from(Uid::effective()),
92+
group_id: u32::from(Gid::effective()),
93+
})
94+
);
9195
}
9296

9397
#[test]

src/native/windows.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use super::CurrentUser;
12
use crate::cmd::KillFailedError;
2-
use failure::Error;
3+
use failure::{bail, Error};
34
use std::fs::File;
45
use std::path::Path;
56
use winapi::um::handleapi::CloseHandle;
@@ -25,7 +26,7 @@ pub(crate) fn kill_process(id: u32) -> Result<(), KillFailedError> {
2526
Ok(())
2627
}
2728

28-
pub(crate) fn current_user() -> Option<u32> {
29+
pub(crate) fn current_user() -> Option<CurrentUser> {
2930
None
3031
}
3132

tests/buildtest/inside_docker.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ impl CommandExt for Command {
6363
fn map_user_group(&mut self) -> Result<&mut Self, Error> {
6464
use std::os::unix::fs::MetadataExt;
6565
let gid = std::fs::metadata(DOCKER_SOCKET)?.gid();
66-
self.arg("-e")
67-
.arg(format!("MAP_USER_ID={}", nix::unistd::Uid::effective()))
68-
.arg("-e")
69-
.arg(format!("MAP_GROUP_ID={}", gid));
66+
let uid = nix::unistd::Uid::effective();
67+
68+
self.arg("--user").arg(format!("{}:{}", uid, gid));
7069
Ok(self)
7170
}
7271

0 commit comments

Comments
 (0)