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

Use DialTLSContextFunc instead of tls.Config for NewProxyAutoTLSTransport #2842

Merged
merged 3 commits into from
Oct 16, 2023
Merged

Conversation

nak3
Copy link
Contributor

@nak3 nak3 commented Sep 27, 2023

Current NewProxyAutoTLSTransport takes TLSConfig. It is easy to use but impossible to customize to verify custom SAN.
So, this patch replaces it with DialTLSContext.

Alternative

I considered a downstream may be using NewProxyAutoTLSTransport with TLSConfig. But NewProxyAutoTLSTransport was added for internal encryption #2479 1 year ago. If an user wants NewProxyAutoTLSTransport to take TLSConfig we can add it later.

See also knative/serving#14452

@knative-prow knative-prow bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 27, 2023
@knative-prow knative-prow bot requested review from aslom and Cali0707 September 27, 2023 11:53
@nak3
Copy link
Contributor Author

nak3 commented Sep 27, 2023

@codecov
Copy link

codecov bot commented Sep 27, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (0f52db7) 81.82% compared to head (aa6e971) 81.81%.
Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2842      +/-   ##
==========================================
- Coverage   81.82%   81.81%   -0.02%     
==========================================
  Files         167      167              
  Lines       10224    10222       -2     
==========================================
- Hits         8366     8363       -3     
  Misses       1612     1612              
- Partials      246      247       +1     
Files Coverage Δ
network/h2c.go 16.66% <0.00%> (+0.66%) ⬆️
network/transports.go 72.83% <0.00%> (+0.88%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@KauzClay
Copy link
Contributor

So by switching to use the DialTLSContext, it will let us insert some custom logic to handle setting up the TLS connection, such as verifying a custom SAN?

Reading this, and your linked PR to serving, it looks like it goes together nicely.

But just for my understanding, why is the GetCertificate function on the TLSConfig not enough for this case? My understanding was you could use that to get SAN info from the client hello, and in there you could return the correct cert. Perhaps I'm mixing things up?

@nak3
Copy link
Contributor Author

nak3 commented Sep 28, 2023

So by switching to use the DialTLSContext, it will let us insert some custom logic to handle setting up the TLS connection, such as verifying a custom SAN?

Yes, that's right.

But just for my understanding, why is the GetCertificate function on the TLSConfig not enough for this case? My understanding was you could use that to get SAN info from the client hello, and in there you could return the correct cert. Perhaps I'm mixing things up?

We want the client to validate if the server certificate matches the certificate of the expected namespace - i.e., to prevent MITM attacks.
"you could use that to get SAN info from the client hello, and in there you could return the correct cert." allows the server to return a correct certificate, yes, but the client does not validate the SAN even if an impostor returns a certificate by using another namespace's certificate.

@KauzClay
Copy link
Contributor

We want the client to validate if the server certificate matches the certificate of the expected namespace - i.e., to prevent MITM attacks.
"you could use that to get SAN info from the client hello, and in there you could return the correct cert." allows the server to return a correct certificate, yes, but the client does not validate the SAN even if an impostor returns a certificate by using another namespace's certificate.

Okay I see, I was mixing up client and server activities. Thanks for taking the time to explain that, I appreciate it 👍

Seems like a good change to me, but perhaps you want Dave or Reto to see it as well?

@KauzClay
Copy link
Contributor

I learned how to use these labels lol

It looks good to me, but I'll put it on hold. @nak3 feel free to unhold, or keep it held for Reto or someone else to look at.

/lgtm
/hold

@knative-prow knative-prow bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 28, 2023
@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Sep 28, 2023
@nak3
Copy link
Contributor Author

nak3 commented Oct 2, 2023

@dprotaso Could you please take a look at this and knative/serving#14452 ?
Then if you are alright, override Downstream Knative / Unit Test (knative/serving) ?
I guess override is not necessary for this repo?

Copy link
Member

@dprotaso dprotaso left a comment

Choose a reason for hiding this comment

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

It might be simpler to plumb in a tls.Config with GetConfigForClient set?

return DialTLSWithBackOff(context.Background(),
netw, addr, tlsConf)
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
return tlsContext(ctx, network, addr)
Copy link
Member

Choose a reason for hiding this comment

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

Any reason for dropping DialTLSWithBackOff ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DialWithBackOff is called downstream knative/serving#14452.
If we keep DialWithBackOff and call it here, we need to bring tlsConf as an extra argument of newH2Transport(). Is it better to call DialTLSWithBackOff here?

@knative-prow knative-prow bot removed the lgtm Indicates that a PR is ready to be merged. label Oct 3, 2023
@nak3
Copy link
Contributor Author

nak3 commented Oct 3, 2023

It might be simpler to plumb in a tls.Config with GetConfigForClient set?

GetConfigForClient is only available to verify client certificates on server side[1]. We want to verify server certificate so it seems not available here.

[1] https://pkg.go.dev/crypto/tls#Config

// GetConfigForClient, if not nil, is called after a ClientHello is
// received from a client.

@nak3
Copy link
Contributor Author

nak3 commented Oct 3, 2023

/test unit-tests

@dprotaso
Copy link
Member

/lgtm
/approve

@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label Oct 16, 2023
@knative-prow
Copy link

knative-prow bot commented Oct 16, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dprotaso, nak3

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

The pull request process is described here

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

@dprotaso
Copy link
Member

/hold cancel

@knative-prow knative-prow bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 16, 2023
@knative-prow knative-prow bot merged commit 44a8a5e into knative:main Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. 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.

3 participants