This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauthclientregistry.go
466 lines (416 loc) · 15.8 KB
/
oauthclientregistry.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package oauthclient
import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
clientv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
apiserverserviceaccount "k8s.io/apiserver/pkg/authentication/serviceaccount"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
kapi "k8s.io/kubernetes/pkg/api"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/serviceaccount"
scopeauthorizer "github.com/openshift/origin/pkg/authorization/authorizer/scope"
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
"github.com/openshift/origin/pkg/oauth/registry/oauthclient"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
routeclient "github.com/openshift/origin/pkg/route/generated/internalclientset/typed/route/internalversion"
)
const (
OAuthWantChallengesAnnotationPrefix = "serviceaccounts.openshift.io/oauth-want-challenges"
// Prefix used for statically specifying redirect URIs for a service account via annotations
// The value can be partially supplied with the dynamic prefix to override the resource's defaults
OAuthRedirectModelAnnotationURIPrefix = "serviceaccounts.openshift.io/oauth-redirecturi."
// Prefix used for dynamically specifying redirect URIs using resources for a service account via annotations
OAuthRedirectModelAnnotationReferencePrefix = "serviceaccounts.openshift.io/oauth-redirectreference."
routeKind = "Route"
// TODO add ingress support
// IngressKind = "Ingress"
)
var modelPrefixes = []string{
OAuthRedirectModelAnnotationURIPrefix,
OAuthRedirectModelAnnotationReferencePrefix,
}
// namesToObjMapperFunc is linked to a given GroupKind.
// Based on the namespace and names provided, it builds a map of resource name to redirect URIs.
// The redirect URIs represent the default values as specified by the resource.
// These values can be overridden by user specified data. Errors returned are informative and non-fatal.
type namesToObjMapperFunc func(namespace string, names sets.String) (map[string]redirectURIList, []error)
var emptyGroupKind = schema.GroupKind{} // Used with static redirect URIs
var routeGroupKind = routeapi.SchemeGroupVersion.WithKind(routeKind).GroupKind()
var legacyRouteGroupKind = routeapi.LegacySchemeGroupVersion.WithKind(routeKind).GroupKind() // to support redirect reference with old group
// TODO add ingress support
// var ingressGroupKind = routeapi.SchemeGroupVersion.WithKind(IngressKind).GroupKind()
type saOAuthClientAdapter struct {
saClient kcoreclient.ServiceAccountsGetter
secretClient kcoreclient.SecretsGetter
eventRecorder record.EventRecorder
routeClient routeclient.RoutesGetter
// TODO add ingress support
//ingressClient ??
delegate oauthclient.Getter
grantMethod oauthapi.GrantHandlerType
decoder runtime.Decoder
}
// model holds fields that could be used to build redirect URI(s).
// The resource components define where to get the default redirect data from.
// If specified, the uri components are used to override the default data.
// As long as the resulting URI(s) have a scheme and a host, they are considered valid.
type model struct {
scheme string
port string
path string
host string
group string
kind string
name string
}
// getGroupKind is used to determine if a group and kind combination is supported.
func (m *model) getGroupKind() schema.GroupKind {
return schema.GroupKind{Group: m.group, Kind: m.kind}
}
// updateFromURI updates the data in the model with the user provided URL data.
func (m *model) updateFromURI(u *url.URL) {
m.scheme, m.host, m.path = u.Scheme, u.Host, u.Path
if h, p, err := net.SplitHostPort(m.host); err == nil {
m.host = h
m.port = p
}
}
// updateFromReference updates the data in the model with the user provided object reference data.
func (m *model) updateFromReference(r *oauthapi.RedirectReference) {
m.group, m.kind, m.name = r.Group, r.Kind, r.Name
}
type modelList []model
// getNames determines the unique, non-empty resource names specified by the models.
func (ml modelList) getNames() sets.String {
data := sets.NewString()
for _, model := range ml {
if len(model.name) > 0 {
data.Insert(model.name)
}
}
return data
}
// getRedirectURIs uses the mapping provided by a namesToObjMapperFunc to enumerate all of the redirect URIs
// based on the name of each resource. The user provided data in the model overrides the data in the mapping.
// The returned redirect URIs may contain duplicate and invalid entries. All items in the modelList must have a
// uniform group/kind, and the objMapper must be specifically for that group/kind.
func (ml modelList) getRedirectURIs(objMapper map[string]redirectURIList) redirectURIList {
var data redirectURIList
for _, m := range ml {
if uris, ok := objMapper[m.name]; ok {
for _, uri := range uris {
u := uri // Make sure we do not mutate objMapper
u.merge(&m)
data = append(data, u)
}
}
}
return data
}
type redirectURI struct {
scheme string
host string
port string
path string
}
func (uri *redirectURI) String() string {
host := uri.host
if len(uri.port) > 0 {
host = net.JoinHostPort(host, uri.port)
}
return (&url.URL{Scheme: uri.scheme, Host: host, Path: uri.path}).String()
}
// isValid returns true when both scheme and host are non-empty.
func (uri *redirectURI) isValid() bool {
return len(uri.scheme) > 0 && len(uri.host) > 0
}
type redirectURIList []redirectURI
// extractValidRedirectURIStrings returns the redirect URIs that are valid per `isValid` as strings.
func (rl redirectURIList) extractValidRedirectURIStrings() []string {
var data []string
for _, u := range rl {
if u.isValid() {
data = append(data, u.String())
}
}
return data
}
// merge overrides the default data in the uri with the user provided data in the model.
func (uri *redirectURI) merge(m *model) {
if len(m.scheme) > 0 {
uri.scheme = m.scheme
}
if len(m.path) > 0 {
uri.path = m.path
}
if len(m.port) > 0 {
uri.port = m.port
}
if len(m.host) > 0 {
uri.host = m.host
}
}
var _ oauthclient.Getter = &saOAuthClientAdapter{}
func NewServiceAccountOAuthClientGetter(
saClient kcoreclient.ServiceAccountsGetter,
secretClient kcoreclient.SecretsGetter,
eventClient corev1.EventInterface,
routeClient routeclient.RoutesGetter,
delegate oauthclient.Getter,
grantMethod oauthapi.GrantHandlerType,
) oauthclient.Getter {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&corev1.EventSinkImpl{Interface: eventClient})
recorder := eventBroadcaster.NewRecorder(kapi.Scheme, clientv1.EventSource{Component: "service-account-oauth-client-getter"})
return &saOAuthClientAdapter{
saClient: saClient,
secretClient: secretClient,
eventRecorder: recorder,
routeClient: routeClient,
delegate: delegate,
grantMethod: grantMethod,
decoder: kapi.Codecs.UniversalDecoder(),
}
}
func (a *saOAuthClientAdapter) Get(name string, options metav1.GetOptions) (*oauthapi.OAuthClient, error) {
var err error
saNamespace, saName, err := apiserverserviceaccount.SplitUsername(name)
if err != nil {
return a.delegate.Get(name, options)
}
sa, err := a.saClient.ServiceAccounts(saNamespace).Get(saName, metav1.GetOptions{})
if err != nil {
return nil, err
}
var saErrors []error
var failReason string
// Create a warning event combining the collected annotation errors upon failure.
defer func() {
if err != nil && len(saErrors) > 0 && len(failReason) > 0 {
a.eventRecorder.Event(sa, kapi.EventTypeWarning, failReason, utilerrors.NewAggregate(saErrors).Error())
}
}()
redirectURIs := []string{}
modelsMap, errs := parseModelsMap(sa.Annotations, a.decoder)
if len(errs) > 0 {
saErrors = append(saErrors, errs...)
}
if len(modelsMap) > 0 {
uris, extractErrors := a.extractRedirectURIs(modelsMap, saNamespace)
if len(uris) > 0 {
redirectURIs = append(redirectURIs, uris.extractValidRedirectURIStrings()...)
}
if len(extractErrors) > 0 {
saErrors = append(saErrors, extractErrors...)
}
}
if len(redirectURIs) == 0 {
err = fmt.Errorf("%v has no redirectURIs; set %v<some-value>=<redirect> or create a dynamic URI using %v<some-value>=<reference>",
name, OAuthRedirectModelAnnotationURIPrefix, OAuthRedirectModelAnnotationReferencePrefix,
)
failReason = "NoSAOAuthRedirectURIs"
saErrors = append(saErrors, err)
return nil, err
}
tokens, err := a.getServiceAccountTokens(sa)
if err != nil {
return nil, err
}
if len(tokens) == 0 {
err = fmt.Errorf("%v has no tokens", name)
failReason = "NoSAOAuthTokens"
saErrors = append(saErrors, err)
return nil, err
}
saWantsChallenges, _ := strconv.ParseBool(sa.Annotations[OAuthWantChallengesAnnotationPrefix])
saClient := &oauthapi.OAuthClient{
ObjectMeta: metav1.ObjectMeta{Name: name},
ScopeRestrictions: getScopeRestrictionsFor(saNamespace, saName),
AdditionalSecrets: tokens,
RespondWithChallenges: saWantsChallenges,
// TODO update this to allow https redirection to any
// 1. service IP (useless in general)
// 2. service DNS (useless in general)
// 3. loopback? (useful, but maybe a bit weird)
RedirectURIs: sets.NewString(redirectURIs...).List(),
GrantMethod: a.grantMethod,
}
return saClient, nil
}
// parseModelsMap builds a map of model name to model using a service account's annotations.
// The model name is only used for building the map (it ties together the uri and reference annotations)
// and serves no functional purpose other than making testing easier. Errors returned are informative and non-fatal.
func parseModelsMap(annotations map[string]string, decoder runtime.Decoder) (map[string]model, []error) {
models := map[string]model{}
parseErrors := []error{}
for key, value := range annotations {
prefix, name, ok := parseModelPrefixName(key)
if !ok {
continue
}
m := models[name]
switch prefix {
case OAuthRedirectModelAnnotationURIPrefix:
if u, err := url.Parse(value); err == nil {
m.updateFromURI(u)
} else {
parseErrors = append(parseErrors, err)
}
case OAuthRedirectModelAnnotationReferencePrefix:
r := &oauthapi.OAuthRedirectReference{}
if err := runtime.DecodeInto(decoder, []byte(value), r); err == nil {
m.updateFromReference(&r.Reference)
} else {
parseErrors = append(parseErrors, err)
}
}
models[name] = m
}
return models, parseErrors
}
// parseModelPrefixName determines if the given key is a model prefix.
// Returns what prefix was used, the name of the model, and true if a model prefix was actually used.
func parseModelPrefixName(key string) (string, string, bool) {
for _, prefix := range modelPrefixes {
if strings.HasPrefix(key, prefix) {
return prefix, key[len(prefix):], true
}
}
return "", "", false
}
// extractRedirectURIs builds redirect URIs using the given models and namespace.
// The returned redirect URIs may contain duplicates and invalid entries. Errors returned are informative and non-fatal.
func (a *saOAuthClientAdapter) extractRedirectURIs(modelsMap map[string]model, namespace string) (redirectURIList, []error) {
var data redirectURIList
routeErrors := []error{}
groupKindModelListMapper := map[schema.GroupKind]modelList{} // map of GroupKind to all models belonging to it
groupKindModelToURI := map[schema.GroupKind]namesToObjMapperFunc{
routeGroupKind: a.redirectURIsFromRoutes,
// TODO add support for ingresses by creating the appropriate GroupKind and namesToObjMapperFunc
// ingressGroupKind: a.redirectURIsFromIngresses,
}
for _, m := range modelsMap {
gk := m.getGroupKind()
if gk == legacyRouteGroupKind {
gk = routeGroupKind // support legacy route group without doing extra API calls
}
if len(m.name) == 0 && gk == emptyGroupKind { // Is this a static redirect URI?
uri := redirectURI{} // No defaults wanted
uri.merge(&m)
data = append(data, uri)
} else if _, ok := groupKindModelToURI[gk]; ok { // a GroupKind is valid if we have a namesToObjMapperFunc to handle it
groupKindModelListMapper[gk] = append(groupKindModelListMapper[gk], m)
}
}
for gk, models := range groupKindModelListMapper {
if names := models.getNames(); names.Len() > 0 {
objMapper, errs := groupKindModelToURI[gk](namespace, names)
if len(objMapper) > 0 {
data = append(data, models.getRedirectURIs(objMapper)...)
}
if len(errs) > 0 {
routeErrors = append(routeErrors, errs...)
}
}
}
return data, routeErrors
}
// redirectURIsFromRoutes is the namesToObjMapperFunc specific to Routes.
// Returns a map of route name to redirect URIs that contain the default data as specified by the route's ingresses.
// Errors returned are informative and non-fatal.
func (a *saOAuthClientAdapter) redirectURIsFromRoutes(namespace string, osRouteNames sets.String) (map[string]redirectURIList, []error) {
var routes []routeapi.Route
routeErrors := []error{}
routeInterface := a.routeClient.Routes(namespace)
if osRouteNames.Len() > 1 {
if r, err := routeInterface.List(metav1.ListOptions{}); err == nil {
routes = r.Items
} else {
routeErrors = append(routeErrors, err)
}
} else {
if r, err := routeInterface.Get(osRouteNames.List()[0], metav1.GetOptions{}); err == nil {
routes = append(routes, *r)
} else {
routeErrors = append(routeErrors, err)
}
}
routeMap := map[string]redirectURIList{}
for _, route := range routes {
if osRouteNames.Has(route.Name) {
routeMap[route.Name] = redirectURIsFromRoute(&route)
}
}
return routeMap, routeErrors
}
// redirectURIsFromRoute returns a list of redirect URIs that contain the default data as specified by the given route's ingresses.
func redirectURIsFromRoute(route *routeapi.Route) redirectURIList {
var uris redirectURIList
uri := redirectURI{scheme: "https"} // Default to TLS
uri.path = route.Spec.Path
if route.Spec.TLS == nil {
uri.scheme = "http"
}
for _, ingress := range route.Status.Ingress {
if !isRouteIngressValid(&ingress) {
continue
}
u := uri // Copy to avoid mutating the base uri
u.host = ingress.Host
uris = append(uris, u)
}
// If we get this far we know the Route does actually exist, so we need to have at least one uri
// to allow the user to override it in their annotations in case there is no valid ingress
// `extractValidRedirectURIStrings` guarantees that we eventually have the minimum set of required fields
if len(uris) == 0 {
uris = append(uris, uri)
}
return uris
}
// isRouteIngressValid determines if the RouteIngress has a host and that its conditions has an element with Type=RouteAdmitted and Status=ConditionTrue
func isRouteIngressValid(routeIngress *routeapi.RouteIngress) bool {
if len(routeIngress.Host) == 0 {
return false
}
for _, condition := range routeIngress.Conditions {
if condition.Type == routeapi.RouteAdmitted && condition.Status == kapi.ConditionTrue {
return true
}
}
return false
}
func getScopeRestrictionsFor(namespace, name string) []oauthapi.ScopeRestriction {
return []oauthapi.ScopeRestriction{
{ExactValues: []string{
scopeauthorizer.UserInfo,
scopeauthorizer.UserAccessCheck,
scopeauthorizer.UserListScopedProjects,
scopeauthorizer.UserListAllProjects,
}},
{ClusterRole: &oauthapi.ClusterRoleScopeRestriction{RoleNames: []string{"*"}, Namespaces: []string{namespace}, AllowEscalation: true}},
}
}
// getServiceAccountTokens returns all ServiceAccountToken secrets for the given ServiceAccount
func (a *saOAuthClientAdapter) getServiceAccountTokens(sa *kapi.ServiceAccount) ([]string, error) {
allSecrets, err := a.secretClient.Secrets(sa.Namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
tokens := []string{}
for i := range allSecrets.Items {
secret := &allSecrets.Items[i]
if serviceaccount.InternalIsServiceAccountToken(secret, sa) {
tokens = append(tokens, string(secret.Data[kapi.ServiceAccountTokenKey]))
}
}
return tokens, nil
}