-
Notifications
You must be signed in to change notification settings - Fork 23
wip: add refreshable *tls.Config param support #761
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
Draft
sidkmenon
wants to merge
2
commits into
palantir:develop
Choose a base branch
from
sidkmenon:sm/update_fn_alt
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,87 @@ package refreshingclient | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/palantir/pkg/refreshable" | ||
"github.com/palantir/pkg/tlsconfig" | ||
werror "github.com/palantir/witchcraft-go-error" | ||
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log" | ||
) | ||
|
||
type RefreshableTLSConfig interface { | ||
GetTLSConfig(ctx context.Context) *tls.Config | ||
SubscribeToTLSConfig(consumer func(*tls.Config)) (unsubscribe func()) | ||
} | ||
|
||
var _ RefreshableTLSConfig = (*MappedRefreshableTLSConfig)(nil) | ||
|
||
func ConfigureTLSConfig(r RefreshableTLSConfig, mapFn func(conf *tls.Config) *tls.Config) RefreshableTLSConfig { | ||
var m MappedRefreshableTLSConfig | ||
r.SubscribeToTLSConfig(func(c *tls.Config) { | ||
m.Update(mapFn(c)) | ||
}) | ||
return &m | ||
} | ||
|
||
type MappedRefreshableTLSConfig struct { | ||
conf atomic.Pointer[tls.Config] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we reimplementing refreshable? |
||
|
||
mu sync.Mutex // protects subscribers | ||
subscribers []*func(*tls.Config) | ||
} | ||
|
||
// NewMappedRefreshableTLSConfig returns a new *MappedRefreshableTLSConfig. | ||
func NewMappedRefreshableTLSConfig(conf *tls.Config) *MappedRefreshableTLSConfig { | ||
var m MappedRefreshableTLSConfig | ||
m.conf.Store(conf) | ||
return &m | ||
} | ||
|
||
// GetTLSConfig implements RefreshableTLSConf. | ||
func (m *MappedRefreshableTLSConfig) GetTLSConfig(ctx context.Context) *tls.Config { | ||
return m.conf.Load() | ||
} | ||
|
||
func (m *MappedRefreshableTLSConfig) Update(conf *tls.Config) { | ||
m.conf.Store(conf) | ||
|
||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
for _, sub := range m.subscribers { | ||
(*sub)(conf) | ||
} | ||
} | ||
|
||
// SubscribeToTLSConfig implements RefreshableTLSConf. | ||
func (m *MappedRefreshableTLSConfig) SubscribeToTLSConfig(consumer func(*tls.Config)) (unsubscribe func()) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
consumerFnPtr := &consumer | ||
m.subscribers = append(m.subscribers, consumerFnPtr) | ||
return func() { | ||
m.unsubscribe(consumerFnPtr) | ||
} | ||
} | ||
|
||
func (m *MappedRefreshableTLSConfig) unsubscribe(consumerFnPtr *func(*tls.Config)) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
matchIdx := -1 | ||
for idx, currSub := range m.subscribers { | ||
if currSub == consumerFnPtr { | ||
matchIdx = idx | ||
break | ||
} | ||
} | ||
if matchIdx != -1 { | ||
m.subscribers = append(m.subscribers[:matchIdx], m.subscribers[matchIdx+1:]...) | ||
} | ||
} | ||
|
||
// TLSParams contains the parameters needed to build a *tls.Config. | ||
// Its fields must all be compatible with reflect.DeepEqual. | ||
type TLSParams struct { | ||
|
@@ -37,6 +111,8 @@ type TLSProvider interface { | |
GetTLSConfig(ctx context.Context) *tls.Config | ||
} | ||
|
||
var _ RefreshableTLSConfig = (*StaticTLSConfigProvider)(nil) | ||
|
||
// StaticTLSConfigProvider is a TLSProvider that always returns the same *tls.Config. | ||
type StaticTLSConfigProvider tls.Config | ||
|
||
|
@@ -48,37 +124,49 @@ func (p *StaticTLSConfigProvider) GetTLSConfig(context.Context) *tls.Config { | |
return (*tls.Config)(p) | ||
} | ||
|
||
type RefreshableTLSConfig struct { | ||
// SubscribeToTLSConfig implements RefreshableTLSConf. | ||
func (p *StaticTLSConfigProvider) SubscribeToTLSConfig(consumer func(*tls.Config)) (unsubscribe func()) { | ||
return nil | ||
} | ||
|
||
type WrappedRefreshableTLSConfig struct { | ||
r *refreshable.ValidatingRefreshable // contains *tls.Config | ||
} | ||
|
||
// NewRefreshableTLSConfig evaluates the provided TLSParams and returns a RefreshableTLSConfig that will update the | ||
// NewRefreshableTLSConfigFromParams evaluates the provided TLSParams and returns a RefreshableTLSConfig that will update the | ||
// underlying *tls.Config when the TLSParams change. | ||
// IF the initial TLSParams are invalid, NewRefreshableTLSConfig will return an error. | ||
// IF the initial TLSParams are invalid, NewRefreshableTLSConfigFromParams will return an error. | ||
// If the updated TLSParams are invalid, the RefreshableTLSConfig will continue to use the previous value and log the error. | ||
// | ||
// N.B. This subscription only fires when the paths are updated, not when the contents of the files are updated. | ||
// We could consider adding a file refreshable to watch the key and cert files. | ||
func NewRefreshableTLSConfig(ctx context.Context, params RefreshableTLSParams) (TLSProvider, error) { | ||
func NewRefreshableTLSConfigFromParams(ctx context.Context, params RefreshableTLSParams) (RefreshableTLSConfig, error) { | ||
r, err := refreshable.NewMapValidatingRefreshable(params, func(i interface{}) (interface{}, error) { | ||
return NewTLSConfig(ctx, i.(TLSParams)) | ||
}) | ||
if err != nil { | ||
return nil, werror.WrapWithContextParams(ctx, err, "failed to build RefreshableTLSConfig") | ||
} | ||
return RefreshableTLSConfig{r: r}, nil | ||
return WrappedRefreshableTLSConfig{r: r}, nil | ||
} | ||
|
||
// GetTLSConfig returns the most recent valid *tls.Config. | ||
// If the last refreshable update resulted in an error, that error is logged and | ||
// the previous value is returned. | ||
func (r RefreshableTLSConfig) GetTLSConfig(ctx context.Context) *tls.Config { | ||
func (r WrappedRefreshableTLSConfig) GetTLSConfig(ctx context.Context) *tls.Config { | ||
if err := r.r.LastValidateErr(); err != nil { | ||
svc1log.FromContext(ctx).Warn("Invalid TLS config. Using previous value.", svc1log.Stacktrace(err)) | ||
} | ||
return r.r.Current().(*tls.Config) | ||
} | ||
|
||
// SubscribeToTLSConfig implements RefreshableTLSConf. | ||
func (r WrappedRefreshableTLSConfig) SubscribeToTLSConfig(consumer func(*tls.Config)) (unsubscribe func()) { | ||
return r.r.Subscribe(func(i interface{}) { | ||
consumer(i.(*tls.Config)) | ||
}) | ||
} | ||
|
||
// NewTLSConfig returns a *tls.Config built from the provided TLSParams. | ||
func NewTLSConfig(ctx context.Context, p TLSParams) (*tls.Config, error) { | ||
var tlsParams []tlsconfig.ClientParam | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,12 @@ package refreshingclient | |
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net/http" | ||
"net/url" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/palantir/pkg/refreshable" | ||
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log" | ||
"golang.org/x/net/http2" | ||
) | ||
|
@@ -42,12 +43,21 @@ type TransportParams struct { | |
TLS TLSParams | ||
} | ||
|
||
func NewRefreshableTransport(ctx context.Context, p RefreshableTransportParams, tlsProvider TLSProvider, dialer ContextDialer) http.RoundTripper { | ||
return &RefreshableTransport{ | ||
Refreshable: p.MapTransportParams(func(p TransportParams) interface{} { | ||
return newTransport(ctx, p, tlsProvider, dialer) | ||
}), | ||
} | ||
func NewRefreshableTransport(ctx context.Context, p RefreshableTransportParams, t RefreshableTLSConfig, dialer ContextDialer) http.RoundTripper { | ||
var refreshingTransport RefreshableTransport | ||
|
||
// initialize the transport the first time. | ||
refreshingTransport.Update(ctx, p.CurrentTransportParams(), t.GetTLSConfig(ctx), dialer) | ||
|
||
// also subscribe to updates on transport params and the tls provider. | ||
p.SubscribeToTransportParams(func(tp TransportParams) { | ||
refreshingTransport.Update(ctx, tp, t.GetTLSConfig(ctx), dialer) | ||
}) | ||
t.SubscribeToTLSConfig(func(conf *tls.Config) { | ||
refreshingTransport.Update(ctx, p.CurrentTransportParams(), conf, dialer) | ||
}) | ||
|
||
return &refreshingTransport | ||
} | ||
|
||
// ConfigureTransport accepts a mapping function which will be applied to the params value as it is evaluated. | ||
|
@@ -61,14 +71,14 @@ func ConfigureTransport(r RefreshableTransportParams, mapFn func(p TransportPara | |
// RefreshableTransport implements http.RoundTripper backed by a refreshable *http.Transport. | ||
// The transport and internal dialer are each rebuilt when any of their respective parameters are updated. | ||
type RefreshableTransport struct { | ||
refreshable.Refreshable // contains *http.Transport | ||
atomic.Pointer[http.Transport] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why change this? |
||
} | ||
|
||
func (r *RefreshableTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return r.Current().(*http.Transport).RoundTrip(req) | ||
return r.Load().RoundTrip(req) | ||
} | ||
|
||
func newTransport(ctx context.Context, p TransportParams, tlsProvider TLSProvider, dialer ContextDialer) *http.Transport { | ||
func (r *RefreshableTransport) Update(ctx context.Context, p TransportParams, tlsConfig *tls.Config, dialer ContextDialer) { | ||
svc1log.FromContext(ctx).Debug("Reconstructing HTTP Transport") | ||
|
||
var transportProxy func(*http.Request) (*url.URL, error) | ||
|
@@ -78,7 +88,6 @@ func newTransport(ctx context.Context, p TransportParams, tlsProvider TLSProvide | |
transportProxy = http.ProxyFromEnvironment | ||
} | ||
|
||
tlsConfig := tlsProvider.GetTLSConfig(ctx) | ||
transport := &http.Transport{ | ||
Proxy: transportProxy, | ||
DialContext: dialer.DialContext, | ||
|
@@ -115,6 +124,5 @@ func newTransport(ctx context.Context, p TransportParams, tlsProvider TLSProvide | |
http2Transport.PingTimeout = p.HTTP2PingTimeout | ||
} | ||
} | ||
|
||
return transport | ||
r.Store(transport) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this, the clients would be responsible for calling
updateFn
whenever they want to refresh the underlying*tls.Config
of their clients.