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

Cleanup genericapiserver handler chain #33164

Merged
merged 3 commits into from Sep 23, 2016

Conversation

sttts
Copy link
Contributor

@sttts sttts commented Sep 21, 2016

  • move generic (api independent) handler filters to pkg/genericapiserver/filters
  • entangle genericapiserver.New()
  • unify signature of all handler filters (also those in pkg/apiserver)

This change is Reviewable

@sttts sttts added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. area/apiserver labels Sep 21, 2016
@k8s-github-robot k8s-github-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. release-note-label-needed labels Sep 21, 2016
@sttts sttts added release-note-none Denotes a PR that doesn't merit a release note. and removed release-note-label-needed labels Sep 21, 2016
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 21, 2016
@sttts sttts changed the title Cleanup genericapiserver handler chain WIP: Cleanup genericapiserver handler chain Sep 21, 2016
}), nil
} else {
return handler, errors.New("Unknown RequestContextMapper implementation.")
func WithRequestContext(handler http.Handler, mapper RequestContextMapper) http.Handler {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any idea why it's in this package? Doesn't have to be this pull, but unless there's a strong reason, I'd like it moved eventually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had the same question. Also the Context interface looks api independent. Doesn't have to be so deep in the package hierarchy. A good topic for a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Had the same question. Also the Context interface looks api independent. Doesn't have to be so deep in the package hierarchy. A good topic for a follow-up PR.

agreed.

@@ -462,7 +144,7 @@ func (r *requestAttributeGetter) GetAttribs(req *http.Request) authorizer.Attrib
}

// WithAuthorizationCheck passes all authorized requests on to handler, and returns a forbidden error otherwise.
func WithAuthorizationCheck(handler http.Handler, getAttribs RequestAttributeGetter, a authorizer.Authorizer) http.Handler {
func WithAuthorization(handler http.Handler, getAttribs RequestAttributeGetter, a authorizer.Authorizer) http.Handler {
Copy link
Contributor

Choose a reason for hiding this comment

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

If a == nil, let's be friendly, skip adding this, and glog a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most filters just return the passed handler when the parameters are nil. Will do the same here.

@@ -337,18 +346,64 @@ func (c Config) New() (*GenericAPIServer, error) {
})
}

if len(c.AuditLogPath) != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add to our running issue. I think all the audit stuff should be gathered up into a pointer to a struct.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not in this pull.

}

func (s *GenericAPIServer) buildHandlerChains(c *Config, handler http.Handler) (secure http.Handler, insecure http.Handler) {
longRunningRE := regexp.MustCompile(c.LongRunningRequestRE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we push these into WithTimeoutForNonLongRunningRequests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, makes sense

longRunningFunc := genericfilters.BasicLongRunningRequestCheck(longRunningRE, map[string]string{"watch": "true"})

// common filters
handler = genericfilters.WithCORS(handler, allowedOriginRegexps(c.CorsAllowedOriginList), nil, nil, "true")
Copy link
Contributor

Choose a reason for hiding this comment

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

common?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

common for secure and insecure.

Copy link
Contributor

Choose a reason for hiding this comment

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

common for secure and insecure.

Yeah, I was suggesting a rename. Doesn't really matter to me.

attributeGetter := apiserver.NewRequestAttributeGetter(c.RequestContextMapper, s.NewRequestInfoResolver())
secure = handler
secure = apiserver.WithAuthorization(secure, attributeGetter, c.Authorizer)
secure = audit.WithAudit(secure, attributeGetter, s.auditWriter) // before impersonation to read original user
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this after impersonation right now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good spot. In origin it's correct.

return s, nil
}

func (s *GenericAPIServer) buildHandlerChains(c *Config, handler http.Handler) (secure http.Handler, insecure http.Handler) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So much cleaner. TODO to type the function and take it as an optional struct value in genericapiserver.

return
}

func allowedOriginRegexps(allowedOrigins []string) []*regexp.Regexp {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this should live in your cors file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved

"strings"
)

// TODO: use restful.CrossOriginResourceSharing
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we're likely to do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somebody somewhen was planning to do that :)

// WithCORS is a simple CORS implementation that wraps an http Handler.
// Pass nil for allowedMethods and allowedHeaders to use the defaults. If allowedOriginPatterns
// is empty or nil, no CORS support is installed.
func WithCORS(handler http.Handler, allowedOriginPatterns []*regexp.Regexp, allowedMethods []string, allowedHeaders []string, allowCredentials string) http.Handler {
Copy link
Contributor

Choose a reason for hiding this comment

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

straight move?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

@deads2k
Copy link
Contributor

deads2k commented Sep 21, 2016

Assuming most of the code is straight moves, I've got a few minor comments, but this looks a lot better.

Any specific reason its still WIP? I'd be fine tying off this chunk without adding more to it.

@deads2k
Copy link
Contributor

deads2k commented Sep 21, 2016

lgtm. squash and tag when you're happy with it.

secure = apiserver.WithImpersonation(secure, c.RequestContextMapper, c.Authorizer)
secure = audit.WithAudit(secure, attributeGetter, s.auditWriter) // before impersonation to read original user
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@sttts sttts changed the title WIP: Cleanup genericapiserver handler chain Cleanup genericapiserver handler chain Sep 21, 2016
@sttts sttts mentioned this pull request Sep 21, 2016
20 tasks
@sttts sttts added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 22, 2016
@sttts
Copy link
Contributor Author

sttts commented Sep 22, 2016

Squashed. Waiting for #33095 to merge.

@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 22, 2016
k8s-github-robot pushed a commit that referenced this pull request Sep 22, 2016
Automatic merge from submit-queue

Remove closing audit log file and add error check when writing to audit

This picks the order fix from #33164. Additionally I've removed entirely closing the log file, since it didn't make sense where it was. I've also added error checks when actually writing to audit logs.

@sttts ptal

**1.4 justification:**

Risk: the code only runs if auditing is enabled with an apiserver flag. So the risk is low.
Rollback: nothing should depend on this
Cost: the auditing feature is broken because the impersonation filter is applied before and you might not see the proper user when using `--as` flag. Additionally no errors are logged if writing to audit fails.
@sttts
Copy link
Contributor Author

sttts commented Sep 23, 2016

Rebased.

@sttts sttts added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Sep 23, 2016
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 23, 2016
@k8s-ci-robot
Copy link
Contributor

Jenkins verification failed for commit 11a608a6599ec1c7c705906b89f53798e8a71774. Full PR test history.

The magic incantation to run this job again is @k8s-bot verify test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@sttts sttts added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Sep 23, 2016
@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 23, 2016
@sttts sttts added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Sep 23, 2016
@k8s-github-robot k8s-github-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Sep 23, 2016
@sttts sttts added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Sep 23, 2016
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 23, 2016
@k8s-github-robot
Copy link

@k8s-bot test this [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@k8s-github-robot k8s-github-robot merged commit 0829f39 into kubernetes:master Sep 23, 2016
@sttts sttts deleted the sttts-handler-chain-cleanup branch September 23, 2016 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiserver kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants