-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
147 lines (113 loc) · 3.43 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package admin
import (
"context"
"fmt"
"strings"
"github.com/raito-io/cli/base/util/config"
gcpadmin "google.golang.org/api/admin/directory/v1"
"github.com/raito-io/cli-plugin-gcp/internal/common"
"github.com/raito-io/cli-plugin-gcp/internal/iam"
)
const maxPageItems = 100
type AdminRepository struct {
client *gcpadmin.Service
customerId string
}
func NewAdminRepository(client *gcpadmin.Service, configMap *config.ConfigMap) *AdminRepository {
return &AdminRepository{
client: client,
customerId: configMap.GetString(common.GsuiteCustomerId),
}
}
func (r *AdminRepository) GetUsers(ctx context.Context, fn func(ctx context.Context, entity *iam.UserEntity) error) error {
nextPageToken := ""
for {
usersCall := r.client.Users.List().Customer(r.customerId).MaxResults(maxPageItems)
if nextPageToken != "" {
usersCall.PageToken(nextPageToken)
}
users, err := usersCall.Do()
if common.IsGoogle400Error(err) {
common.Logger.Warn(fmt.Sprintf("Encountered 4xx error while fetching users: %s", err.Error()))
continue
} else if err != nil {
return fmt.Errorf("listing users: %s", err.Error())
}
for _, u := range users.Users {
if u.Suspended {
continue
}
err = fn(ctx, &iam.UserEntity{ExternalId: fmt.Sprintf("user:%s", u.PrimaryEmail), Name: u.Name.FullName, Email: u.PrimaryEmail})
if err != nil {
return err
}
}
if users.NextPageToken != "" {
nextPageToken = users.NextPageToken
} else {
break
}
}
return nil
}
func (r *AdminRepository) GetGroups(ctx context.Context, fn func(ctx context.Context, entity *iam.GroupEntity) error) error {
nextPageToken := ""
for {
groupsCall := r.client.Groups.List().Customer(r.customerId).MaxResults(maxPageItems)
if nextPageToken != "" {
groupsCall.PageToken(nextPageToken)
}
groups, err := groupsCall.Do()
if common.IsGoogle400Error(err) {
common.Logger.Warn(fmt.Sprintf("Encountered 4xx error while fetching groups: %s", err.Error()))
continue
} else if err != nil {
return fmt.Errorf("listing groups: %s", err.Error())
}
for _, g := range groups.Groups {
groupMembers, err2 := r.groupMembers(g.Id)
if err2 != nil {
return fmt.Errorf("group members of group %q: %w", g.Id, err2)
}
err2 = fn(ctx, &iam.GroupEntity{ExternalId: fmt.Sprintf("group:%s", g.Email), Email: g.Email, Members: groupMembers})
if err2 != nil {
return err2
}
}
if groups.NextPageToken != "" {
nextPageToken = groups.NextPageToken
} else {
break
}
}
return nil
}
func (r *AdminRepository) groupMembers(groupId string) ([]string, error) {
nextPageToken := ""
var memberIds []string
for {
membersCall := r.client.Members.List(groupId).MaxResults(maxPageItems)
if nextPageToken != "" {
membersCall.PageToken(nextPageToken)
}
members, err := membersCall.Do()
if err != nil {
return nil, fmt.Errorf("fetching members for group %s: %s", groupId, err.Error())
}
for _, m := range members.Members {
if strings.EqualFold(m.Type, "user") {
memberIds = append(memberIds, fmt.Sprintf("user:%s", m.Email))
} else if strings.EqualFold(m.Type, "group") {
memberIds = append(memberIds, fmt.Sprintf("group:%s", m.Email))
} else {
common.Logger.Warn(fmt.Sprintf("Found unknown member type %s for group %s (id: %s; email: %s)", m.Type, groupId, m.Id, m.Email))
}
}
if members.NextPageToken != "" {
nextPageToken = members.NextPageToken
} else {
break
}
}
return memberIds, nil
}