auth.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 server
  14. import (
  15. "net/http"
  16. "strings"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/apiserver/pkg/authentication/authenticator"
  19. "k8s.io/apiserver/pkg/authentication/user"
  20. "k8s.io/apiserver/pkg/authorization/authorizer"
  21. "k8s.io/klog"
  22. )
  23. // KubeletAuth implements AuthInterface
  24. type KubeletAuth struct {
  25. // authenticator identifies the user for requests to the Kubelet API
  26. authenticator.Request
  27. // authorizerAttributeGetter builds authorization.Attributes for a request to the Kubelet API
  28. authorizer.RequestAttributesGetter
  29. // authorizer determines whether a given authorization.Attributes is allowed
  30. authorizer.Authorizer
  31. }
  32. // NewKubeletAuth returns a kubelet.AuthInterface composed of the given authenticator, attribute getter, and authorizer
  33. func NewKubeletAuth(authenticator authenticator.Request, authorizerAttributeGetter authorizer.RequestAttributesGetter, authorizer authorizer.Authorizer) AuthInterface {
  34. return &KubeletAuth{authenticator, authorizerAttributeGetter, authorizer}
  35. }
  36. // NewNodeAuthorizerAttributesGetter creates a new authorizer.RequestAttributesGetter for the node.
  37. func NewNodeAuthorizerAttributesGetter(nodeName types.NodeName) authorizer.RequestAttributesGetter {
  38. return nodeAuthorizerAttributesGetter{nodeName: nodeName}
  39. }
  40. type nodeAuthorizerAttributesGetter struct {
  41. nodeName types.NodeName
  42. }
  43. func isSubpath(subpath, path string) bool {
  44. path = strings.TrimSuffix(path, "/")
  45. return subpath == path || (strings.HasPrefix(subpath, path) && subpath[len(path)] == '/')
  46. }
  47. // GetRequestAttributes populates authorizer attributes for the requests to the kubelet API.
  48. // Default attributes are: {apiVersion=v1,verb=<http verb from request>,resource=nodes,name=<node name>,subresource=proxy}
  49. // More specific verb/resource is set for the following request patterns:
  50. // /stats/* => verb=<api verb from request>, resource=nodes, name=<node name>, subresource=stats
  51. // /metrics/* => verb=<api verb from request>, resource=nodes, name=<node name>, subresource=metrics
  52. // /logs/* => verb=<api verb from request>, resource=nodes, name=<node name>, subresource=log
  53. // /spec/* => verb=<api verb from request>, resource=nodes, name=<node name>, subresource=spec
  54. func (n nodeAuthorizerAttributesGetter) GetRequestAttributes(u user.Info, r *http.Request) authorizer.Attributes {
  55. apiVerb := ""
  56. switch r.Method {
  57. case "POST":
  58. apiVerb = "create"
  59. case "GET":
  60. apiVerb = "get"
  61. case "PUT":
  62. apiVerb = "update"
  63. case "PATCH":
  64. apiVerb = "patch"
  65. case "DELETE":
  66. apiVerb = "delete"
  67. }
  68. requestPath := r.URL.Path
  69. // Default attributes mirror the API attributes that would allow this access to the kubelet API
  70. attrs := authorizer.AttributesRecord{
  71. User: u,
  72. Verb: apiVerb,
  73. Namespace: "",
  74. APIGroup: "",
  75. APIVersion: "v1",
  76. Resource: "nodes",
  77. Subresource: "proxy",
  78. Name: string(n.nodeName),
  79. ResourceRequest: true,
  80. Path: requestPath,
  81. }
  82. // Override subresource for specific paths
  83. // This allows subdividing access to the kubelet API
  84. switch {
  85. case isSubpath(requestPath, statsPath):
  86. attrs.Subresource = "stats"
  87. case isSubpath(requestPath, metricsPath):
  88. attrs.Subresource = "metrics"
  89. case isSubpath(requestPath, logsPath):
  90. // "log" to match other log subresources (pods/log, etc)
  91. attrs.Subresource = "log"
  92. case isSubpath(requestPath, specPath):
  93. attrs.Subresource = "spec"
  94. }
  95. klog.V(5).Infof("Node request attributes: user=%#v attrs=%#v", attrs.GetUser(), attrs)
  96. return attrs
  97. }