diff --git a/testcontainers/src/core/containers/request.rs b/testcontainers/src/core/containers/request.rs index a4044e2e..65c33453 100644 --- a/testcontainers/src/core/containers/request.rs +++ b/testcontainers/src/core/containers/request.rs @@ -35,6 +35,8 @@ pub struct ContainerRequest { pub(crate) privileged: bool, pub(crate) cap_add: Option>, pub(crate) cap_drop: Option>, + pub(crate) readonly_rootfs: bool, + pub(crate) security_opts: Option>, pub(crate) shm_size: Option, pub(crate) cgroupns_mode: Option, pub(crate) userns_mode: Option, @@ -198,6 +200,14 @@ impl ContainerRequest { pub fn user(&self) -> Option<&str> { self.user.as_deref() } + + pub fn security_opts(&self) -> Option<&Vec> { + self.security_opts.as_ref() + } + + pub fn readonly_rootfs(&self) -> bool { + self.readonly_rootfs + } } impl From for ContainerRequest { @@ -219,6 +229,8 @@ impl From for ContainerRequest { privileged: false, cap_add: None, cap_drop: None, + security_opts: None, + readonly_rootfs: false, shm_size: None, cgroupns_mode: None, userns_mode: None, diff --git a/testcontainers/src/core/image/image_ext.rs b/testcontainers/src/core/image/image_ext.rs index 907a9b24..29a51544 100644 --- a/testcontainers/src/core/image/image_ext.rs +++ b/testcontainers/src/core/image/image_ext.rs @@ -169,6 +169,12 @@ pub trait ImageExt { /// Sets the user that commands are run as inside the container. fn with_user(self, user: impl Into) -> ContainerRequest; + + /// Sets the container's root filesystem to be mounted as read-only + fn with_readonly_rootfs(self, readonly_rootfs: bool) -> ContainerRequest; + + /// Sets security options for the container + fn with_security_opt(self, security_opt: impl Into) -> ContainerRequest; } /// Implements the [`ImageExt`] trait for the every type that can be converted into a [`ContainerRequest`]. @@ -391,4 +397,22 @@ impl>, I: Image> ImageExt for RI { ..container_req } } + + fn with_readonly_rootfs(self, readonly_rootfs: bool) -> ContainerRequest { + let container_req = self.into(); + ContainerRequest { + readonly_rootfs, + ..container_req + } + } + + fn with_security_opt(self, security_opt: impl Into) -> ContainerRequest { + let mut container_req = self.into(); + container_req + .security_opts + .get_or_insert_with(Vec::new) + .push(security_opt.into()); + + container_req + } } diff --git a/testcontainers/src/runners/async_runner.rs b/testcontainers/src/runners/async_runner.rs index 3d98bf1d..84335151 100644 --- a/testcontainers/src/runners/async_runner.rs +++ b/testcontainers/src/runners/async_runner.rs @@ -146,6 +146,8 @@ where userns_mode: container_req.userns_mode().map(|v| v.to_string()), cap_add: container_req.cap_add().cloned(), cap_drop: container_req.cap_drop().cloned(), + readonly_rootfs: Some(container_req.readonly_rootfs()), + security_opt: container_req.security_opts().cloned(), ..Default::default() }), working_dir: container_req.working_dir().map(|dir| dir.to_string()),