Skip to content

Commit 6d5f8ee

Browse files
test(oci): cover default and policy identities in the oci-image suite
Add two scenarios to the shared oci-image feature suite: - An image without USER, with no policy identity, runs as the numeric default 1000:1000 without the supplementary groups of the image's UID 1000 account. - A policy run_as_user and run_as_group replace an image's USER root. Both check the main process, sandbox exec, and the workload container user. Factor policy temp files into a small helper. Closes #4431. Signed-off-by: Matthew Grossman <[email protected]>
1 parent 82977e2 commit 6d5f8ee

1 file changed

Lines changed: 73 additions & 9 deletions

File tree

‎tests/suites/features/oci-image/tests/oci_image.rs‎

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,66 @@ USER 2345:2346
9494
"
9595
),
9696
)?;
97-
let policy_file = tempfile::NamedTempFile::new()
98-
.map_err(|error| format!("create policy file: {error}"))?;
99-
std::fs::write(policy_file.path(), IMAGE_IDENTITY_POLICY)
100-
.map_err(|error| format!("write policy file: {error}"))?;
101-
let policy = policy_file
102-
.path()
103-
.to_str()
104-
.ok_or("policy path is not UTF-8")?;
97+
let policy = PolicyFile::new(IMAGE_IDENTITY_POLICY)?;
10598
let checks = workspace_checks("2345:2346", "/home/app/project", true);
106-
let sandbox = create_sandbox(runner, "pu", &image, Some(policy), &checks).await?;
99+
let sandbox =
100+
create_sandbox(runner, "pu", &image, Some(policy.path()?), &checks).await?;
107101
container_checks(engine, &sandbox, &image, "2345:2346")
108102
},
109103
)
110104
.await;
111105
}
112106

107+
/// An image without `USER`, with no policy identity, runs as the numeric
108+
/// default `1000:1000`. The base image has an account with UID 1000 in other
109+
/// groups, such as `sudo`, and the default identity does not get them.
110+
#[tokio::test]
111+
async fn userless_image_uses_default_identity() {
112+
run("oci-image/userless-default", async |runner, engine| {
113+
let image = engine.build_image("userless", &format!("FROM {BASE_IMAGE}\n"))?;
114+
let policy = PolicyFile::new(IMAGE_IDENTITY_POLICY)?;
115+
let checks = format!(
116+
"{} test \"$(id -G)\" = 1000;",
117+
workspace_checks("1000:1000", "/sandbox", false)
118+
);
119+
let sandbox = create_sandbox(runner, "ud", &image, Some(policy.path()?), &checks).await?;
120+
container_checks(engine, &sandbox, &image, "1000:1000")
121+
})
122+
.await;
123+
}
124+
125+
/// A policy `run_as_user` and `run_as_group` replace an image's `USER root`.
126+
/// The image has no `WORKDIR`, so the managed `/sandbox` workspace is
127+
/// writable by the policy identity and only identity resolution is tested.
128+
#[tokio::test]
129+
async fn policy_identity_overrides_root_image_user() {
130+
run("oci-image/policy-over-root", async |runner, engine| {
131+
let image = engine.build_image(
132+
"root-user",
133+
&format!(
134+
"FROM {BASE_IMAGE}
135+
RUN groupadd -g 4235 appstaff && useradd -m -u 4234 -g appstaff app
136+
USER root
137+
"
138+
),
139+
)?;
140+
let policy = PolicyFile::new(&format!(
141+
"{IMAGE_IDENTITY_POLICY}
142+
process:
143+
run_as_user: \"4234\"
144+
run_as_group: \"4235\"
145+
"
146+
))?;
147+
let checks = format!(
148+
"{} test \"$(id -G)\" = 4235;",
149+
workspace_checks("4234:4235", "/sandbox", false)
150+
);
151+
let sandbox = create_sandbox(runner, "pr", &image, Some(policy.path()?), &checks).await?;
152+
container_checks(engine, &sandbox, &image, "4234:4235")
153+
})
154+
.await;
155+
}
156+
113157
/// An image without a `WORKDIR` uses the managed `/sandbox` workspace, owned
114158
/// by the image user, even when the image does not contain `/sandbox`.
115159
#[tokio::test]
@@ -507,6 +551,26 @@ async fn exec(
507551
.await
508552
}
509553

554+
/// A sandbox policy written to a temporary file for `sandbox create --policy`.
555+
struct PolicyFile(tempfile::NamedTempFile);
556+
557+
impl PolicyFile {
558+
fn new(contents: &str) -> Result<Self, String> {
559+
let file = tempfile::NamedTempFile::new()
560+
.map_err(|error| format!("create policy file: {error}"))?;
561+
std::fs::write(file.path(), contents)
562+
.map_err(|error| format!("write policy file: {error}"))?;
563+
Ok(Self(file))
564+
}
565+
566+
fn path(&self) -> Result<&str, String> {
567+
self.0
568+
.path()
569+
.to_str()
570+
.ok_or_else(|| "policy path is not UTF-8".to_string())
571+
}
572+
}
573+
510574
/// A built fixture's mutable tag and image ID captured before sandbox creation.
511575
struct TestImage {
512576
tag: String,

0 commit comments

Comments
 (0)