-
Notifications
You must be signed in to change notification settings - Fork 23
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
wip: refreshable *tls.Config support for CGR clients #725
Draft
sidkmenon
wants to merge
2
commits into
palantir:develop
Choose a base branch
from
sidkmenon:sm/refreshable_tls
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 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 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 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,64 @@ package refreshingclient | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"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 RefreshableTLSConf interface { | ||
GetTLSConfig(ctx context.Context) *tls.Config | ||
SubscribeToTLSConfig(consumer func(*tls.Config)) (unsubscribe func()) | ||
} | ||
|
||
var _ RefreshableTLSConf = (*MappedRefreshableTLSConfig)(nil) | ||
|
||
func ConfigureTLSConfig(r RefreshableTLSConf, mapFn func(conf *tls.Config) *tls.Config) RefreshableTLSConf { | ||
var m MappedRefreshableTLSConfig | ||
r.SubscribeToTLSConfig(func(c *tls.Config) { | ||
m.update(mapFn(c)) | ||
}) | ||
return &m | ||
} | ||
|
||
type MappedRefreshableTLSConfig struct { | ||
conf atomic.Pointer[tls.Config] | ||
|
||
mu sync.Mutex // protects subscribers | ||
subscribers []*func(*tls.Config) | ||
} | ||
|
||
// 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) | ||
// TODO(smenon): implement unsubcribe | ||
return func() {} | ||
} | ||
|
||
// TLSParams contains the parameters needed to build a *tls.Config. | ||
// Its fields must all be compatible with reflect.DeepEqual. | ||
type TLSParams struct { | ||
|
@@ -40,45 +91,74 @@ type TLSProvider interface { | |
// StaticTLSConfigProvider is a TLSProvider that always returns the same *tls.Config. | ||
type StaticTLSConfigProvider tls.Config | ||
|
||
func NewStaticTLSConfigProvider(tlsConfig *tls.Config) *StaticTLSConfigProvider { | ||
func NewStaticTLSConfigProvider(tlsConfig *tls.Config) RefreshableTLSConf { | ||
return (*StaticTLSConfigProvider)(tlsConfig) | ||
} | ||
|
||
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) (RefreshableTLSConf, 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 | ||
} | ||
|
||
func NewRefreshableTLSConfigFromRefreshable(r refreshable.Refreshable) (RefreshableTLSConf, error) { | ||
validating, err := refreshable.NewValidatingRefreshable(r, func(i interface{}) error { | ||
_, ok := r.Current().(*tls.Config) | ||
if !ok { | ||
// TODO(smenon): proper error msg. | ||
return errors.New("invalid type for refreshable") | ||
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. generally we are ok with panicking if the type of a refreshable is incorrect (as it's programmer error and should easily get caught the first time the code runs) so I don't think you need NewValidatingRefreshable here |
||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, werror.Wrap(err, "failed to build RefreshableTLSConfig") | ||
} | ||
return WrappedRefreshableTLSConfig{ | ||
r: validating, | ||
}, 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 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
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.
nit: Spell out
RefreshableTLSConfig
to matchtls.Config