Skip to content

Commit 7db35a4

Browse files
authored
feat: allow security_opt and readonly_rootfs to be configured (#787)
1 parent 040b59f commit 7db35a4

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

testcontainers/src/core/containers/request.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub struct ContainerRequest<I: Image> {
3535
pub(crate) privileged: bool,
3636
pub(crate) cap_add: Option<Vec<String>>,
3737
pub(crate) cap_drop: Option<Vec<String>>,
38+
pub(crate) readonly_rootfs: bool,
39+
pub(crate) security_opts: Option<Vec<String>>,
3840
pub(crate) shm_size: Option<u64>,
3941
pub(crate) cgroupns_mode: Option<CgroupnsMode>,
4042
pub(crate) userns_mode: Option<String>,
@@ -198,6 +200,14 @@ impl<I: Image> ContainerRequest<I> {
198200
pub fn user(&self) -> Option<&str> {
199201
self.user.as_deref()
200202
}
203+
204+
pub fn security_opts(&self) -> Option<&Vec<String>> {
205+
self.security_opts.as_ref()
206+
}
207+
208+
pub fn readonly_rootfs(&self) -> bool {
209+
self.readonly_rootfs
210+
}
201211
}
202212

203213
impl<I: Image> From<I> for ContainerRequest<I> {
@@ -219,6 +229,8 @@ impl<I: Image> From<I> for ContainerRequest<I> {
219229
privileged: false,
220230
cap_add: None,
221231
cap_drop: None,
232+
security_opts: None,
233+
readonly_rootfs: false,
222234
shm_size: None,
223235
cgroupns_mode: None,
224236
userns_mode: None,

testcontainers/src/core/image/image_ext.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ pub trait ImageExt<I: Image> {
169169

170170
/// Sets the user that commands are run as inside the container.
171171
fn with_user(self, user: impl Into<String>) -> ContainerRequest<I>;
172+
173+
/// Sets the container's root filesystem to be mounted as read-only
174+
fn with_readonly_rootfs(self, readonly_rootfs: bool) -> ContainerRequest<I>;
175+
176+
/// Sets security options for the container
177+
fn with_security_opt(self, security_opt: impl Into<String>) -> ContainerRequest<I>;
172178
}
173179

174180
/// Implements the [`ImageExt`] trait for the every type that can be converted into a [`ContainerRequest`].
@@ -391,4 +397,22 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
391397
..container_req
392398
}
393399
}
400+
401+
fn with_readonly_rootfs(self, readonly_rootfs: bool) -> ContainerRequest<I> {
402+
let container_req = self.into();
403+
ContainerRequest {
404+
readonly_rootfs,
405+
..container_req
406+
}
407+
}
408+
409+
fn with_security_opt(self, security_opt: impl Into<String>) -> ContainerRequest<I> {
410+
let mut container_req = self.into();
411+
container_req
412+
.security_opts
413+
.get_or_insert_with(Vec::new)
414+
.push(security_opt.into());
415+
416+
container_req
417+
}
394418
}

testcontainers/src/runners/async_runner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ where
146146
userns_mode: container_req.userns_mode().map(|v| v.to_string()),
147147
cap_add: container_req.cap_add().cloned(),
148148
cap_drop: container_req.cap_drop().cloned(),
149+
readonly_rootfs: Some(container_req.readonly_rootfs()),
150+
security_opt: container_req.security_opts().cloned(),
149151
..Default::default()
150152
}),
151153
working_dir: container_req.working_dir().map(|dir| dir.to_string()),

0 commit comments

Comments
 (0)