Skip to content

Commit 2e2f51a

Browse files
committed
Plumb failure policy from config to webhook construction
1 parent a000af2 commit 2e2f51a

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

pkg/kubeapiserver/authorizer/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,21 @@ func (config Config) New() (authorizer.Authorizer, authorizer.RuleResolver, erro
118118
if err != nil {
119119
return nil, nil, err
120120
}
121+
var decisionOnError authorizer.Decision
122+
switch configuredAuthorizer.Webhook.FailurePolicy {
123+
case authzconfig.FailurePolicyNoOpinion:
124+
decisionOnError = authorizer.DecisionNoOpinion
125+
case authzconfig.FailurePolicyDeny:
126+
decisionOnError = authorizer.DecisionDeny
127+
default:
128+
return nil, nil, fmt.Errorf("unknown failurePolicy %q", configuredAuthorizer.Webhook.FailurePolicy)
129+
}
121130
webhookAuthorizer, err := webhook.New(clientConfig,
122131
configuredAuthorizer.Webhook.SubjectAccessReviewVersion,
123132
configuredAuthorizer.Webhook.AuthorizedTTL.Duration,
124133
configuredAuthorizer.Webhook.UnauthorizedTTL.Duration,
125134
*config.WebhookRetryBackoff,
135+
decisionOnError,
126136
configuredAuthorizer.Webhook.MatchConditions,
127137
)
128138
if err != nil {

staging/src/k8s.io/apiserver/pkg/authorization/authorizerfactory/delegating.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (c DelegatingAuthorizerConfig) New() (authorizer.Authorizer, error) {
5454
c.AllowCacheTTL,
5555
c.DenyCacheTTL,
5656
*c.WebhookRetryBackoff,
57+
authorizer.DecisionNoOpinion,
5758
webhook.AuthorizerMetrics{
5859
RecordRequestTotal: RecordRequestTotal,
5960
RecordRequestLatency: RecordRequestLatency,

staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook/webhook.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ type WebhookAuthorizer struct {
7575
}
7676

7777
// NewFromInterface creates a WebhookAuthorizer using the given subjectAccessReview client
78-
func NewFromInterface(subjectAccessReview authorizationv1client.AuthorizationV1Interface, authorizedTTL, unauthorizedTTL time.Duration, retryBackoff wait.Backoff, metrics AuthorizerMetrics) (*WebhookAuthorizer, error) {
79-
return newWithBackoff(&subjectAccessReviewV1Client{subjectAccessReview.RESTClient()}, authorizedTTL, unauthorizedTTL, retryBackoff, nil, metrics)
78+
func NewFromInterface(subjectAccessReview authorizationv1client.AuthorizationV1Interface, authorizedTTL, unauthorizedTTL time.Duration, retryBackoff wait.Backoff, decisionOnError authorizer.Decision, metrics AuthorizerMetrics) (*WebhookAuthorizer, error) {
79+
return newWithBackoff(&subjectAccessReviewV1Client{subjectAccessReview.RESTClient()}, authorizedTTL, unauthorizedTTL, retryBackoff, decisionOnError, nil, metrics)
8080
}
8181

8282
// New creates a new WebhookAuthorizer from the provided kubeconfig file.
@@ -98,19 +98,19 @@ func NewFromInterface(subjectAccessReview authorizationv1client.AuthorizationV1I
9898
//
9999
// For additional HTTP configuration, refer to the kubeconfig documentation
100100
// https://kubernetes.io/docs/user-guide/kubeconfig-file/.
101-
func New(config *rest.Config, version string, authorizedTTL, unauthorizedTTL time.Duration, retryBackoff wait.Backoff, matchConditions []apiserver.WebhookMatchCondition) (*WebhookAuthorizer, error) {
101+
func New(config *rest.Config, version string, authorizedTTL, unauthorizedTTL time.Duration, retryBackoff wait.Backoff, decisionOnError authorizer.Decision, matchConditions []apiserver.WebhookMatchCondition) (*WebhookAuthorizer, error) {
102102
subjectAccessReview, err := subjectAccessReviewInterfaceFromConfig(config, version, retryBackoff)
103103
if err != nil {
104104
return nil, err
105105
}
106-
return newWithBackoff(subjectAccessReview, authorizedTTL, unauthorizedTTL, retryBackoff, matchConditions, AuthorizerMetrics{
106+
return newWithBackoff(subjectAccessReview, authorizedTTL, unauthorizedTTL, retryBackoff, decisionOnError, matchConditions, AuthorizerMetrics{
107107
RecordRequestTotal: noopMetrics{}.RecordRequestTotal,
108108
RecordRequestLatency: noopMetrics{}.RecordRequestLatency,
109109
})
110110
}
111111

112112
// newWithBackoff allows tests to skip the sleep.
113-
func newWithBackoff(subjectAccessReview subjectAccessReviewer, authorizedTTL, unauthorizedTTL time.Duration, retryBackoff wait.Backoff, matchConditions []apiserver.WebhookMatchCondition, metrics AuthorizerMetrics) (*WebhookAuthorizer, error) {
113+
func newWithBackoff(subjectAccessReview subjectAccessReviewer, authorizedTTL, unauthorizedTTL time.Duration, retryBackoff wait.Backoff, decisionOnError authorizer.Decision, matchConditions []apiserver.WebhookMatchCondition, metrics AuthorizerMetrics) (*WebhookAuthorizer, error) {
114114
// compile all expressions once in validation and save the results to be used for eval later
115115
cm, fieldErr := apiservervalidation.ValidateAndCompileMatchConditions(matchConditions)
116116
if err := fieldErr.ToAggregate(); err != nil {
@@ -122,7 +122,7 @@ func newWithBackoff(subjectAccessReview subjectAccessReviewer, authorizedTTL, un
122122
authorizedTTL: authorizedTTL,
123123
unauthorizedTTL: unauthorizedTTL,
124124
retryBackoff: retryBackoff,
125-
decisionOnError: authorizer.DecisionNoOpinion,
125+
decisionOnError: decisionOnError,
126126
metrics: metrics,
127127
celMatcher: cm,
128128
}, nil

staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook/webhook_v1_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
utiltesting "k8s.io/client-go/util/testing"
3838

3939
"github.com/google/go-cmp/cmp"
40+
4041
authorizationv1 "k8s.io/api/authorization/v1"
4142
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4243
"k8s.io/apimachinery/pkg/util/wait"
@@ -209,7 +210,7 @@ current-context: default
209210
if err != nil {
210211
return fmt.Errorf("error building sar client: %v", err)
211212
}
212-
_, err = newWithBackoff(sarClient, 0, 0, testRetryBackoff, []apiserver.WebhookMatchCondition{}, noopAuthorizerMetrics())
213+
_, err = newWithBackoff(sarClient, 0, 0, testRetryBackoff, authorizer.DecisionNoOpinion, []apiserver.WebhookMatchCondition{}, noopAuthorizerMetrics())
213214
return err
214215
}()
215216
if err != nil && !tt.wantErr {
@@ -352,7 +353,7 @@ func newV1Authorizer(callbackURL string, clientCert, clientKey, ca []byte, cache
352353
if err != nil {
353354
return nil, fmt.Errorf("error building sar client: %v", err)
354355
}
355-
return newWithBackoff(sarClient, cacheTime, cacheTime, testRetryBackoff, expressions, metrics)
356+
return newWithBackoff(sarClient, cacheTime, cacheTime, testRetryBackoff, authorizer.DecisionNoOpinion, expressions, metrics)
356357
}
357358

358359
func TestV1TLSConfig(t *testing.T) {

staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook/webhook_v1beta1_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"time"
3636

3737
"github.com/google/go-cmp/cmp"
38+
3839
authorizationv1beta1 "k8s.io/api/authorization/v1beta1"
3940
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4041
authzconfig "k8s.io/apiserver/pkg/apis/apiserver"
@@ -196,7 +197,7 @@ current-context: default
196197
if err != nil {
197198
return fmt.Errorf("error building sar client: %v", err)
198199
}
199-
_, err = newWithBackoff(sarClient, 0, 0, testRetryBackoff, []authzconfig.WebhookMatchCondition{}, noopAuthorizerMetrics())
200+
_, err = newWithBackoff(sarClient, 0, 0, testRetryBackoff, authorizer.DecisionNoOpinion, []authzconfig.WebhookMatchCondition{}, noopAuthorizerMetrics())
200201
return err
201202
}()
202203
if err != nil && !tt.wantErr {
@@ -339,7 +340,7 @@ func newV1beta1Authorizer(callbackURL string, clientCert, clientKey, ca []byte,
339340
if err != nil {
340341
return nil, fmt.Errorf("error building sar client: %v", err)
341342
}
342-
return newWithBackoff(sarClient, cacheTime, cacheTime, testRetryBackoff, []authzconfig.WebhookMatchCondition{}, noopAuthorizerMetrics())
343+
return newWithBackoff(sarClient, cacheTime, cacheTime, testRetryBackoff, authorizer.DecisionNoOpinion, []authzconfig.WebhookMatchCondition{}, noopAuthorizerMetrics())
343344
}
344345

345346
func TestV1beta1TLSConfig(t *testing.T) {

0 commit comments

Comments
 (0)