Skip to content

chore: shell out to getent to get user home directory#30

Open
kian99 wants to merge 1 commit intojuju:masterfrom
kian99:improve-get-home
Open

chore: shell out to getent to get user home directory#30
kian99 wants to merge 1 commit intojuju:masterfrom
kian99:improve-get-home

Conversation

@kian99
Copy link

@kian99 kian99 commented Jul 8, 2025

I encountered the following error when running a Juju test that uses the Mongo suite.
mgo.go:566: failed to find HOME directory: UNIX user 1591302234 not found

This only occured after switching to a Canonical corporate laptop. I was able to trace the issue down to how we determine the user's home directory in tests that spin up Mongo.

On coporate laptops with authd, the user's account does not exist in /etc/passwd. Instead NSS (Name Server Switch) is used to obtain the user's details. To fix the issue, shell out to getent passwd <userID> to get the user's home directory and fallback to reading /etc/passwd in case the command execution fails.

Some semi-related docs that helped explain the issue can be found here and here.

On coporate laptops with authd, the user's account does not exist in /etc/passwd. Instead NSS (Name Server Switch) is used to contact an external service for auth details.
Shell out to `get passwd <userID>` to get the user's home directory and fallback to reading /etc/passwd.
@kian99
Copy link
Author

kian99 commented Jul 8, 2025

Tests are failing with,

07:02:07 [github-make-check-mgo] $ /bin/sh /tmp/jenkins4416084786602636508.sh
07:02:07 Bad series noble
07:02:07 Build step 'Execute shell' marked build as failure

// falling back to reading /etc/passwd if the command fails.
// In some environments /etc/passwd may not be available or
// is not the source of truth (e.g., LDAP, NSS).
// And don't use os/user package as it requires CGO on Linux.
Copy link
Member

Choose a reason for hiding this comment

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

That's not quite true.

For most Unix systems, this package has two internal implementations of resolving user and group ids to names, and listing supplementary group IDs. One is written in pure Go and parses /etc/passwd and /etc/group. The other is cgo-based and relies on the standard C library (libc) routines such as getpwuid_r, getgrnam_r, and getgrouplist.

See: https://pkg.go.dev/os/user

Copy link
Author

Choose a reason for hiding this comment

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

Yes true, let me know if you want me to add add that clarification, in our case reading /etc/passwd is insufficient and the implementation we want relies on CGO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants