Skip to content

libpriv/passwd: move passwd database to Rust#2348

Merged
openshift-merge-robot merged 1 commit into
coreos:masterfrom
lucab:ups/passwd-rs
Jan 12, 2021
Merged

libpriv/passwd: move passwd database to Rust#2348
openshift-merge-robot merged 1 commit into
coreos:masterfrom
lucab:ups/passwd-rs

Conversation

@lucab
Copy link
Copy Markdown
Contributor

@lucab lucab commented Dec 4, 2020

This moves to Rust the in-memory structure holding passwd entries
(users and groups).

{
GLNX_AUTO_PREFIX_ERROR ("Converting /var to tmpfiles.d", error);

g_autoptr(RpmOstreePasswdDB) pwdb = rpmostree_passwddb_open (rootfs_dfd, cancellable, error);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note: the cancellable was not really used even in the C logic, so I just dropped it here.

@ashcrow ashcrow requested review from cgwalters and removed request for ashcrow December 4, 2020 15:53
@lucab
Copy link
Copy Markdown
Contributor Author

lucab commented Dec 4, 2020

Self-shaming: I originally made a typo-mistake when porting the logic, and CI saved my bacon.

travier
travier previously approved these changes Dec 7, 2020
Copy link
Copy Markdown
Member

@travier travier left a comment

Choose a reason for hiding this comment

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

LGTM

@cgwalters
Copy link
Copy Markdown
Member

cgwalters commented Dec 7, 2020

I'm not opposed to this and am OK merging mostly as is if you prefer but the added ratio of "unsafe FFI glue" here is the same reason I abandoned my attempt to use this approach for oxidizing the origin file.

To me the biggest value using Rust comes when we have nontrivial logic inside the Rust. The original usage of parsing the treefile was a perfect example; serde gives so much better error messages, allowed us to implement treefile include: and add inline unit tests conveniently etc. We aren't doing that here yet right?

So WDYT about trying this again after #2336 lands? It's getting closer.

@lucab
Copy link
Copy Markdown
Contributor Author

lucab commented Dec 8, 2020

/hold

@ashcrow
Copy link
Copy Markdown
Member

ashcrow commented Dec 8, 2020

Rebase needed

@cgwalters
Copy link
Copy Markdown
Member

OK (next year or whenever you're ready) want to take a crack at rebasing on cxx-rs work from #2336 ?

@lucab
Copy link
Copy Markdown
Contributor Author

lucab commented Jan 12, 2021

/hold cancel

Reworked on top of cxxbridge, this is now ready for review.

Comment thread rust/src/passwd.rs
let c_path: CUtf8Buf = passwd_path.to_string().into();
let db_ptr = self as *mut Self;
let mut gerror: *mut glib_sys::GError = std::ptr::null_mut();
// TODO(lucab): find a replacement for `fgetpwent` and drop this.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd like to get rid of the TODO after landing this PR. I can either look at adding fgetpwent_r to the libc crate, or port the existing logic to Rust.

I have a slight preference for the latter as it is a small line-based parser, and the unsafe string handling is quite cumbersome anyway. @cgwalters @jlebon thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, rewriting in Rust seems probably better here - if there's an existing crate that'd be an even more obvious path. But it's a simple format anyways.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/kstep/parsswd looks a bit stale but it's the best candidate I found on crates.io.
But it doesn't look much aligned with the glibc logic above. I'll spelunk a bit more, possibly upstreaming a port of the glibc parser there.

Copy link
Copy Markdown
Member

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

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

Overall LGTM, a few minor bits.

Comment thread rust/src/passwd.rs Outdated
match self.groups.get(&key) {
Some(group) => Ok(group.clone()),
None => bail!("failed to find group ID '{}'", gid),
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is totally fine, I am just personally trying to use combinators more lately, so it'd look more like

self.groups.get(&key).cloned().ok_or_else(|| anyhow!("failed to find group ID '{}'", gid))

Comment thread rust/src/passwd.rs
let c_path: CUtf8Buf = passwd_path.to_string().into();
let db_ptr = self as *mut Self;
let mut gerror: *mut glib_sys::GError = std::ptr::null_mut();
// TODO(lucab): find a replacement for `fgetpwent` and drop this.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, rewriting in Rust seems probably better here - if there's an existing crate that'd be an even more obvious path. But it's a simple format anyways.

Comment thread rust/src/ffiutil.rs Outdated
if res != 0 {
Ok(())
} else {
anyhow::ensure!(!gerror.is_null(), "unknown failure (NULL Gerror)");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd lean towards aborting here (i.e. panic!()) since it's a bad bug if it happens, and it really shouldn't - it's definitely not a "user error".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ack, I've turned this one into an assert!(),

This moves to Rust the in-memory structure holding passwd entries
(users and groups).
@cgwalters
Copy link
Copy Markdown
Member

Thanks a ton for holding on this until the cxx-rs stuff had landed! I think the result is worth it.
This one will definitely be cleaner once we go pure Rust too.
/lgtm

@openshift-ci-robot
Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cgwalters, lucab

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-robot openshift-merge-robot merged commit bdf8269 into coreos:master Jan 12, 2021
@lucab lucab deleted the ups/passwd-rs branch January 13, 2021 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants