-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: os/user: add iterators over users and groups #47907
Comments
Change https://golang.org/cl/345592 mentions this issue: |
How does this deal with groups in NIS/AD/other non-local sources? Looking at the CL, iteration on Unix seems to only consult /etc/passwd and /etc/group. |
@neild when cgo is enabled |
@mjonaitis1 I think it would be better to port getpwent and getgrent from C to Go, otherwise pure-Go programs will work differently with regards to this functionality. |
The most common and desired case (in fact, the reason why we made this pull request) — the glibc's |
There is no POSIX api for such iteration and the implementation might trigger a huge number of element lookups when applied to a corporate user/group database with 100000 and more entries. I would prefer to call a more specialized user directory function in that case and work with libraries better suited for such lookups and filtering and did so when I needed it. |
Why does this need to be in the standard library? |
For an internal project, we needed an API to iterate over system users and groups. Go standard library package os/user already handles lookup, but not listing or iteration. While a third-party package for iteration can be done, code will need to be copied, since most of the machinery is private to os/user package. We think it may be useful to have iteration/listing to the broader community. Furthermore, we have some plans to open source an nss library in Golang. Since our library supports iteration (i.e. backs glibc’s getgrnam(3), getgrgid(3), getpwnam(3), getpwuid(3)), it would be helpful to have an API to piggy-back to; a standard way to iterate over users and groups would allow the library to support a single api and be a drop-in extension. |
Even if getpwent could be ported to pure Go, it might not be a good idea. The setpwent/getpwnent/endpwent appear to have been designed without much regard to multi-threading. For example, if two threads want to iterate and not be exposed to non-deterministic gaps, they must agree to coordinate their iteration sessions through some mutex. This is cumbersome and slow. So instead of porting these functions to go, we can simply reuse them since they are posix compliant. |
I don't think Damien's comment has really been answered. I also don't think #47907 (comment) has really been answered. It seems like this can easily live outside the standard library for the few programs that need it. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
My knowledge about nss is limited, but I assume that libc routines like
The main argument why it should be in the standard library is that there is already similar functionality in |
No change in consensus, so declined. |
Currently
os/user
package supports users and groups lookup via name or id. Some applications might need functionality to iterate over all available users/groups in the system. On unix there isgetent
command which can retrieve users or groups by callinggetent passwd
orgetent group
respectively. However, this is not very convenient. A go-native functionality could remove the need of calling external commands.Update: use slice returning function based api instead of callback based iterator api
After receiving some internal feedback, I was recommended to not use callback based iterator API. Since callback based api introduces a lot of unnecessary complexity, slice returning function API might be more reasonable.
Updated public facing API:
Example usage:
However, more feedback from go maintainers is needed in order to proceed.
Initial callback based proposal
Here is a proposed user facing public api in
os/user
:Example usage:
The text was updated successfully, but these errors were encountered: