-
Notifications
You must be signed in to change notification settings - Fork 67
/
uaa.go
215 lines (193 loc) · 5.62 KB
/
uaa.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package uaa
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/xchapter7x/lo"
uaaclient "github.com/cloudfoundry-community/go-uaa"
)
type uaa interface {
CreateUser(user uaaclient.User) (*uaaclient.User, error)
ListUsers(filter string, sortBy string, attributes string, sortOrder uaaclient.SortOrder, startIndex int, itemsPerPage int) ([]uaaclient.User, uaaclient.Page, error)
}
// Manager -
type Manager interface {
//Returns a map keyed and valued by user id. User id is converted to lowercase
ListUsers() (*Users, error)
CreateExternalUser(userName, userEmail, externalID, origin string) (err error)
}
// DefaultUAAManager -
type DefaultUAAManager struct {
Peek bool
Client uaa
Users *Users
}
type User struct {
Username string
ExternalID string
Email string
Origin string
GUID string
}
// NewDefaultUAAManager -
func NewDefaultUAAManager(sysDomain, clientID, clientSecret, userAgent string, httpClient *http.Client, peek bool) (Manager, error) {
target := fmt.Sprintf("https://uaa.%s", sysDomain)
client, err := uaaclient.New(
target,
uaaclient.WithClientCredentials(clientID, clientSecret, uaaclient.OpaqueToken),
uaaclient.WithUserAgent(userAgent),
uaaclient.WithClient(httpClient),
uaaclient.WithSkipSSLValidation(true),
)
if err != nil {
return nil, err
}
return &DefaultUAAManager{
Client: client,
Peek: peek,
}, nil
}
func (m *DefaultUAAManager) addUser(user User) {
if m.Users == nil {
m.Users = &Users{}
}
m.Users.Add(user)
}
// CreateExternalUser -
func (m *DefaultUAAManager) CreateExternalUser(userName, userEmail, externalID, origin string) error {
if userName == "" || userEmail == "" || externalID == "" {
return fmt.Errorf("skipping user as missing name[%s], email[%s] or externalID[%s]", userName, userEmail, externalID)
}
if m.Peek {
lo.G.Infof("[dry-run]: successfully added user [%s]", userName)
m.addUser(User{
Username: userName,
Email: userEmail,
ExternalID: externalID,
Origin: origin,
GUID: fmt.Sprintf("dry-run-%s-%s-guid", userName, origin),
})
return nil
}
createdUser, err := m.Client.CreateUser(uaaclient.User{
Username: userName,
ExternalID: externalID,
Origin: origin,
Emails: []uaaclient.Email{
{
Value: userEmail,
},
},
})
if err != nil {
var requestError uaaclient.RequestError
if errors.As(err, &requestError) {
return fmt.Errorf("got an error calling %s with response %s", requestError.Url, requestError.ErrorResponse)
}
return err
}
m.addUser(User{
Username: userName,
Email: userEmail,
ExternalID: externalID,
Origin: origin,
GUID: createdUser.ID,
})
lo.G.Infof("successfully added user [%s]", userName)
return nil
}
// ListUsers - returns uaa.Users
func (m *DefaultUAAManager) ListUsers() (*Users, error) {
if m.Users != nil {
return m.Users, nil
}
users := &Users{}
lo.G.Debug("Getting users from UAA")
userList, err := m.ListAllUsers()
if err != nil {
var requestError uaaclient.RequestError
if errors.As(err, &requestError) {
return nil, fmt.Errorf("got an error calling %s with response %s", requestError.Url, requestError.ErrorResponse)
}
return nil, err
}
lo.G.Debugf("Found %d users in the CF instance", len(userList))
for _, user := range userList {
userName := strings.Trim(user.Username, " ")
externalID := strings.Trim(user.ExternalID, " ")
lo.G.Debugf("Adding to users userID [%s], externalID [%s], origin [%s], email [%s], GUID [%s]", userName, externalID, user.Origin, Email(user), user.ID)
users.Add(User{
Username: userName,
ExternalID: user.ExternalID,
Email: Email(user),
Origin: user.Origin,
GUID: user.ID,
})
}
m.Users = users
return users, nil
}
func (m *DefaultUAAManager) ListAllUsers() ([]uaaclient.User, error) {
page := uaaclient.Page{
StartIndex: 1,
ItemsPerPage: 500,
}
var (
results []uaaclient.User
currentPage []uaaclient.User
err error
totalResults int
)
currentPage, page, err = m.Client.ListUsers("", "id", "userName,id,externalId,emails,origin", "", page.StartIndex, page.ItemsPerPage)
totalResults = page.TotalResults
if err != nil {
return nil, err
}
results = append(results, currentPage...)
if (page.StartIndex + page.ItemsPerPage) <= page.TotalResults {
page.StartIndex = page.StartIndex + page.ItemsPerPage
for {
currentPage, page, err = m.Client.ListUsers("", "id", "userName,id,externalId,emails,origin", "", page.StartIndex, page.ItemsPerPage)
if err != nil {
return nil, err
}
if totalResults != page.TotalResults {
lo.G.Infof("Result size changed during pagination from %d to %d", totalResults, page.TotalResults)
totalResults = page.TotalResults
}
results = append(results, currentPage...)
if (page.StartIndex + page.ItemsPerPage) > page.TotalResults {
break
}
page.StartIndex = page.StartIndex + page.ItemsPerPage
}
}
if len(results) != totalResults {
return nil, fmt.Errorf("results %d is not equal to expected results %d", len(results), totalResults)
}
uniqueMap := make(map[string]string)
for _, user := range results {
userKey := fmt.Sprintf("%s-%s", user.Username, user.Origin)
if userId, ok := uniqueMap[userKey]; ok {
return nil, fmt.Errorf("user with userName [%s], origin [%s], id [%s] already returned with id [%s]", user.Username, user.Origin, userId, user.ID)
} else {
uniqueMap[userKey] = user.ID
}
}
return results, nil
}
func Email(u uaaclient.User) string {
for _, email := range u.Emails {
if email.Primary == nil {
continue
}
if *email.Primary {
return email.Value
}
}
if len(u.Emails) > 0 {
return u.Emails[0].Value
}
return ""
}