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

Fix close in use certificate providers after double Close() method call on wrapper object #7128

Merged
merged 5 commits into from
May 29, 2024
42 changes: 40 additions & 2 deletions credentials/tls/certprovider/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package certprovider

import (
"context"
"fmt"
"sync"
"sync/atomic"
)

// provStore is the global singleton certificate provider store.
Expand Down Expand Up @@ -53,6 +55,22 @@ type wrappedProvider struct {
store *store
}

// closedProvider always returns errProviderClosed error.
type closedProvider struct{}

func (c closedProvider) KeyMaterial(ctx context.Context) (*KeyMaterial, error) {
return nil, errProviderClosed
}

func (c closedProvider) Close() {
}

// singleCloseWrappedProvider wraps a provider instance with a reference count
// to properly handle multiple calls to Close.
type singleCloseWrappedProvider struct {
provider atomic.Pointer[Provider]
}

// store is a collection of provider instances, safe for concurrent access.
type store struct {
mu sync.Mutex
Expand All @@ -75,6 +93,26 @@ func (wp *wrappedProvider) Close() {
}
}

// Close overrides the Close method of the embedded provider to avoid release the
// already released reference.
func (w *singleCloseWrappedProvider) Close() {
newProvider := Provider(closedProvider{})
easwars marked this conversation as resolved.
Show resolved Hide resolved
oldProvider := w.provider.Swap(&newProvider)
(*oldProvider).Close()
}

// KeyMaterial returns the key material sourced by the Provider.
// Callers are expected to use the returned value as read-only.
func (w *singleCloseWrappedProvider) KeyMaterial(ctx context.Context) (*KeyMaterial, error) {
return (*w.provider.Load()).KeyMaterial(ctx)
}

// withProvider set provider to wrapper.
func (w *singleCloseWrappedProvider) withProvider(provider Provider) *singleCloseWrappedProvider {
dfawley marked this conversation as resolved.
Show resolved Hide resolved
w.provider.Store(&provider)
return w
}

// BuildableConfig wraps parsed provider configuration and functionality to
// instantiate provider instances.
type BuildableConfig struct {
Expand Down Expand Up @@ -112,7 +150,7 @@ func (bc *BuildableConfig) Build(opts BuildOptions) (Provider, error) {
}
if wp, ok := provStore.providers[sk]; ok {
wp.refCount++
return wp, nil
return (&singleCloseWrappedProvider{}).withProvider(wp), nil
}

provider := bc.starter(opts)
Expand All @@ -126,7 +164,7 @@ func (bc *BuildableConfig) Build(opts BuildOptions) (Provider, error) {
store: provStore,
}
provStore.providers[sk] = wp
return wp, nil
return (&singleCloseWrappedProvider{}).withProvider(wp), nil
}

// String returns the provider name and config as a colon separated string.
Expand Down
Loading