storage.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package tokenreview
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "net/http"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apiserver/pkg/authentication/authenticator"
  23. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  24. "k8s.io/apiserver/pkg/registry/rest"
  25. "k8s.io/klog"
  26. "k8s.io/kubernetes/pkg/apis/authentication"
  27. )
  28. var badAuthenticatorAuds = apierrors.NewInternalError(errors.New("error validating audiences"))
  29. type REST struct {
  30. tokenAuthenticator authenticator.Request
  31. apiAudiences []string
  32. }
  33. func NewREST(tokenAuthenticator authenticator.Request, apiAudiences []string) *REST {
  34. return &REST{
  35. tokenAuthenticator: tokenAuthenticator,
  36. apiAudiences: apiAudiences,
  37. }
  38. }
  39. func (r *REST) NamespaceScoped() bool {
  40. return false
  41. }
  42. func (r *REST) New() runtime.Object {
  43. return &authentication.TokenReview{}
  44. }
  45. func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  46. tokenReview, ok := obj.(*authentication.TokenReview)
  47. if !ok {
  48. return nil, apierrors.NewBadRequest(fmt.Sprintf("not a TokenReview: %#v", obj))
  49. }
  50. namespace := genericapirequest.NamespaceValue(ctx)
  51. if len(namespace) != 0 {
  52. return nil, apierrors.NewBadRequest(fmt.Sprintf("namespace is not allowed on this type: %v", namespace))
  53. }
  54. if len(tokenReview.Spec.Token) == 0 {
  55. return nil, apierrors.NewBadRequest(fmt.Sprintf("token is required for TokenReview in authentication"))
  56. }
  57. if createValidation != nil {
  58. if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
  59. return nil, err
  60. }
  61. }
  62. if r.tokenAuthenticator == nil {
  63. return tokenReview, nil
  64. }
  65. // create a header that contains nothing but the token
  66. fakeReq := &http.Request{Header: http.Header{}}
  67. fakeReq.Header.Add("Authorization", "Bearer "+tokenReview.Spec.Token)
  68. auds := tokenReview.Spec.Audiences
  69. if len(auds) == 0 {
  70. auds = r.apiAudiences
  71. }
  72. if len(auds) > 0 {
  73. fakeReq = fakeReq.WithContext(authenticator.WithAudiences(fakeReq.Context(), auds))
  74. }
  75. resp, ok, err := r.tokenAuthenticator.AuthenticateRequest(fakeReq)
  76. tokenReview.Status.Authenticated = ok
  77. if err != nil {
  78. tokenReview.Status.Error = err.Error()
  79. }
  80. if len(auds) > 0 && resp != nil && len(authenticator.Audiences(auds).Intersect(resp.Audiences)) == 0 {
  81. klog.Errorf("error validating audience. want=%q got=%q", auds, resp.Audiences)
  82. return nil, badAuthenticatorAuds
  83. }
  84. if resp != nil && resp.User != nil {
  85. tokenReview.Status.User = authentication.UserInfo{
  86. Username: resp.User.GetName(),
  87. UID: resp.User.GetUID(),
  88. Groups: resp.User.GetGroups(),
  89. Extra: map[string]authentication.ExtraValue{},
  90. }
  91. for k, v := range resp.User.GetExtra() {
  92. tokenReview.Status.User.Extra[k] = authentication.ExtraValue(v)
  93. }
  94. tokenReview.Status.Audiences = resp.Audiences
  95. }
  96. return tokenReview, nil
  97. }