-
Notifications
You must be signed in to change notification settings - Fork 9
fix: fall back to getent for users that are not findable in /etc/passwd #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tonyandrewmeyer
merged 8 commits into
canonical:main
from
tonyandrewmeyer:fix-username-lookup-no-passwd
May 19, 2026
+122
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d475091
fix: fall back to getent for users that are not findable in /etc/passwd
tonyandrewmeyer 8fa8f73
Merge branch 'main' into fix-username-lookup-no-passwd
tonyandrewmeyer 7668056
Fall back on only the specific error.
tonyandrewmeyer e7a10f2
Drive-by bump of Go from .1 to .2 to clear out the static check failu…
tonyandrewmeyer 45bfaff
Explain how the test still passes even on the problem systems.
tonyandrewmeyer a4f41db
Merge branch 'main' into fix-username-lookup-no-passwd
tonyandrewmeyer 767e354
fix(system): Distinguish getent failure modes
tonyandrewmeyer 0feaf48
Merge branch 'main' into fix-username-lookup-no-passwd
tonyandrewmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package system | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os/exec" | ||
| "os/user" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLookupUserGetent(t *testing.T) { | ||
| // Verify getent is available on this system. | ||
| if _, err := exec.LookPath("getent"); err != nil { | ||
| t.Skip("getent not available on this system") | ||
| } | ||
|
|
||
| // user.Current works even for SSSD/LDAP users not in /etc/passwd, because | ||
| // it reads the UID from /proc/self rather than searching /etc/passwd by | ||
| // name. Use it as the reference to verify lookupUserGetent returns the | ||
| // same information. | ||
| current, err := user.Current() | ||
| if err != nil { | ||
| t.Fatalf("user.Current() failed: %v", err) | ||
| } | ||
|
|
||
| got, err := lookupUserGetent(current.Username) | ||
| if err != nil { | ||
| t.Fatalf("lookupUserGetent(%q) failed: %v", current.Username, err) | ||
| } | ||
|
|
||
| if got.Username != current.Username { | ||
| t.Errorf("Username: got %q, want %q", got.Username, current.Username) | ||
| } | ||
| if got.Uid != current.Uid { | ||
| t.Errorf("Uid: got %q, want %q", got.Uid, current.Uid) | ||
| } | ||
| if got.Gid != current.Gid { | ||
| t.Errorf("Gid: got %q, want %q", got.Gid, current.Gid) | ||
| } | ||
| if got.HomeDir != current.HomeDir { | ||
| t.Errorf("HomeDir: got %q, want %q", got.HomeDir, current.HomeDir) | ||
| } | ||
| } | ||
|
|
||
| func TestLookupUserGetentUnknownUser(t *testing.T) { | ||
| if _, err := exec.LookPath("getent"); err != nil { | ||
| t.Skip("getent not available on this system") | ||
| } | ||
|
|
||
| _, err := lookupUserGetent("nonexistent-user-that-should-not-exist") | ||
| var unknownUserErr user.UnknownUserError | ||
| if !errors.As(err, &unknownUserErr) { | ||
| t.Fatalf("expected user.UnknownUserError, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestLookupUserGetentBinaryMissing(t *testing.T) { | ||
| t.Cleanup(func() { getentBinary = "getent" }) | ||
| getentBinary = "/nonexistent/path/to/getent" | ||
|
|
||
| _, err := lookupUserGetent("anyone") | ||
| if err == nil { | ||
| t.Fatal("expected error when getent binary is missing, got nil") | ||
| } | ||
| // A missing binary must not be reported as "unknown user" — that would | ||
| // hide the real failure from the operator. | ||
| var unknownUserErr user.UnknownUserError | ||
| if errors.As(err, &unknownUserErr) { | ||
| t.Fatalf("missing binary should not surface as UnknownUserError, got %v", err) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.