-
Notifications
You must be signed in to change notification settings - Fork 60
/
okta.go
279 lines (227 loc) · 7.33 KB
/
okta.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package syncer
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"sync"
"github.com/okta/okta-sdk-golang/v2/okta/query"
"github.com/okta/okta-sdk-golang/v2/okta"
userv1 "github.com/openshift/api/user/v1"
"github.com/redhat-cop/group-sync-operator/api/v1alpha1"
"github.com/redhat-cop/group-sync-operator/pkg/constants"
"github.com/redhat-cop/operator-utils/pkg/util"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/validation"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)
var (
oktaLogger = logf.Log.WithName("syncer_okta")
)
const (
// API token given by Okta application
secretOktaTokenKey = "okta-api-token"
activeStatus = "ACTIVE"
)
type OktaSyncer struct {
cachedGroups map[string]*okta.Group
cachedGroupMembers map[string][]*okta.User
credentialsSecret *corev1.Secret
goOkta *okta.Client
GroupSync *v1alpha1.GroupSync
Name string
Provider *v1alpha1.OktaProvider
ReconcilerBase util.ReconcilerBase
}
func (o *OktaSyncer) Init() bool {
o.cachedGroupMembers = make(map[string][]*okta.User)
o.cachedGroups = make(map[string]*okta.Group)
if o.Provider.GroupLimit == 0 {
o.Provider.GroupLimit = 1000
}
if o.Provider.ProfileKey == "" {
o.Provider.ProfileKey = "login"
return true
}
return false
}
func (o *OktaSyncer) Validate() error {
const validations = 2
validationErrors := make([]error, validations)
credentialsSecret, err := o.getSecrets()
if err != nil {
validationErrors = append(validationErrors, err)
} else {
if _, found := credentialsSecret.Data[secretOktaTokenKey]; !found {
validationErrors = append(validationErrors, fmt.Errorf("could not find api token '%s' in namespace '%s'", o.Provider.CredentialsSecret.Name, o.Provider.CredentialsSecret.Namespace))
}
o.credentialsSecret = credentialsSecret
}
if _, err := url.ParseRequestURI(o.Provider.URL); err != nil {
validationErrors = append(validationErrors, err)
}
return utilerrors.NewAggregate(validationErrors)
}
func (o OktaSyncer) getSecrets() (*corev1.Secret, error) {
credentialsSecret := &corev1.Secret{}
nameSpacedName := types.NamespacedName{
Name: o.Provider.CredentialsSecret.Name,
Namespace: o.Provider.CredentialsSecret.Namespace,
}
err := o.ReconcilerBase.GetClient().Get(context.TODO(), nameSpacedName, credentialsSecret)
return credentialsSecret, err
}
func (o *OktaSyncer) Bind() error {
var err error
_, o.goOkta, err = okta.NewClient(context.TODO(),
okta.WithOrgUrl(o.Provider.URL),
okta.WithToken(string(o.credentialsSecret.Data[secretOktaTokenKey])))
if err != nil {
oktaLogger.Error(err, "establishing new okta client")
return err
}
return nil
}
func (o *OktaSyncer) Sync() ([]userv1.Group, error) {
groups, err := o.getGroups()
if err != nil {
oktaLogger.Error(err, "failed to get Groups", "Provider", o.Name)
return nil, err
}
for _, group := range groups {
if _, groupFound := o.cachedGroups[group.Id]; !groupFound {
if err := o.processGroupsAndMembers(group); err != nil {
oktaLogger.Error(err, "processing groups and members")
}
}
}
providerUrl, err := url.Parse(o.Provider.URL)
if err != nil {
return nil, err
}
var ocpGroups []userv1.Group
for _, cachedGroup := range o.cachedGroups {
validatedGroupAttr := map[string]string{}
groupAttributes := o.mapAttributes(cachedGroup)
for key, value := range groupAttributes {
if errs := validation.IsQualifiedName(key); len(errs) != 0 {
oktaLogger.Info("unable to add annotation to", "group", cachedGroup.Profile.Name, "key", key, "value", value)
} else {
validatedGroupAttr[key] = value
}
}
ocpGroup := userv1.Group{
TypeMeta: v1.TypeMeta{
Kind: "Group",
APIVersion: userv1.GroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Name: cachedGroup.Profile.Name,
Annotations: validatedGroupAttr,
Labels: map[string]string{},
},
Users: []string{},
}
ocpGroup.GetAnnotations()[constants.SyncSourceHost] = providerUrl.Host
ocpGroup.GetAnnotations()[constants.SyncSourceUID] = cachedGroup.Id
users := o.cachedGroupMembers[cachedGroup.Id]
for _, user := range users {
profile := *user.Profile
if user.Status == activeStatus {
if userName, ok := profile[o.Provider.ProfileKey].(string); !ok {
oktaLogger.Info("attribute unavailable on okta user profile " + o.Provider.ProfileKey)
} else if o.Provider.ExtractLoginUsername {
userName = strings.Split(userName, "@")[0]
ocpGroup.Users = append(ocpGroup.Users, userName)
} else {
ocpGroup.Users = append(ocpGroup.Users, userName)
}
}
}
ocpGroups = append(ocpGroups, ocpGroup)
}
return ocpGroups, nil
}
func (o OktaSyncer) getGroups() ([]*okta.Group, error) {
var (
groups []*okta.Group
)
appGroups, resp, err := o.goOkta.Application.ListApplicationGroupAssignments(context.TODO(), o.Provider.AppId, query.NewQueryParams(query.WithLimit(int64(o.Provider.GroupLimit))))
if err != nil {
oktaLogger.Error(err, "getting groups for specified application")
return nil, err
}
for resp.HasNextPage() {
var nextAppGroups []*okta.ApplicationGroupAssignment
resp, err = resp.Next(context.TODO(), &nextAppGroups)
if err != nil {
oktaLogger.Error(err, "getting groups for specified application")
return nil, err
}
appGroups = append(appGroups, nextAppGroups...)
}
groups, err = o.fetchGroupsAsync(appGroups)
return groups, err
}
func (o OktaSyncer) fetchGroupsAsync(appGroups []*okta.ApplicationGroupAssignment) ([]*okta.Group, error) {
wg := &sync.WaitGroup{}
groupCh := make(chan *okta.Group, len(appGroups))
wg.Add(len(appGroups))
for _, appGroup := range appGroups {
go getGroup(appGroup, groupCh, o.goOkta.Group, wg)
}
wg.Wait()
close(groupCh)
var groups []*okta.Group
for v := range groupCh {
groups = append(groups, v)
}
if len(groups) != len(appGroups) {
return groups, errors.New("failed to retrieve all groups")
}
return groups, nil
}
func getGroup(app *okta.ApplicationGroupAssignment, groupChan chan *okta.Group, resource *okta.GroupResource, wg *sync.WaitGroup) {
defer wg.Done()
group, _, err := resource.GetGroup(context.TODO(), app.Id)
if err != nil {
oktaLogger.Error(err, "fetching group id "+app.Id)
} else {
groupChan <- group
}
}
func (o OktaSyncer) mapAttributes(group *okta.Group) map[string]string {
attr := make(map[string]string)
attr["created"] = group.Created.String()
attr["description"] = group.Profile.Description
attr["id"] = group.Id
attr["lastUpdated"] = group.LastUpdated.String()
attr["lastMembershipUpdated"] = group.LastMembershipUpdated.String()
attr["name"] = group.Profile.Name
attr["objectClass"] = strings.Join(group.ObjectClass, ",")
attr["type"] = group.Type
return attr
}
func (o *OktaSyncer) processGroupsAndMembers(group *okta.Group) error {
if !isGroupAllowed(group.Profile.Name, o.Provider.Groups) {
return nil
}
o.cachedGroups[group.Id] = group
users, _, err := o.goOkta.Group.ListGroupUsers(context.TODO(), group.Id, nil)
if err != nil {
oktaLogger.Error(err, "failed to get users", "Provider", o.Name)
return err
}
o.cachedGroupMembers[group.Id] = users
return nil
}
func (o *OktaSyncer) GetProviderName() string {
return o.Name
}
func (o *OktaSyncer) GetPrune() bool {
return o.Provider.Prune
}