k8s.io/apiserver@v0.31.1/pkg/authorization/authorizerfactory/delegating.go (about)

     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 authorizerfactory
    18  
    19  import (
    20  	"errors"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/util/wait"
    24  	"k8s.io/apiserver/pkg/authorization/authorizer"
    25  	"k8s.io/apiserver/plugin/pkg/authorizer/webhook"
    26  	authorizationclient "k8s.io/client-go/kubernetes/typed/authorization/v1"
    27  )
    28  
    29  // DelegatingAuthorizerConfig is the minimal configuration needed to create an authorizer
    30  // built to delegate authorization to a kube API server
    31  type DelegatingAuthorizerConfig struct {
    32  	SubjectAccessReviewClient authorizationclient.AuthorizationV1Interface
    33  
    34  	// AllowCacheTTL is the length of time that a successful authorization response will be cached
    35  	AllowCacheTTL time.Duration
    36  
    37  	// DenyCacheTTL is the length of time that an unsuccessful authorization response will be cached.
    38  	// You generally want more responsive, "deny, try again" flows.
    39  	DenyCacheTTL time.Duration
    40  
    41  	// WebhookRetryBackoff specifies the backoff parameters for the authorization webhook retry logic.
    42  	// This allows us to configure the sleep time at each iteration and the maximum number of retries allowed
    43  	// before we fail the webhook call in order to limit the fan out that ensues when the system is degraded.
    44  	WebhookRetryBackoff *wait.Backoff
    45  }
    46  
    47  func (c DelegatingAuthorizerConfig) New() (authorizer.Authorizer, error) {
    48  	if c.WebhookRetryBackoff == nil {
    49  		return nil, errors.New("retry backoff parameters for delegating authorization webhook has not been specified")
    50  	}
    51  
    52  	return webhook.NewFromInterface(
    53  		c.SubjectAccessReviewClient,
    54  		c.AllowCacheTTL,
    55  		c.DenyCacheTTL,
    56  		*c.WebhookRetryBackoff,
    57  		authorizer.DecisionNoOpinion,
    58  		NewDelegatingAuthorizerMetrics(),
    59  	)
    60  }