Skip to content

Commit 628251b

Browse files
committed
Review comments #1.
1 parent 98e61c7 commit 628251b

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

credentials/tls/certprovider/distributor.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package certprovider
2020

2121
import (
2222
"context"
23-
"crypto/tls"
24-
"crypto/x509"
2523
"sync"
2624

2725
"google.golang.org/grpc/internal/grpcsync"
@@ -38,11 +36,13 @@ import (
3836
// by the provider.
3937
// - When users of the provider call Close(), the channel returned by the
4038
// Done() method will be closed. So, provider implementations can select on
41-
// the channel returned by Done() to perform cleanup work.
39+
// the channel returned by Done() to perform cleanup work, or override
40+
// Close(), in which case they must invoke Distributor.Close() when the
41+
// provider is closed.
4242
type Distributor struct {
43-
mu sync.Mutex
44-
certs []tls.Certificate
45-
roots *x509.CertPool
43+
mu sync.Mutex
44+
km *KeyMaterial
45+
4646
ready *grpcsync.Event
4747
closed *grpcsync.Event
4848
}
@@ -58,8 +58,7 @@ func NewDistributor() *Distributor {
5858
// Set updates the key material in the distributor with km.
5959
func (d *Distributor) Set(km *KeyMaterial) {
6060
d.mu.Lock()
61-
d.certs = km.Certs
62-
d.roots = km.Roots
61+
d.km = km
6362
d.ready.Fire()
6463
d.mu.Unlock()
6564
}
@@ -70,7 +69,7 @@ func (d *Distributor) Set(km *KeyMaterial) {
7069
// arrives.
7170
func (d *Distributor) KeyMaterial(ctx context.Context, opts KeyMaterialOptions) (*KeyMaterial, error) {
7271
if d.closed.HasFired() {
73-
return nil, ErrProviderClosed
72+
return nil, errProviderClosed
7473
}
7574

7675
if d.ready.HasFired() {
@@ -81,15 +80,15 @@ func (d *Distributor) KeyMaterial(ctx context.Context, opts KeyMaterialOptions)
8180
case <-ctx.Done():
8281
return nil, ctx.Err()
8382
case <-d.closed.Done():
84-
return nil, ErrProviderClosed
83+
return nil, errProviderClosed
8584
case <-d.ready.Done():
8685
return d.keyMaterial(), nil
8786
}
8887
}
8988

9089
func (d *Distributor) keyMaterial() *KeyMaterial {
9190
d.mu.Lock()
92-
km := &KeyMaterial{Certs: d.certs, Roots: d.roots}
91+
km := d.km
9392
d.mu.Unlock()
9493
return km
9594
}

credentials/tls/certprovider/distributor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ func (s) TestDistributor(t *testing.T) {
9494
}
9595
proceedCh <- struct{}{}
9696

97-
// This call to KeyMaterial() should eventually return ErrProviderClosed
97+
// This call to KeyMaterial() should eventually return errProviderClosed
9898
// error.
9999
ctx, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)
100100
defer cancel()
101101
for {
102-
if _, err := dist.KeyMaterial(ctx, KeyMaterialOptions{}); err == ErrProviderClosed {
102+
if _, err := dist.KeyMaterial(ctx, KeyMaterialOptions{}); err == errProviderClosed {
103103
doneCh := dist.Done()
104104
if _, ok := <-doneCh; ok {
105105
errCh <- errors.New("distributor done channel not closed")

credentials/tls/certprovider/provider.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import (
2929
"crypto/tls"
3030
"crypto/x509"
3131
"errors"
32-
"strings"
3332
)
3433

3534
var (
36-
// ErrProviderClosed may be returned from a call to KeyMaterial() when the
35+
// errProviderClosed may be returned from a call to KeyMaterial() when the
3736
// underlying provider instance is closed.
38-
ErrProviderClosed = errors.New("provider instance is closed")
37+
errProviderClosed = errors.New("provider instance is closed")
3938

4039
// m is a map from name to provider builder.
4140
m = make(map[string]Builder)
@@ -44,13 +43,13 @@ var (
4443
// Register registers the provider builder, whose name as returned by its
4544
// Name() method will be used as the name registered with this builder.
4645
func Register(b Builder) {
47-
m[strings.ToLower(b.Name())] = b
46+
m[b.Name()] = b
4847
}
4948

50-
// Get returns the provider builder registered with the given name.
49+
// getBuilder returns the provider builder registered with the given name.
5150
// If no builder is registered with the provided name, nil will be returned.
52-
func Get(name string) Builder {
53-
if b, ok := m[strings.ToLower(name)]; ok {
51+
func getBuilder(name string) Builder {
52+
if b, ok := m[name]; ok {
5453
return b
5554
}
5655
return nil
@@ -64,6 +63,8 @@ type Builder interface {
6463
// ParseConfig converts config input in a format specific to individual
6564
// implementations and returns an implementation of the StableConfig
6665
// interface.
66+
// Equivalent configurations should return StableConfig types whose
67+
// Canonical() method returns the same output.
6768
ParseConfig(interface{}) (StableConfig, error)
6869

6970
// Name returns the name of providers built by this builder.

credentials/tls/certprovider/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (ps *Store) GetProvider(key Key) Provider {
9090
return wp
9191
}
9292

93-
b := Get(key.Name)
93+
b := getBuilder(key.Name)
9494
if b == nil {
9595
return nil
9696
}

credentials/tls/certprovider/store_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func makeProvider(t *testing.T, name, config string) (Provider, *fakeProvider) {
135135
t.Helper()
136136

137137
// Grab the provider builder.
138-
b := Get(name)
138+
b := getBuilder(name)
139139
if b == nil {
140140
t.Fatalf("no provider builder found for name : %s", name)
141141
}
@@ -195,8 +195,8 @@ func (s) TestStoreWithSingleProvider(t *testing.T) {
195195
// Close the provider and retry the KeyMaterial() call, and expect it to
196196
// fail with a known error.
197197
prov.Close()
198-
if _, err := prov.KeyMaterial(ctx, KeyMaterialOptions{}); err != ErrProviderClosed {
199-
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, ErrProviderClosed)
198+
if _, err := prov.KeyMaterial(ctx, KeyMaterialOptions{}); err != errProviderClosed {
199+
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, errProviderClosed)
200200
}
201201
}
202202

@@ -234,8 +234,8 @@ func (s) TestStoreWithSingleProviderWithSharing(t *testing.T) {
234234
}
235235

236236
prov2.Close()
237-
if _, err := prov2.KeyMaterial(ctx, KeyMaterialOptions{}); err != ErrProviderClosed {
238-
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, ErrProviderClosed)
237+
if _, err := prov2.KeyMaterial(ctx, KeyMaterialOptions{}); err != errProviderClosed {
238+
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, errProviderClosed)
239239
}
240240
}
241241

@@ -302,8 +302,8 @@ func (s) TestStoreWithSingleProviderWithoutSharing(t *testing.T) {
302302
}
303303

304304
prov2.Close()
305-
if _, err := prov2.KeyMaterial(ctx, KeyMaterialOptions{}); err != ErrProviderClosed {
306-
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, ErrProviderClosed)
305+
if _, err := prov2.KeyMaterial(ctx, KeyMaterialOptions{}); err != errProviderClosed {
306+
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, errProviderClosed)
307307
}
308308
}
309309

@@ -351,7 +351,7 @@ func (s) TestStoreWithMultipleProviders(t *testing.T) {
351351
}
352352

353353
prov2.Close()
354-
if _, err := prov2.KeyMaterial(ctx, KeyMaterialOptions{}); err != ErrProviderClosed {
355-
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, ErrProviderClosed)
354+
if _, err := prov2.KeyMaterial(ctx, KeyMaterialOptions{}); err != errProviderClosed {
355+
t.Fatalf("provider.KeyMaterial() = %v, wantErr: %v", err, errProviderClosed)
356356
}
357357
}

0 commit comments

Comments
 (0)