| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package options |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/spf13/pflag" |
| 24 |
|
| 25 |
"k8s.io/apimachinery/pkg/util/wait" |
| 26 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 27 |
"k8s.io/apiserver/pkg/authorization/authorizerfactory" |
| 28 |
"k8s.io/apiserver/pkg/authorization/path" |
| 29 |
"k8s.io/apiserver/pkg/authorization/union" |
| 30 |
"k8s.io/apiserver/pkg/server" |
| 31 |
"k8s.io/client-go/kubernetes" |
| 32 |
"k8s.io/client-go/rest" |
| 33 |
"k8s.io/client-go/tools/clientcmd" |
| 34 |
"k8s.io/client-go/transport" |
| 35 |
"k8s.io/klog/v2" |
| 36 |
) |
| 37 |
|
| 38 |
// DelegatingAuthorizationOptions provides an easy way for composing API servers to delegate their authorization to |
| 39 |
// the root kube API server. |
| 40 |
// WARNING: never assume that every authenticated incoming request already does authorization. |
| 41 |
// The aggregator in the kube API server does this today, but this behaviour is not |
| 42 |
// guaranteed in the future. |
| 43 |
type DelegatingAuthorizationOptions struct { |
| 44 |
// RemoteKubeConfigFile is the file to use to connect to a "normal" kube API server which hosts the |
| 45 |
// SubjectAccessReview.authorization.k8s.io endpoint for checking tokens. |
| 46 |
RemoteKubeConfigFile string |
| 47 |
// RemoteKubeConfigFileOptional is specifying whether not specifying the kubeconfig or |
| 48 |
// a missing in-cluster config will be fatal. |
| 49 |
RemoteKubeConfigFileOptional bool |
| 50 |
|
| 51 |
// AllowCacheTTL is the length of time that a successful authorization response will be cached |
| 52 |
AllowCacheTTL time.Duration |
| 53 |
|
| 54 |
// DenyCacheTTL is the length of time that an unsuccessful authorization response will be cached. |
| 55 |
// You generally want more responsive, "deny, try again" flows. |
| 56 |
DenyCacheTTL time.Duration |
| 57 |
|
| 58 |
// AlwaysAllowPaths are HTTP paths which are excluded from authorization. They can be plain |
| 59 |
// paths or end in * in which case prefix-match is applied. A leading / is optional. |
| 60 |
AlwaysAllowPaths []string |
| 61 |
|
| 62 |
// AlwaysAllowGroups are groups which are allowed to take any actions. In kube, this is system:masters. |
| 63 |
AlwaysAllowGroups []string |
| 64 |
|
| 65 |
// ClientTimeout specifies a time limit for requests made by SubjectAccessReviews client. |
| 66 |
// The default value is set to 10 seconds. |
| 67 |
ClientTimeout time.Duration |
| 68 |
|
| 69 |
// WebhookRetryBackoff specifies the backoff parameters for the authorization webhook retry logic. |
| 70 |
// This allows us to configure the sleep time at each iteration and the maximum number of retries allowed |
| 71 |
// before we fail the webhook call in order to limit the fan out that ensues when the system is degraded. |
| 72 |
WebhookRetryBackoff *wait.Backoff |
| 73 |
|
| 74 |
// CustomRoundTripperFn allows for specifying a middleware function for custom HTTP behaviour for the authorization webhook client. |
| 75 |
CustomRoundTripperFn transport.WrapperFunc |
| 76 |
} |
| 77 |
|
| 78 |
func NewDelegatingAuthorizationOptions() *DelegatingAuthorizationOptions { |
| 79 |
return &DelegatingAuthorizationOptions{ |
| 80 |
// very low for responsiveness, but high enough to handle storms |
| 81 |
AllowCacheTTL: 10 * time.Second, |
| 82 |
DenyCacheTTL: 10 * time.Second, |
| 83 |
ClientTimeout: 10 * time.Second, |
| 84 |
WebhookRetryBackoff: DefaultAuthWebhookRetryBackoff(), |
| 85 |
// This allows the kubelet to always get health and readiness without causing an authorization check. |
| 86 |
// This field can be cleared by callers if they don't want this behavior. |
| 87 |
AlwaysAllowPaths: []string{"/healthz", "/readyz", "/livez"}, |
| 88 |
// In an authorization call delegated to a kube-apiserver (the expected common-case), system:masters has full |
| 89 |
// authority in a hard-coded authorizer. This means that our default can reasonably be to skip an authorization |
| 90 |
// check for system:masters. |
| 91 |
// This field can be cleared by callers if they don't want this behavior. |
| 92 |
AlwaysAllowGroups: []string{"system:masters"}, |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// WithAlwaysAllowGroups appends the list of paths to AlwaysAllowGroups |
| 97 |
func (s *DelegatingAuthorizationOptions) WithAlwaysAllowGroups(groups ...string) *DelegatingAuthorizationOptions { |
| 98 |
s.AlwaysAllowGroups = append(s.AlwaysAllowGroups, groups...) |
| 99 |
return s |
| 100 |
} |
| 101 |
|
| 102 |
// WithAlwaysAllowPaths appends the list of paths to AlwaysAllowPaths |
| 103 |
func (s *DelegatingAuthorizationOptions) WithAlwaysAllowPaths(paths ...string) *DelegatingAuthorizationOptions { |
| 104 |
s.AlwaysAllowPaths = append(s.AlwaysAllowPaths, paths...) |
| 105 |
return s |
| 106 |
} |
| 107 |
|
| 108 |
// WithClientTimeout sets the given timeout for SAR client used by this authorizer |
| 109 |
func (s *DelegatingAuthorizationOptions) WithClientTimeout(timeout time.Duration) { |
| 110 |
s.ClientTimeout = timeout |
| 111 |
} |
| 112 |
|
| 113 |
// WithCustomRetryBackoff sets the custom backoff parameters for the authorization webhook retry logic. |
| 114 |
func (s *DelegatingAuthorizationOptions) WithCustomRetryBackoff(backoff wait.Backoff) { |
| 115 |
s.WebhookRetryBackoff = &backoff |
| 116 |
} |
| 117 |
|
| 118 |
// WithCustomRoundTripper allows for specifying a middleware function for custom HTTP behaviour for the authorization webhook client. |
| 119 |
func (s *DelegatingAuthorizationOptions) WithCustomRoundTripper(rt transport.WrapperFunc) { |
| 120 |
s.CustomRoundTripperFn = rt |
| 121 |
} |
| 122 |
|
| 123 |
func (s *DelegatingAuthorizationOptions) Validate() []error { |
| 124 |
if s == nil { |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
allErrors := []error{} |
| 129 |
if s.WebhookRetryBackoff != nil && s.WebhookRetryBackoff.Steps <= 0 { |
| 130 |
allErrors = append(allErrors, fmt.Errorf("number of webhook retry attempts must be greater than 1, but is: %d", s.WebhookRetryBackoff.Steps)) |
| 131 |
} |
| 132 |
|
| 133 |
return allErrors |
| 134 |
} |
| 135 |
|
| 136 |
func (s *DelegatingAuthorizationOptions) AddFlags(fs *pflag.FlagSet) { |
| 137 |
if s == nil { |
| 138 |
return |
| 139 |
} |
| 140 |
|
| 141 |
var optionalKubeConfigSentence string |
| 142 |
if s.RemoteKubeConfigFileOptional { |
| 143 |
optionalKubeConfigSentence = " This is optional. If empty, all requests not skipped by authorization are forbidden." |
| 144 |
} |
| 145 |
fs.StringVar(&s.RemoteKubeConfigFile, "authorization-kubeconfig", s.RemoteKubeConfigFile, |
| 146 |
"kubeconfig file pointing at the 'core' kubernetes server with enough rights to create "+ |
| 147 |
"subjectaccessreviews.authorization.k8s.io."+optionalKubeConfigSentence) |
| 148 |
|
| 149 |
fs.DurationVar(&s.AllowCacheTTL, "authorization-webhook-cache-authorized-ttl", |
| 150 |
s.AllowCacheTTL, |
| 151 |
"The duration to cache 'authorized' responses from the webhook authorizer.") |
| 152 |
|
| 153 |
fs.DurationVar(&s.DenyCacheTTL, |
| 154 |
"authorization-webhook-cache-unauthorized-ttl", s.DenyCacheTTL, |
| 155 |
"The duration to cache 'unauthorized' responses from the webhook authorizer.") |
| 156 |
|
| 157 |
fs.StringSliceVar(&s.AlwaysAllowPaths, "authorization-always-allow-paths", s.AlwaysAllowPaths, |
| 158 |
"A list of HTTP paths to skip during authorization, i.e. these are authorized without "+ |
| 159 |
"contacting the 'core' kubernetes server.") |
| 160 |
} |
| 161 |
|
| 162 |
func (s *DelegatingAuthorizationOptions) ApplyTo(c *server.AuthorizationInfo) error { |
| 163 |
if s == nil { |
| 164 |
c.Authorizer = authorizerfactory.NewAlwaysAllowAuthorizer() |
| 165 |
return nil |
| 166 |
} |
| 167 |
|
| 168 |
client, err := s.getClient() |
| 169 |
if err != nil { |
| 170 |
return err |
| 171 |
} |
| 172 |
|
| 173 |
c.Authorizer, err = s.toAuthorizer(client) |
| 174 |
return err |
| 175 |
} |
| 176 |
|
| 177 |
func (s *DelegatingAuthorizationOptions) toAuthorizer(client kubernetes.Interface) (authorizer.Authorizer, error) { |
| 178 |
var authorizers []authorizer.Authorizer |
| 179 |
|
| 180 |
if len(s.AlwaysAllowGroups) > 0 { |
| 181 |
authorizers = append(authorizers, authorizerfactory.NewPrivilegedGroups(s.AlwaysAllowGroups...)) |
| 182 |
} |
| 183 |
|
| 184 |
if len(s.AlwaysAllowPaths) > 0 { |
| 185 |
a, err := path.NewAuthorizer(s.AlwaysAllowPaths) |
| 186 |
if err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
authorizers = append(authorizers, a) |
| 190 |
} |
| 191 |
|
| 192 |
if client == nil { |
| 193 |
klog.Warning("No authorization-kubeconfig provided, so SubjectAccessReview of authorization tokens won't work.") |
| 194 |
} else { |
| 195 |
cfg := authorizerfactory.DelegatingAuthorizerConfig{ |
| 196 |
SubjectAccessReviewClient: client.AuthorizationV1(), |
| 197 |
AllowCacheTTL: s.AllowCacheTTL, |
| 198 |
DenyCacheTTL: s.DenyCacheTTL, |
| 199 |
WebhookRetryBackoff: s.WebhookRetryBackoff, |
| 200 |
} |
| 201 |
delegatedAuthorizer, err := cfg.New() |
| 202 |
if err != nil { |
| 203 |
return nil, err |
| 204 |
} |
| 205 |
authorizers = append(authorizers, delegatedAuthorizer) |
| 206 |
} |
| 207 |
|
| 208 |
return union.New(authorizers...), nil |
| 209 |
} |
| 210 |
|
| 211 |
func (s *DelegatingAuthorizationOptions) getClient() (kubernetes.Interface, error) { |
| 212 |
var clientConfig *rest.Config |
| 213 |
var err error |
| 214 |
if len(s.RemoteKubeConfigFile) > 0 { |
| 215 |
loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: s.RemoteKubeConfigFile} |
| 216 |
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) |
| 217 |
|
| 218 |
clientConfig, err = loader.ClientConfig() |
| 219 |
} else { |
| 220 |
// without the remote kubeconfig file, try to use the in-cluster config. Most addon API servers will |
| 221 |
// use this path. If it is optional, ignore errors. |
| 222 |
clientConfig, err = rest.InClusterConfig() |
| 223 |
if err != nil && s.RemoteKubeConfigFileOptional { |
| 224 |
if err != rest.ErrNotInCluster { |
| 225 |
klog.Warningf("failed to read in-cluster kubeconfig for delegated authorization: %v", err) |
| 226 |
} |
| 227 |
return nil, nil |
| 228 |
} |
| 229 |
} |
| 230 |
if err != nil { |
| 231 |
return nil, fmt.Errorf("failed to get delegated authorization kubeconfig: %v", err) |
| 232 |
} |
| 233 |
|
| 234 |
// set high qps/burst limits since this will effectively limit API server responsiveness |
| 235 |
clientConfig.QPS = 200 |
| 236 |
clientConfig.Burst = 400 |
| 237 |
clientConfig.Timeout = s.ClientTimeout |
| 238 |
if s.CustomRoundTripperFn != nil { |
| 239 |
clientConfig.Wrap(s.CustomRoundTripperFn) |
| 240 |
} |
| 241 |
|
| 242 |
return kubernetes.NewForConfig(clientConfig) |
| 243 |
} |