Skip to content

add NewServerTLS - #2708

Closed
davidhadas wants to merge 1 commit into
knative:mainfrom
davidhadas:addNewServerTLS
Closed

davidhadas wants to merge 1 commit into
knative:mainfrom
davidhadas:addNewServerTLS

Conversation

@davidhadas

Copy link
Copy Markdown
Contributor

Add another public function to pkg h2c that allows configuring the server's tls.Config

Fix #2699

@knative-prow knative-prow Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Mar 27, 2023
@knative-prow

knative-prow Bot commented Mar 27, 2023

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: davidhadas
Once this PR has been reviewed and has the lgtm label, please assign dprotaso for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow
knative-prow Bot requested review from pradnyavmw and skonto March 27, 2023 07:50
@davidhadas

Copy link
Copy Markdown
Contributor Author

/cc @evankanderson

@knative-prow
knative-prow Bot requested a review from evankanderson March 27, 2023 07:50
@davidhadas

Copy link
Copy Markdown
Contributor Author

/retest

2 similar comments
@davidhadas

Copy link
Copy Markdown
Contributor Author

/retest

@davidhadas

Copy link
Copy Markdown
Contributor Author

/retest

Comment thread network/h2c.go
Comment on lines +30 to +41
// NewServerTLS is same as NewServer but allows configuring the server's TLSConfig
func NewServerTLS(addr string, h http.Handler, tlsConfig *tls.Config) *http.Server {
//nolint:gosec
h1s := &http.Server{
Addr: addr,
Handler: h2c.NewHandler(h, &http2.Server{}),
TLSConfig: tlsConfig,
}

return h1s
}

@evankanderson evankanderson Mar 27, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider using the functional options pattern:

https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

This would look like:

type ServerOption func(*http.Server)

func WithTLSConfig(tlsConfig *tls.Config) ServerOption {
  return func(s *http.Server) {
    s.TLSConfig = tlsConfig
  }
}

func NewServer(addr string, h http.Handler, opts ...ServerOption) *http.Server {
  h1s := ...

  for _, opt := range opts {
    opt(h1s)
  }

  return h1s
}

Among other things, it will ensure that the NewServerTLS and other implementations also implement the slowloris attack mitigation on line 47.

Also, it will avoid creating many NewServerFoo implementations, at the cost of writing:

srv = NewServer(":8080", myHandler, WithTLSConfig(myTLS))

@dprotaso

Copy link
Copy Markdown
Member

TLSConfig is a public property on the http.Server - the helper here isn't necessary because end-users can just configure TLS directly

server := pkg.NewServer(...)
server.TLSConfig = ...

@davidhadas

Copy link
Copy Markdown
Contributor Author

TLSConfig is a public property on the http.Server - the helper here isn't necessary because end-users can just configure TLS directly

server := pkg.NewServer(...)
server.TLSConfig = ...

That is a very good point :)
Closing this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support creating TLS servers with mutual trust

3 participants