...
1
16
17 package server
18
19 import (
20 "net/http"
21 "strings"
22
23 "k8s.io/apimachinery/pkg/types"
24 "k8s.io/apiserver/pkg/authentication/authenticator"
25 "k8s.io/apiserver/pkg/authentication/user"
26 "k8s.io/apiserver/pkg/authorization/authorizer"
27 "k8s.io/klog/v2"
28 )
29
30
31 type KubeletAuth struct {
32
33 authenticator.Request
34
35 authorizer.RequestAttributesGetter
36
37 authorizer.Authorizer
38 }
39
40
41 func NewKubeletAuth(authenticator authenticator.Request, authorizerAttributeGetter authorizer.RequestAttributesGetter, authorizer authorizer.Authorizer) AuthInterface {
42 return &KubeletAuth{authenticator, authorizerAttributeGetter, authorizer}
43 }
44
45
46 func NewNodeAuthorizerAttributesGetter(nodeName types.NodeName) authorizer.RequestAttributesGetter {
47 return nodeAuthorizerAttributesGetter{nodeName: nodeName}
48 }
49
50 type nodeAuthorizerAttributesGetter struct {
51 nodeName types.NodeName
52 }
53
54 func isSubpath(subpath, path string) bool {
55 path = strings.TrimSuffix(path, "/")
56 return subpath == path || (strings.HasPrefix(subpath, path) && subpath[len(path)] == '/')
57 }
58
59
60
61
62
63
64
65
66 func (n nodeAuthorizerAttributesGetter) GetRequestAttributes(u user.Info, r *http.Request) authorizer.Attributes {
67
68 apiVerb := ""
69 switch r.Method {
70 case "POST":
71 apiVerb = "create"
72 case "GET":
73 apiVerb = "get"
74 case "PUT":
75 apiVerb = "update"
76 case "PATCH":
77 apiVerb = "patch"
78 case "DELETE":
79 apiVerb = "delete"
80 }
81
82 requestPath := r.URL.Path
83
84
85 attrs := authorizer.AttributesRecord{
86 User: u,
87 Verb: apiVerb,
88 Namespace: "",
89 APIGroup: "",
90 APIVersion: "v1",
91 Resource: "nodes",
92 Subresource: "proxy",
93 Name: string(n.nodeName),
94 ResourceRequest: true,
95 Path: requestPath,
96 }
97
98
99
100 switch {
101 case isSubpath(requestPath, statsPath):
102 attrs.Subresource = "stats"
103 case isSubpath(requestPath, metricsPath):
104 attrs.Subresource = "metrics"
105 case isSubpath(requestPath, logsPath):
106
107 attrs.Subresource = "log"
108 case isSubpath(requestPath, checkpointPath):
109 attrs.Subresource = "checkpoint"
110 }
111
112 klog.V(5).InfoS("Node request attributes", "user", attrs.GetUser().GetName(), "verb", attrs.GetVerb(), "resource", attrs.GetResource(), "subresource", attrs.GetSubresource())
113
114 return attrs
115 }
116
View as plain text