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

xDS: Atomically read and write xDS security configuration client side #6796

Merged
merged 2 commits into from Nov 15, 2023

Conversation

zasweq
Copy link
Contributor

@zasweq zasweq commented Nov 15, 2023

This PR does a few things:

  • Makes handshake info fields set at init time and read only after that, eliminating need for mutex
  • Scale up newHandshakeInfo() constructor to all fields
  • Atomically write and read handshake info for cds

This eliminates acting on partial updates due to synchronization only between setters on individual fields, so reads and writes (within the context of one Handshake and one new update) could arbitrarily intersplice and cause correctness issues due to partial reads of handshake info configuration. Security configuration still needs to be per cluster and not set by the top level cluster, but this fixes the race.

RELEASE NOTES:

  • xDS: Atomically read and write xDS security configuration client side

@zasweq zasweq requested a review from dfawley November 15, 2023 02:19
@zasweq zasweq added this to the 1.61 Release milestone Nov 15, 2023
@zasweq zasweq changed the title xDS: Atomically write and read xDS handshake info client side xDS: Atomically write and read handshake info client side Nov 15, 2023
Copy link

codecov bot commented Nov 15, 2023

Codecov Report

Merging #6796 (73b7d07) into master (8645f95) will decrease coverage by 0.04%.
Report is 5 commits behind head on master.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6796      +/-   ##
==========================================
- Coverage   83.57%   83.53%   -0.04%     
==========================================
  Files         285      285              
  Lines       30950    30935      -15     
==========================================
- Hits        25865    25841      -24     
- Misses       4021     4027       +6     
- Partials     1064     1067       +3     
Files Coverage Δ
credentials/xds/xds.go 87.59% <100.00%> (+0.09%) ⬆️
internal/credentials/xds/handshake_info.go 88.81% <100.00%> (-0.32%) ⬇️
xds/internal/balancer/cdsbalancer/cdsbalancer.go 80.37% <100.00%> (+0.13%) ⬆️
xds/internal/server/conn_wrapper.go 73.61% <100.00%> (-0.72%) ⬇️

... and 18 files with indirect coverage changes

@zasweq zasweq changed the title xDS: Atomically write and read handshake info client side xDS: Atomically write and read xDS security configuration client side Nov 15, 2023
@zasweq zasweq changed the title xDS: Atomically write and read xDS security configuration client side xDS: Atomically read and write xDS security configuration client side Nov 15, 2023
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

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

Logic all LGTM...just a few mostly minor nits/suggestions/etc.

@@ -219,11 +220,13 @@ func newTestContextWithHandshakeInfo(parent context.Context, root, identity cert
// Creating the HandshakeInfo and adding it to the attributes is very
// similar to what the CDS balancer would do when it intercepts calls to
// NewSubConn().
info := xdsinternal.NewHandshakeInfo(root, identity)
var sm []matcher.StringMatcher
Copy link
Member

Choose a reason for hiding this comment

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

Nit/optional: sms to pluralize string matcher.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair. Switched.

Comment on lines 567 to 568
addr = xdsinternal.SetHandshakeInfo(resolver.Address{}, &uPtr)
ctx = icredentials.NewClientHandshakeInfoContext(ctx, credentials.ClientHandshakeInfo{Attributes: addr.Attributes})
Copy link
Member

Choose a reason for hiding this comment

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

Can we just update the existing pointer instead of creating a new entry in the context? That's how the production code will work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. Changed.

addr.Attributes = addr.Attributes.WithValue(handshakeAttrKey{}, hInfo)
// updated with hiPtr.
func SetHandshakeInfo(addr resolver.Address, hiPtr *unsafe.Pointer) resolver.Address {
addr.Attributes = addr.Attributes.WithValue(handshakeAttrKey{}, hiPtr)
return addr
}

// GetHandshakeInfo returns a pointer to the HandshakeInfo stored in attr.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe "pointer to the *HandshakeInfo"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah fair. Switched.

Comment on lines 85 to 86
//
// Safe for concurrent access.
Copy link
Member

Choose a reason for hiding this comment

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

Delete this now? Or say it's immutable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like technically it's fine to call any methods on it concurrently since it's all read only and set at init time, but since it's not really important to call out/doesn't apply I'll delete it.

@@ -128,8 +128,7 @@ func (c *connWrapper) XDSHandshakeInfo() (*xdsinternal.HandshakeInfo, error) {
c.identityProvider = ip
c.rootProvider = rp

xdsHI := xdsinternal.NewHandshakeInfo(c.rootProvider, c.identityProvider)
xdsHI.SetRequireClientCert(secCfg.RequireClientCert)
xdsHI := xdsinternal.NewHandshakeInfo(c.rootProvider, c.identityProvider, nil, secCfg.RequireClientCert)
Copy link
Member

Choose a reason for hiding this comment

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

Optional: return directly instead of using a temp var.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Good point. Switched.

@dfawley dfawley assigned zasweq and unassigned dfawley Nov 15, 2023
@zasweq zasweq merged commit 59c0aec into grpc:master Nov 15, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants