Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p/a/path: fail on broken pattern early, add tests #250

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

ibihim
Copy link
Collaborator

@ibihim ibihim commented Jul 11, 2023

What

  • Change path-authorizer to act like upstream (only postfix patterns).
  • Fail on broken pattern.
  • Add unit tests for Ignore-Path-Case

Why

  • Upstream doesn't use complex patter matching from the standard library. It is prone to misconfiguration.
  • If a broken pattern is given, it behaved like a pattern match.
  • There were no unit tests for the Ignore-Path-Case.

@ibihim ibihim force-pushed the path-authorizer-fix-test branch 5 times, most recently from b3ed530 to aaf2a62 Compare July 12, 2023 12:56
cmd/kube-rbac-proxy/app/kube-rbac-proxy.go Outdated Show resolved Hide resolved
cmd/kube-rbac-proxy/app/kube-rbac-proxy.go Outdated Show resolved Hide resolved
pkg/authorization/path/path.go Show resolved Hide resolved
pkg/authorization/rewrite/rewrite.go Outdated Show resolved Hide resolved
cmd/kube-rbac-proxy/app/kube-rbac-proxy.go Outdated Show resolved Hide resolved
cmd/kube-rbac-proxy/app/kube-rbac-proxy.go Outdated Show resolved Hide resolved
pkg/authorization/rewrite/rewrite.go Outdated Show resolved Hide resolved
docs/path.md Outdated Show resolved Hide resolved
docs/path.md Outdated
Paths that are provided by `--ignore-path` won't be rejected.
It is only possible to define one of both.

## Change
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a separate section for changes to previous kube-rbac-versions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean in addition to the CHANGELOG.md?

docs/path.md Outdated Show resolved Hide resolved
docs/path.md Outdated
E.g. `--allow-path="/api/v1/*/values"` will cause kube-rbac-proxy fail to start.
E.g. `--ignore-path="/api/v1/*/values"` will cause kube-rbac-proxy fail to start.

Previously `*` would count as a wildcard in between `/`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't it a single-path-segment wildcard?

docs/path.md Outdated Show resolved Hide resolved
Comment on lines 27 to 40
type pathAuthorizer struct {
matchDecision, noMatchDecision authorizer.Decision

paths sets.String
pathPatterns []string
}

func newPathAuthorizer(onMatch, onNoMatch authorizer.Decision, inputPaths []string) *pathAuthorizer {
var patterns []string
paths := sets.NewString() // faster than trying to match every pattern every time
for _, p := range inputPaths {
p = strings.TrimPrefix(p, "/")
if len(p) == 0 {
// matches "/"
paths.Insert(p)
continue
}
if strings.ContainsRune(p, '*') {
patterns = append(patterns, p)
} else {
paths.Insert(p)
}
}

return &pathAuthorizer{
matchDecision: onMatch,
noMatchDecision: onNoMatch,
paths: paths,
pathPatterns: patterns,
}
delegatedPathAuthorizer authorizer.Authorizer
}

func (a *pathAuthorizer) Authorize(ctx context.Context, attr authorizer.Attributes) (authorizer.Decision, string, error) {
pth := strings.TrimPrefix(attr.GetPath(), "/")
if a.paths.Has(pth) {
return a.matchDecision, "", nil
}
decision, reason, err := a.delegatedPathAuthorizer.Authorize(ctx, attr)

for _, pattern := range a.pathPatterns {
if found, err := path.Match(pattern, pth); err != nil {
return authorizer.DecisionNoOpinion, "Error", err
} else if found {
return a.matchDecision, "", nil
}
// There is a match on the path, so we have no opinion and let subsequent
// authorizers in a union decide.
if err == nil && decision == authorizer.DecisionAllow {
return authorizer.DecisionNoOpinion, reason, nil
}

return a.noMatchDecision, "", nil
return authorizer.DecisionDeny, fmt.Sprintf("NOT(%s)", reason), err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a new type for the allowedPathsAuthorizer - inline as

// pseudocode
func NewAllowedPathsAuthorizer(allowPaths []string) (authorizer.Authorizer, error) {
   pathAuthorizer = NewAuthorizer()
   return authorizer.AuthorizerFunc(func() {
        // Authorize() logic here
    })
}

pkg/authorization/path/path.go Outdated Show resolved Hide resolved
docs/path.md Outdated

Previously `*` would count as a single-path-segment wildcard ([path.Match](https://pkg.go.dev/path#Match)).
Now the wildcard is matching any string, even if it contains `/`.
E.g. `--allow-path="/api/v1/*"` would have rejected `/api/v1/label/values`, not it evaluated.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not it evaluated <- that sentence appears to be missing something

docs/path.md Outdated
Previously `*` would count as a single-path-segment wildcard ([path.Match](https://pkg.go.dev/path#Match)).
Now the wildcard is matching any string, even if it contains `/`.
E.g. `--allow-path="/api/v1/*"` would have rejected `/api/v1/label/values`, not it evaluated.
E.g. `--ignore-path="/api/v1/*"` would have evaluated `/api/v1/label/values`, now it is passed through.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
E.g. `--ignore-path="/api/v1/*"` would have evaluated `/api/v1/label/values`, now it is passed through.
E.g. `--ignore-path="/api/v1/*"` would have matched `/api/v1/label/values`, now it is passed through.

docs/path.md Outdated
@@ -0,0 +1,44 @@
# Allow Path and Ignore Path
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may want to create a separate docs/migration folder for these files

@ibihim ibihim force-pushed the path-authorizer-fix-test branch 2 times, most recently from fd06738 to b42539e Compare November 6, 2023 14:36
@ibihim ibihim merged commit f5d9a22 into brancz:sig-auth-acceptance Nov 6, 2023
7 checks passed
@ibihim ibihim deleted the path-authorizer-fix-test branch November 6, 2023 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants