1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
24 type resourceList interface {
26 GetItems() []interface{}
29 // GroupInfo tracks previous and current members of a particular Group
30 type GroupInfo struct {
32 PreviousMembers map[string]bool
33 CurrentMembers map[string]bool
36 // GetUserID returns the correct user id value depending on the selector
37 func GetUserID(u arvados.User, idSelector string) (string, error) {
42 return u.Username, nil
44 return "", fmt.Errorf("cannot identify user by %q selector", idSelector)
48 // UserList implements resourceList interface
49 type UserList struct {
53 // Len returns the amount of items this list holds
54 func (l UserList) Len() int {
58 // GetItems returns the list of items
59 func (l UserList) GetItems() (out []interface{}) {
60 for _, item := range l.Items {
61 out = append(out, item)
66 // GroupList implements resourceList interface
67 type GroupList struct {
71 // Len returns the amount of items this list holds
72 func (l GroupList) Len() int {
76 // GetItems returns the list of items
77 func (l GroupList) GetItems() (out []interface{}) {
78 for _, item := range l.Items {
79 out = append(out, item)
84 // LinkList implements resourceList interface
85 type LinkList struct {
89 // Len returns the amount of items this list holds
90 func (l LinkList) Len() int {
94 // GetItems returns the list of items
95 func (l LinkList) GetItems() (out []interface{}) {
96 for _, item := range l.Items {
97 out = append(out, item)
103 // Parse & validate arguments, set up arvados client.
104 cfg, err := GetConfig()
106 log.Fatalf("%v", err)
109 if err := doMain(&cfg); err != nil {
110 log.Fatalf("%v", err)
114 // ConfigParams holds configuration data for this tool
115 type ConfigParams struct {
119 ParentGroupUUID string
120 ParentGroupName string
122 Client *arvados.Client
125 // ParseFlags parses and validates command line arguments
126 func ParseFlags(config *ConfigParams) error {
127 // Acceptable attributes to identify a user on the CSV file
128 userIDOpts := map[string]bool{
129 "email": true, // default
133 flags := flag.NewFlagSet("arv-sync-groups", flag.ExitOnError)
135 // Set up usage message
136 flags.Usage = func() {
137 usageStr := `Synchronize remote groups into Arvados from a CSV format file with 2 columns:
138 * 1st column: Group name
139 * 2nd column: User identifier`
140 fmt.Fprintf(os.Stderr, "%s\n\n", usageStr)
141 fmt.Fprintf(os.Stderr, "Usage:\n%s [OPTIONS] <input-file.csv>\n\n", os.Args[0])
142 fmt.Fprintf(os.Stderr, "Options:\n")
143 flags.PrintDefaults()
146 // Set up option flags
147 userID := flags.String(
150 "Attribute by which every user is identified. Valid values are: email and username.")
151 verbose := flags.Bool(
154 "Log informational messages. Off by default.")
155 getVersion := flags.Bool(
158 "Print version information and exit.")
159 parentGroupUUID := flags.String(
162 "Use given group UUID as a parent for the remote groups. Should be owned by the system user. If not specified, a group named '"+config.ParentGroupName+"' will be used (and created if nonexistant).")
164 // Parse args; omit the first arg which is the command name
165 flags.Parse(os.Args[1:])
167 // Print version information if requested
169 fmt.Printf("arv-sync-groups %s\n", version)
173 // Input file as a required positional argument
174 if flags.NArg() == 0 {
175 return fmt.Errorf("please provide a path to an input file")
177 srcPath := &os.Args[flags.NFlag()+1]
181 return fmt.Errorf("input file path invalid")
183 if !userIDOpts[*userID] {
185 for opt := range userIDOpts {
186 options = append(options, opt)
188 return fmt.Errorf("user ID must be one of: %s", strings.Join(options, ", "))
191 config.Path = *srcPath
192 config.ParentGroupUUID = *parentGroupUUID
193 config.UserID = *userID
194 config.Verbose = *verbose
199 // SetParentGroup finds/create parent group of all remote groups
200 func SetParentGroup(cfg *ConfigParams) error {
201 var parentGroup arvados.Group
202 if cfg.ParentGroupUUID == "" {
203 // UUID not provided, search for preexisting parent group
205 params := arvados.ResourceListParams{
206 Filters: []arvados.Filter{{
209 Operand: cfg.ParentGroupName,
213 Operand: cfg.SysUserUUID,
216 if err := cfg.Client.RequestAndDecode(&gl, "GET", "/arvados/v1/groups", nil, params); err != nil {
217 return fmt.Errorf("error searching for parent group: %s", err)
219 if len(gl.Items) == 0 {
220 // Default parent group not existant, create one.
222 log.Println("Default parent group not found, creating...")
224 groupData := map[string]string{
225 "name": cfg.ParentGroupName,
226 "owner_uuid": cfg.SysUserUUID,
228 if err := CreateGroup(cfg, &parentGroup, groupData); err != nil {
229 return fmt.Errorf("error creating system user owned group named %q: %s", groupData["name"], err)
231 } else if len(gl.Items) == 1 {
232 // Default parent group found.
233 parentGroup = gl.Items[0]
235 // This should never happen, as there's an unique index for
236 // (owner_uuid, name) on groups.
237 return fmt.Errorf("bug: found %d groups owned by system user and named %q", len(gl.Items), cfg.ParentGroupName)
239 cfg.ParentGroupUUID = parentGroup.UUID
241 // UUID provided. Check if exists and if it's owned by system user
242 if err := GetGroup(cfg, &parentGroup, cfg.ParentGroupUUID); err != nil {
243 return fmt.Errorf("error searching for parent group with UUID %q: %s", cfg.ParentGroupUUID, err)
245 if parentGroup.OwnerUUID != cfg.SysUserUUID {
246 return fmt.Errorf("parent group %q (%s) must be owned by system user", parentGroup.Name, cfg.ParentGroupUUID)
252 // GetConfig sets up a ConfigParams struct
253 func GetConfig() (config ConfigParams, err error) {
254 config.ParentGroupName = "Externally synchronized groups"
257 err = ParseFlags(&config)
262 // Arvados Client setup
263 config.Client = arvados.NewClientFromEnv()
265 // Check current user permissions & get System user's UUID
266 u, err := config.Client.CurrentUser()
268 return config, fmt.Errorf("error getting the current user: %s", err)
270 if !u.IsActive || !u.IsAdmin {
271 return config, fmt.Errorf("current user (%s) is not an active admin user", u.UUID)
273 config.SysUserUUID = u.UUID[:12] + "000000000000000"
275 // Set up remote groups' parent
276 if err = SetParentGroup(&config); err != nil {
283 func doMain(cfg *ConfigParams) error {
284 // Try opening the input file early, just in case there's a problem.
285 f, err := os.Open(cfg.Path)
287 return fmt.Errorf("%s", err)
291 log.Printf("arv-sync-groups %s started. Using %q as users id and parent group UUID %q", version, cfg.UserID, cfg.ParentGroupUUID)
293 // Get the complete user list to minimize API Server requests
294 allUsers := make(map[string]arvados.User)
295 userIDToUUID := make(map[string]string) // Index by email or username
296 results, err := GetAll(cfg.Client, "users", arvados.ResourceListParams{}, &UserList{})
298 return fmt.Errorf("error getting user list: %s", err)
300 log.Printf("Found %d users", len(results))
301 for _, item := range results {
302 u := item.(arvados.User)
304 uID, err := GetUserID(u, cfg.UserID)
308 userIDToUUID[uID] = u.UUID
310 log.Printf("Seen user %q (%s)", u.Username, u.Email)
314 // Get remote groups and their members
315 remoteGroups, groupNameToUUID, err := GetRemoteGroups(cfg, allUsers)
319 log.Printf("Found %d remote groups", len(remoteGroups))
321 membershipsRemoved := 0
324 groupsCreated, membershipsAdded, membershipsSkipped, err := ProcessFile(cfg, f, userIDToUUID, groupNameToUUID, remoteGroups, allUsers)
329 // Remove previous members not listed on this run
330 for groupUUID := range remoteGroups {
331 gi := remoteGroups[groupUUID]
332 evictedMembers := subtract(gi.PreviousMembers, gi.CurrentMembers)
333 groupName := gi.Group.Name
334 if len(evictedMembers) > 0 {
335 log.Printf("Removing %d users from group %q", len(evictedMembers), groupName)
337 for evictedUser := range evictedMembers {
338 if err := RemoveMemberFromGroup(cfg, allUsers[userIDToUUID[evictedUser]], gi.Group); err != nil {
344 log.Printf("Groups created: %d. Memberships added: %d, removed: %d, skipped: %d", groupsCreated, membershipsAdded, membershipsRemoved, membershipsSkipped)
349 // ProcessFile reads the CSV file and process every record
353 userIDToUUID map[string]string,
354 groupNameToUUID map[string]string,
355 remoteGroups map[string]*GroupInfo,
356 allUsers map[string]arvados.User,
357 ) (groupsCreated, membersAdded, membersSkipped int, err error) {
359 csvReader := csv.NewReader(f)
360 csvReader.FieldsPerRecord = 2
362 record, e := csvReader.Read()
368 err = fmt.Errorf("error parsing %q, line %d", cfg.Path, lineNo)
371 groupName := strings.TrimSpace(record[0])
372 groupMember := strings.TrimSpace(record[1]) // User ID (username or email)
373 if groupName == "" || groupMember == "" {
374 log.Printf("Warning: CSV record has at least one empty field (%s, %s). Skipping", groupName, groupMember)
378 if _, found := userIDToUUID[groupMember]; !found {
379 // User not present on the system, skip.
380 log.Printf("Warning: there's no user with %s %q on the system, skipping.", cfg.UserID, groupMember)
384 if _, found := groupNameToUUID[groupName]; !found {
385 // Group doesn't exist, create it before continuing
387 log.Printf("Remote group %q not found, creating...", groupName)
389 var newGroup arvados.Group
390 groupData := map[string]string{
392 "owner_uuid": cfg.ParentGroupUUID,
393 "group_class": "role",
395 if e := CreateGroup(cfg, &newGroup, groupData); e != nil {
396 err = fmt.Errorf("error creating group named %q: %s", groupName, err)
399 // Update cached group data
400 groupNameToUUID[groupName] = newGroup.UUID
401 remoteGroups[newGroup.UUID] = &GroupInfo{
403 PreviousMembers: make(map[string]bool), // Empty set
404 CurrentMembers: make(map[string]bool), // Empty set
408 // Both group & user exist, check if user is a member
409 groupUUID := groupNameToUUID[groupName]
410 gi := remoteGroups[groupUUID]
411 if !gi.PreviousMembers[groupMember] && !gi.CurrentMembers[groupMember] {
413 log.Printf("Adding %q to group %q", groupMember, groupName)
415 // User wasn't a member, but should be.
416 if e := AddMemberToGroup(cfg, allUsers[userIDToUUID[groupMember]], gi.Group); e != nil {
422 gi.CurrentMembers[groupMember] = true
427 // GetAll : Adds all objects of type 'resource' to the 'allItems' list
428 func GetAll(c *arvados.Client, res string, params arvados.ResourceListParams, page resourceList) (allItems []interface{}, err error) {
429 // Use the maximum page size the server allows
431 params.Limit = &limit
433 params.Order = "uuid"
435 if err = GetResourceList(c, &page, res, params); err != nil {
438 // Have we finished paging?
442 for _, i := range page.GetItems() {
443 allItems = append(allItems, i)
445 params.Offset += page.Len()
450 func subtract(setA map[string]bool, setB map[string]bool) map[string]bool {
451 result := make(map[string]bool)
452 for element := range setA {
454 result[element] = true
460 func jsonReader(rscName string, ob interface{}) io.Reader {
461 j, err := json.Marshal(ob)
466 v[rscName] = []string{string(j)}
467 return bytes.NewBufferString(v.Encode())
470 // GetRemoteGroups fetches all remote groups with their members
471 func GetRemoteGroups(cfg *ConfigParams, allUsers map[string]arvados.User) (remoteGroups map[string]*GroupInfo, groupNameToUUID map[string]string, err error) {
472 remoteGroups = make(map[string]*GroupInfo)
473 groupNameToUUID = make(map[string]string) // Index by group name
475 params := arvados.ResourceListParams{
476 Filters: []arvados.Filter{{
479 Operand: cfg.ParentGroupUUID,
482 results, err := GetAll(cfg.Client, "groups", params, &GroupList{})
484 return remoteGroups, groupNameToUUID, fmt.Errorf("error getting remote groups: %s", err)
486 for _, item := range results {
487 group := item.(arvados.Group)
488 // Group -> User filter
489 g2uFilter := arvados.ResourceListParams{
490 Filters: []arvados.Filter{{
493 Operand: cfg.SysUserUUID,
497 Operand: "permission",
509 Operand: "arvados#user",
512 // User -> Group filter
513 u2gFilter := arvados.ResourceListParams{
514 Filters: []arvados.Filter{{
517 Operand: cfg.SysUserUUID,
521 Operand: "permission",
525 Operand: "can_write",
533 Operand: "arvados#user",
536 g2uLinks, err := GetAll(cfg.Client, "links", g2uFilter, &LinkList{})
538 return remoteGroups, groupNameToUUID, fmt.Errorf("error getting member (can_read) links for group %q: %s", group.Name, err)
540 u2gLinks, err := GetAll(cfg.Client, "links", u2gFilter, &LinkList{})
542 return remoteGroups, groupNameToUUID, fmt.Errorf("error getting member (can_write) links for group %q: %s", group.Name, err)
544 // Build a list of user ids (email or username) belonging to this group
545 membersSet := make(map[string]bool)
546 u2gLinkSet := make(map[string]bool)
547 for _, l := range u2gLinks {
548 linkedMemberUUID := l.(arvados.Link).TailUUID
549 u2gLinkSet[linkedMemberUUID] = true
551 for _, item := range g2uLinks {
552 link := item.(arvados.Link)
553 // We may have received an old link pointing to a removed account.
554 if _, found := allUsers[link.HeadUUID]; !found {
557 // The matching User -> Group link may not exist if the link
558 // creation failed on a previous run. If that's the case, don't
559 // include this account on the "previous members" list.
560 if _, found := u2gLinkSet[link.HeadUUID]; !found {
563 memberID, err := GetUserID(allUsers[link.HeadUUID], cfg.UserID)
565 return remoteGroups, groupNameToUUID, err
567 membersSet[memberID] = true
569 remoteGroups[group.UUID] = &GroupInfo{
571 PreviousMembers: membersSet,
572 CurrentMembers: make(map[string]bool), // Empty set
574 groupNameToUUID[group.Name] = group.UUID
576 return remoteGroups, groupNameToUUID, nil
579 // RemoveMemberFromGroup remove all links related to the membership
580 func RemoveMemberFromGroup(cfg *ConfigParams, user arvados.User, group arvados.Group) error {
582 log.Printf("Getting group membership links for user %q (%s) on group %q (%s)", user.Email, user.UUID, group.Name, group.UUID)
584 var links []interface{}
585 // Search for all group<->user links (both ways)
586 for _, filterset := range [][]arvados.Filter{
591 Operand: "permission",
605 Operand: "permission",
616 l, err := GetAll(cfg.Client, "links", arvados.ResourceListParams{Filters: filterset}, &LinkList{})
618 userID, _ := GetUserID(user, cfg.UserID)
619 return fmt.Errorf("error getting links needed to remove user %q from group %q: %s", userID, group.Name, err)
621 for _, link := range l {
622 links = append(links, link)
625 for _, item := range links {
626 link := item.(arvados.Link)
627 userID, _ := GetUserID(user, cfg.UserID)
629 log.Printf("Removing %q permission link for %q on group %q", link.Name, userID, group.Name)
631 if err := DeleteLink(cfg, link.UUID); err != nil {
632 return fmt.Errorf("error removing user %q from group %q: %s", userID, group.Name, err)
638 // AddMemberToGroup create membership links
639 func AddMemberToGroup(cfg *ConfigParams, user arvados.User, group arvados.Group) error {
640 var newLink arvados.Link
641 linkData := map[string]string{
642 "owner_uuid": cfg.SysUserUUID,
643 "link_class": "permission",
645 "tail_uuid": group.UUID,
646 "head_uuid": user.UUID,
648 if err := CreateLink(cfg, &newLink, linkData); err != nil {
649 userID, _ := GetUserID(user, cfg.UserID)
650 return fmt.Errorf("error adding group %q -> user %q read permission: %s", group.Name, userID, err)
652 linkData = map[string]string{
653 "owner_uuid": cfg.SysUserUUID,
654 "link_class": "permission",
656 "tail_uuid": user.UUID,
657 "head_uuid": group.UUID,
659 if err := CreateLink(cfg, &newLink, linkData); err != nil {
660 userID, _ := GetUserID(user, cfg.UserID)
661 return fmt.Errorf("error adding user %q -> group %q write permission: %s", userID, group.Name, err)
666 // CreateGroup creates a group with groupData parameters, assigns it to dst
667 func CreateGroup(cfg *ConfigParams, dst *arvados.Group, groupData map[string]string) error {
668 return cfg.Client.RequestAndDecode(dst, "POST", "/arvados/v1/groups", jsonReader("group", groupData), nil)
671 // GetGroup fetches a group by its UUID
672 func GetGroup(cfg *ConfigParams, dst *arvados.Group, groupUUID string) error {
673 return cfg.Client.RequestAndDecode(&dst, "GET", "/arvados/v1/groups/"+groupUUID, nil, nil)
676 // CreateLink creates a link with linkData parameters, assigns it to dst
677 func CreateLink(cfg *ConfigParams, dst *arvados.Link, linkData map[string]string) error {
678 return cfg.Client.RequestAndDecode(dst, "POST", "/arvados/v1/links", jsonReader("link", linkData), nil)
681 // DeleteLink deletes a link by its UUID
682 func DeleteLink(cfg *ConfigParams, linkUUID string) error {
684 return fmt.Errorf("cannot delete link with invalid UUID: %q", linkUUID)
686 return cfg.Client.RequestAndDecode(&arvados.Link{}, "DELETE", "/arvados/v1/links/"+linkUUID, nil, nil)
689 // GetResourceList fetches res list using params
690 func GetResourceList(c *arvados.Client, dst *resourceList, res string, params interface{}) error {
691 return c.RequestAndDecode(dst, "GET", "/arvados/v1/"+res, nil, params)