Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions credentials/tls/certprovider/pemfile/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync/atomic"
"time"

"google.golang.org/grpc/credentials/tls/certprovider"
Expand Down Expand Up @@ -130,6 +131,7 @@ type watcher struct {
certFileContents []byte
keyFileContents []byte
rootFileContents []byte
closeMark atomic.Bool
cancel context.CancelFunc
}

Expand Down Expand Up @@ -256,5 +258,8 @@ func (w *watcher) KeyMaterial(ctx context.Context) (*certprovider.KeyMaterial, e

// Close cleans up resources allocated by the watcher.
func (w *watcher) Close() {
if w.closeMark.Swap(true) {
panic("Close called more than once on certificate provider")
}
w.cancel()
}
1 change: 1 addition & 0 deletions credentials/tls/certprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Provider interface {
KeyMaterial(ctx context.Context) (*KeyMaterial, error)

// Close cleans up resources allocated by the Provider.
// This method panic when called more than once.
Close()
}

Expand Down
1 change: 1 addition & 0 deletions credentials/tls/certprovider/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (c closedProvider) KeyMaterial(ctx context.Context) (*KeyMaterial, error) {
}

func (c closedProvider) Close() {
panic("Close called more than once on certificate provider")
}

// singleCloseWrappedProvider wraps a provider instance with a reference count to avoid double
Expand Down
14 changes: 12 additions & 2 deletions credentials/tls/certprovider/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ func (s) TestStoreSingleProviderDifferentConfigs(t *testing.T) {
cfg2 := fakeConfig + "2222"

prov1 := createProvider(t, fakeProvider1Name, cfg1, opts)
defer prov1.Close()
defer func() {
if prov1 != nil {
prov1.Close()
}
}()
// Our fakeProviderBuilder pushes newly created providers on a channel. Grab
// the fake provider from that channel.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down Expand Up @@ -349,6 +353,7 @@ func (s) TestStoreSingleProviderDifferentConfigs(t *testing.T) {

// Close one of the providers and verify that the other one is not affected.
prov1.Close()
prov1 = nil
if err := readAndVerifyKeyMaterial(ctx, prov2, km2); err != nil {
t.Fatal(err)
}
Expand All @@ -359,7 +364,11 @@ func (s) TestStoreSingleProviderDifferentConfigs(t *testing.T) {
func (s) TestStoreMultipleProviders(t *testing.T) {
opts := BuildOptions{CertName: "foo"}
prov1 := createProvider(t, fakeProvider1Name, fakeConfig, opts)
defer prov1.Close()
defer func() {
if prov1 != nil {
prov1.Close()
}
}()
// Our fakeProviderBuilder pushes newly created providers on a channel. Grab
// the fake provider from that channel.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down Expand Up @@ -394,6 +403,7 @@ func (s) TestStoreMultipleProviders(t *testing.T) {

// Close one of the providers and verify that the other one is not affected.
prov1.Close()
prov1 = nil
if err := readAndVerifyKeyMaterial(ctx, prov2, km2); err != nil {
t.Fatal(err)
}
Expand Down