-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delegated authz: add AlwaysAllowPaths mechanism to exclude e.g. /healthz
Kubernetes-commit: 6142e2f8f7c8b1c5d32a2f9aa3715ea0b5baf167
- Loading branch information
1 parent
362a8e8
commit 468536b
Showing
5 changed files
with
198 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package path contains an authorizer that allows certain paths and path prefixes. | ||
package path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package path | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apiserver/pkg/authorization/authorizer" | ||
) | ||
|
||
// NewAuthorizer returns an authorizer which accepts a given set of paths. | ||
// Each path is either a fully matching path or it ends in * in case a prefix match is done. A leading / is optional. | ||
func NewAuthorizer(alwaysAllowPaths []string) (authorizer.Authorizer, error) { | ||
var prefixes []string | ||
paths := sets.NewString() | ||
for _, p := range alwaysAllowPaths { | ||
p = strings.TrimPrefix(p, "/") | ||
if len(p) == 0 { | ||
// matches "/" | ||
paths.Insert(p) | ||
continue | ||
} | ||
if strings.ContainsRune(p[:len(p)-1], '*') { | ||
return nil, fmt.Errorf("only trailing * allowed in %q", p) | ||
} | ||
if strings.HasSuffix(p, "*") { | ||
prefixes = append(prefixes, p[:len(p)-1]) | ||
} else { | ||
paths.Insert(p) | ||
} | ||
} | ||
|
||
return authorizer.AuthorizerFunc(func(a authorizer.Attributes) (authorizer.Decision, string, error) { | ||
if a.IsResourceRequest() { | ||
return authorizer.DecisionNoOpinion, "", nil | ||
} | ||
|
||
pth := strings.TrimPrefix(a.GetPath(), "/") | ||
if paths.Has(pth) { | ||
return authorizer.DecisionAllow, "", nil | ||
} | ||
|
||
for _, prefix := range prefixes { | ||
if strings.HasPrefix(pth, prefix) { | ||
return authorizer.DecisionAllow, "", nil | ||
} | ||
} | ||
|
||
return authorizer.DecisionNoOpinion, "", nil | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package path | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apiserver/pkg/authorization/authorizer" | ||
) | ||
|
||
func TestNewAuthorizer(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
excludedPaths []string | ||
allowed, denied, noOpinion []string | ||
wantErr bool | ||
}{ | ||
{"inner star", []string{"/foo*bar"}, nil, nil, nil, true}, | ||
{"double star", []string{"/foo**"}, nil, nil, nil, true}, | ||
{"empty", nil, nil, nil, []string{"/"}, false}, | ||
{"slash", []string{"/"}, []string{"/"}, nil, []string{"/foo", "//"}, false}, | ||
{"foo", []string{"/foo"}, []string{"/foo", "foo"}, nil, []string{"/", "", "/bar", "/foo/", "/fooooo", "//foo"}, false}, | ||
{"foo slash", []string{"/foo/"}, []string{"/foo/"}, nil, []string{"/", "", "/bar", "/foo", "/fooooo"}, false}, | ||
{"foo slash star", []string{"/foo/*"}, []string{"/foo/", "/foo/bar/bla"}, nil, []string{"/", "", "/foo", "/bar", "/fooooo"}, false}, | ||
{"foo bar", []string{"/foo", "/bar"}, []string{"/foo", "/bar"}, nil, []string{"/", "", "/foo/", "/bar/", "/fooooo"}, false}, | ||
{"foo star", []string{"/foo*"}, []string{"/foo", "/foooo"}, nil, []string{"/", "", "/fo", "/bar"}, false}, | ||
{"star", []string{"/*"}, []string{"/", "", "/foo", "/foooo"}, nil, nil, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a, err := NewAuthorizer(tt.excludedPaths) | ||
if err != nil && !tt.wantErr { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if err == nil && tt.wantErr { | ||
t.Fatalf("expected error, didn't get any") | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, cases := range []struct { | ||
paths []string | ||
want authorizer.Decision | ||
}{ | ||
{tt.allowed, authorizer.DecisionAllow}, | ||
{tt.denied, authorizer.DecisionDeny}, | ||
{tt.noOpinion, authorizer.DecisionNoOpinion}, | ||
} { | ||
for _, pth := range cases.paths { | ||
info := authorizer.AttributesRecord{ | ||
Path: pth, | ||
} | ||
if got, _, err := a.Authorize(info); err != nil { | ||
t.Errorf("NewAuthorizer(%v).Authorize(%q) return unexpected error: %v", tt.excludedPaths, pth, err) | ||
} else if got != cases.want { | ||
t.Errorf("NewAuthorizer(%v).Authorize(%q) = %v, want %v", tt.excludedPaths, pth, got, cases.want) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters