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
41 changes: 39 additions & 2 deletions credentials/tls/certprovider/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package certprovider

import (
"context"
"fmt"
"sync"
)
Expand Down Expand Up @@ -53,6 +54,23 @@ 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 avoid double
// close still in use provider.
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

... with a reference count to properly handle multiple calls to Close.

type singleCloseWrappedProvider struct {
mu sync.RWMutex
provider Provider
}

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

// Close overrides the Close method of the embedded provider to avoid release the
// already released reference.
func (w *singleCloseWrappedProvider) Close() {
w.mu.Lock()
defer w.mu.Unlock()

w.provider.Close()
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 do this call without the lock held?

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 have replaced the use of sync.RWMutex with atomic.Pointer

w.provider = closedProvider{}
}

// 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) {
w.mu.RLock()
defer w.mu.RUnlock()

return w.provider.KeyMaterial(ctx)
}

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

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

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