Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit bd09788

Browse files
author
Tony Nyurkin
committed
1 parent 6220679 commit bd09788

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Flags:
119119
-e, --endpoint string AWS SSO SCIM API Endpoint
120120
-u, --google-admin string Google Workspace admin user email
121121
-c, --google-credentials string path to Google Workspace credentials file (default "credentials.json")
122-
-g, --group-match string Google Workspace Groups filter query parameter, example: 'name:Admin* email:aws-*', see: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups
122+
-g, --group-match strings Google Workspace Groups filter query parameter, example: 'name:Admin* email:aws-*', see: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups (You can specify this flag multiple times for OR clause)
123123
-h, --help help for ssosync
124124
--ignore-groups strings ignores these Google Workspace groups
125125
--ignore-users strings ignores these Google Workspace users

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func addFlags(cmd *cobra.Command, cfg *config.Config) {
172172
rootCmd.Flags().StringSliceVar(&cfg.IgnoreGroups, "ignore-groups", []string{}, "ignores these Google Workspace groups")
173173
rootCmd.Flags().StringSliceVar(&cfg.IncludeGroups, "include-groups", []string{}, "include only these Google Workspace groups, NOTE: only works when --sync-method 'users_groups'")
174174
rootCmd.Flags().StringVarP(&cfg.UserMatch, "user-match", "m", "", "Google Workspace Users filter query parameter, example: 'name:John* email:admin*', see: https://developers.google.com/admin-sdk/directory/v1/guides/search-users")
175-
rootCmd.Flags().StringVarP(&cfg.GroupMatch, "group-match", "g", "", "Google Workspace Groups filter query parameter, example: 'name:Admin* email:aws-*', see: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups")
175+
rootCmd.Flags().StringSliceVarP(&cfg.GroupMatch, "group-match", "g", []string{""}, "Google Workspace Groups filter query parameter, example: 'name:Admin* email:aws-*', see: https://developers.google.com/admin-sdk/directory/v1/guides/search-groups (You can specify this flag multiple times for OR clause)")
176176
rootCmd.Flags().StringVarP(&cfg.SyncMethod, "sync-method", "s", config.DefaultSyncMethod, "Sync method to use (users_groups|groups)")
177177
}
178178

internal/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Config struct {
1616
// UserMatch ...
1717
UserMatch string `mapstructure:"user_match"`
1818
// GroupFilter ...
19-
GroupMatch string `mapstructure:"group_match"`
19+
GroupMatch []string `mapstructure:"group_match"`
2020
// SCIMEndpoint ....
2121
SCIMEndpoint string `mapstructure:"scim_endpoint"`
2222
// SCIMAccessToken ...

internal/sync.go

+43-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"io/ioutil"
22+
"time"
2223

2324
"github.com/awslabs/ssosync/internal/aws"
2425
"github.com/awslabs/ssosync/internal/config"
@@ -32,8 +33,8 @@ import (
3233
// SyncGSuite is the interface for synchronizing users/groups
3334
type SyncGSuite interface {
3435
SyncUsers(string) error
35-
SyncGroups(string) error
36-
SyncGroupsUsers(string) error
36+
SyncGroups([]string) error
37+
SyncGroupsUsers([]string) error
3738
}
3839

3940
// SyncGSuite is an object type that will synchronize real users and groups
@@ -165,10 +166,8 @@ func (s *syncGSuite) SyncUsers(query string) error {
165166
// name:contact* email:contact*
166167
// name:Admin* email:aws-*
167168
// email:aws-*
168-
func (s *syncGSuite) SyncGroups(query string) error {
169-
170-
log.WithField("query", query).Debug("get google groups")
171-
googleGroups, err := s.google.GetGroups(query)
169+
func (s *syncGSuite) SyncGroups(queries []string) error {
170+
googleGroups, err := s.getGroups(queries)
172171
if err != nil {
173172
return err
174173
}
@@ -270,10 +269,8 @@ func (s *syncGSuite) SyncGroups(query string) error {
270269
// 4) add groups in aws and add its members, these were added in google
271270
// 5) validate equals aws an google groups members
272271
// 6) delete groups in aws, these were deleted in google
273-
func (s *syncGSuite) SyncGroupsUsers(query string) error {
274-
275-
log.WithField("query", query).Info("get google groups")
276-
googleGroups, err := s.google.GetGroups(query)
272+
func (s *syncGSuite) SyncGroupsUsers(queries []string) error {
273+
googleGroups, err := s.getGroups(queries)
277274
if err != nil {
278275
return err
279276
}
@@ -359,14 +356,16 @@ func (s *syncGSuite) SyncGroupsUsers(query string) error {
359356
// add aws users (added in google)
360357
log.Debug("creating aws users added in google")
361358
for _, awsUser := range addAWSUsers {
359+
user, _ := s.aws.FindUserByEmail(awsUser.Username)
360+
if user == nil {
361+
log := log.WithFields(log.Fields{"user": awsUser.Username})
362362

363-
log := log.WithFields(log.Fields{"user": awsUser.Username})
364-
365-
log.Info("creating user")
366-
_, err := s.aws.CreateUser(awsUser)
367-
if err != nil {
368-
log.Error("error creating user")
369-
return err
363+
log.Info("creating user")
364+
_, err := s.aws.CreateUser(awsUser)
365+
if err != nil {
366+
log.Error("error creating user")
367+
return err
368+
}
370369
}
371370
}
372371

@@ -503,6 +502,7 @@ func (s *syncGSuite) getGoogleGroupsAndUsers(googleGroups []*admin.Group) ([]*ad
503502

504503
log.WithField("id", m.Email).Debug("get user")
505504
q := fmt.Sprintf("email:%s", m.Email)
505+
time.Sleep(1 * time.Second)
506506
u, err := s.google.GetUsers(q) // TODO: implement GetUser(m.Email)
507507
if err != nil {
508508
return nil, nil, err
@@ -555,6 +555,32 @@ func (s *syncGSuite) getAWSGroupsAndUsers(awsGroups []*aws.Group, awsUsers []*aw
555555
return awsGroupsUsers, nil
556556
}
557557

558+
// getGroups returns Google Groups from multiple queries.
559+
func (s *syncGSuite) getGroups(queries []string) ([]*admin.Group, error) {
560+
uniqueGroups := map[string]*admin.Group{}
561+
562+
for _, query := range queries {
563+
log.WithField("query", query).Debug("get google groups")
564+
googleGroups, err := s.google.GetGroups(query)
565+
if err != nil {
566+
return nil, err
567+
}
568+
569+
for _, group := range googleGroups {
570+
uniqueGroups[group.Id] = group
571+
}
572+
}
573+
574+
groups := make([]*admin.Group, len(uniqueGroups))
575+
var i int
576+
for _, group := range uniqueGroups {
577+
groups[i] = group
578+
i++
579+
}
580+
581+
return groups, nil
582+
}
583+
558584
// getGroupOperations returns the groups of AWS that must be added, deleted and are equals
559585
func getGroupOperations(awsGroups []*aws.Group, googleGroups []*admin.Group) (add []*aws.Group, delete []*aws.Group, equals []*aws.Group) {
560586

0 commit comments

Comments
 (0)