storage_authentication.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2016 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 rest
  14. import (
  15. authenticationv1 "k8s.io/api/authentication/v1"
  16. authenticationv1beta1 "k8s.io/api/authentication/v1beta1"
  17. "k8s.io/apiserver/pkg/authentication/authenticator"
  18. "k8s.io/apiserver/pkg/registry/generic"
  19. "k8s.io/apiserver/pkg/registry/rest"
  20. genericapiserver "k8s.io/apiserver/pkg/server"
  21. serverstorage "k8s.io/apiserver/pkg/server/storage"
  22. "k8s.io/kubernetes/pkg/api/legacyscheme"
  23. "k8s.io/kubernetes/pkg/apis/authentication"
  24. "k8s.io/kubernetes/pkg/registry/authentication/tokenreview"
  25. )
  26. type RESTStorageProvider struct {
  27. Authenticator authenticator.Request
  28. APIAudiences authenticator.Audiences
  29. }
  30. func (p RESTStorageProvider) NewRESTStorage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) (genericapiserver.APIGroupInfo, bool) {
  31. // TODO figure out how to make the swagger generation stable, while allowing this endpoint to be disabled.
  32. // if p.Authenticator == nil {
  33. // return genericapiserver.APIGroupInfo{}, false
  34. // }
  35. apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(authentication.GroupName, legacyscheme.Scheme, legacyscheme.ParameterCodec, legacyscheme.Codecs)
  36. // If you add a version here, be sure to add an entry in `k8s.io/kubernetes/cmd/kube-apiserver/app/aggregator.go with specific priorities.
  37. // TODO refactor the plumbing to provide the information in the APIGroupInfo
  38. if apiResourceConfigSource.VersionEnabled(authenticationv1beta1.SchemeGroupVersion) {
  39. apiGroupInfo.VersionedResourcesStorageMap[authenticationv1beta1.SchemeGroupVersion.Version] = p.v1beta1Storage(apiResourceConfigSource, restOptionsGetter)
  40. }
  41. if apiResourceConfigSource.VersionEnabled(authenticationv1.SchemeGroupVersion) {
  42. apiGroupInfo.VersionedResourcesStorageMap[authenticationv1.SchemeGroupVersion.Version] = p.v1Storage(apiResourceConfigSource, restOptionsGetter)
  43. }
  44. return apiGroupInfo, true
  45. }
  46. func (p RESTStorageProvider) v1beta1Storage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) map[string]rest.Storage {
  47. storage := map[string]rest.Storage{}
  48. // tokenreviews
  49. tokenReviewStorage := tokenreview.NewREST(p.Authenticator, p.APIAudiences)
  50. storage["tokenreviews"] = tokenReviewStorage
  51. return storage
  52. }
  53. func (p RESTStorageProvider) v1Storage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) map[string]rest.Storage {
  54. storage := map[string]rest.Storage{}
  55. // tokenreviews
  56. tokenReviewStorage := tokenreview.NewREST(p.Authenticator, p.APIAudiences)
  57. storage["tokenreviews"] = tokenReviewStorage
  58. return storage
  59. }
  60. func (p RESTStorageProvider) GroupName() string {
  61. return authentication.GroupName
  62. }