Skip to content

Commit

Permalink
[antlir2][clone] use facts db to get user information
Browse files Browse the repository at this point in the history
Summary:
This was half done already in the depgraph analysis, but was not completed for
the compilation part.

I also fixed a bug where setting `user+group` was using the `src_layer` ids and
not the destination, oops! All existing usage was just to forcibly change
ownership to `root:root`, so no one noticed.

Test Plan:
```
❯ buck2 test fbcode//antlir/antlir2/features/clone/...
Buck UI: https://www.internalfb.com/buck2/13713e1d-085c-4dad-ade7-c632b3d3c7cc
Test UI: https://www.internalfb.com/intern/testinfra/testrun/17451448589444477
Note:    Using experimental modern dice
Network: Up: 0B  Down: 0B
Jobs completed: 68. Time elapsed: 1.8s.
Tests finished: Pass 20. Fail 0. Fatal 0. Skip 0. Build failure 0
```

Reviewed By: sergeyfd

Differential Revision: D57226818

fbshipit-source-id: 41a4fb31b1413d40827e1a4939ac1c9021218899
  • Loading branch information
vmagro authored and facebook-github-bot committed May 14, 2024
1 parent 8199184 commit 674ba60
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
1 change: 0 additions & 1 deletion antlir/antlir2/features/clone/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ feature_impl(
deps = [
"walkdir",
"//antlir/antlir2/antlir2_facts:antlir2_facts",
"//antlir/antlir2/antlir2_users:antlir2_users",
],
)
61 changes: 25 additions & 36 deletions antlir/antlir2/features/clone/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use antlir2_depgraph::item::Path as PathItem;
use antlir2_depgraph::requires_provides::Requirement;
use antlir2_depgraph::requires_provides::Validator;
use antlir2_facts::fact::dir_entry::DirEntry;
use antlir2_facts::fact::user::Group;
use antlir2_facts::fact::user::User;
use antlir2_features::types::GroupName;
use antlir2_features::types::LayerInfo;
use antlir2_features::types::PathInLayer;
use antlir2_features::types::UserName;
use antlir2_users::group::EtcGroup;
use antlir2_users::passwd::EtcPasswd;
use anyhow::Context;
use anyhow::Result;
use serde::Deserialize;
Expand Down Expand Up @@ -276,52 +276,41 @@ impl antlir2_compile::CompileFeature for Clone {
// sure that we look up the src ids and copy the _names_ instead of
// just the ids

let src_userdb: EtcPasswd =
std::fs::read_to_string(self.src_layer.subvol_symlink.join("etc/passwd"))
.and_then(|s| s.parse().map_err(std::io::Error::other))
.unwrap_or_else(|_| Default::default());
let src_groupdb: EtcGroup =
std::fs::read_to_string(self.src_layer.subvol_symlink.join("etc/group"))
.and_then(|s| s.parse().map_err(std::io::Error::other))
.unwrap_or_else(|_| Default::default());
let src_facts = antlir2_facts::RoDatabase::open(&self.src_layer.facts_db)
.context("while opening src_layer facts db")?;

let meta = entry.metadata().map_err(std::io::Error::from)?;

let (new_uid, new_gid) = match &self.usergroup {
Some(usergroup) => (
ctx.uid(
&src_userdb
.get_user_by_name(&usergroup.user)
.with_context(|| {
format!("src_layer missing passwd entry for {}", usergroup.user)
})?
.name,
)?,
ctx.gid(
&src_groupdb
.get_group_by_name(&usergroup.group)
.with_context(|| {
format!("src_layer missing group entry for {}", usergroup.group)
})?
.name,
)?,
),
Some(usergroup) => (ctx.uid(&usergroup.user)?, ctx.gid(&usergroup.group)?),
None => (
ctx.uid(
&src_userdb
.get_user_by_id(meta.uid().into())
src_facts
.iter::<User>()
.context("while iterating over src users")?
.find(|u| u.id() == meta.uid())
.with_context(|| {
format!("src_layer missing passwd entry for {}", meta.uid())
format!(
"src_layer {} missing entry for user id {}",
self.src_layer.label,
meta.uid()
)
})?
.name,
.name(),
)?,
ctx.gid(
&src_groupdb
.get_group_by_id(meta.gid().into())
src_facts
.iter::<Group>()
.context("while iterating over src groups")?
.find(|g| g.id() == meta.gid())
.with_context(|| {
format!("src_layer missing group entry for {}", meta.gid())
format!(
"src_layer {} missing entry for group id {}",
self.src_layer.label,
meta.gid()
)
})?
.name,
.name(),
)?,
),
};
Expand Down
23 changes: 17 additions & 6 deletions antlir/antlir2/features/clone/tests/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ image.layer(
gid = 10,
groupname = "antlir",
),
feature.user_add(
home_dir = "/",
primary_group = "bar",
shell = "/fakeshell",
uid = 11,
username = "foo",
),
feature.group_add(
gid = 12,
groupname = "bar",
),
],
)

Expand All @@ -228,22 +239,22 @@ image_diff_test(
)

image.layer(
name = "clone-file-chown-root",
name = "clone-file-chown-user",
features = [
feature.clone(
dst_path = "/cloned-file",
group = "root",
group = "bar",
src_layer = ":clone-src",
src_path = "/f",
user = "root",
user = "foo",
),
],
parent_layer = ":base-ids-remap",
)

image_diff_test(
name = "clone-file-chown-root-test",
diff = "clone-file-root.toml",
name = "clone-file-chown-user-test",
diff = "clone-file-user.toml",
diff_type = "file",
layer = ":clone-file-chown-root",
layer = ":clone-file-chown-user",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ op = 'added'
[file.cloned-file.diff]
mode = 'u+r,g+r,o+r'
file-type = 'regular-file'
user = "root"
group = "root"
user = "foo"
group = "bar"
text = '''
This file will be cloned!
'''

0 comments on commit 674ba60

Please sign in to comment.