...
1
16
17 package certificates
18
19 import (
20 "context"
21 "strings"
22
23 "k8s.io/apiserver/pkg/authentication/user"
24 "k8s.io/apiserver/pkg/authorization/authorizer"
25 "k8s.io/klog/v2"
26 )
27
28
29
30
31
32
33
34 func IsAuthorizedForSignerName(ctx context.Context, authz authorizer.Authorizer, info user.Info, verb, signerName string) bool {
35
36 attr := buildAttributes(info, verb, signerName)
37 decision, reason, err := authz.Authorize(ctx, attr)
38 switch {
39 case err != nil:
40 klog.V(3).Infof("cannot authorize %q %q for policy: %v,%v", verb, attr.GetName(), reason, err)
41 case decision == authorizer.DecisionAllow:
42 return true
43 }
44
45
46
47 attr = buildWildcardAttributes(info, verb, signerName)
48 decision, reason, err = authz.Authorize(ctx, attr)
49 switch {
50 case err != nil:
51 klog.V(3).Infof("cannot authorize %q %q for policy: %v,%v", verb, attr.GetName(), reason, err)
52 case decision == authorizer.DecisionAllow:
53 return true
54 }
55
56 return false
57 }
58
59 func buildAttributes(info user.Info, verb, signerName string) authorizer.Attributes {
60 return authorizer.AttributesRecord{
61 User: info,
62 Verb: verb,
63 Name: signerName,
64 APIGroup: "certificates.k8s.io",
65 APIVersion: "*",
66 Resource: "signers",
67 ResourceRequest: true,
68 }
69 }
70
71 func buildWildcardAttributes(info user.Info, verb, signerName string) authorizer.Attributes {
72 parts := strings.Split(signerName, "/")
73 domain := parts[0]
74 return buildAttributes(info, verb, domain+"/*")
75 }
76
View as plain text