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

kaps: create container with uid and gid from the spec #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Cargo.lock

.idea
*.tar.gz
.vscode
ctr-bundle
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ authors = ["Polytech Montpellier - DevOps"]
clap = { version = "3.0.5", features = ["derive"] }
lazy_static = "1.4.0"
oci-spec = "0.5.3"
unshare = { git = "https://github.com/virt-do/unshare", branch = "main" }
# unshare = { git = "https://github.com/virt-do/unshare", branch = "main" }
unshare = { path = "../unshare" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you fixed/improved the unsharecrate, please send a PR against https://github.com/virt-do/unshare

32 changes: 32 additions & 0 deletions src/container/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use oci_spec::runtime::Spec;
use unshare::{UidMap, GidMap};

use crate::container::environment::Environment;
use command::Command;
Expand Down Expand Up @@ -29,6 +30,8 @@ pub type Result<T> = std::result::Result<T, Error>;
/// Some OCI constants useful for our container implementation.
const OCI_RUNTIME_SPEC_FILE: &str = "config.json";
const OCI_RUNTIME_SPEC_ROOTFS: &str = "rootfs";
const OCI_RUNTIME_SPEC_USER_UID: u32 = 0;
const OCI_RUNTIME_SPEC_USER_GID: u32 = 0;

/// The `Container` struct provides a simple way to
/// create and run a container on the host.
Expand All @@ -44,6 +47,10 @@ pub struct Container {
environment: Environment,
/// The command entrypoint
command: Command,
/// User uid, default: 0 (root)
uid: u32,
/// User gid, default: 0 (root)
gid: u32,
}

impl Container {
Expand Down Expand Up @@ -72,11 +79,24 @@ impl Container {
Namespaces::from(linux.namespaces())
});

let uid: u32 = spec
.process()
.as_ref()
.map_or(OCI_RUNTIME_SPEC_USER_UID, |process| process.user().uid());

let gid: u32 = spec
.process()
.as_ref()
.map_or(OCI_RUNTIME_SPEC_USER_GID, |process| process.user().gid());


Ok(Container {
environment: Environment::from(spec.process()),
command: Command::from(spec.process()),
namespaces,
rootfs,
uid,
gid,
..Default::default()
})
}
Expand All @@ -87,6 +107,18 @@ impl Container {
let code = unsafe {
unshare::Command::from(&self.command)
.chroot_dir(&self.rootfs)
.set_id_maps(
vec![UidMap {
count: 1,
inside_uid: 0,
outside_uid: 0,
}],
vec![GidMap {
count: 1,
inside_gid: 0,
outside_gid: 0,
}],
)
.unshare(&*self.namespaces.get())
.pre_exec(move || Mounts::apply(&mounts))
.envs(self.environment.get())
Expand Down